blob: 48235933171f2d24e942c38217c390a92b63a366 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static void list_free __ARGS((list_T *l));
405static listitem_T *listitem_alloc __ARGS((void));
406static void listitem_free __ARGS((listitem_T *item));
407static void listitem_remove __ARGS((list_T *l, listitem_T *item));
408static long list_len __ARGS((list_T *l));
409static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
410static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
411static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static listitem_T *list_find __ARGS((list_T *l, long n));
413static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static void list_append __ARGS((list_T *l, listitem_T *item));
415static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000416static int list_append_string __ARGS((list_T *l, char_u *str, int len));
417static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
419static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
420static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
423static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000424static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000425static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
426static void set_ref_in_list __ARGS((list_T *l, int copyID));
427static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void dict_unref __ARGS((dict_T *d));
429static void dict_free __ARGS((dict_T *d));
430static dictitem_T *dictitem_alloc __ARGS((char_u *key));
431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
433static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int dict_add __ARGS((dict_T *d, dictitem_T *item));
436static long dict_len __ARGS((dict_T *d));
437static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
438static char_u *dict2string __ARGS((typval_T *tv));
439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
442static char_u *string_quote __ARGS((char_u *str, int function));
443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
447static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000448static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
450static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000469#if defined(FEAT_INS_EXPAND)
470static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
472#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
478static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000504static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000506static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000512static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000520static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000543static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000549static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000566static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570#ifdef vim_mkdir
571static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
572#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000577static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000579static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000591static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000598static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000603static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000604static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
607#ifdef HAVE_STRFTIME
608static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
609#endif
610static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000622static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000623static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000625static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000640static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
643static int get_env_len __ARGS((char_u **arg));
644static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000645static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000646static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
647#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
648#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
649 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000650static 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 +0000651static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000652static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000653static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
654static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static typval_T *alloc_tv __ARGS((void));
656static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void init_tv __ARGS((typval_T *varp));
658static long get_tv_number __ARGS((typval_T *varp));
659static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000660static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static char_u *get_tv_string __ARGS((typval_T *varp));
662static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000663static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000665static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
667static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
668static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
669static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
670static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
671static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
672static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000673static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000675static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
677static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
678static int eval_fname_script __ARGS((char_u *p));
679static int eval_fname_sid __ARGS((char_u *p));
680static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static ufunc_T *find_func __ARGS((char_u *name));
682static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000683static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000684#ifdef FEAT_PROFILE
685static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000686static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
687static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
688static int
689# ifdef __BORLANDC__
690 _RTLENTRYF
691# endif
692 prof_total_cmp __ARGS((const void *s1, const void *s2));
693static int
694# ifdef __BORLANDC__
695 _RTLENTRYF
696# endif
697 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000698#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000699static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000700static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000701static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void func_free __ARGS((ufunc_T *fp));
703static void func_unref __ARGS((char_u *name));
704static void func_ref __ARGS((char_u *name));
705static 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));
706static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
707
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000708/* Character used as separated in autoload function/variable names. */
709#define AUTOLOAD_CHAR '#'
710
Bram Moolenaar33570922005-01-25 22:26:29 +0000711/*
712 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000713 */
714 void
715eval_init()
716{
Bram Moolenaar33570922005-01-25 22:26:29 +0000717 int i;
718 struct vimvar *p;
719
720 init_var_dict(&globvardict, &globvars_var);
721 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000722 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000723 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000724
725 for (i = 0; i < VV_LEN; ++i)
726 {
727 p = &vimvars[i];
728 STRCPY(p->vv_di.di_key, p->vv_name);
729 if (p->vv_flags & VV_RO)
730 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
731 else if (p->vv_flags & VV_RO_SBX)
732 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
733 else
734 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000735
736 /* add to v: scope dict, unless the value is not always available */
737 if (p->vv_type != VAR_UNKNOWN)
738 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000740 /* add to compat scope dict */
741 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000742 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000743}
744
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000745#if defined(EXITFREE) || defined(PROTO)
746 void
747eval_clear()
748{
749 int i;
750 struct vimvar *p;
751
752 for (i = 0; i < VV_LEN; ++i)
753 {
754 p = &vimvars[i];
755 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000756 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000757 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758 p->vv_di.di_tv.vval.v_string = NULL;
759 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000760 }
761 hash_clear(&vimvarht);
762 hash_clear(&compat_hashtab);
763
764 /* script-local variables */
765 for (i = 1; i <= ga_scripts.ga_len; ++i)
766 vars_clear(&SCRIPT_VARS(i));
767 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000768 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000769
770 /* global variables */
771 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000772
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000773 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000774 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000775 hash_clear(&func_hashtab);
776
777 /* unreferenced lists and dicts */
778 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000779}
780#endif
781
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * Return the name of the executed function.
784 */
785 char_u *
786func_name(cookie)
787 void *cookie;
788{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000789 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
792/*
793 * Return the address holding the next breakpoint line for a funccall cookie.
794 */
795 linenr_T *
796func_breakpoint(cookie)
797 void *cookie;
798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
802/*
803 * Return the address holding the debug tick for a funccall cookie.
804 */
805 int *
806func_dbg_tick(cookie)
807 void *cookie;
808{
Bram Moolenaar33570922005-01-25 22:26:29 +0000809 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
812/*
813 * Return the nesting level for a funccall cookie.
814 */
815 int
816func_level(cookie)
817 void *cookie;
818{
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820}
821
822/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000823funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825/*
826 * Return TRUE when a function was ended by a ":return" command.
827 */
828 int
829current_func_returned()
830{
831 return current_funccal->returned;
832}
833
834
835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * Set an internal variable to a string value. Creates the variable if it does
837 * not already exist.
838 */
839 void
840set_internal_string_var(name, value)
841 char_u *name;
842 char_u *value;
843{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000844 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000845 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847 val = vim_strsave(value);
848 if (val != NULL)
849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000851 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000853 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000854 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856 }
857}
858
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859static lval_T *redir_lval = NULL;
860static char_u *redir_endp = NULL;
861static char_u *redir_varname = NULL;
862
863/*
864 * Start recording command output to a variable
865 * Returns OK if successfully completed the setup. FAIL otherwise.
866 */
867 int
868var_redir_start(name, append)
869 char_u *name;
870 int append; /* append to an existing variable */
871{
872 int save_emsg;
873 int err;
874 typval_T tv;
875
876 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000877 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000878 {
879 EMSG(_(e_invarg));
880 return FAIL;
881 }
882
883 redir_varname = vim_strsave(name);
884 if (redir_varname == NULL)
885 return FAIL;
886
887 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
888 if (redir_lval == NULL)
889 {
890 var_redir_stop();
891 return FAIL;
892 }
893
894 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000895 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
896 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000897 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
898 {
899 if (redir_endp != NULL && *redir_endp != NUL)
900 /* Trailing characters are present after the variable name */
901 EMSG(_(e_trailing));
902 else
903 EMSG(_(e_invarg));
904 var_redir_stop();
905 return FAIL;
906 }
907
908 /* check if we can write to the variable: set it to or append an empty
909 * string */
910 save_emsg = did_emsg;
911 did_emsg = FALSE;
912 tv.v_type = VAR_STRING;
913 tv.vval.v_string = (char_u *)"";
914 if (append)
915 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
916 else
917 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
918 err = did_emsg;
919 did_emsg += save_emsg;
920 if (err)
921 {
922 var_redir_stop();
923 return FAIL;
924 }
925 if (redir_lval->ll_newkey != NULL)
926 {
927 /* Dictionary item was created, don't do it again. */
928 vim_free(redir_lval->ll_newkey);
929 redir_lval->ll_newkey = NULL;
930 }
931
932 return OK;
933}
934
935/*
936 * Append "value[len]" to the variable set by var_redir_start().
937 */
938 void
939var_redir_str(value, len)
940 char_u *value;
941 int len;
942{
943 char_u *val;
944 typval_T tv;
945 int save_emsg;
946 int err;
947
948 if (redir_lval == NULL)
949 return;
950
951 if (len == -1)
952 /* Append the entire string */
953 val = vim_strsave(value);
954 else
955 /* Append only the specified number of characters */
956 val = vim_strnsave(value, len);
957 if (val == NULL)
958 return;
959
960 tv.v_type = VAR_STRING;
961 tv.vval.v_string = val;
962
963 save_emsg = did_emsg;
964 did_emsg = FALSE;
965 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
966 err = did_emsg;
967 did_emsg += save_emsg;
968 if (err)
969 var_redir_stop();
970
971 vim_free(tv.vval.v_string);
972}
973
974/*
975 * Stop redirecting command output to a variable.
976 */
977 void
978var_redir_stop()
979{
980 if (redir_lval != NULL)
981 {
982 clear_lval(redir_lval);
983 vim_free(redir_lval);
984 redir_lval = NULL;
985 }
986 vim_free(redir_varname);
987 redir_varname = NULL;
988}
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990# if defined(FEAT_MBYTE) || defined(PROTO)
991 int
992eval_charconvert(enc_from, enc_to, fname_from, fname_to)
993 char_u *enc_from;
994 char_u *enc_to;
995 char_u *fname_from;
996 char_u *fname_to;
997{
998 int err = FALSE;
999
1000 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1001 set_vim_var_string(VV_CC_TO, enc_to, -1);
1002 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1003 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1004 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1005 err = TRUE;
1006 set_vim_var_string(VV_CC_FROM, NULL, -1);
1007 set_vim_var_string(VV_CC_TO, NULL, -1);
1008 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1009 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1010
1011 if (err)
1012 return FAIL;
1013 return OK;
1014}
1015# endif
1016
1017# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1018 int
1019eval_printexpr(fname, args)
1020 char_u *fname;
1021 char_u *args;
1022{
1023 int err = FALSE;
1024
1025 set_vim_var_string(VV_FNAME_IN, fname, -1);
1026 set_vim_var_string(VV_CMDARG, args, -1);
1027 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1028 err = TRUE;
1029 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1030 set_vim_var_string(VV_CMDARG, NULL, -1);
1031
1032 if (err)
1033 {
1034 mch_remove(fname);
1035 return FAIL;
1036 }
1037 return OK;
1038}
1039# endif
1040
1041# if defined(FEAT_DIFF) || defined(PROTO)
1042 void
1043eval_diff(origfile, newfile, outfile)
1044 char_u *origfile;
1045 char_u *newfile;
1046 char_u *outfile;
1047{
1048 int err = FALSE;
1049
1050 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1051 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1052 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1053 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1054 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1055 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1056 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1057}
1058
1059 void
1060eval_patch(origfile, difffile, outfile)
1061 char_u *origfile;
1062 char_u *difffile;
1063 char_u *outfile;
1064{
1065 int err;
1066
1067 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1068 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1069 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1070 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1071 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1072 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1073 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1074}
1075# endif
1076
1077/*
1078 * Top level evaluation function, returning a boolean.
1079 * Sets "error" to TRUE if there was an error.
1080 * Return TRUE or FALSE.
1081 */
1082 int
1083eval_to_bool(arg, error, nextcmd, skip)
1084 char_u *arg;
1085 int *error;
1086 char_u **nextcmd;
1087 int skip; /* only parse, don't execute */
1088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 int retval = FALSE;
1091
1092 if (skip)
1093 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 else
1097 {
1098 *error = FALSE;
1099 if (!skip)
1100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001101 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001102 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
1104 }
1105 if (skip)
1106 --emsg_skip;
1107
1108 return retval;
1109}
1110
1111/*
1112 * Top level evaluation function, returning a string. If "skip" is TRUE,
1113 * only parsing to "nextcmd" is done, without reporting errors. Return
1114 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1115 */
1116 char_u *
1117eval_to_string_skip(arg, nextcmd, skip)
1118 char_u *arg;
1119 char_u **nextcmd;
1120 int skip; /* only parse, don't execute */
1121{
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 char_u *retval;
1124
1125 if (skip)
1126 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 retval = NULL;
1129 else
1130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 retval = vim_strsave(get_tv_string(&tv));
1132 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 }
1134 if (skip)
1135 --emsg_skip;
1136
1137 return retval;
1138}
1139
1140/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001141 * Skip over an expression at "*pp".
1142 * Return FAIL for an error, OK otherwise.
1143 */
1144 int
1145skip_expr(pp)
1146 char_u **pp;
1147{
Bram Moolenaar33570922005-01-25 22:26:29 +00001148 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001149
1150 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001152}
1153
1154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 * Top level evaluation function, returning a string.
1156 * Return pointer to allocated memory, or NULL for failure.
1157 */
1158 char_u *
1159eval_to_string(arg, nextcmd)
1160 char_u *arg;
1161 char_u **nextcmd;
1162{
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 char_u *retval;
1165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = NULL;
1168 else
1169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 retval = vim_strsave(get_tv_string(&tv));
1171 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173
1174 return retval;
1175}
1176
1177/*
1178 * Call eval_to_string() with "sandbox" set and not using local variables.
1179 */
1180 char_u *
1181eval_to_string_safe(arg, nextcmd)
1182 char_u *arg;
1183 char_u **nextcmd;
1184{
1185 char_u *retval;
1186 void *save_funccalp;
1187
1188 save_funccalp = save_funccal();
1189 ++sandbox;
1190 retval = eval_to_string(arg, nextcmd);
1191 --sandbox;
1192 restore_funccal(save_funccalp);
1193 return retval;
1194}
1195
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196/*
1197 * Top level evaluation function, returning a number.
1198 * Evaluates "expr" silently.
1199 * Returns -1 for an error.
1200 */
1201 int
1202eval_to_number(expr)
1203 char_u *expr;
1204{
Bram Moolenaar33570922005-01-25 22:26:29 +00001205 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001207 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
1209 ++emsg_off;
1210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 retval = -1;
1213 else
1214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001215 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001216 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 --emsg_off;
1219
1220 return retval;
1221}
1222
Bram Moolenaara40058a2005-07-11 22:42:07 +00001223/*
1224 * Prepare v: variable "idx" to be used.
1225 * Save the current typeval in "save_tv".
1226 * When not used yet add the variable to the v: hashtable.
1227 */
1228 static void
1229prepare_vimvar(idx, save_tv)
1230 int idx;
1231 typval_T *save_tv;
1232{
1233 *save_tv = vimvars[idx].vv_tv;
1234 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1235 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1236}
1237
1238/*
1239 * Restore v: variable "idx" to typeval "save_tv".
1240 * When no longer defined, remove the variable from the v: hashtable.
1241 */
1242 static void
1243restore_vimvar(idx, save_tv)
1244 int idx;
1245 typval_T *save_tv;
1246{
1247 hashitem_T *hi;
1248
1249 clear_tv(&vimvars[idx].vv_tv);
1250 vimvars[idx].vv_tv = *save_tv;
1251 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1252 {
1253 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1254 if (HASHITEM_EMPTY(hi))
1255 EMSG2(_(e_intern2), "restore_vimvar()");
1256 else
1257 hash_remove(&vimvarht, hi);
1258 }
1259}
1260
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001261#if defined(FEAT_SYN_HL) || defined(PROTO)
1262/*
1263 * Evaluate an expression to a list with suggestions.
1264 * For the "expr:" part of 'spellsuggest'.
1265 */
1266 list_T *
1267eval_spell_expr(badword, expr)
1268 char_u *badword;
1269 char_u *expr;
1270{
1271 typval_T save_val;
1272 typval_T rettv;
1273 list_T *list = NULL;
1274 char_u *p = skipwhite(expr);
1275
1276 /* Set "v:val" to the bad word. */
1277 prepare_vimvar(VV_VAL, &save_val);
1278 vimvars[VV_VAL].vv_type = VAR_STRING;
1279 vimvars[VV_VAL].vv_str = badword;
1280 if (p_verbose == 0)
1281 ++emsg_off;
1282
1283 if (eval1(&p, &rettv, TRUE) == OK)
1284 {
1285 if (rettv.v_type != VAR_LIST)
1286 clear_tv(&rettv);
1287 else
1288 list = rettv.vval.v_list;
1289 }
1290
1291 if (p_verbose == 0)
1292 --emsg_off;
1293 vimvars[VV_VAL].vv_str = NULL;
1294 restore_vimvar(VV_VAL, &save_val);
1295
1296 return list;
1297}
1298
1299/*
1300 * "list" is supposed to contain two items: a word and a number. Return the
1301 * word in "pp" and the number as the return value.
1302 * Return -1 if anything isn't right.
1303 * Used to get the good word and score from the eval_spell_expr() result.
1304 */
1305 int
1306get_spellword(list, pp)
1307 list_T *list;
1308 char_u **pp;
1309{
1310 listitem_T *li;
1311
1312 li = list->lv_first;
1313 if (li == NULL)
1314 return -1;
1315 *pp = get_tv_string(&li->li_tv);
1316
1317 li = li->li_next;
1318 if (li == NULL)
1319 return -1;
1320 return get_tv_number(&li->li_tv);
1321}
1322#endif
1323
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001324/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001325 * Top level evaluation function.
1326 * Returns an allocated typval_T with the result.
1327 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001328 */
1329 typval_T *
1330eval_expr(arg, nextcmd)
1331 char_u *arg;
1332 char_u **nextcmd;
1333{
1334 typval_T *tv;
1335
1336 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001337 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001338 {
1339 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001340 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001341 }
1342
1343 return tv;
1344}
1345
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1348/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001349 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001351 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001353 static int
1354call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *func;
1356 int argc;
1357 char_u **argv;
1358 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 long n;
1363 int len;
1364 int i;
1365 int doesrange;
1366 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001367 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 for (i = 0; i < argc; i++)
1374 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001375 /* Pass a NULL or empty argument as an empty string */
1376 if (argv[i] == NULL || *argv[i] == NUL)
1377 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001378 argvars[i].v_type = VAR_STRING;
1379 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001380 continue;
1381 }
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 /* Recognize a number argument, the others must be strings. */
1384 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1385 if (len != 0 && len == (int)STRLEN(argv[i]))
1386 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001387 argvars[i].v_type = VAR_NUMBER;
1388 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 else
1391 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001392 argvars[i].v_type = VAR_STRING;
1393 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395 }
1396
1397 if (safe)
1398 {
1399 save_funccalp = save_funccal();
1400 ++sandbox;
1401 }
1402
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001403 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1404 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 if (safe)
1408 {
1409 --sandbox;
1410 restore_funccal(save_funccalp);
1411 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001412 vim_free(argvars);
1413
1414 if (ret == FAIL)
1415 clear_tv(rettv);
1416
1417 return ret;
1418}
1419
1420/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001421 * Call vimL function "func" and return the result as a string.
1422 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 * Uses argv[argc] for the function arguments.
1424 */
1425 void *
1426call_func_retstr(func, argc, argv, safe)
1427 char_u *func;
1428 int argc;
1429 char_u **argv;
1430 int safe; /* use the sandbox */
1431{
1432 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001433 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434
1435 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1436 return NULL;
1437
1438 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001439 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 return retval;
1441}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001443#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001445 * Call vimL function "func" and return the result as a number.
1446 * Returns -1 when calling the function fails.
1447 * Uses argv[argc] for the function arguments.
1448 */
1449 long
1450call_func_retnr(func, argc, argv, safe)
1451 char_u *func;
1452 int argc;
1453 char_u **argv;
1454 int safe; /* use the sandbox */
1455{
1456 typval_T rettv;
1457 long retval;
1458
1459 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1460 return -1;
1461
1462 retval = get_tv_number_chk(&rettv, NULL);
1463 clear_tv(&rettv);
1464 return retval;
1465}
1466#endif
1467
1468/*
1469 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470 * Uses argv[argc] for the function arguments.
1471 */
1472 void *
1473call_func_retlist(func, argc, argv, safe)
1474 char_u *func;
1475 int argc;
1476 char_u **argv;
1477 int safe; /* use the sandbox */
1478{
1479 typval_T rettv;
1480
1481 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1482 return NULL;
1483
1484 if (rettv.v_type != VAR_LIST)
1485 {
1486 clear_tv(&rettv);
1487 return NULL;
1488 }
1489
1490 return rettv.vval.v_list;
1491}
1492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493#endif
1494
1495/*
1496 * Save the current function call pointer, and set it to NULL.
1497 * Used when executing autocommands and for ":source".
1498 */
1499 void *
1500save_funccal()
1501{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001502 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 current_funccal = NULL;
1505 return (void *)fc;
1506}
1507
1508 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001509restore_funccal(vfc)
1510 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001512 funccall_T *fc = (funccall_T *)vfc;
1513
1514 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515}
1516
Bram Moolenaar05159a02005-02-26 23:04:13 +00001517#if defined(FEAT_PROFILE) || defined(PROTO)
1518/*
1519 * Prepare profiling for entering a child or something else that is not
1520 * counted for the script/function itself.
1521 * Should always be called in pair with prof_child_exit().
1522 */
1523 void
1524prof_child_enter(tm)
1525 proftime_T *tm; /* place to store waittime */
1526{
1527 funccall_T *fc = current_funccal;
1528
1529 if (fc != NULL && fc->func->uf_profiling)
1530 profile_start(&fc->prof_child);
1531 script_prof_save(tm);
1532}
1533
1534/*
1535 * Take care of time spent in a child.
1536 * Should always be called after prof_child_enter().
1537 */
1538 void
1539prof_child_exit(tm)
1540 proftime_T *tm; /* where waittime was stored */
1541{
1542 funccall_T *fc = current_funccal;
1543
1544 if (fc != NULL && fc->func->uf_profiling)
1545 {
1546 profile_end(&fc->prof_child);
1547 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1548 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1549 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1550 }
1551 script_prof_restore(tm);
1552}
1553#endif
1554
1555
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#ifdef FEAT_FOLDING
1557/*
1558 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1559 * it in "*cp". Doesn't give error messages.
1560 */
1561 int
1562eval_foldexpr(arg, cp)
1563 char_u *arg;
1564 int *cp;
1565{
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 int retval;
1568 char_u *s;
1569
1570 ++emsg_off;
1571 ++sandbox;
1572 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001573 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 retval = 0;
1575 else
1576 {
1577 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 if (tv.v_type == VAR_NUMBER)
1579 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001580 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 retval = 0;
1582 else
1583 {
1584 /* If the result is a string, check if there is a non-digit before
1585 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001586 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (!VIM_ISDIGIT(*s) && *s != '-')
1588 *cp = *s++;
1589 retval = atol((char *)s);
1590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001591 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 --emsg_off;
1594 --sandbox;
1595
1596 return retval;
1597}
1598#endif
1599
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 * ":let" list all variable values
1602 * ":let var1 var2" list variable values
1603 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001604 * ":let var += expr" assignment command.
1605 * ":let var -= expr" assignment command.
1606 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 */
1609 void
1610ex_let(eap)
1611 exarg_T *eap;
1612{
1613 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001615 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 int var_count = 0;
1618 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621 expr = skip_var_list(arg, &var_count, &semicolon);
1622 if (expr == NULL)
1623 return;
1624 expr = vim_strchr(expr, '=');
1625 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001627 /*
1628 * ":let" without "=": list variables
1629 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 if (*arg == '[')
1631 EMSG(_(e_invarg));
1632 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 /* ":let var1 var2" */
1634 arg = list_arg_vars(eap, arg);
1635 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001638 list_glob_vars();
1639 list_buf_vars();
1640 list_win_vars();
1641 list_vim_vars();
1642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 eap->nextcmd = check_nextcmd(arg);
1644 }
1645 else
1646 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 op[0] = '=';
1648 op[1] = NUL;
1649 if (expr > arg)
1650 {
1651 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1652 op[0] = expr[-1]; /* +=, -= or .= */
1653 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (eap->skip)
1657 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (eap->skip)
1660 {
1661 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 --emsg_skip;
1664 }
1665 else if (i != FAIL)
1666 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 }
1672}
1673
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674/*
1675 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1676 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001677 * When "nextchars" is not NULL it points to a string with characters that
1678 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1679 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 * Returns OK or FAIL;
1681 */
1682 static int
1683ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1684 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001685 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 int copy; /* copy values from "tv", don't move */
1687 int semicolon; /* from skip_var_list() */
1688 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001689 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690{
1691 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001692 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 listitem_T *item;
1695 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696
1697 if (*arg != '[')
1698 {
1699 /*
1700 * ":let var = expr" or ":for var in list"
1701 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 return FAIL;
1704 return OK;
1705 }
1706
1707 /*
1708 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1709 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001710 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 {
1712 EMSG(_(e_listreq));
1713 return FAIL;
1714 }
1715
1716 i = list_len(l);
1717 if (semicolon == 0 && var_count < i)
1718 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001719 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 return FAIL;
1721 }
1722 if (var_count - semicolon > i)
1723 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001724 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 return FAIL;
1726 }
1727
1728 item = l->lv_first;
1729 while (*arg != ']')
1730 {
1731 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733 item = item->li_next;
1734 if (arg == NULL)
1735 return FAIL;
1736
1737 arg = skipwhite(arg);
1738 if (*arg == ';')
1739 {
1740 /* Put the rest of the list (may be empty) in the var after ';'.
1741 * Create a new list for this. */
1742 l = list_alloc();
1743 if (l == NULL)
1744 return FAIL;
1745 while (item != NULL)
1746 {
1747 list_append_tv(l, &item->li_tv);
1748 item = item->li_next;
1749 }
1750
1751 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001752 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753 ltv.vval.v_list = l;
1754 l->lv_refcount = 1;
1755
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1757 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 clear_tv(&ltv);
1759 if (arg == NULL)
1760 return FAIL;
1761 break;
1762 }
1763 else if (*arg != ',' && *arg != ']')
1764 {
1765 EMSG2(_(e_intern2), "ex_let_vars()");
1766 return FAIL;
1767 }
1768 }
1769
1770 return OK;
1771}
1772
1773/*
1774 * Skip over assignable variable "var" or list of variables "[var, var]".
1775 * Used for ":let varvar = expr" and ":for varvar in expr".
1776 * For "[var, var]" increment "*var_count" for each variable.
1777 * for "[var, var; var]" set "semicolon".
1778 * Return NULL for an error.
1779 */
1780 static char_u *
1781skip_var_list(arg, var_count, semicolon)
1782 char_u *arg;
1783 int *var_count;
1784 int *semicolon;
1785{
1786 char_u *p, *s;
1787
1788 if (*arg == '[')
1789 {
1790 /* "[var, var]": find the matching ']'. */
1791 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001792 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 {
1794 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1795 s = skip_var_one(p);
1796 if (s == p)
1797 {
1798 EMSG2(_(e_invarg2), p);
1799 return NULL;
1800 }
1801 ++*var_count;
1802
1803 p = skipwhite(s);
1804 if (*p == ']')
1805 break;
1806 else if (*p == ';')
1807 {
1808 if (*semicolon == 1)
1809 {
1810 EMSG(_("Double ; in list of variables"));
1811 return NULL;
1812 }
1813 *semicolon = 1;
1814 }
1815 else if (*p != ',')
1816 {
1817 EMSG2(_(e_invarg2), p);
1818 return NULL;
1819 }
1820 }
1821 return p + 1;
1822 }
1823 else
1824 return skip_var_one(arg);
1825}
1826
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001828 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1829 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001830 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 static char_u *
1832skip_var_one(arg)
1833 char_u *arg;
1834{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001835 if (*arg == '@' && arg[1] != NUL)
1836 return arg + 2;
1837 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1838 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839}
1840
Bram Moolenaara7043832005-01-21 11:56:39 +00001841/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 * List variables for hashtab "ht" with prefix "prefix".
1843 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001844 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001846list_hashtable_vars(ht, prefix, empty)
1847 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001848 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001850{
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 hashitem_T *hi;
1852 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001853 int todo;
1854
1855 todo = ht->ht_used;
1856 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1857 {
1858 if (!HASHITEM_EMPTY(hi))
1859 {
1860 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 di = HI2DI(hi);
1862 if (empty || di->di_tv.v_type != VAR_STRING
1863 || di->di_tv.vval.v_string != NULL)
1864 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001865 }
1866 }
1867}
1868
1869/*
1870 * List global variables.
1871 */
1872 static void
1873list_glob_vars()
1874{
Bram Moolenaar33570922005-01-25 22:26:29 +00001875 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001876}
1877
1878/*
1879 * List buffer variables.
1880 */
1881 static void
1882list_buf_vars()
1883{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884 char_u numbuf[NUMBUFLEN];
1885
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001887
1888 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1889 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001890}
1891
1892/*
1893 * List window variables.
1894 */
1895 static void
1896list_win_vars()
1897{
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001899}
1900
1901/*
1902 * List Vim variables.
1903 */
1904 static void
1905list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906{
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908}
1909
1910/*
1911 * List variables in "arg".
1912 */
1913 static char_u *
1914list_arg_vars(eap, arg)
1915 exarg_T *eap;
1916 char_u *arg;
1917{
1918 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001919 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 char_u *name_start;
1922 char_u *arg_subsc;
1923 char_u *tofree;
1924 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925
1926 while (!ends_excmd(*arg) && !got_int)
1927 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001928 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001930 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001931 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1932 {
1933 emsg_severe = TRUE;
1934 EMSG(_(e_trailing));
1935 break;
1936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940 /* get_name_len() takes care of expanding curly braces */
1941 name_start = name = arg;
1942 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1943 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001945 /* This is mainly to keep test 49 working: when expanding
1946 * curly braces fails overrule the exception error message. */
1947 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001949 emsg_severe = TRUE;
1950 EMSG2(_(e_invarg2), arg);
1951 break;
1952 }
1953 error = TRUE;
1954 }
1955 else
1956 {
1957 if (tofree != NULL)
1958 name = tofree;
1959 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 else
1962 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001963 /* handle d.key, l[idx], f(expr) */
1964 arg_subsc = arg;
1965 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001966 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001969 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001971 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001972 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001973 case 'g': list_glob_vars(); break;
1974 case 'b': list_buf_vars(); break;
1975 case 'w': list_win_vars(); break;
1976 case 'v': list_vim_vars(); break;
1977 default:
1978 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001979 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001980 }
1981 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982 {
1983 char_u numbuf[NUMBUFLEN];
1984 char_u *tf;
1985 int c;
1986 char_u *s;
1987
1988 s = echo_string(&tv, &tf, numbuf);
1989 c = *arg;
1990 *arg = NUL;
1991 list_one_var_a((char_u *)"",
1992 arg == arg_subsc ? name : name_start,
1993 tv.v_type, s == NULL ? (char_u *)"" : s);
1994 *arg = c;
1995 vim_free(tf);
1996 }
1997 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 }
2000 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001
2002 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002003 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002004
2005 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 }
2007
2008 return arg;
2009}
2010
2011/*
2012 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2013 * Returns a pointer to the char just after the var name.
2014 * Returns NULL if there is an error.
2015 */
2016 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002017ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002019 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023{
2024 int c1;
2025 char_u *name;
2026 char_u *p;
2027 char_u *arg_end = NULL;
2028 int len;
2029 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031
2032 /*
2033 * ":let $VAR = expr": Set environment variable.
2034 */
2035 if (*arg == '$')
2036 {
2037 /* Find the end of the name. */
2038 ++arg;
2039 name = arg;
2040 len = get_env_len(&arg);
2041 if (len == 0)
2042 EMSG2(_(e_invarg2), name - 1);
2043 else
2044 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002045 if (op != NULL && (*op == '+' || *op == '-'))
2046 EMSG2(_(e_letwrong), op);
2047 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 EMSG(_(e_letunexp));
2050 else
2051 {
2052 c1 = name[len];
2053 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002054 p = get_tv_string_chk(tv);
2055 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002056 {
2057 int mustfree = FALSE;
2058 char_u *s = vim_getenv(name, &mustfree);
2059
2060 if (s != NULL)
2061 {
2062 p = tofree = concat_str(s, p);
2063 if (mustfree)
2064 vim_free(s);
2065 }
2066 }
2067 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002069 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002070 if (STRICMP(name, "HOME") == 0)
2071 init_homedir();
2072 else if (didset_vim && STRICMP(name, "VIM") == 0)
2073 didset_vim = FALSE;
2074 else if (didset_vimruntime
2075 && STRICMP(name, "VIMRUNTIME") == 0)
2076 didset_vimruntime = FALSE;
2077 arg_end = arg;
2078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002079 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002080 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 }
2082 }
2083 }
2084
2085 /*
2086 * ":let &option = expr": Set option value.
2087 * ":let &l:option = expr": Set local option value.
2088 * ":let &g:option = expr": Set global option value.
2089 */
2090 else if (*arg == '&')
2091 {
2092 /* Find the end of the name. */
2093 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 if (p == NULL || (endchars != NULL
2095 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 EMSG(_(e_letunexp));
2097 else
2098 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099 long n;
2100 int opt_type;
2101 long numval;
2102 char_u *stringval = NULL;
2103 char_u *s;
2104
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 c1 = *p;
2106 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002107
2108 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002109 s = get_tv_string_chk(tv); /* != NULL if number or string */
2110 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002111 {
2112 opt_type = get_option_value(arg, &numval,
2113 &stringval, opt_flags);
2114 if ((opt_type == 1 && *op == '.')
2115 || (opt_type == 0 && *op != '.'))
2116 EMSG2(_(e_letwrong), op);
2117 else
2118 {
2119 if (opt_type == 1) /* number */
2120 {
2121 if (*op == '+')
2122 n = numval + n;
2123 else
2124 n = numval - n;
2125 }
2126 else if (opt_type == 0 && stringval != NULL) /* string */
2127 {
2128 s = concat_str(stringval, s);
2129 vim_free(stringval);
2130 stringval = s;
2131 }
2132 }
2133 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 if (s != NULL)
2135 {
2136 set_option_value(arg, n, s, opt_flags);
2137 arg_end = p;
2138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002140 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 }
2142 }
2143
2144 /*
2145 * ":let @r = expr": Set register contents.
2146 */
2147 else if (*arg == '@')
2148 {
2149 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002150 if (op != NULL && (*op == '+' || *op == '-'))
2151 EMSG2(_(e_letwrong), op);
2152 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002153 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 EMSG(_(e_letunexp));
2155 else
2156 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002157 char_u *tofree = NULL;
2158 char_u *s;
2159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002160 p = get_tv_string_chk(tv);
2161 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002163 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 if (s != NULL)
2165 {
2166 p = tofree = concat_str(s, p);
2167 vim_free(s);
2168 }
2169 }
2170 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002173 arg_end = arg + 1;
2174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 }
2177 }
2178
2179 /*
2180 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002183 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002185 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002187 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2191 EMSG(_(e_letunexp));
2192 else
2193 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002194 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 arg_end = p;
2196 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
2200
2201 else
2202 EMSG2(_(e_invarg2), arg);
2203
2204 return arg_end;
2205}
2206
2207/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002208 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2209 */
2210 static int
2211check_changedtick(arg)
2212 char_u *arg;
2213{
2214 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2215 {
2216 EMSG2(_(e_readonlyvar), arg);
2217 return TRUE;
2218 }
2219 return FALSE;
2220}
2221
2222/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 * Get an lval: variable, Dict item or List item that can be assigned a value
2224 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2225 * "name.key", "name.key[expr]" etc.
2226 * Indexing only works if "name" is an existing List or Dictionary.
2227 * "name" points to the start of the name.
2228 * If "rettv" is not NULL it points to the value to be assigned.
2229 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2230 * wrong; must end in space or cmd separator.
2231 *
2232 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002233 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 */
2236 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002237get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002239 typval_T *rettv;
2240 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 int unlet;
2242 int skip;
2243 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002244 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 char_u *expr_start, *expr_end;
2248 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002249 dictitem_T *v;
2250 typval_T var1;
2251 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002253 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002254 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002255 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002256 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002259 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260
2261 if (skip)
2262 {
2263 /* When skipping just find the end of the name. */
2264 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002265 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 }
2267
2268 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002269 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 if (expr_start != NULL)
2271 {
2272 /* Don't expand the name when we already know there is an error. */
2273 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2274 && *p != '[' && *p != '.')
2275 {
2276 EMSG(_(e_trailing));
2277 return NULL;
2278 }
2279
2280 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2281 if (lp->ll_exp_name == NULL)
2282 {
2283 /* Report an invalid expression in braces, unless the
2284 * expression evaluation has been cancelled due to an
2285 * aborting error, an interrupt, or an exception. */
2286 if (!aborting() && !quiet)
2287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002288 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 EMSG2(_(e_invarg2), name);
2290 return NULL;
2291 }
2292 }
2293 lp->ll_name = lp->ll_exp_name;
2294 }
2295 else
2296 lp->ll_name = name;
2297
2298 /* Without [idx] or .key we are done. */
2299 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2300 return p;
2301
2302 cc = *p;
2303 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 if (v == NULL && !quiet)
2306 EMSG2(_(e_undefvar), lp->ll_name);
2307 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 if (v == NULL)
2309 return NULL;
2310
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002311 /*
2312 * Loop until no more [idx] or .key is following.
2313 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002314 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2318 && !(lp->ll_tv->v_type == VAR_DICT
2319 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 if (!quiet)
2322 EMSG(_("E689: Can only index a List or Dictionary"));
2323 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327 if (!quiet)
2328 EMSG(_("E708: [:] must come last"));
2329 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002331
Bram Moolenaar8c711452005-01-14 21:53:12 +00002332 len = -1;
2333 if (*p == '.')
2334 {
2335 key = p + 1;
2336 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2337 ;
2338 if (len == 0)
2339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 if (!quiet)
2341 EMSG(_(e_emptykey));
2342 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002343 }
2344 p = key + len;
2345 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002346 else
2347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002348 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002349 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 if (*p == ':')
2351 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002352 else
2353 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 empty1 = FALSE;
2355 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002357 if (get_tv_string_chk(&var1) == NULL)
2358 {
2359 /* not a number or string */
2360 clear_tv(&var1);
2361 return NULL;
2362 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 }
2364
2365 /* Optionally get the second index [ :expr]. */
2366 if (*p == ':')
2367 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002372 if (!empty1)
2373 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002375 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (rettv != NULL && (rettv->v_type != VAR_LIST
2377 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 if (!quiet)
2380 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 if (!empty1)
2382 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002384 }
2385 p = skipwhite(p + 1);
2386 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 else
2389 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002391 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2392 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002393 if (!empty1)
2394 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 if (get_tv_string_chk(&var2) == NULL)
2398 {
2399 /* not a number or string */
2400 if (!empty1)
2401 clear_tv(&var1);
2402 clear_tv(&var2);
2403 return NULL;
2404 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002407 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002408 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002410
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002412 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (!quiet)
2414 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 if (!empty1)
2416 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 }
2421
2422 /* Skip to past ']'. */
2423 ++p;
2424 }
2425
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 {
2428 if (len == -1)
2429 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 if (*key == NUL)
2433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (!quiet)
2435 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 }
2439 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 lp->ll_list = NULL;
2441 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002442 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002445 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 if (len == -1)
2451 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 }
2454 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 if (len == -1)
2459 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 p = NULL;
2462 break;
2463 }
2464 if (len == -1)
2465 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 }
2468 else
2469 {
2470 /*
2471 * Get the number and item for the only or first index of the List.
2472 */
2473 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 else
2476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 clear_tv(&var1);
2479 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 lp->ll_list = lp->ll_tv->vval.v_list;
2482 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2483 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (!quiet)
2486 EMSGN(_(e_listidx), lp->ll_n1);
2487 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 }
2491
2492 /*
2493 * May need to find the item or absolute index for the second
2494 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 * When no index given: "lp->ll_empty2" is TRUE.
2496 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 if (ni == NULL)
2506 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 if (!quiet)
2508 EMSGN(_(e_listidx), lp->ll_n2);
2509 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 }
2513
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2515 if (lp->ll_n1 < 0)
2516 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2517 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (!quiet)
2520 EMSGN(_(e_listidx), lp->ll_n2);
2521 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002522 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002523 }
2524
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
2528
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 return p;
2530}
2531
2532/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 */
2535 static void
2536clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538{
2539 vim_free(lp->ll_exp_name);
2540 vim_free(lp->ll_newkey);
2541}
2542
2543/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002544 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 */
2548 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002554 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555{
2556 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 listitem_T *ri;
2558 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559
2560 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 cc = *endp;
2565 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566 if (op != NULL && *op != '=')
2567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002570 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002571 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2572 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002573 {
2574 if (tv_op(&tv, rettv, op) == OK)
2575 set_var(lp->ll_name, &tv, FALSE);
2576 clear_tv(&tv);
2577 }
2578 }
2579 else
2580 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002584 else if (tv_check_lock(lp->ll_newkey == NULL
2585 ? lp->ll_tv->v_lock
2586 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2587 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 else if (lp->ll_range)
2589 {
2590 /*
2591 * Assign the List values to the list items.
2592 */
2593 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002594 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002595 if (op != NULL && *op != '=')
2596 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2597 else
2598 {
2599 clear_tv(&lp->ll_li->li_tv);
2600 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2601 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 ri = ri->li_next;
2603 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2604 break;
2605 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002608 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 ri = NULL;
2611 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002612 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002613 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 lp->ll_li = lp->ll_li->li_next;
2615 ++lp->ll_n1;
2616 }
2617 if (ri != NULL)
2618 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002619 else if (lp->ll_empty2
2620 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 : lp->ll_n1 != lp->ll_n2)
2622 EMSG(_("E711: List value has not enough items"));
2623 }
2624 else
2625 {
2626 /*
2627 * Assign to a List or Dictionary item.
2628 */
2629 if (lp->ll_newkey != NULL)
2630 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002631 if (op != NULL && *op != '=')
2632 {
2633 EMSG2(_(e_letwrong), op);
2634 return;
2635 }
2636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002638 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (di == NULL)
2640 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002641 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2642 {
2643 vim_free(di);
2644 return;
2645 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002648 else if (op != NULL && *op != '=')
2649 {
2650 tv_op(lp->ll_tv, rettv, op);
2651 return;
2652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 /*
2657 * Assign the value to the variable or list item.
2658 */
2659 if (copy)
2660 copy_tv(rettv, lp->ll_tv);
2661 else
2662 {
2663 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002664 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
2667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668}
2669
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002670/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2672 * Returns OK or FAIL.
2673 */
2674 static int
2675tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002676 typval_T *tv1;
2677 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002678 char_u *op;
2679{
2680 long n;
2681 char_u numbuf[NUMBUFLEN];
2682 char_u *s;
2683
2684 /* Can't do anything with a Funcref or a Dict on the right. */
2685 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2686 {
2687 switch (tv1->v_type)
2688 {
2689 case VAR_DICT:
2690 case VAR_FUNC:
2691 break;
2692
2693 case VAR_LIST:
2694 if (*op != '+' || tv2->v_type != VAR_LIST)
2695 break;
2696 /* List += List */
2697 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2698 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2699 return OK;
2700
2701 case VAR_NUMBER:
2702 case VAR_STRING:
2703 if (tv2->v_type == VAR_LIST)
2704 break;
2705 if (*op == '+' || *op == '-')
2706 {
2707 /* nr += nr or nr -= nr*/
2708 n = get_tv_number(tv1);
2709 if (*op == '+')
2710 n += get_tv_number(tv2);
2711 else
2712 n -= get_tv_number(tv2);
2713 clear_tv(tv1);
2714 tv1->v_type = VAR_NUMBER;
2715 tv1->vval.v_number = n;
2716 }
2717 else
2718 {
2719 /* str .= str */
2720 s = get_tv_string(tv1);
2721 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2722 clear_tv(tv1);
2723 tv1->v_type = VAR_STRING;
2724 tv1->vval.v_string = s;
2725 }
2726 return OK;
2727 }
2728 }
2729
2730 EMSG2(_(e_letwrong), op);
2731 return FAIL;
2732}
2733
2734/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002735 * Add a watcher to a list.
2736 */
2737 static void
2738list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002739 list_T *l;
2740 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002741{
2742 lw->lw_next = l->lv_watch;
2743 l->lv_watch = lw;
2744}
2745
2746/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002747 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002748 * No warning when it isn't found...
2749 */
2750 static void
2751list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002752 list_T *l;
2753 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002754{
Bram Moolenaar33570922005-01-25 22:26:29 +00002755 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002756
2757 lwp = &l->lv_watch;
2758 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2759 {
2760 if (lw == lwrem)
2761 {
2762 *lwp = lw->lw_next;
2763 break;
2764 }
2765 lwp = &lw->lw_next;
2766 }
2767}
2768
2769/*
2770 * Just before removing an item from a list: advance watchers to the next
2771 * item.
2772 */
2773 static void
2774list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002775 list_T *l;
2776 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002777{
Bram Moolenaar33570922005-01-25 22:26:29 +00002778 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002779
2780 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2781 if (lw->lw_item == item)
2782 lw->lw_item = item->li_next;
2783}
2784
2785/*
2786 * Evaluate the expression used in a ":for var in expr" command.
2787 * "arg" points to "var".
2788 * Set "*errp" to TRUE for an error, FALSE otherwise;
2789 * Return a pointer that holds the info. Null when there is an error.
2790 */
2791 void *
2792eval_for_line(arg, errp, nextcmdp, skip)
2793 char_u *arg;
2794 int *errp;
2795 char_u **nextcmdp;
2796 int skip;
2797{
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 typval_T tv;
2801 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802
2803 *errp = TRUE; /* default: there is an error */
2804
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002806 if (fi == NULL)
2807 return NULL;
2808
2809 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2810 if (expr == NULL)
2811 return fi;
2812
2813 expr = skipwhite(expr);
2814 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2815 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002816 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002817 return fi;
2818 }
2819
2820 if (skip)
2821 ++emsg_skip;
2822 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2823 {
2824 *errp = FALSE;
2825 if (!skip)
2826 {
2827 l = tv.vval.v_list;
2828 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002829 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002831 clear_tv(&tv);
2832 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 else
2834 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002835 /* No need to increment the refcount, it's already set for the
2836 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002837 fi->fi_list = l;
2838 list_add_watch(l, &fi->fi_lw);
2839 fi->fi_lw.lw_item = l->lv_first;
2840 }
2841 }
2842 }
2843 if (skip)
2844 --emsg_skip;
2845
2846 return fi;
2847}
2848
2849/*
2850 * Use the first item in a ":for" list. Advance to the next.
2851 * Assign the values to the variable (list). "arg" points to the first one.
2852 * Return TRUE when a valid item was found, FALSE when at end of list or
2853 * something wrong.
2854 */
2855 int
2856next_for_item(fi_void, arg)
2857 void *fi_void;
2858 char_u *arg;
2859{
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002861 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863
2864 item = fi->fi_lw.lw_item;
2865 if (item == NULL)
2866 result = FALSE;
2867 else
2868 {
2869 fi->fi_lw.lw_item = item->li_next;
2870 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2871 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2872 }
2873 return result;
2874}
2875
2876/*
2877 * Free the structure used to store info used by ":for".
2878 */
2879 void
2880free_for_info(fi_void)
2881 void *fi_void;
2882{
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002885 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002886 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002887 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002888 list_unref(fi->fi_list);
2889 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002890 vim_free(fi);
2891}
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2894
2895 void
2896set_context_for_expression(xp, arg, cmdidx)
2897 expand_T *xp;
2898 char_u *arg;
2899 cmdidx_T cmdidx;
2900{
2901 int got_eq = FALSE;
2902 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 if (cmdidx == CMD_let)
2906 {
2907 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002908 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002909 {
2910 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002911 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 {
2913 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002915 if (vim_iswhite(*p))
2916 break;
2917 }
2918 return;
2919 }
2920 }
2921 else
2922 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2923 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 while ((xp->xp_pattern = vim_strpbrk(arg,
2925 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2926 {
2927 c = *xp->xp_pattern;
2928 if (c == '&')
2929 {
2930 c = xp->xp_pattern[1];
2931 if (c == '&')
2932 {
2933 ++xp->xp_pattern;
2934 xp->xp_context = cmdidx != CMD_let || got_eq
2935 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2936 }
2937 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002940 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2941 xp->xp_pattern += 2;
2942
2943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
2945 else if (c == '$')
2946 {
2947 /* environment variable */
2948 xp->xp_context = EXPAND_ENV_VARS;
2949 }
2950 else if (c == '=')
2951 {
2952 got_eq = TRUE;
2953 xp->xp_context = EXPAND_EXPRESSION;
2954 }
2955 else if (c == '<'
2956 && xp->xp_context == EXPAND_FUNCTIONS
2957 && vim_strchr(xp->xp_pattern, '(') == NULL)
2958 {
2959 /* Function name can start with "<SNR>" */
2960 break;
2961 }
2962 else if (cmdidx != CMD_let || got_eq)
2963 {
2964 if (c == '"') /* string */
2965 {
2966 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2967 if (c == '\\' && xp->xp_pattern[1] != NUL)
2968 ++xp->xp_pattern;
2969 xp->xp_context = EXPAND_NOTHING;
2970 }
2971 else if (c == '\'') /* literal string */
2972 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002973 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2975 /* skip */ ;
2976 xp->xp_context = EXPAND_NOTHING;
2977 }
2978 else if (c == '|')
2979 {
2980 if (xp->xp_pattern[1] == '|')
2981 {
2982 ++xp->xp_pattern;
2983 xp->xp_context = EXPAND_EXPRESSION;
2984 }
2985 else
2986 xp->xp_context = EXPAND_COMMANDS;
2987 }
2988 else
2989 xp->xp_context = EXPAND_EXPRESSION;
2990 }
2991 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002992 /* Doesn't look like something valid, expand as an expression
2993 * anyway. */
2994 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 arg = xp->xp_pattern;
2996 if (*arg != NUL)
2997 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2998 /* skip */ ;
2999 }
3000 xp->xp_pattern = arg;
3001}
3002
3003#endif /* FEAT_CMDL_COMPL */
3004
3005/*
3006 * ":1,25call func(arg1, arg2)" function call.
3007 */
3008 void
3009ex_call(eap)
3010 exarg_T *eap;
3011{
3012 char_u *arg = eap->arg;
3013 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 linenr_T lnum;
3019 int doesrange;
3020 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003021 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003023 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3024 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003025 if (tofree == NULL)
3026 return;
3027
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003028 /* Increase refcount on dictionary, it could get deleted when evaluating
3029 * the arguments. */
3030 if (fudi.fd_dict != NULL)
3031 ++fudi.fd_dict->dv_refcount;
3032
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3034 len = STRLEN(tofree);
3035 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Bram Moolenaar532c7802005-01-27 14:44:31 +00003037 /* Skip white space to allow ":call func ()". Not good, but required for
3038 * backward compatibility. */
3039 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003040 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042 if (*startarg != '(')
3043 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003044 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 goto end;
3046 }
3047
3048 /*
3049 * When skipping, evaluate the function once, to find the end of the
3050 * arguments.
3051 * When the function takes a range, this is discovered after the first
3052 * call, and the loop is broken.
3053 */
3054 if (eap->skip)
3055 {
3056 ++emsg_skip;
3057 lnum = eap->line2; /* do it once, also with an invalid range */
3058 }
3059 else
3060 lnum = eap->line1;
3061 for ( ; lnum <= eap->line2; ++lnum)
3062 {
3063 if (!eap->skip && eap->addr_count > 0)
3064 {
3065 curwin->w_cursor.lnum = lnum;
3066 curwin->w_cursor.col = 0;
3067 }
3068 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003070 eap->line1, eap->line2, &doesrange,
3071 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
3073 failed = TRUE;
3074 break;
3075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 if (doesrange || eap->skip)
3078 break;
3079 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003080 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003081 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003082 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 if (aborting())
3084 break;
3085 }
3086 if (eap->skip)
3087 --emsg_skip;
3088
3089 if (!failed)
3090 {
3091 /* Check for trailing illegal characters and a following command. */
3092 if (!ends_excmd(*arg))
3093 {
3094 emsg_severe = TRUE;
3095 EMSG(_(e_trailing));
3096 }
3097 else
3098 eap->nextcmd = check_nextcmd(arg);
3099 }
3100
3101end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003102 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003103 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104}
3105
3106/*
3107 * ":unlet[!] var1 ... " command.
3108 */
3109 void
3110ex_unlet(eap)
3111 exarg_T *eap;
3112{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003113 ex_unletlock(eap, eap->arg, 0);
3114}
3115
3116/*
3117 * ":lockvar" and ":unlockvar" commands
3118 */
3119 void
3120ex_lockvar(eap)
3121 exarg_T *eap;
3122{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003124 int deep = 2;
3125
3126 if (eap->forceit)
3127 deep = -1;
3128 else if (vim_isdigit(*arg))
3129 {
3130 deep = getdigits(&arg);
3131 arg = skipwhite(arg);
3132 }
3133
3134 ex_unletlock(eap, arg, deep);
3135}
3136
3137/*
3138 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3139 */
3140 static void
3141ex_unletlock(eap, argstart, deep)
3142 exarg_T *eap;
3143 char_u *argstart;
3144 int deep;
3145{
3146 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003149 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 do
3152 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003153 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003154 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3155 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003156 if (lv.ll_name == NULL)
3157 error = TRUE; /* error but continue parsing */
3158 if (name_end == NULL || (!vim_iswhite(*name_end)
3159 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003161 if (name_end != NULL)
3162 {
3163 emsg_severe = TRUE;
3164 EMSG(_(e_trailing));
3165 }
3166 if (!(eap->skip || error))
3167 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 break;
3169 }
3170
3171 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003172 {
3173 if (eap->cmdidx == CMD_unlet)
3174 {
3175 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3176 error = TRUE;
3177 }
3178 else
3179 {
3180 if (do_lock_var(&lv, name_end, deep,
3181 eap->cmdidx == CMD_lockvar) == FAIL)
3182 error = TRUE;
3183 }
3184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003186 if (!eap->skip)
3187 clear_lval(&lv);
3188
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 arg = skipwhite(name_end);
3190 } while (!ends_excmd(*arg));
3191
3192 eap->nextcmd = check_nextcmd(arg);
3193}
3194
Bram Moolenaar8c711452005-01-14 21:53:12 +00003195 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003196do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003198 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003199 int forceit;
3200{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003201 int ret = OK;
3202 int cc;
3203
3204 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003206 cc = *name_end;
3207 *name_end = NUL;
3208
3209 /* Normal name or expanded name. */
3210 if (check_changedtick(lp->ll_name))
3211 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003212 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003213 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003214 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003215 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003216 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3217 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003218 else if (lp->ll_range)
3219 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003221
3222 /* Delete a range of List items. */
3223 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3224 {
3225 li = lp->ll_li->li_next;
3226 listitem_remove(lp->ll_list, lp->ll_li);
3227 lp->ll_li = li;
3228 ++lp->ll_n1;
3229 }
3230 }
3231 else
3232 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 /* unlet a List item. */
3235 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003237 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003238 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 }
3240
3241 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003242}
3243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244/*
3245 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003246 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 */
3248 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003249do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003251 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
Bram Moolenaar33570922005-01-25 22:26:29 +00003253 hashtab_T *ht;
3254 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003255 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar33570922005-01-25 22:26:29 +00003257 ht = find_var_ht(name, &varname);
3258 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003260 hi = hash_find(ht, varname);
3261 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003262 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003263 if (var_check_ro(HI2DI(hi)->di_flags, name))
3264 return FAIL;
3265 delete_var(ht, hi);
3266 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003269 if (forceit)
3270 return OK;
3271 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 return FAIL;
3273}
3274
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003275/*
3276 * Lock or unlock variable indicated by "lp".
3277 * "deep" is the levels to go (-1 for unlimited);
3278 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3279 */
3280 static int
3281do_lock_var(lp, name_end, deep, lock)
3282 lval_T *lp;
3283 char_u *name_end;
3284 int deep;
3285 int lock;
3286{
3287 int ret = OK;
3288 int cc;
3289 dictitem_T *di;
3290
3291 if (deep == 0) /* nothing to do */
3292 return OK;
3293
3294 if (lp->ll_tv == NULL)
3295 {
3296 cc = *name_end;
3297 *name_end = NUL;
3298
3299 /* Normal name or expanded name. */
3300 if (check_changedtick(lp->ll_name))
3301 ret = FAIL;
3302 else
3303 {
3304 di = find_var(lp->ll_name, NULL);
3305 if (di == NULL)
3306 ret = FAIL;
3307 else
3308 {
3309 if (lock)
3310 di->di_flags |= DI_FLAGS_LOCK;
3311 else
3312 di->di_flags &= ~DI_FLAGS_LOCK;
3313 item_lock(&di->di_tv, deep, lock);
3314 }
3315 }
3316 *name_end = cc;
3317 }
3318 else if (lp->ll_range)
3319 {
3320 listitem_T *li = lp->ll_li;
3321
3322 /* (un)lock a range of List items. */
3323 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3324 {
3325 item_lock(&li->li_tv, deep, lock);
3326 li = li->li_next;
3327 ++lp->ll_n1;
3328 }
3329 }
3330 else if (lp->ll_list != NULL)
3331 /* (un)lock a List item. */
3332 item_lock(&lp->ll_li->li_tv, deep, lock);
3333 else
3334 /* un(lock) a Dictionary item. */
3335 item_lock(&lp->ll_di->di_tv, deep, lock);
3336
3337 return ret;
3338}
3339
3340/*
3341 * Lock or unlock an item. "deep" is nr of levels to go.
3342 */
3343 static void
3344item_lock(tv, deep, lock)
3345 typval_T *tv;
3346 int deep;
3347 int lock;
3348{
3349 static int recurse = 0;
3350 list_T *l;
3351 listitem_T *li;
3352 dict_T *d;
3353 hashitem_T *hi;
3354 int todo;
3355
3356 if (recurse >= DICT_MAXNEST)
3357 {
3358 EMSG(_("E743: variable nested too deep for (un)lock"));
3359 return;
3360 }
3361 if (deep == 0)
3362 return;
3363 ++recurse;
3364
3365 /* lock/unlock the item itself */
3366 if (lock)
3367 tv->v_lock |= VAR_LOCKED;
3368 else
3369 tv->v_lock &= ~VAR_LOCKED;
3370
3371 switch (tv->v_type)
3372 {
3373 case VAR_LIST:
3374 if ((l = tv->vval.v_list) != NULL)
3375 {
3376 if (lock)
3377 l->lv_lock |= VAR_LOCKED;
3378 else
3379 l->lv_lock &= ~VAR_LOCKED;
3380 if (deep < 0 || deep > 1)
3381 /* recursive: lock/unlock the items the List contains */
3382 for (li = l->lv_first; li != NULL; li = li->li_next)
3383 item_lock(&li->li_tv, deep - 1, lock);
3384 }
3385 break;
3386 case VAR_DICT:
3387 if ((d = tv->vval.v_dict) != NULL)
3388 {
3389 if (lock)
3390 d->dv_lock |= VAR_LOCKED;
3391 else
3392 d->dv_lock &= ~VAR_LOCKED;
3393 if (deep < 0 || deep > 1)
3394 {
3395 /* recursive: lock/unlock the items the List contains */
3396 todo = d->dv_hashtab.ht_used;
3397 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3398 {
3399 if (!HASHITEM_EMPTY(hi))
3400 {
3401 --todo;
3402 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3403 }
3404 }
3405 }
3406 }
3407 }
3408 --recurse;
3409}
3410
Bram Moolenaara40058a2005-07-11 22:42:07 +00003411/*
3412 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3413 * it refers to a List or Dictionary that is locked.
3414 */
3415 static int
3416tv_islocked(tv)
3417 typval_T *tv;
3418{
3419 return (tv->v_lock & VAR_LOCKED)
3420 || (tv->v_type == VAR_LIST
3421 && tv->vval.v_list != NULL
3422 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3423 || (tv->v_type == VAR_DICT
3424 && tv->vval.v_dict != NULL
3425 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3426}
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3429/*
3430 * Delete all "menutrans_" variables.
3431 */
3432 void
3433del_menutrans_vars()
3434{
Bram Moolenaar33570922005-01-25 22:26:29 +00003435 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003436 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaar33570922005-01-25 22:26:29 +00003438 hash_lock(&globvarht);
3439 todo = globvarht.ht_used;
3440 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003441 {
3442 if (!HASHITEM_EMPTY(hi))
3443 {
3444 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003445 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3446 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003447 }
3448 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003449 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450}
3451#endif
3452
3453#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3454
3455/*
3456 * Local string buffer for the next two functions to store a variable name
3457 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3458 * get_user_var_name().
3459 */
3460
3461static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3462
3463static char_u *varnamebuf = NULL;
3464static int varnamebuflen = 0;
3465
3466/*
3467 * Function to concatenate a prefix and a variable name.
3468 */
3469 static char_u *
3470cat_prefix_varname(prefix, name)
3471 int prefix;
3472 char_u *name;
3473{
3474 int len;
3475
3476 len = (int)STRLEN(name) + 3;
3477 if (len > varnamebuflen)
3478 {
3479 vim_free(varnamebuf);
3480 len += 10; /* some additional space */
3481 varnamebuf = alloc(len);
3482 if (varnamebuf == NULL)
3483 {
3484 varnamebuflen = 0;
3485 return NULL;
3486 }
3487 varnamebuflen = len;
3488 }
3489 *varnamebuf = prefix;
3490 varnamebuf[1] = ':';
3491 STRCPY(varnamebuf + 2, name);
3492 return varnamebuf;
3493}
3494
3495/*
3496 * Function given to ExpandGeneric() to obtain the list of user defined
3497 * (global/buffer/window/built-in) variable names.
3498 */
3499/*ARGSUSED*/
3500 char_u *
3501get_user_var_name(xp, idx)
3502 expand_T *xp;
3503 int idx;
3504{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003505 static long_u gdone;
3506 static long_u bdone;
3507 static long_u wdone;
3508 static int vidx;
3509 static hashitem_T *hi;
3510 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003513 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003514
3515 /* Global variables */
3516 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003518 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003520 else
3521 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003522 while (HASHITEM_EMPTY(hi))
3523 ++hi;
3524 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3525 return cat_prefix_varname('g', hi->hi_key);
3526 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003528
3529 /* b: variables */
3530 ht = &curbuf->b_vars.dv_hashtab;
3531 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003533 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003534 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003535 else
3536 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003537 while (HASHITEM_EMPTY(hi))
3538 ++hi;
3539 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003541 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003543 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 return (char_u *)"b:changedtick";
3545 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003546
3547 /* w: variables */
3548 ht = &curwin->w_vars.dv_hashtab;
3549 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003551 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003553 else
3554 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003555 while (HASHITEM_EMPTY(hi))
3556 ++hi;
3557 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003559
3560 /* v: variables */
3561 if (vidx < VV_LEN)
3562 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 vim_free(varnamebuf);
3565 varnamebuf = NULL;
3566 varnamebuflen = 0;
3567 return NULL;
3568}
3569
3570#endif /* FEAT_CMDL_COMPL */
3571
3572/*
3573 * types for expressions.
3574 */
3575typedef enum
3576{
3577 TYPE_UNKNOWN = 0
3578 , TYPE_EQUAL /* == */
3579 , TYPE_NEQUAL /* != */
3580 , TYPE_GREATER /* > */
3581 , TYPE_GEQUAL /* >= */
3582 , TYPE_SMALLER /* < */
3583 , TYPE_SEQUAL /* <= */
3584 , TYPE_MATCH /* =~ */
3585 , TYPE_NOMATCH /* !~ */
3586} exptype_T;
3587
3588/*
3589 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003590 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3592 */
3593
3594/*
3595 * Handle zero level expression.
3596 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003597 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003598 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 * Return OK or FAIL.
3600 */
3601 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003602eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u **nextcmd;
3606 int evaluate;
3607{
3608 int ret;
3609 char_u *p;
3610
3611 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003612 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (ret == FAIL || !ends_excmd(*p))
3614 {
3615 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 /*
3618 * Report the invalid expression unless the expression evaluation has
3619 * been cancelled due to an aborting error, an interrupt, or an
3620 * exception.
3621 */
3622 if (!aborting())
3623 EMSG2(_(e_invexpr2), arg);
3624 ret = FAIL;
3625 }
3626 if (nextcmd != NULL)
3627 *nextcmd = check_nextcmd(p);
3628
3629 return ret;
3630}
3631
3632/*
3633 * Handle top level expression:
3634 * expr1 ? expr0 : expr0
3635 *
3636 * "arg" must point to the first non-white of the expression.
3637 * "arg" is advanced to the next non-white after the recognized expression.
3638 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003639 * Note: "rettv.v_lock" is not set.
3640 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 * Return OK or FAIL.
3642 */
3643 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003644eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 int evaluate;
3648{
3649 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003650 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 /*
3653 * Get the first variable.
3654 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return FAIL;
3657
3658 if ((*arg)[0] == '?')
3659 {
3660 result = FALSE;
3661 if (evaluate)
3662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003663 int error = FALSE;
3664
3665 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003667 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003668 if (error)
3669 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671
3672 /*
3673 * Get the second variable.
3674 */
3675 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return FAIL;
3678
3679 /*
3680 * Check for the ":".
3681 */
3682 if ((*arg)[0] != ':')
3683 {
3684 EMSG(_("E109: Missing ':' after '?'"));
3685 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003686 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return FAIL;
3688 }
3689
3690 /*
3691 * Get the third variable.
3692 */
3693 *arg = skipwhite(*arg + 1);
3694 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3695 {
3696 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 return FAIL;
3699 }
3700 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003701 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703
3704 return OK;
3705}
3706
3707/*
3708 * Handle first level expression:
3709 * expr2 || expr2 || expr2 logical OR
3710 *
3711 * "arg" must point to the first non-white of the expression.
3712 * "arg" is advanced to the next non-white after the recognized expression.
3713 *
3714 * Return OK or FAIL.
3715 */
3716 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 int evaluate;
3721{
Bram Moolenaar33570922005-01-25 22:26:29 +00003722 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 long result;
3724 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003725 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 /*
3728 * Get the first variable.
3729 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003730 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 return FAIL;
3732
3733 /*
3734 * Repeat until there is no following "||".
3735 */
3736 first = TRUE;
3737 result = FALSE;
3738 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3739 {
3740 if (evaluate && first)
3741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003742 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003744 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003745 if (error)
3746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 first = FALSE;
3748 }
3749
3750 /*
3751 * Get the second variable.
3752 */
3753 *arg = skipwhite(*arg + 2);
3754 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3755 return FAIL;
3756
3757 /*
3758 * Compute the result.
3759 */
3760 if (evaluate && !result)
3761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003764 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 if (error)
3766 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
3768 if (evaluate)
3769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 rettv->v_type = VAR_NUMBER;
3771 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 }
3774
3775 return OK;
3776}
3777
3778/*
3779 * Handle second level expression:
3780 * expr3 && expr3 && expr3 logical AND
3781 *
3782 * "arg" must point to the first non-white of the expression.
3783 * "arg" is advanced to the next non-white after the recognized expression.
3784 *
3785 * Return OK or FAIL.
3786 */
3787 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003788eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 int evaluate;
3792{
Bram Moolenaar33570922005-01-25 22:26:29 +00003793 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 long result;
3795 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003796 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798 /*
3799 * Get the first variable.
3800 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return FAIL;
3803
3804 /*
3805 * Repeat until there is no following "&&".
3806 */
3807 first = TRUE;
3808 result = TRUE;
3809 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3810 {
3811 if (evaluate && first)
3812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003816 if (error)
3817 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 first = FALSE;
3819 }
3820
3821 /*
3822 * Get the second variable.
3823 */
3824 *arg = skipwhite(*arg + 2);
3825 if (eval4(arg, &var2, evaluate && result) == FAIL)
3826 return FAIL;
3827
3828 /*
3829 * Compute the result.
3830 */
3831 if (evaluate && result)
3832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003833 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003836 if (error)
3837 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 if (evaluate)
3840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 rettv->v_type = VAR_NUMBER;
3842 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844 }
3845
3846 return OK;
3847}
3848
3849/*
3850 * Handle third level expression:
3851 * var1 == var2
3852 * var1 =~ var2
3853 * var1 != var2
3854 * var1 !~ var2
3855 * var1 > var2
3856 * var1 >= var2
3857 * var1 < var2
3858 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003859 * var1 is var2
3860 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 *
3862 * "arg" must point to the first non-white of the expression.
3863 * "arg" is advanced to the next non-white after the recognized expression.
3864 *
3865 * Return OK or FAIL.
3866 */
3867 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 int evaluate;
3872{
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 char_u *p;
3875 int i;
3876 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003877 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 int len = 2;
3879 long n1, n2;
3880 char_u *s1, *s2;
3881 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3882 regmatch_T regmatch;
3883 int ic;
3884 char_u *save_cpo;
3885
3886 /*
3887 * Get the first variable.
3888 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return FAIL;
3891
3892 p = *arg;
3893 switch (p[0])
3894 {
3895 case '=': if (p[1] == '=')
3896 type = TYPE_EQUAL;
3897 else if (p[1] == '~')
3898 type = TYPE_MATCH;
3899 break;
3900 case '!': if (p[1] == '=')
3901 type = TYPE_NEQUAL;
3902 else if (p[1] == '~')
3903 type = TYPE_NOMATCH;
3904 break;
3905 case '>': if (p[1] != '=')
3906 {
3907 type = TYPE_GREATER;
3908 len = 1;
3909 }
3910 else
3911 type = TYPE_GEQUAL;
3912 break;
3913 case '<': if (p[1] != '=')
3914 {
3915 type = TYPE_SMALLER;
3916 len = 1;
3917 }
3918 else
3919 type = TYPE_SEQUAL;
3920 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003921 case 'i': if (p[1] == 's')
3922 {
3923 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3924 len = 5;
3925 if (!vim_isIDc(p[len]))
3926 {
3927 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3928 type_is = TRUE;
3929 }
3930 }
3931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933
3934 /*
3935 * If there is a comparitive operator, use it.
3936 */
3937 if (type != TYPE_UNKNOWN)
3938 {
3939 /* extra question mark appended: ignore case */
3940 if (p[len] == '?')
3941 {
3942 ic = TRUE;
3943 ++len;
3944 }
3945 /* extra '#' appended: match case */
3946 else if (p[len] == '#')
3947 {
3948 ic = FALSE;
3949 ++len;
3950 }
3951 /* nothing appened: use 'ignorecase' */
3952 else
3953 ic = p_ic;
3954
3955 /*
3956 * Get the second variable.
3957 */
3958 *arg = skipwhite(p + len);
3959 if (eval5(arg, &var2, evaluate) == FAIL)
3960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003961 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 return FAIL;
3963 }
3964
3965 if (evaluate)
3966 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003967 if (type_is && rettv->v_type != var2.v_type)
3968 {
3969 /* For "is" a different type always means FALSE, for "notis"
3970 * it means TRUE. */
3971 n1 = (type == TYPE_NEQUAL);
3972 }
3973 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3974 {
3975 if (type_is)
3976 {
3977 n1 = (rettv->v_type == var2.v_type
3978 && rettv->vval.v_list == var2.vval.v_list);
3979 if (type == TYPE_NEQUAL)
3980 n1 = !n1;
3981 }
3982 else if (rettv->v_type != var2.v_type
3983 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3984 {
3985 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003986 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003987 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003988 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003989 clear_tv(rettv);
3990 clear_tv(&var2);
3991 return FAIL;
3992 }
3993 else
3994 {
3995 /* Compare two Lists for being equal or unequal. */
3996 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3997 if (type == TYPE_NEQUAL)
3998 n1 = !n1;
3999 }
4000 }
4001
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004002 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4003 {
4004 if (type_is)
4005 {
4006 n1 = (rettv->v_type == var2.v_type
4007 && rettv->vval.v_dict == var2.vval.v_dict);
4008 if (type == TYPE_NEQUAL)
4009 n1 = !n1;
4010 }
4011 else if (rettv->v_type != var2.v_type
4012 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4013 {
4014 if (rettv->v_type != var2.v_type)
4015 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4016 else
4017 EMSG(_("E736: Invalid operation for Dictionary"));
4018 clear_tv(rettv);
4019 clear_tv(&var2);
4020 return FAIL;
4021 }
4022 else
4023 {
4024 /* Compare two Dictionaries for being equal or unequal. */
4025 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4026 if (type == TYPE_NEQUAL)
4027 n1 = !n1;
4028 }
4029 }
4030
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004031 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4032 {
4033 if (rettv->v_type != var2.v_type
4034 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4035 {
4036 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004037 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004038 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004039 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004040 clear_tv(rettv);
4041 clear_tv(&var2);
4042 return FAIL;
4043 }
4044 else
4045 {
4046 /* Compare two Funcrefs for being equal or unequal. */
4047 if (rettv->vval.v_string == NULL
4048 || var2.vval.v_string == NULL)
4049 n1 = FALSE;
4050 else
4051 n1 = STRCMP(rettv->vval.v_string,
4052 var2.vval.v_string) == 0;
4053 if (type == TYPE_NEQUAL)
4054 n1 = !n1;
4055 }
4056 }
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 /*
4059 * If one of the two variables is a number, compare as a number.
4060 * When using "=~" or "!~", always compare as string.
4061 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004062 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 n1 = get_tv_number(rettv);
4066 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 switch (type)
4068 {
4069 case TYPE_EQUAL: n1 = (n1 == n2); break;
4070 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4071 case TYPE_GREATER: n1 = (n1 > n2); break;
4072 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4073 case TYPE_SMALLER: n1 = (n1 < n2); break;
4074 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4075 case TYPE_UNKNOWN:
4076 case TYPE_MATCH:
4077 case TYPE_NOMATCH: break; /* avoid gcc warning */
4078 }
4079 }
4080 else
4081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 s1 = get_tv_string_buf(rettv, buf1);
4083 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4085 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4086 else
4087 i = 0;
4088 n1 = FALSE;
4089 switch (type)
4090 {
4091 case TYPE_EQUAL: n1 = (i == 0); break;
4092 case TYPE_NEQUAL: n1 = (i != 0); break;
4093 case TYPE_GREATER: n1 = (i > 0); break;
4094 case TYPE_GEQUAL: n1 = (i >= 0); break;
4095 case TYPE_SMALLER: n1 = (i < 0); break;
4096 case TYPE_SEQUAL: n1 = (i <= 0); break;
4097
4098 case TYPE_MATCH:
4099 case TYPE_NOMATCH:
4100 /* avoid 'l' flag in 'cpoptions' */
4101 save_cpo = p_cpo;
4102 p_cpo = (char_u *)"";
4103 regmatch.regprog = vim_regcomp(s2,
4104 RE_MAGIC + RE_STRING);
4105 regmatch.rm_ic = ic;
4106 if (regmatch.regprog != NULL)
4107 {
4108 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4109 vim_free(regmatch.regprog);
4110 if (type == TYPE_NOMATCH)
4111 n1 = !n1;
4112 }
4113 p_cpo = save_cpo;
4114 break;
4115
4116 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4117 }
4118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 clear_tv(rettv);
4120 clear_tv(&var2);
4121 rettv->v_type = VAR_NUMBER;
4122 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 }
4125
4126 return OK;
4127}
4128
4129/*
4130 * Handle fourth level expression:
4131 * + number addition
4132 * - number subtraction
4133 * . string concatenation
4134 *
4135 * "arg" must point to the first non-white of the expression.
4136 * "arg" is advanced to the next non-white after the recognized expression.
4137 *
4138 * Return OK or FAIL.
4139 */
4140 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int evaluate;
4145{
Bram Moolenaar33570922005-01-25 22:26:29 +00004146 typval_T var2;
4147 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int op;
4149 long n1, n2;
4150 char_u *s1, *s2;
4151 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4152 char_u *p;
4153
4154 /*
4155 * Get the first variable.
4156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Repeat computing, until no '+', '-' or '.' is following.
4162 */
4163 for (;;)
4164 {
4165 op = **arg;
4166 if (op != '+' && op != '-' && op != '.')
4167 break;
4168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 if (op != '+' || rettv->v_type != VAR_LIST)
4170 {
4171 /* For "list + ...", an illegal use of the first operand as
4172 * a number cannot be determined before evaluating the 2nd
4173 * operand: if this is also a list, all is ok.
4174 * For "something . ...", "something - ..." or "non-list + ...",
4175 * we know that the first operand needs to be a string or number
4176 * without evaluating the 2nd operand. So check before to avoid
4177 * side effects after an error. */
4178 if (evaluate && get_tv_string_chk(rettv) == NULL)
4179 {
4180 clear_tv(rettv);
4181 return FAIL;
4182 }
4183 }
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /*
4186 * Get the second variable.
4187 */
4188 *arg = skipwhite(*arg + 1);
4189 if (eval6(arg, &var2, evaluate) == FAIL)
4190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193 }
4194
4195 if (evaluate)
4196 {
4197 /*
4198 * Compute the result.
4199 */
4200 if (op == '.')
4201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4203 s2 = get_tv_string_buf_chk(&var2, buf2);
4204 if (s2 == NULL) /* type error ? */
4205 {
4206 clear_tv(rettv);
4207 clear_tv(&var2);
4208 return FAIL;
4209 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004210 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
4212 rettv->v_type = VAR_STRING;
4213 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004215 else if (op == '+' && rettv->v_type == VAR_LIST
4216 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004217 {
4218 /* concatenate Lists */
4219 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4220 &var3) == FAIL)
4221 {
4222 clear_tv(rettv);
4223 clear_tv(&var2);
4224 return FAIL;
4225 }
4226 clear_tv(rettv);
4227 *rettv = var3;
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 else
4230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231 int error = FALSE;
4232
4233 n1 = get_tv_number_chk(rettv, &error);
4234 if (error)
4235 {
4236 /* This can only happen for "list + non-list".
4237 * For "non-list + ..." or "something - ...", we returned
4238 * before evaluating the 2nd operand. */
4239 clear_tv(rettv);
4240 return FAIL;
4241 }
4242 n2 = get_tv_number_chk(&var2, &error);
4243 if (error)
4244 {
4245 clear_tv(rettv);
4246 clear_tv(&var2);
4247 return FAIL;
4248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (op == '+')
4251 n1 = n1 + n2;
4252 else
4253 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 rettv->v_type = VAR_NUMBER;
4255 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 }
4260 return OK;
4261}
4262
4263/*
4264 * Handle fifth level expression:
4265 * * number multiplication
4266 * / number division
4267 * % number modulo
4268 *
4269 * "arg" must point to the first non-white of the expression.
4270 * "arg" is advanced to the next non-white after the recognized expression.
4271 *
4272 * Return OK or FAIL.
4273 */
4274 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int evaluate;
4279{
Bram Moolenaar33570922005-01-25 22:26:29 +00004280 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 int op;
4282 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
4285 /*
4286 * Get the first variable.
4287 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return FAIL;
4290
4291 /*
4292 * Repeat computing, until no '*', '/' or '%' is following.
4293 */
4294 for (;;)
4295 {
4296 op = **arg;
4297 if (op != '*' && op != '/' && op != '%')
4298 break;
4299
4300 if (evaluate)
4301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 if (error)
4305 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 else
4308 n1 = 0;
4309
4310 /*
4311 * Get the second variable.
4312 */
4313 *arg = skipwhite(*arg + 1);
4314 if (eval7(arg, &var2, evaluate) == FAIL)
4315 return FAIL;
4316
4317 if (evaluate)
4318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (error)
4322 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 /*
4325 * Compute the result.
4326 */
4327 if (op == '*')
4328 n1 = n1 * n2;
4329 else if (op == '/')
4330 {
4331 if (n2 == 0) /* give an error message? */
4332 n1 = 0x7fffffffL;
4333 else
4334 n1 = n1 / n2;
4335 }
4336 else
4337 {
4338 if (n2 == 0) /* give an error message? */
4339 n1 = 0;
4340 else
4341 n1 = n1 % n2;
4342 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 rettv->v_type = VAR_NUMBER;
4344 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346 }
4347
4348 return OK;
4349}
4350
4351/*
4352 * Handle sixth level expression:
4353 * number number constant
4354 * "string" string contstant
4355 * 'string' literal string contstant
4356 * &option-name option value
4357 * @r register contents
4358 * identifier variable value
4359 * function() function call
4360 * $VAR environment variable
4361 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004362 * [expr, expr] List
4363 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 *
4365 * Also handle:
4366 * ! in front logical NOT
4367 * - in front unary minus
4368 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 * trailing [] subscript in String or List
4370 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *
4372 * "arg" must point to the first non-white of the expression.
4373 * "arg" is advanced to the next non-white after the recognized expression.
4374 *
4375 * Return OK or FAIL.
4376 */
4377 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 int evaluate;
4382{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 long n;
4384 int len;
4385 char_u *s;
4386 int val;
4387 char_u *start_leader, *end_leader;
4388 int ret = OK;
4389 char_u *alias;
4390
4391 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004393 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004395 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
4397 /*
4398 * Skip '!' and '-' characters. They are handled later.
4399 */
4400 start_leader = *arg;
4401 while (**arg == '!' || **arg == '-' || **arg == '+')
4402 *arg = skipwhite(*arg + 1);
4403 end_leader = *arg;
4404
4405 switch (**arg)
4406 {
4407 /*
4408 * Number constant.
4409 */
4410 case '0':
4411 case '1':
4412 case '2':
4413 case '3':
4414 case '4':
4415 case '5':
4416 case '6':
4417 case '7':
4418 case '8':
4419 case '9':
4420 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4421 *arg += len;
4422 if (evaluate)
4423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 rettv->v_type = VAR_NUMBER;
4425 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427 break;
4428
4429 /*
4430 * String constant: "string".
4431 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434
4435 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004439 break;
4440
4441 /*
4442 * List: [expr, expr]
4443 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 break;
4446
4447 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004448 * Dictionary: {key: val, key: val}
4449 */
4450 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4451 break;
4452
4453 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004454 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004456 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 break;
4458
4459 /*
4460 * Environment variable: $VAR.
4461 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 break;
4464
4465 /*
4466 * Register contents: @r.
4467 */
4468 case '@': ++*arg;
4469 if (evaluate)
4470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004472 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 if (**arg != NUL)
4475 ++*arg;
4476 break;
4477
4478 /*
4479 * nested expression: (expression).
4480 */
4481 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (**arg == ')')
4484 ++*arg;
4485 else if (ret == OK)
4486 {
4487 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004488 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 ret = FAIL;
4490 }
4491 break;
4492
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 break;
4495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496
4497 if (ret == NOTDONE)
4498 {
4499 /*
4500 * Must be a variable or function name.
4501 * Can also be a curly-braces kind of name: {expr}.
4502 */
4503 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004504 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 if (alias != NULL)
4506 s = alias;
4507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004508 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 ret = FAIL;
4510 else
4511 {
4512 if (**arg == '(') /* recursive! */
4513 {
4514 /* If "s" is the name of a variable of type VAR_FUNC
4515 * use its contents. */
4516 s = deref_func_name(s, &len);
4517
4518 /* Invoke the function. */
4519 ret = get_func_tv(s, len, rettv, arg,
4520 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004521 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 /* Stop the expression evaluation when immediately
4523 * aborting on error, or when an interrupt occurred or
4524 * an exception was thrown but not caught. */
4525 if (aborting())
4526 {
4527 if (ret == OK)
4528 clear_tv(rettv);
4529 ret = FAIL;
4530 }
4531 }
4532 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004533 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004534 else
4535 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 }
4537
4538 if (alias != NULL)
4539 vim_free(alias);
4540 }
4541
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 *arg = skipwhite(*arg);
4543
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004544 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4545 * expr(expr). */
4546 if (ret == OK)
4547 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 /*
4550 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4551 */
4552 if (ret == OK && evaluate && end_leader > start_leader)
4553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004554 int error = FALSE;
4555
4556 val = get_tv_number_chk(rettv, &error);
4557 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004559 clear_tv(rettv);
4560 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 else
4563 {
4564 while (end_leader > start_leader)
4565 {
4566 --end_leader;
4567 if (*end_leader == '!')
4568 val = !val;
4569 else if (*end_leader == '-')
4570 val = -val;
4571 }
4572 clear_tv(rettv);
4573 rettv->v_type = VAR_NUMBER;
4574 rettv->vval.v_number = val;
4575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
4577
4578 return ret;
4579}
4580
4581/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 * Evaluate an "[expr]" or "[expr:expr]" index.
4583 * "*arg" points to the '['.
4584 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4585 */
4586 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004587eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004589 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004591 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592{
4593 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004594 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 long len = -1;
4597 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004599 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004600
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004603 if (verbose)
4604 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 return FAIL;
4606 }
4607
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 /*
4611 * dict.name
4612 */
4613 key = *arg + 1;
4614 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4615 ;
4616 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004618 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 }
4620 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 /*
4623 * something[idx]
4624 *
4625 * Get the (first) variable from inside the [].
4626 */
4627 *arg = skipwhite(*arg + 1);
4628 if (**arg == ':')
4629 empty1 = TRUE;
4630 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4631 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4633 {
4634 /* not a number or string */
4635 clear_tv(&var1);
4636 return FAIL;
4637 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638
4639 /*
4640 * Get the second variable from inside the [:].
4641 */
4642 if (**arg == ':')
4643 {
4644 range = TRUE;
4645 *arg = skipwhite(*arg + 1);
4646 if (**arg == ']')
4647 empty2 = TRUE;
4648 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 if (!empty1)
4651 clear_tv(&var1);
4652 return FAIL;
4653 }
4654 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4655 {
4656 /* not a number or string */
4657 if (!empty1)
4658 clear_tv(&var1);
4659 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 return FAIL;
4661 }
4662 }
4663
4664 /* Check for the ']'. */
4665 if (**arg != ']')
4666 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004667 if (verbose)
4668 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669 clear_tv(&var1);
4670 if (range)
4671 clear_tv(&var2);
4672 return FAIL;
4673 }
4674 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 }
4676
4677 if (evaluate)
4678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 n1 = 0;
4680 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004682 n1 = get_tv_number(&var1);
4683 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 }
4685 if (range)
4686 {
4687 if (empty2)
4688 n2 = -1;
4689 else
4690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 n2 = get_tv_number(&var2);
4692 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004693 }
4694 }
4695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 {
4698 case VAR_NUMBER:
4699 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 len = (long)STRLEN(s);
4702 if (range)
4703 {
4704 /* The resulting variable is a substring. If the indexes
4705 * are out of range the result is empty. */
4706 if (n1 < 0)
4707 {
4708 n1 = len + n1;
4709 if (n1 < 0)
4710 n1 = 0;
4711 }
4712 if (n2 < 0)
4713 n2 = len + n2;
4714 else if (n2 >= len)
4715 n2 = len;
4716 if (n1 >= len || n2 < 0 || n1 > n2)
4717 s = NULL;
4718 else
4719 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4720 }
4721 else
4722 {
4723 /* The resulting variable is a string of a single
4724 * character. If the index is too big or negative the
4725 * result is empty. */
4726 if (n1 >= len || n1 < 0)
4727 s = NULL;
4728 else
4729 s = vim_strnsave(s + n1, 1);
4730 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 clear_tv(rettv);
4732 rettv->v_type = VAR_STRING;
4733 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 break;
4735
4736 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 if (n1 < 0)
4739 n1 = len + n1;
4740 if (!empty1 && (n1 < 0 || n1 >= len))
4741 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004742 if (verbose)
4743 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 return FAIL;
4745 }
4746 if (range)
4747 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004748 list_T *l;
4749 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750
4751 if (n2 < 0)
4752 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004755 if (verbose)
4756 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 return FAIL;
4758 }
4759 l = list_alloc();
4760 if (l == NULL)
4761 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 n1 <= n2; ++n1)
4764 {
4765 if (list_append_tv(l, &item->li_tv) == FAIL)
4766 {
4767 list_free(l);
4768 return FAIL;
4769 }
4770 item = item->li_next;
4771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 clear_tv(rettv);
4773 rettv->v_type = VAR_LIST;
4774 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004775 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 }
4777 else
4778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(rettv);
4782 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 }
4784 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785
4786 case VAR_DICT:
4787 if (range)
4788 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004789 if (verbose)
4790 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 if (len == -1)
4792 clear_tv(&var1);
4793 return FAIL;
4794 }
4795 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004796 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797
4798 if (len == -1)
4799 {
4800 key = get_tv_string(&var1);
4801 if (*key == NUL)
4802 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004803 if (verbose)
4804 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 clear_tv(&var1);
4806 return FAIL;
4807 }
4808 }
4809
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004810 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004811
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004812 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004813 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 if (len == -1)
4815 clear_tv(&var1);
4816 if (item == NULL)
4817 return FAIL;
4818
4819 copy_tv(&item->di_tv, &var1);
4820 clear_tv(rettv);
4821 *rettv = var1;
4822 }
4823 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 }
4825 }
4826
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 return OK;
4828}
4829
4830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 * Get an option value.
4832 * "arg" points to the '&' or '+' before the option name.
4833 * "arg" is advanced to character after the option name.
4834 * Return OK or FAIL.
4835 */
4836 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004839 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 int evaluate;
4841{
4842 char_u *option_end;
4843 long numval;
4844 char_u *stringval;
4845 int opt_type;
4846 int c;
4847 int working = (**arg == '+'); /* has("+option") */
4848 int ret = OK;
4849 int opt_flags;
4850
4851 /*
4852 * Isolate the option name and find its value.
4853 */
4854 option_end = find_option_end(arg, &opt_flags);
4855 if (option_end == NULL)
4856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 EMSG2(_("E112: Option name missing: %s"), *arg);
4859 return FAIL;
4860 }
4861
4862 if (!evaluate)
4863 {
4864 *arg = option_end;
4865 return OK;
4866 }
4867
4868 c = *option_end;
4869 *option_end = NUL;
4870 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 if (opt_type == -3) /* invalid name */
4874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 EMSG2(_("E113: Unknown option: %s"), *arg);
4877 ret = FAIL;
4878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 if (opt_type == -2) /* hidden string option */
4882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 rettv->v_type = VAR_STRING;
4884 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886 else if (opt_type == -1) /* hidden number option */
4887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 rettv->v_type = VAR_NUMBER;
4889 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
4891 else if (opt_type == 1) /* number option */
4892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 rettv->v_type = VAR_NUMBER;
4894 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
4896 else /* string option */
4897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 rettv->v_type = VAR_STRING;
4899 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901 }
4902 else if (working && (opt_type == -2 || opt_type == -1))
4903 ret = FAIL;
4904
4905 *option_end = c; /* put back for error messages */
4906 *arg = option_end;
4907
4908 return ret;
4909}
4910
4911/*
4912 * Allocate a variable for a string constant.
4913 * Return OK or FAIL.
4914 */
4915 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004916get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 int evaluate;
4920{
4921 char_u *p;
4922 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 int extra = 0;
4924
4925 /*
4926 * Find the end of the string, skipping backslashed characters.
4927 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004928 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
4930 if (*p == '\\' && p[1] != NUL)
4931 {
4932 ++p;
4933 /* A "\<x>" form occupies at least 4 characters, and produces up
4934 * to 6 characters: reserve space for 2 extra */
4935 if (*p == '<')
4936 extra += 2;
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939
4940 if (*p != '"')
4941 {
4942 EMSG2(_("E114: Missing quote: %s"), *arg);
4943 return FAIL;
4944 }
4945
4946 /* If only parsing, set *arg and return here */
4947 if (!evaluate)
4948 {
4949 *arg = p + 1;
4950 return OK;
4951 }
4952
4953 /*
4954 * Copy the string into allocated memory, handling backslashed
4955 * characters.
4956 */
4957 name = alloc((unsigned)(p - *arg + extra));
4958 if (name == NULL)
4959 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 rettv->v_type = VAR_STRING;
4961 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
4965 if (*p == '\\')
4966 {
4967 switch (*++p)
4968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 case 'b': *name++ = BS; ++p; break;
4970 case 'e': *name++ = ESC; ++p; break;
4971 case 'f': *name++ = FF; ++p; break;
4972 case 'n': *name++ = NL; ++p; break;
4973 case 'r': *name++ = CAR; ++p; break;
4974 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975
4976 case 'X': /* hex: "\x1", "\x12" */
4977 case 'x':
4978 case 'u': /* Unicode: "\u0023" */
4979 case 'U':
4980 if (vim_isxdigit(p[1]))
4981 {
4982 int n, nr;
4983 int c = toupper(*p);
4984
4985 if (c == 'X')
4986 n = 2;
4987 else
4988 n = 4;
4989 nr = 0;
4990 while (--n >= 0 && vim_isxdigit(p[1]))
4991 {
4992 ++p;
4993 nr = (nr << 4) + hex2nr(*p);
4994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996#ifdef FEAT_MBYTE
4997 /* For "\u" store the number according to
4998 * 'encoding'. */
4999 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 else
5002#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 break;
5006
5007 /* octal: "\1", "\12", "\123" */
5008 case '0':
5009 case '1':
5010 case '2':
5011 case '3':
5012 case '4':
5013 case '5':
5014 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 case '7': *name = *p++ - '0';
5016 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 *name = (*name << 3) + *p++ - '0';
5019 if (*p >= '0' && *p <= '7')
5020 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 break;
5024
5025 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (extra != 0)
5028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 break;
5031 }
5032 /* FALLTHROUGH */
5033
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036 }
5037 }
5038 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 *arg = p + 1;
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 return OK;
5046}
5047
5048/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 * Return OK or FAIL.
5051 */
5052 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int evaluate;
5057{
5058 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 int reduce = 0;
5061
5062 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 */
5065 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5066 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 break;
5071 ++reduce;
5072 ++p;
5073 }
5074 }
5075
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 return FAIL;
5080 }
5081
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 if (!evaluate)
5084 {
5085 *arg = p + 1;
5086 return OK;
5087 }
5088
5089 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 */
5092 str = alloc((unsigned)((p - *arg) - reduce));
5093 if (str == NULL)
5094 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 rettv->v_type = VAR_STRING;
5096 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 break;
5104 ++p;
5105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 *arg = p + 1;
5110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 return OK;
5112}
5113
5114/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 * Allocate a variable for a List and fill it from "*arg".
5116 * Return OK or FAIL.
5117 */
5118 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005121 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 int evaluate;
5123{
Bram Moolenaar33570922005-01-25 22:26:29 +00005124 list_T *l = NULL;
5125 typval_T tv;
5126 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127
5128 if (evaluate)
5129 {
5130 l = list_alloc();
5131 if (l == NULL)
5132 return FAIL;
5133 }
5134
5135 *arg = skipwhite(*arg + 1);
5136 while (**arg != ']' && **arg != NUL)
5137 {
5138 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5139 goto failret;
5140 if (evaluate)
5141 {
5142 item = listitem_alloc();
5143 if (item != NULL)
5144 {
5145 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005146 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 list_append(l, item);
5148 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005149 else
5150 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 }
5152
5153 if (**arg == ']')
5154 break;
5155 if (**arg != ',')
5156 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 goto failret;
5159 }
5160 *arg = skipwhite(*arg + 1);
5161 }
5162
5163 if (**arg != ']')
5164 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166failret:
5167 if (evaluate)
5168 list_free(l);
5169 return FAIL;
5170 }
5171
5172 *arg = skipwhite(*arg + 1);
5173 if (evaluate)
5174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 rettv->v_type = VAR_LIST;
5176 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 ++l->lv_refcount;
5178 }
5179
5180 return OK;
5181}
5182
5183/*
5184 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005185 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005187 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188list_alloc()
5189{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005190 list_T *l;
5191
5192 l = (list_T *)alloc_clear(sizeof(list_T));
5193 if (l != NULL)
5194 {
5195 /* Prepend the list to the list of lists for garbage collection. */
5196 if (first_list != NULL)
5197 first_list->lv_used_prev = l;
5198 l->lv_used_prev = NULL;
5199 l->lv_used_next = first_list;
5200 first_list = l;
5201 }
5202 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203}
5204
5205/*
5206 * Unreference a list: decrement the reference count and free it when it
5207 * becomes zero.
5208 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005209 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005211 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005213 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5214 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215}
5216
5217/*
5218 * Free a list, including all items it points to.
5219 * Ignores the reference count.
5220 */
5221 static void
5222list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005223 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224{
Bram Moolenaar33570922005-01-25 22:26:29 +00005225 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226
Bram Moolenaard9fba312005-06-26 22:34:35 +00005227 /* Avoid that recursive reference to the list frees us again. */
5228 l->lv_refcount = DEL_REFCOUNT;
5229
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230 /* Remove the list from the list of lists for garbage collection. */
5231 if (l->lv_used_prev == NULL)
5232 first_list = l->lv_used_next;
5233 else
5234 l->lv_used_prev->lv_used_next = l->lv_used_next;
5235 if (l->lv_used_next != NULL)
5236 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5237
Bram Moolenaard9fba312005-06-26 22:34:35 +00005238 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005240 /* Remove the item before deleting it. */
5241 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 listitem_free(item);
5243 }
5244 vim_free(l);
5245}
5246
5247/*
5248 * Allocate a list item.
5249 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005250 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251listitem_alloc()
5252{
Bram Moolenaar33570922005-01-25 22:26:29 +00005253 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254}
5255
5256/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005257 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 */
5259 static void
5260listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005263 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 vim_free(item);
5265}
5266
5267/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005268 * Remove a list item from a List and free it. Also clears the value.
5269 */
5270 static void
5271listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 list_T *l;
5273 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005274{
5275 list_remove(l, item, item);
5276 listitem_free(item);
5277}
5278
5279/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 * Get the number of items in a list.
5281 */
5282 static long
5283list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 if (l == NULL)
5287 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005288 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289}
5290
5291/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005292 * Return TRUE when two lists have exactly the same values.
5293 */
5294 static int
5295list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005296 list_T *l1;
5297 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005298 int ic; /* ignore case for strings */
5299{
Bram Moolenaar33570922005-01-25 22:26:29 +00005300 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005301
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005302 if (list_len(l1) != list_len(l2))
5303 return FALSE;
5304
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005305 for (item1 = l1->lv_first, item2 = l2->lv_first;
5306 item1 != NULL && item2 != NULL;
5307 item1 = item1->li_next, item2 = item2->li_next)
5308 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5309 return FALSE;
5310 return item1 == NULL && item2 == NULL;
5311}
5312
5313/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005314 * Return TRUE when two dictionaries have exactly the same key/values.
5315 */
5316 static int
5317dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005318 dict_T *d1;
5319 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005320 int ic; /* ignore case for strings */
5321{
Bram Moolenaar33570922005-01-25 22:26:29 +00005322 hashitem_T *hi;
5323 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005324 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005325
5326 if (dict_len(d1) != dict_len(d2))
5327 return FALSE;
5328
Bram Moolenaar33570922005-01-25 22:26:29 +00005329 todo = d1->dv_hashtab.ht_used;
5330 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005332 if (!HASHITEM_EMPTY(hi))
5333 {
5334 item2 = dict_find(d2, hi->hi_key, -1);
5335 if (item2 == NULL)
5336 return FALSE;
5337 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5338 return FALSE;
5339 --todo;
5340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005341 }
5342 return TRUE;
5343}
5344
5345/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005346 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005347 * Compares the items just like "==" would compare them, but strings and
5348 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005349 */
5350 static int
5351tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 typval_T *tv1;
5353 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005354 int ic; /* ignore case */
5355{
5356 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005357 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005358
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005359 if (tv1->v_type != tv2->v_type)
5360 return FALSE;
5361
5362 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005363 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005364 case VAR_LIST:
5365 /* recursive! */
5366 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5367
5368 case VAR_DICT:
5369 /* recursive! */
5370 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5371
5372 case VAR_FUNC:
5373 return (tv1->vval.v_string != NULL
5374 && tv2->vval.v_string != NULL
5375 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5376
5377 case VAR_NUMBER:
5378 return tv1->vval.v_number == tv2->vval.v_number;
5379
5380 case VAR_STRING:
5381 s1 = get_tv_string_buf(tv1, buf1);
5382 s2 = get_tv_string_buf(tv2, buf2);
5383 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005384 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005385
5386 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005387 return TRUE;
5388}
5389
5390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 * Locate item with index "n" in list "l" and return it.
5392 * A negative index is counted from the end; -1 is the last item.
5393 * Returns NULL when "n" is out of range.
5394 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 long n;
5399{
Bram Moolenaar33570922005-01-25 22:26:29 +00005400 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 long idx;
5402
5403 if (l == NULL)
5404 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005405
5406 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005408 n = l->lv_len + n;
5409
5410 /* Check for index out of range. */
5411 if (n < 0 || n >= l->lv_len)
5412 return NULL;
5413
5414 /* When there is a cached index may start search from there. */
5415 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005417 if (n < l->lv_idx / 2)
5418 {
5419 /* closest to the start of the list */
5420 item = l->lv_first;
5421 idx = 0;
5422 }
5423 else if (n > (l->lv_idx + l->lv_len) / 2)
5424 {
5425 /* closest to the end of the list */
5426 item = l->lv_last;
5427 idx = l->lv_len - 1;
5428 }
5429 else
5430 {
5431 /* closest to the cached index */
5432 item = l->lv_idx_item;
5433 idx = l->lv_idx;
5434 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 }
5436 else
5437 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005438 if (n < l->lv_len / 2)
5439 {
5440 /* closest to the start of the list */
5441 item = l->lv_first;
5442 idx = 0;
5443 }
5444 else
5445 {
5446 /* closest to the end of the list */
5447 item = l->lv_last;
5448 idx = l->lv_len - 1;
5449 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005451
5452 while (n > idx)
5453 {
5454 /* search forward */
5455 item = item->li_next;
5456 ++idx;
5457 }
5458 while (n < idx)
5459 {
5460 /* search backward */
5461 item = item->li_prev;
5462 --idx;
5463 }
5464
5465 /* cache the used index */
5466 l->lv_idx = idx;
5467 l->lv_idx_item = item;
5468
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 return item;
5470}
5471
5472/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005473 * Locate "item" list "l" and return its index.
5474 * Returns -1 when "item" is not in the list.
5475 */
5476 static long
5477list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005478 list_T *l;
5479 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005480{
5481 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005482 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005483
5484 if (l == NULL)
5485 return -1;
5486 idx = 0;
5487 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5488 ++idx;
5489 if (li == NULL)
5490 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005491 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005492}
5493
5494/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 * Append item "item" to the end of list "l".
5496 */
5497 static void
5498list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 list_T *l;
5500 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501{
5502 if (l->lv_last == NULL)
5503 {
5504 /* empty list */
5505 l->lv_first = item;
5506 l->lv_last = item;
5507 item->li_prev = NULL;
5508 }
5509 else
5510 {
5511 l->lv_last->li_next = item;
5512 item->li_prev = l->lv_last;
5513 l->lv_last = item;
5514 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005515 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 item->li_next = NULL;
5517}
5518
5519/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005521 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 */
5523 static int
5524list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005525 list_T *l;
5526 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005528 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529
Bram Moolenaar05159a02005-02-26 23:04:13 +00005530 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005532 copy_tv(tv, &li->li_tv);
5533 list_append(l, li);
5534 return OK;
5535}
5536
5537/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005538 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005539 * Return FAIL when out of memory.
5540 */
5541 int
5542list_append_dict(list, dict)
5543 list_T *list;
5544 dict_T *dict;
5545{
5546 listitem_T *li = listitem_alloc();
5547
5548 if (li == NULL)
5549 return FAIL;
5550 li->li_tv.v_type = VAR_DICT;
5551 li->li_tv.v_lock = 0;
5552 li->li_tv.vval.v_dict = dict;
5553 list_append(list, li);
5554 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 return OK;
5556}
5557
5558/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005559 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005560 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005561 * Returns FAIL when out of memory.
5562 */
5563 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005564list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005565 list_T *l;
5566 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005567 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005568{
5569 listitem_T *li = listitem_alloc();
5570
5571 if (li == NULL)
5572 return FAIL;
5573 list_append(l, li);
5574 li->li_tv.v_type = VAR_STRING;
5575 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005576 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5577 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005578 return FAIL;
5579 return OK;
5580}
5581
5582/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005583 * Append "n" to list "l".
5584 * Returns FAIL when out of memory.
5585 */
5586 static int
5587list_append_number(l, n)
5588 list_T *l;
5589 varnumber_T n;
5590{
5591 listitem_T *li;
5592
5593 li = listitem_alloc();
5594 if (li == NULL)
5595 return FAIL;
5596 li->li_tv.v_type = VAR_NUMBER;
5597 li->li_tv.v_lock = 0;
5598 li->li_tv.vval.v_number = n;
5599 list_append(l, li);
5600 return OK;
5601}
5602
5603/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005604 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005605 * If "item" is NULL append at the end.
5606 * Return FAIL when out of memory.
5607 */
5608 static int
5609list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005610 list_T *l;
5611 typval_T *tv;
5612 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005613{
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005615
5616 if (ni == NULL)
5617 return FAIL;
5618 copy_tv(tv, &ni->li_tv);
5619 if (item == NULL)
5620 /* Append new item at end of list. */
5621 list_append(l, ni);
5622 else
5623 {
5624 /* Insert new item before existing item. */
5625 ni->li_prev = item->li_prev;
5626 ni->li_next = item;
5627 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005628 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005629 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005630 ++l->lv_idx;
5631 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005632 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005633 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005634 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005635 l->lv_idx_item = NULL;
5636 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005637 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005638 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005639 }
5640 return OK;
5641}
5642
5643/*
5644 * Extend "l1" with "l2".
5645 * If "bef" is NULL append at the end, otherwise insert before this item.
5646 * Returns FAIL when out of memory.
5647 */
5648 static int
5649list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005650 list_T *l1;
5651 list_T *l2;
5652 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005653{
Bram Moolenaar33570922005-01-25 22:26:29 +00005654 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005655
5656 for (item = l2->lv_first; item != NULL; item = item->li_next)
5657 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5658 return FAIL;
5659 return OK;
5660}
5661
5662/*
5663 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5664 * Return FAIL when out of memory.
5665 */
5666 static int
5667list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005668 list_T *l1;
5669 list_T *l2;
5670 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005671{
Bram Moolenaar33570922005-01-25 22:26:29 +00005672 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005673
5674 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005675 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005676 if (l == NULL)
5677 return FAIL;
5678 tv->v_type = VAR_LIST;
5679 tv->vval.v_list = l;
5680
5681 /* append all items from the second list */
5682 return list_extend(l, l2, NULL);
5683}
5684
5685/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005686 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005688 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 * Returns NULL when out of memory.
5690 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005692list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005695 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696{
Bram Moolenaar33570922005-01-25 22:26:29 +00005697 list_T *copy;
5698 listitem_T *item;
5699 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700
5701 if (orig == NULL)
5702 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703
5704 copy = list_alloc();
5705 if (copy != NULL)
5706 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005707 if (copyID != 0)
5708 {
5709 /* Do this before adding the items, because one of the items may
5710 * refer back to this list. */
5711 orig->lv_copyID = copyID;
5712 orig->lv_copylist = copy;
5713 }
5714 for (item = orig->lv_first; item != NULL && !got_int;
5715 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 {
5717 ni = listitem_alloc();
5718 if (ni == NULL)
5719 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005720 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005721 {
5722 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5723 {
5724 vim_free(ni);
5725 break;
5726 }
5727 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005729 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 list_append(copy, ni);
5731 }
5732 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005733 if (item != NULL)
5734 {
5735 list_unref(copy);
5736 copy = NULL;
5737 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 }
5739
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 return copy;
5741}
5742
5743/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005744 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005745 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005747 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005748list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *l;
5750 listitem_T *item;
5751 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005752{
Bram Moolenaar33570922005-01-25 22:26:29 +00005753 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005755 /* notify watchers */
5756 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005758 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005759 list_fix_watch(l, ip);
5760 if (ip == item2)
5761 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005763
5764 if (item2->li_next == NULL)
5765 l->lv_last = item->li_prev;
5766 else
5767 item2->li_next->li_prev = item->li_prev;
5768 if (item->li_prev == NULL)
5769 l->lv_first = item2->li_next;
5770 else
5771 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005772 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773}
5774
5775/*
5776 * Return an allocated string with the string representation of a list.
5777 * May return NULL.
5778 */
5779 static char_u *
5780list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005781 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782{
5783 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784
5785 if (tv->vval.v_list == NULL)
5786 return NULL;
5787 ga_init2(&ga, (int)sizeof(char), 80);
5788 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005789 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5790 {
5791 vim_free(ga.ga_data);
5792 return NULL;
5793 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005794 ga_append(&ga, ']');
5795 ga_append(&ga, NUL);
5796 return (char_u *)ga.ga_data;
5797}
5798
5799/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 * Join list "l" into a string in "*gap", using separator "sep".
5801 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005802 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005804 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805list_join(gap, l, sep, echo)
5806 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005807 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005808 char_u *sep;
5809 int echo;
5810{
5811 int first = TRUE;
5812 char_u *tofree;
5813 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005814 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 char_u *s;
5816
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005817 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 {
5819 if (first)
5820 first = FALSE;
5821 else
5822 ga_concat(gap, sep);
5823
5824 if (echo)
5825 s = echo_string(&item->li_tv, &tofree, numbuf);
5826 else
5827 s = tv2string(&item->li_tv, &tofree, numbuf);
5828 if (s != NULL)
5829 ga_concat(gap, s);
5830 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005831 if (s == NULL)
5832 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005834 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835}
5836
5837/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005838 * Garbage collection for lists and dictionaries.
5839 *
5840 * We use reference counts to be able to free most items right away when they
5841 * are no longer used. But for composite items it's possible that it becomes
5842 * unused while the reference count is > 0: When there is a recursive
5843 * reference. Example:
5844 * :let l = [1, 2, 3]
5845 * :let d = {9: l}
5846 * :let l[1] = d
5847 *
5848 * Since this is quite unusual we handle this with garbage collection: every
5849 * once in a while find out which lists and dicts are not referenced from any
5850 * variable.
5851 *
5852 * Here is a good reference text about garbage collection (refers to Python
5853 * but it applies to all reference-counting mechanisms):
5854 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005855 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005856
5857/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005858 * Do garbage collection for lists and dicts.
5859 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005860 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005861 int
5862garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005863{
5864 dict_T *dd;
5865 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005866 int copyID = ++current_copyID;
5867 buf_T *buf;
5868 win_T *wp;
5869 int i;
5870 funccall_T *fc;
5871 int did_free = FALSE;
5872
5873 /*
5874 * 1. Go through all accessible variables and mark all lists and dicts
5875 * with copyID.
5876 */
5877 /* script-local variables */
5878 for (i = 1; i <= ga_scripts.ga_len; ++i)
5879 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5880
5881 /* buffer-local variables */
5882 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5883 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5884
5885 /* window-local variables */
5886 FOR_ALL_WINDOWS(wp)
5887 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5888
5889 /* global variables */
5890 set_ref_in_ht(&globvarht, copyID);
5891
5892 /* function-local variables */
5893 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5894 {
5895 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5896 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5897 }
5898
5899 /*
5900 * 2. Go through the list of dicts and free items without the copyID.
5901 */
5902 for (dd = first_dict; dd != NULL; )
5903 if (dd->dv_copyID != copyID)
5904 {
5905 dict_free(dd);
5906 did_free = TRUE;
5907
5908 /* restart, next dict may also have been freed */
5909 dd = first_dict;
5910 }
5911 else
5912 dd = dd->dv_used_next;
5913
5914 /*
5915 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005916 * But don't free a list that has a watcher (used in a for loop), these
5917 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005918 */
5919 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005920 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005921 {
5922 list_free(ll);
5923 did_free = TRUE;
5924
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005925 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005926 ll = first_list;
5927 }
5928 else
5929 ll = ll->lv_used_next;
5930
5931 return did_free;
5932}
5933
5934/*
5935 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5936 */
5937 static void
5938set_ref_in_ht(ht, copyID)
5939 hashtab_T *ht;
5940 int copyID;
5941{
5942 int todo;
5943 hashitem_T *hi;
5944
5945 todo = ht->ht_used;
5946 for (hi = ht->ht_array; todo > 0; ++hi)
5947 if (!HASHITEM_EMPTY(hi))
5948 {
5949 --todo;
5950 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5951 }
5952}
5953
5954/*
5955 * Mark all lists and dicts referenced through list "l" with "copyID".
5956 */
5957 static void
5958set_ref_in_list(l, copyID)
5959 list_T *l;
5960 int copyID;
5961{
5962 listitem_T *li;
5963
5964 for (li = l->lv_first; li != NULL; li = li->li_next)
5965 set_ref_in_item(&li->li_tv, copyID);
5966}
5967
5968/*
5969 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5970 */
5971 static void
5972set_ref_in_item(tv, copyID)
5973 typval_T *tv;
5974 int copyID;
5975{
5976 dict_T *dd;
5977 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005978
5979 switch (tv->v_type)
5980 {
5981 case VAR_DICT:
5982 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005983 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005984 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005985 /* Didn't see this dict yet. */
5986 dd->dv_copyID = copyID;
5987 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005988 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005989 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005990
5991 case VAR_LIST:
5992 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005993 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005994 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005995 /* Didn't see this list yet. */
5996 ll->lv_copyID = copyID;
5997 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005998 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005999 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006000 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006001 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006002}
6003
6004/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006005 * Allocate an empty header for a dictionary.
6006 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006007 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008dict_alloc()
6009{
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006011
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006013 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006014 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006015 /* Add the list to the hashtable for garbage collection. */
6016 if (first_dict != NULL)
6017 first_dict->dv_used_prev = d;
6018 d->dv_used_next = first_dict;
6019 d->dv_used_prev = NULL;
6020
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006022 d->dv_lock = 0;
6023 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006024 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006025 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006026 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006027}
6028
6029/*
6030 * Unreference a Dictionary: decrement the reference count and free it when it
6031 * becomes zero.
6032 */
6033 static void
6034dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006036{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6038 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006039}
6040
6041/*
6042 * Free a Dictionary, including all items it contains.
6043 * Ignores the reference count.
6044 */
6045 static void
6046dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006048{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006049 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006051 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006052
Bram Moolenaard9fba312005-06-26 22:34:35 +00006053 /* Avoid that recursive reference to the dict frees us again. */
6054 d->dv_refcount = DEL_REFCOUNT;
6055
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 /* Remove the dict from the list of dicts for garbage collection. */
6057 if (d->dv_used_prev == NULL)
6058 first_dict = d->dv_used_next;
6059 else
6060 d->dv_used_prev->dv_used_next = d->dv_used_next;
6061 if (d->dv_used_next != NULL)
6062 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6063
6064 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006065 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 todo = d->dv_hashtab.ht_used;
6067 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006068 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006069 if (!HASHITEM_EMPTY(hi))
6070 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006071 /* Remove the item before deleting it, just in case there is
6072 * something recursive causing trouble. */
6073 di = HI2DI(hi);
6074 hash_remove(&d->dv_hashtab, hi);
6075 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006076 --todo;
6077 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006078 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006080 vim_free(d);
6081}
6082
6083/*
6084 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006085 * The "key" is copied to the new item.
6086 * Note that the value of the item "di_tv" still needs to be initialized!
6087 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006088 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006090dictitem_alloc(key)
6091 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006092{
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006094
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006096 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006098 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006099 di->di_flags = 0;
6100 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006102}
6103
6104/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006105 * Make a copy of a Dictionary item.
6106 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006108dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006110{
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006114 if (di != NULL)
6115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006116 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006117 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006118 copy_tv(&org->di_tv, &di->di_tv);
6119 }
6120 return di;
6121}
6122
6123/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124 * Remove item "item" from Dictionary "dict" and free it.
6125 */
6126 static void
6127dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 dict_T *dict;
6129 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006134 if (HASHITEM_EMPTY(hi))
6135 EMSG2(_(e_intern2), "dictitem_remove()");
6136 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006138 dictitem_free(item);
6139}
6140
6141/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006142 * Free a dict item. Also clears the value.
6143 */
6144 static void
6145dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006147{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006148 clear_tv(&item->di_tv);
6149 vim_free(item);
6150}
6151
6152/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006153 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6154 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006155 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006156 * Returns NULL when out of memory.
6157 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006159dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006161 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006162 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006163{
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 dict_T *copy;
6165 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006168
6169 if (orig == NULL)
6170 return NULL;
6171
6172 copy = dict_alloc();
6173 if (copy != NULL)
6174 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006175 if (copyID != 0)
6176 {
6177 orig->dv_copyID = copyID;
6178 orig->dv_copydict = copy;
6179 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006181 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006182 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006183 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006185 --todo;
6186
6187 di = dictitem_alloc(hi->hi_key);
6188 if (di == NULL)
6189 break;
6190 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006191 {
6192 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6193 copyID) == FAIL)
6194 {
6195 vim_free(di);
6196 break;
6197 }
6198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006199 else
6200 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6201 if (dict_add(copy, di) == FAIL)
6202 {
6203 dictitem_free(di);
6204 break;
6205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006208
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006210 if (todo > 0)
6211 {
6212 dict_unref(copy);
6213 copy = NULL;
6214 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006215 }
6216
6217 return copy;
6218}
6219
6220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006221 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006225dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006226 dict_T *d;
6227 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006228{
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006230}
6231
Bram Moolenaar8c711452005-01-14 21:53:12 +00006232/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006233 * Add a number or string entry to dictionary "d".
6234 * When "str" is NULL use number "nr", otherwise use "str".
6235 * Returns FAIL when out of memory and when key already exists.
6236 */
6237 int
6238dict_add_nr_str(d, key, nr, str)
6239 dict_T *d;
6240 char *key;
6241 long nr;
6242 char_u *str;
6243{
6244 dictitem_T *item;
6245
6246 item = dictitem_alloc((char_u *)key);
6247 if (item == NULL)
6248 return FAIL;
6249 item->di_tv.v_lock = 0;
6250 if (str == NULL)
6251 {
6252 item->di_tv.v_type = VAR_NUMBER;
6253 item->di_tv.vval.v_number = nr;
6254 }
6255 else
6256 {
6257 item->di_tv.v_type = VAR_STRING;
6258 item->di_tv.vval.v_string = vim_strsave(str);
6259 }
6260 if (dict_add(d, item) == FAIL)
6261 {
6262 dictitem_free(item);
6263 return FAIL;
6264 }
6265 return OK;
6266}
6267
6268/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006269 * Get the number of items in a Dictionary.
6270 */
6271 static long
6272dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006275 if (d == NULL)
6276 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006278}
6279
6280/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281 * Find item "key[len]" in Dictionary "d".
6282 * If "len" is negative use strlen(key).
6283 * Returns NULL when not found.
6284 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006288 char_u *key;
6289 int len;
6290{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006291#define AKEYLEN 200
6292 char_u buf[AKEYLEN];
6293 char_u *akey;
6294 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006296
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006297 if (len < 0)
6298 akey = key;
6299 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006301 tofree = akey = vim_strnsave(key, len);
6302 if (akey == NULL)
6303 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006304 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006305 else
6306 {
6307 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006308 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006309 akey = buf;
6310 }
6311
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006313 vim_free(tofree);
6314 if (HASHITEM_EMPTY(hi))
6315 return NULL;
6316 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006317}
6318
6319/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006320 * Get a string item from a dictionary in allocated memory.
6321 * Returns NULL if the entry doesn't exist or out of memory.
6322 */
6323 char_u *
6324get_dict_string(d, key)
6325 dict_T *d;
6326 char_u *key;
6327{
6328 dictitem_T *di;
6329
6330 di = dict_find(d, key, -1);
6331 if (di == NULL)
6332 return NULL;
6333 return vim_strsave(get_tv_string(&di->di_tv));
6334}
6335
6336/*
6337 * Get a number item from a dictionary.
6338 * Returns 0 if the entry doesn't exist or out of memory.
6339 */
6340 long
6341get_dict_number(d, key)
6342 dict_T *d;
6343 char_u *key;
6344{
6345 dictitem_T *di;
6346
6347 di = dict_find(d, key, -1);
6348 if (di == NULL)
6349 return 0;
6350 return get_tv_number(&di->di_tv);
6351}
6352
6353/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354 * Return an allocated string with the string representation of a Dictionary.
6355 * May return NULL.
6356 */
6357 static char_u *
6358dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006360{
6361 garray_T ga;
6362 int first = TRUE;
6363 char_u *tofree;
6364 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006366 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006369
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006370 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371 return NULL;
6372 ga_init2(&ga, (int)sizeof(char), 80);
6373 ga_append(&ga, '{');
6374
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006378 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006379 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006380 --todo;
6381
6382 if (first)
6383 first = FALSE;
6384 else
6385 ga_concat(&ga, (char_u *)", ");
6386
6387 tofree = string_quote(hi->hi_key, FALSE);
6388 if (tofree != NULL)
6389 {
6390 ga_concat(&ga, tofree);
6391 vim_free(tofree);
6392 }
6393 ga_concat(&ga, (char_u *)": ");
6394 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6395 if (s != NULL)
6396 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006397 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006398 if (s == NULL)
6399 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006400 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006402 if (todo > 0)
6403 {
6404 vim_free(ga.ga_data);
6405 return NULL;
6406 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006407
6408 ga_append(&ga, '}');
6409 ga_append(&ga, NUL);
6410 return (char_u *)ga.ga_data;
6411}
6412
6413/*
6414 * Allocate a variable for a Dictionary and fill it from "*arg".
6415 * Return OK or FAIL. Returns NOTDONE for {expr}.
6416 */
6417 static int
6418get_dict_tv(arg, rettv, evaluate)
6419 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421 int evaluate;
6422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 dict_T *d = NULL;
6424 typval_T tvkey;
6425 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006429 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430
6431 /*
6432 * First check if it's not a curly-braces thing: {expr}.
6433 * Must do this without evaluating, otherwise a function may be called
6434 * twice. Unfortunately this means we need to call eval1() twice for the
6435 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006436 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006437 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438 if (*start != '}')
6439 {
6440 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6441 return FAIL;
6442 if (*start == '}')
6443 return NOTDONE;
6444 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006445
6446 if (evaluate)
6447 {
6448 d = dict_alloc();
6449 if (d == NULL)
6450 return FAIL;
6451 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006452 tvkey.v_type = VAR_UNKNOWN;
6453 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006454
6455 *arg = skipwhite(*arg + 1);
6456 while (**arg != '}' && **arg != NUL)
6457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006458 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006459 goto failret;
6460 if (**arg != ':')
6461 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006462 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006463 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006464 goto failret;
6465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006466 key = get_tv_string_buf_chk(&tvkey, buf);
6467 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006469 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6470 if (key != NULL)
6471 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006472 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006473 goto failret;
6474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006475
6476 *arg = skipwhite(*arg + 1);
6477 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006479 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006480 goto failret;
6481 }
6482 if (evaluate)
6483 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006484 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006485 if (item != NULL)
6486 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006487 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006488 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006489 clear_tv(&tv);
6490 goto failret;
6491 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006492 item = dictitem_alloc(key);
6493 clear_tv(&tvkey);
6494 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006496 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006497 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 if (dict_add(d, item) == FAIL)
6499 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 }
6501 }
6502
6503 if (**arg == '}')
6504 break;
6505 if (**arg != ',')
6506 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006507 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508 goto failret;
6509 }
6510 *arg = skipwhite(*arg + 1);
6511 }
6512
6513 if (**arg != '}')
6514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006515 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006516failret:
6517 if (evaluate)
6518 dict_free(d);
6519 return FAIL;
6520 }
6521
6522 *arg = skipwhite(*arg + 1);
6523 if (evaluate)
6524 {
6525 rettv->v_type = VAR_DICT;
6526 rettv->vval.v_dict = d;
6527 ++d->dv_refcount;
6528 }
6529
6530 return OK;
6531}
6532
Bram Moolenaar8c711452005-01-14 21:53:12 +00006533/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 * Return a string with the string representation of a variable.
6535 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006537 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 * May return NULL;
6539 */
6540 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006541echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006545{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006546 static int recurse = 0;
6547 char_u *r = NULL;
6548
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006551 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 *tofree = NULL;
6553 return NULL;
6554 }
6555 ++recurse;
6556
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 switch (tv->v_type)
6558 {
6559 case VAR_FUNC:
6560 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006561 r = tv->vval.v_string;
6562 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 case VAR_LIST:
6564 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006565 r = *tofree;
6566 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006567 case VAR_DICT:
6568 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006569 r = *tofree;
6570 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006571 case VAR_STRING:
6572 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 *tofree = NULL;
6574 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006577 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006579 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580
6581 --recurse;
6582 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006583}
6584
6585/*
6586 * Return a string with the string representation of a variable.
6587 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6588 * "numbuf" is used for a number.
6589 * Puts quotes around strings, so that they can be parsed back by eval().
6590 * May return NULL;
6591 */
6592 static char_u *
6593tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006595 char_u **tofree;
6596 char_u *numbuf;
6597{
6598 switch (tv->v_type)
6599 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006600 case VAR_FUNC:
6601 *tofree = string_quote(tv->vval.v_string, TRUE);
6602 return *tofree;
6603 case VAR_STRING:
6604 *tofree = string_quote(tv->vval.v_string, FALSE);
6605 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006606 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006607 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006610 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006611 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006613 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614}
6615
6616/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 * Return string "str" in ' quotes, doubling ' characters.
6618 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006619 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006620 */
6621 static char_u *
6622string_quote(str, function)
6623 char_u *str;
6624 int function;
6625{
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006627 char_u *p, *r, *s;
6628
Bram Moolenaar33570922005-01-25 22:26:29 +00006629 len = (function ? 13 : 3);
6630 if (str != NULL)
6631 {
6632 len += STRLEN(str);
6633 for (p = str; *p != NUL; mb_ptr_adv(p))
6634 if (*p == '\'')
6635 ++len;
6636 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006637 s = r = alloc(len);
6638 if (r != NULL)
6639 {
6640 if (function)
6641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006642 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006643 r += 10;
6644 }
6645 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006646 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006647 if (str != NULL)
6648 for (p = str; *p != NUL; )
6649 {
6650 if (*p == '\'')
6651 *r++ = '\'';
6652 MB_COPY_CHAR(p, r);
6653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006655 if (function)
6656 *r++ = ')';
6657 *r++ = NUL;
6658 }
6659 return s;
6660}
6661
6662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 * Get the value of an environment variable.
6664 * "arg" is pointing to the '$'. It is advanced to after the name.
6665 * If the environment variable was not set, silently assume it is empty.
6666 * Always return OK.
6667 */
6668 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006669get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 int evaluate;
6673{
6674 char_u *string = NULL;
6675 int len;
6676 int cc;
6677 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006678 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680 ++*arg;
6681 name = *arg;
6682 len = get_env_len(arg);
6683 if (evaluate)
6684 {
6685 if (len != 0)
6686 {
6687 cc = name[len];
6688 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006689 /* first try vim_getenv(), fast for normal environment vars */
6690 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006692 {
6693 if (!mustfree)
6694 string = vim_strsave(string);
6695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 else
6697 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006698 if (mustfree)
6699 vim_free(string);
6700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 /* next try expanding things like $VIM and ${HOME} */
6702 string = expand_env_save(name - 1);
6703 if (string != NULL && *string == '$')
6704 {
6705 vim_free(string);
6706 string = NULL;
6707 }
6708 }
6709 name[len] = cc;
6710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006711 rettv->v_type = VAR_STRING;
6712 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 }
6714
6715 return OK;
6716}
6717
6718/*
6719 * Array with names and number of arguments of all internal functions
6720 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6721 */
6722static struct fst
6723{
6724 char *f_name; /* function name */
6725 char f_min_argc; /* minimal number of arguments */
6726 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006727 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729} functions[] =
6730{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006731 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {"append", 2, 2, f_append},
6733 {"argc", 0, 0, f_argc},
6734 {"argidx", 0, 0, f_argidx},
6735 {"argv", 1, 1, f_argv},
6736 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006737 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 {"bufexists", 1, 1, f_bufexists},
6739 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6740 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6741 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6742 {"buflisted", 1, 1, f_buflisted},
6743 {"bufloaded", 1, 1, f_bufloaded},
6744 {"bufname", 1, 1, f_bufname},
6745 {"bufnr", 1, 1, f_bufnr},
6746 {"bufwinnr", 1, 1, f_bufwinnr},
6747 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006748 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006749 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {"char2nr", 1, 1, f_char2nr},
6751 {"cindent", 1, 1, f_cindent},
6752 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006753#if defined(FEAT_INS_EXPAND)
6754 {"complete_add", 1, 1, f_complete_add},
6755 {"complete_check", 0, 0, f_complete_check},
6756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006758 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {"cscope_connection",0,3, f_cscope_connection},
6761 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {"delete", 1, 1, f_delete},
6764 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006765 {"diff_filler", 1, 1, f_diff_filler},
6766 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006767 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {"eventhandler", 0, 0, f_eventhandler},
6771 {"executable", 1, 1, f_executable},
6772 {"exists", 1, 1, f_exists},
6773 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006774 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6776 {"filereadable", 1, 1, f_filereadable},
6777 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006779 {"finddir", 1, 3, f_finddir},
6780 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 {"fnamemodify", 2, 2, f_fnamemodify},
6782 {"foldclosed", 1, 1, f_foldclosed},
6783 {"foldclosedend", 1, 1, f_foldclosedend},
6784 {"foldlevel", 1, 1, f_foldlevel},
6785 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006786 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006789 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006790 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006791 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {"getbufvar", 2, 2, f_getbufvar},
6793 {"getchar", 0, 1, f_getchar},
6794 {"getcharmod", 0, 0, f_getcharmod},
6795 {"getcmdline", 0, 0, f_getcmdline},
6796 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006797 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006799 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006800 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {"getfsize", 1, 1, f_getfsize},
6802 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006803 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006804 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006805 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006806 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {"getregtype", 0, 1, f_getregtype},
6808 {"getwinposx", 0, 0, f_getwinposx},
6809 {"getwinposy", 0, 0, f_getwinposy},
6810 {"getwinvar", 2, 2, f_getwinvar},
6811 {"glob", 1, 1, f_glob},
6812 {"globpath", 2, 2, f_globpath},
6813 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006814 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 {"hasmapto", 1, 2, f_hasmapto},
6816 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6817 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6818 {"histadd", 2, 2, f_histadd},
6819 {"histdel", 1, 2, f_histdel},
6820 {"histget", 1, 2, f_histget},
6821 {"histnr", 1, 1, f_histnr},
6822 {"hlID", 1, 1, f_hlID},
6823 {"hlexists", 1, 1, f_hlexists},
6824 {"hostname", 0, 0, f_hostname},
6825 {"iconv", 3, 3, f_iconv},
6826 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006827 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006828 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006830 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {"inputrestore", 0, 0, f_inputrestore},
6832 {"inputsave", 0, 0, f_inputsave},
6833 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006836 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006838 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006841 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {"libcall", 3, 3, f_libcall},
6843 {"libcallnr", 3, 3, f_libcallnr},
6844 {"line", 1, 1, f_line},
6845 {"line2byte", 1, 1, f_line2byte},
6846 {"lispindent", 1, 1, f_lispindent},
6847 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006848 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {"maparg", 1, 2, f_maparg},
6850 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006851 {"match", 2, 4, f_match},
6852 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006853 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006854 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006855 {"max", 1, 1, f_max},
6856 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006857#ifdef vim_mkdir
6858 {"mkdir", 1, 3, f_mkdir},
6859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {"mode", 0, 0, f_mode},
6861 {"nextnonblank", 1, 1, f_nextnonblank},
6862 {"nr2char", 1, 1, f_nr2char},
6863 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006864 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006865 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006866 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 {"remote_expr", 2, 3, f_remote_expr},
6868 {"remote_foreground", 1, 1, f_remote_foreground},
6869 {"remote_peek", 1, 2, f_remote_peek},
6870 {"remote_read", 1, 1, f_remote_read},
6871 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006872 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006874 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006876 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006878 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {"searchpair", 3, 5, f_searchpair},
6880 {"server2client", 2, 2, f_server2client},
6881 {"serverlist", 0, 0, f_serverlist},
6882 {"setbufvar", 3, 3, f_setbufvar},
6883 {"setcmdpos", 1, 1, f_setcmdpos},
6884 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006885 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {"setreg", 2, 3, f_setreg},
6887 {"setwinvar", 3, 3, f_setwinvar},
6888 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006889 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006890 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006891 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006892 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006893 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894#ifdef HAVE_STRFTIME
6895 {"strftime", 1, 2, f_strftime},
6896#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006897 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006898 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {"strlen", 1, 1, f_strlen},
6900 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006901 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {"strtrans", 1, 1, f_strtrans},
6903 {"submatch", 1, 1, f_submatch},
6904 {"substitute", 4, 4, f_substitute},
6905 {"synID", 3, 3, f_synID},
6906 {"synIDattr", 2, 3, f_synIDattr},
6907 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006908 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006909 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006910 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006912 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {"tolower", 1, 1, f_tolower},
6914 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006915 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006917 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {"virtcol", 1, 1, f_virtcol},
6919 {"visualmode", 0, 1, f_visualmode},
6920 {"winbufnr", 1, 1, f_winbufnr},
6921 {"wincol", 0, 0, f_wincol},
6922 {"winheight", 1, 1, f_winheight},
6923 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006924 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {"winrestcmd", 0, 0, f_winrestcmd},
6926 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006927 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928};
6929
6930#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6931
6932/*
6933 * Function given to ExpandGeneric() to obtain the list of internal
6934 * or user defined function names.
6935 */
6936 char_u *
6937get_function_name(xp, idx)
6938 expand_T *xp;
6939 int idx;
6940{
6941 static int intidx = -1;
6942 char_u *name;
6943
6944 if (idx == 0)
6945 intidx = -1;
6946 if (intidx < 0)
6947 {
6948 name = get_user_func_name(xp, idx);
6949 if (name != NULL)
6950 return name;
6951 }
6952 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6953 {
6954 STRCPY(IObuff, functions[intidx].f_name);
6955 STRCAT(IObuff, "(");
6956 if (functions[intidx].f_max_argc == 0)
6957 STRCAT(IObuff, ")");
6958 return IObuff;
6959 }
6960
6961 return NULL;
6962}
6963
6964/*
6965 * Function given to ExpandGeneric() to obtain the list of internal or
6966 * user defined variable or function names.
6967 */
6968/*ARGSUSED*/
6969 char_u *
6970get_expr_name(xp, idx)
6971 expand_T *xp;
6972 int idx;
6973{
6974 static int intidx = -1;
6975 char_u *name;
6976
6977 if (idx == 0)
6978 intidx = -1;
6979 if (intidx < 0)
6980 {
6981 name = get_function_name(xp, idx);
6982 if (name != NULL)
6983 return name;
6984 }
6985 return get_user_var_name(xp, ++intidx);
6986}
6987
6988#endif /* FEAT_CMDL_COMPL */
6989
6990/*
6991 * Find internal function in table above.
6992 * Return index, or -1 if not found
6993 */
6994 static int
6995find_internal_func(name)
6996 char_u *name; /* name of the function */
6997{
6998 int first = 0;
6999 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7000 int cmp;
7001 int x;
7002
7003 /*
7004 * Find the function name in the table. Binary search.
7005 */
7006 while (first <= last)
7007 {
7008 x = first + ((unsigned)(last - first) >> 1);
7009 cmp = STRCMP(name, functions[x].f_name);
7010 if (cmp < 0)
7011 last = x - 1;
7012 else if (cmp > 0)
7013 first = x + 1;
7014 else
7015 return x;
7016 }
7017 return -1;
7018}
7019
7020/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007021 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7022 * name it contains, otherwise return "name".
7023 */
7024 static char_u *
7025deref_func_name(name, lenp)
7026 char_u *name;
7027 int *lenp;
7028{
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007030 int cc;
7031
7032 cc = name[*lenp];
7033 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007034 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007035 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007039 {
7040 *lenp = 0;
7041 return (char_u *)""; /* just in case */
7042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007043 *lenp = STRLEN(v->di_tv.vval.v_string);
7044 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007045 }
7046
7047 return name;
7048}
7049
7050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 * Allocate a variable for the result of a function.
7052 * Return OK or FAIL.
7053 */
7054 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007055get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7056 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 char_u *name; /* name of the function */
7058 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 char_u **arg; /* argument, pointing to the '(' */
7061 linenr_T firstline; /* first line of range */
7062 linenr_T lastline; /* last line of range */
7063 int *doesrange; /* return: function handled range */
7064 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007065 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
7067 char_u *argp;
7068 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007069 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 int argcount = 0; /* number of arguments found */
7071
7072 /*
7073 * Get the arguments.
7074 */
7075 argp = *arg;
7076 while (argcount < MAX_FUNC_ARGS)
7077 {
7078 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7079 if (*argp == ')' || *argp == ',' || *argp == NUL)
7080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7082 {
7083 ret = FAIL;
7084 break;
7085 }
7086 ++argcount;
7087 if (*argp != ',')
7088 break;
7089 }
7090 if (*argp == ')')
7091 ++argp;
7092 else
7093 ret = FAIL;
7094
7095 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007096 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007097 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 {
7100 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007101 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007103 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
7106 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007107 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
7109 *arg = skipwhite(argp);
7110 return ret;
7111}
7112
7113
7114/*
7115 * Call a function with its resolved parameters
7116 * Return OK or FAIL.
7117 */
7118 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007119call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007120 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 char_u *name; /* name of the function */
7122 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 linenr_T firstline; /* first line of range */
7127 linenr_T lastline; /* last line of range */
7128 int *doesrange; /* return: function handled range */
7129 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133#define ERROR_UNKNOWN 0
7134#define ERROR_TOOMANY 1
7135#define ERROR_TOOFEW 2
7136#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007137#define ERROR_DICT 4
7138#define ERROR_NONE 5
7139#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 int error = ERROR_NONE;
7141 int i;
7142 int llen;
7143 ufunc_T *fp;
7144 int cc;
7145#define FLEN_FIXED 40
7146 char_u fname_buf[FLEN_FIXED + 1];
7147 char_u *fname;
7148
7149 /*
7150 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7151 * Change <SNR>123_name() to K_SNR 123_name().
7152 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7153 */
7154 cc = name[len];
7155 name[len] = NUL;
7156 llen = eval_fname_script(name);
7157 if (llen > 0)
7158 {
7159 fname_buf[0] = K_SPECIAL;
7160 fname_buf[1] = KS_EXTRA;
7161 fname_buf[2] = (int)KE_SNR;
7162 i = 3;
7163 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7164 {
7165 if (current_SID <= 0)
7166 error = ERROR_SCRIPT;
7167 else
7168 {
7169 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7170 i = (int)STRLEN(fname_buf);
7171 }
7172 }
7173 if (i + STRLEN(name + llen) < FLEN_FIXED)
7174 {
7175 STRCPY(fname_buf + i, name + llen);
7176 fname = fname_buf;
7177 }
7178 else
7179 {
7180 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7181 if (fname == NULL)
7182 error = ERROR_OTHER;
7183 else
7184 {
7185 mch_memmove(fname, fname_buf, (size_t)i);
7186 STRCPY(fname + i, name + llen);
7187 }
7188 }
7189 }
7190 else
7191 fname = name;
7192
7193 *doesrange = FALSE;
7194
7195
7196 /* execute the function if no errors detected and executing */
7197 if (evaluate && error == ERROR_NONE)
7198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007199 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 error = ERROR_UNKNOWN;
7201
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007202 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {
7204 /*
7205 * User defined function.
7206 */
7207 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007210 /* Trigger FuncUndefined event, may load the function. */
7211 if (fp == NULL
7212 && apply_autocmds(EVENT_FUNCUNDEFINED,
7213 fname, fname, TRUE, NULL)
7214 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007216 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 fp = find_func(fname);
7218 }
7219#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007220 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007221 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007222 {
7223 /* loaded a package, search for the function again */
7224 fp = find_func(fname);
7225 }
7226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (fp != NULL)
7228 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007229 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007231 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007233 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007235 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 else
7238 {
7239 /*
7240 * Call the user function.
7241 * Save and restore search patterns, script variables and
7242 * redo buffer.
7243 */
7244 save_search_patterns();
7245 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007246 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007247 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007248 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007249 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7250 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7251 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007252 /* Function was unreferenced while being used, free it
7253 * now. */
7254 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 restoreRedobuff();
7256 restore_search_patterns();
7257 error = ERROR_NONE;
7258 }
7259 }
7260 }
7261 else
7262 {
7263 /*
7264 * Find the function name in the table, call its implementation.
7265 */
7266 i = find_internal_func(fname);
7267 if (i >= 0)
7268 {
7269 if (argcount < functions[i].f_min_argc)
7270 error = ERROR_TOOFEW;
7271 else if (argcount > functions[i].f_max_argc)
7272 error = ERROR_TOOMANY;
7273 else
7274 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007276 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 error = ERROR_NONE;
7278 }
7279 }
7280 }
7281 /*
7282 * The function call (or "FuncUndefined" autocommand sequence) might
7283 * have been aborted by an error, an interrupt, or an explicitly thrown
7284 * exception that has not been caught so far. This situation can be
7285 * tested for by calling aborting(). For an error in an internal
7286 * function or for the "E132" error in call_user_func(), however, the
7287 * throw point at which the "force_abort" flag (temporarily reset by
7288 * emsg()) is normally updated has not been reached yet. We need to
7289 * update that flag first to make aborting() reliable.
7290 */
7291 update_force_abort();
7292 }
7293 if (error == ERROR_NONE)
7294 ret = OK;
7295
7296 /*
7297 * Report an error unless the argument evaluation or function call has been
7298 * cancelled due to an aborting error, an interrupt, or an exception.
7299 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 if (!aborting())
7301 {
7302 switch (error)
7303 {
7304 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007305 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 break;
7307 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007308 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 break;
7310 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007311 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 name);
7313 break;
7314 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007315 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316 name);
7317 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007319 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 name);
7321 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 }
7323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
7325 name[len] = cc;
7326 if (fname != name && fname != fname_buf)
7327 vim_free(fname);
7328
7329 return ret;
7330}
7331
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007332/*
7333 * Give an error message with a function name. Handle <SNR> things.
7334 */
7335 static void
7336emsg_funcname(msg, name)
7337 char *msg;
7338 char_u *name;
7339{
7340 char_u *p;
7341
7342 if (*name == K_SPECIAL)
7343 p = concat_str((char_u *)"<SNR>", name + 3);
7344 else
7345 p = name;
7346 EMSG2(_(msg), p);
7347 if (p != name)
7348 vim_free(p);
7349}
7350
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351/*********************************************
7352 * Implementation of the built-in functions
7353 */
7354
7355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007356 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 */
7358 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007359f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 typval_T *argvars;
7361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007365 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007368 if ((l = argvars[0].vval.v_list) != NULL
7369 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7370 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007371 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007372 }
7373 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007374 EMSG(_(e_listreq));
7375}
7376
7377/*
7378 * "append(lnum, string/list)" function
7379 */
7380 static void
7381f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 typval_T *argvars;
7383 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007384{
7385 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007386 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 list_T *l = NULL;
7388 listitem_T *li = NULL;
7389 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007390 long added = 0;
7391
Bram Moolenaar0d660222005-01-07 21:51:51 +00007392 lnum = get_tv_lnum(argvars);
7393 if (lnum >= 0
7394 && lnum <= curbuf->b_ml.ml_line_count
7395 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007397 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007398 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007399 l = argvars[1].vval.v_list;
7400 if (l == NULL)
7401 return;
7402 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007403 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007404 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007405 for (;;)
7406 {
7407 if (l == NULL)
7408 tv = &argvars[1]; /* append a string */
7409 else if (li == NULL)
7410 break; /* end of list */
7411 else
7412 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007413 line = get_tv_string_chk(tv);
7414 if (line == NULL) /* type error */
7415 {
7416 rettv->vval.v_number = 1; /* Failed */
7417 break;
7418 }
7419 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007420 ++added;
7421 if (l == NULL)
7422 break;
7423 li = li->li_next;
7424 }
7425
7426 appended_lines_mark(lnum, added);
7427 if (curwin->w_cursor.lnum > lnum)
7428 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430 else
7431 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
7434/*
7435 * "argc()" function
7436 */
7437/* ARGSUSED */
7438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007439f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007440 typval_T *argvars;
7441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007443 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444}
7445
7446/*
7447 * "argidx()" function
7448 */
7449/* ARGSUSED */
7450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007451f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 typval_T *argvars;
7453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456}
7457
7458/*
7459 * "argv(nr)" function
7460 */
7461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007463 typval_T *argvars;
7464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
7466 int idx;
7467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007468 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472 rettv->vval.v_string = NULL;
7473 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474}
7475
7476/*
7477 * "browse(save, title, initdir, default)" function
7478 */
7479/* ARGSUSED */
7480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007481f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 typval_T *argvars;
7483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485#ifdef FEAT_BROWSE
7486 int save;
7487 char_u *title;
7488 char_u *initdir;
7489 char_u *defname;
7490 char_u buf[NUMBUFLEN];
7491 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007492 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007494 save = get_tv_number_chk(&argvars[0], &error);
7495 title = get_tv_string_chk(&argvars[1]);
7496 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7497 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007499 if (error || title == NULL || initdir == NULL || defname == NULL)
7500 rettv->vval.v_string = NULL;
7501 else
7502 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007503 do_browse(save ? BROWSE_SAVE : 0,
7504 title, defname, NULL, initdir, NULL, curbuf);
7505#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007507#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007509}
7510
7511/*
7512 * "browsedir(title, initdir)" function
7513 */
7514/* ARGSUSED */
7515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 typval_T *argvars;
7518 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007519{
7520#ifdef FEAT_BROWSE
7521 char_u *title;
7522 char_u *initdir;
7523 char_u buf[NUMBUFLEN];
7524
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007525 title = get_tv_string_chk(&argvars[0]);
7526 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007528 if (title == NULL || initdir == NULL)
7529 rettv->vval.v_string = NULL;
7530 else
7531 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007532 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537}
7538
Bram Moolenaar33570922005-01-25 22:26:29 +00007539static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541/*
7542 * Find a buffer by number or exact name.
7543 */
7544 static buf_T *
7545find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547{
7548 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007550 if (avar->v_type == VAR_NUMBER)
7551 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007552 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007554 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007555 if (buf == NULL)
7556 {
7557 /* No full path name match, try a match with a URL or a "nofile"
7558 * buffer, these don't use the full path. */
7559 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7560 if (buf->b_fname != NULL
7561 && (path_with_url(buf->b_fname)
7562#ifdef FEAT_QUICKFIX
7563 || bt_nofile(buf)
7564#endif
7565 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007567 break;
7568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 }
7570 return buf;
7571}
7572
7573/*
7574 * "bufexists(expr)" function
7575 */
7576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007577f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 typval_T *argvars;
7579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007581 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582}
7583
7584/*
7585 * "buflisted(expr)" function
7586 */
7587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007588f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 typval_T *argvars;
7590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
7592 buf_T *buf;
7593
7594 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007595 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596}
7597
7598/*
7599 * "bufloaded(expr)" function
7600 */
7601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 typval_T *argvars;
7604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
7606 buf_T *buf;
7607
7608 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007609 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610}
7611
Bram Moolenaar33570922005-01-25 22:26:29 +00007612static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007613
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614/*
7615 * Get buffer by number or pattern.
7616 */
7617 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 int save_magic;
7623 char_u *save_cpo;
7624 buf_T *buf;
7625
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 if (tv->v_type == VAR_NUMBER)
7627 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007628 if (tv->v_type != VAR_STRING)
7629 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (name == NULL || *name == NUL)
7631 return curbuf;
7632 if (name[0] == '$' && name[1] == NUL)
7633 return lastbuf;
7634
7635 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7636 save_magic = p_magic;
7637 p_magic = TRUE;
7638 save_cpo = p_cpo;
7639 p_cpo = (char_u *)"";
7640
7641 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7642 TRUE, FALSE));
7643
7644 p_magic = save_magic;
7645 p_cpo = save_cpo;
7646
7647 /* If not found, try expanding the name, like done for bufexists(). */
7648 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
7651 return buf;
7652}
7653
7654/*
7655 * "bufname(expr)" function
7656 */
7657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 typval_T *argvars;
7660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
7662 buf_T *buf;
7663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007664 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 buf = get_buf_tv(&argvars[0]);
7667 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007669 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 --emsg_off;
7673}
7674
7675/*
7676 * "bufnr(expr)" function
7677 */
7678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 typval_T *argvars;
7681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 buf_T *buf;
7684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 --emsg_off;
7693}
7694
7695/*
7696 * "bufwinnr(nr)" function
7697 */
7698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007699f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 typval_T *argvars;
7701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
7703#ifdef FEAT_WINDOWS
7704 win_T *wp;
7705 int winnr = 0;
7706#endif
7707 buf_T *buf;
7708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007709 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007711 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712#ifdef FEAT_WINDOWS
7713 for (wp = firstwin; wp; wp = wp->w_next)
7714 {
7715 ++winnr;
7716 if (wp->w_buffer == buf)
7717 break;
7718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007719 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007721 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722#endif
7723 --emsg_off;
7724}
7725
7726/*
7727 * "byte2line(byte)" function
7728 */
7729/*ARGSUSED*/
7730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 typval_T *argvars;
7733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737#else
7738 long boff = 0;
7739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007740 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 (linenr_T)0, &boff);
7746#endif
7747}
7748
7749/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007750 * "byteidx()" function
7751 */
7752/*ARGSUSED*/
7753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 typval_T *argvars;
7756 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007757{
7758#ifdef FEAT_MBYTE
7759 char_u *t;
7760#endif
7761 char_u *str;
7762 long idx;
7763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007764 str = get_tv_string_chk(&argvars[0]);
7765 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007767 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007768 return;
7769
7770#ifdef FEAT_MBYTE
7771 t = str;
7772 for ( ; idx > 0; idx--)
7773 {
7774 if (*t == NUL) /* EOL reached */
7775 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007776 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007779#else
7780 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007782#endif
7783}
7784
7785/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007786 * "call(func, arglist)" function
7787 */
7788 static void
7789f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007790 typval_T *argvars;
7791 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007792{
7793 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007797 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007799
7800 rettv->vval.v_number = 0;
7801 if (argvars[1].v_type != VAR_LIST)
7802 {
7803 EMSG(_(e_listreq));
7804 return;
7805 }
7806 if (argvars[1].vval.v_list == NULL)
7807 return;
7808
7809 if (argvars[0].v_type == VAR_FUNC)
7810 func = argvars[0].vval.v_string;
7811 else
7812 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007813 if (*func == NUL)
7814 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007815
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816 if (argvars[2].v_type != VAR_UNKNOWN)
7817 {
7818 if (argvars[2].v_type != VAR_DICT)
7819 {
7820 EMSG(_(e_dictreq));
7821 return;
7822 }
7823 selfdict = argvars[2].vval.v_dict;
7824 }
7825
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007826 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7827 item = item->li_next)
7828 {
7829 if (argc == MAX_FUNC_ARGS)
7830 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007831 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007832 break;
7833 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007834 /* Make a copy of each argument. This is needed to be able to set
7835 * v_lock to VAR_FIXED in the copy without changing the original list.
7836 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007837 copy_tv(&item->li_tv, &argv[argc++]);
7838 }
7839
7840 if (item == NULL)
7841 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7843 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007844
7845 /* Free the arguments. */
7846 while (argc > 0)
7847 clear_tv(&argv[--argc]);
7848}
7849
7850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 * "char2nr(string)" function
7852 */
7853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007854f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 typval_T *argvars;
7856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858#ifdef FEAT_MBYTE
7859 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007860 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 else
7862#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007863 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864}
7865
7866/*
7867 * "cindent(lnum)" function
7868 */
7869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007871 typval_T *argvars;
7872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874#ifdef FEAT_CINDENT
7875 pos_T pos;
7876 linenr_T lnum;
7877
7878 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7881 {
7882 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 curwin->w_cursor = pos;
7885 }
7886 else
7887#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007888 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891/*
7892 * "col(string)" function
7893 */
7894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007896 typval_T *argvars;
7897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 colnr_T col = 0;
7900 pos_T *fp;
7901
7902 fp = var2fpos(&argvars[0], FALSE);
7903 if (fp != NULL)
7904 {
7905 if (fp->col == MAXCOL)
7906 {
7907 /* '> can be MAXCOL, get the length of the line then */
7908 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7909 col = STRLEN(ml_get(fp->lnum)) + 1;
7910 else
7911 col = MAXCOL;
7912 }
7913 else
7914 {
7915 col = fp->col + 1;
7916#ifdef FEAT_VIRTUALEDIT
7917 /* col(".") when the cursor is on the NUL at the end of the line
7918 * because of "coladd" can be seen as an extra column. */
7919 if (virtual_active() && fp == &curwin->w_cursor)
7920 {
7921 char_u *p = ml_get_cursor();
7922
7923 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7924 curwin->w_virtcol - curwin->w_cursor.coladd))
7925 {
7926# ifdef FEAT_MBYTE
7927 int l;
7928
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007929 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 col += l;
7931# else
7932 if (*p != NUL && p[1] == NUL)
7933 ++col;
7934# endif
7935 }
7936 }
7937#endif
7938 }
7939 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941}
7942
Bram Moolenaar572cb562005-08-05 21:35:02 +00007943#if defined(FEAT_INS_EXPAND)
7944/*
7945 * "complete_add()" function
7946 */
7947/*ARGSUSED*/
7948 static void
7949f_complete_add(argvars, rettv)
7950 typval_T *argvars;
7951 typval_T *rettv;
7952{
7953 char_u *s;
7954
7955 s = get_tv_string_chk(&argvars[0]);
7956 if (s != NULL)
7957 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7958}
7959
7960/*
7961 * "complete_check()" function
7962 */
7963/*ARGSUSED*/
7964 static void
7965f_complete_check(argvars, rettv)
7966 typval_T *argvars;
7967 typval_T *rettv;
7968{
7969 int saved = RedrawingDisabled;
7970
7971 RedrawingDisabled = 0;
7972 ins_compl_check_keys(0);
7973 rettv->vval.v_number = compl_interrupted;
7974 RedrawingDisabled = saved;
7975}
7976#endif
7977
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978/*
7979 * "confirm(message, buttons[, default [, type]])" function
7980 */
7981/*ARGSUSED*/
7982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 typval_T *argvars;
7985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7988 char_u *message;
7989 char_u *buttons = NULL;
7990 char_u buf[NUMBUFLEN];
7991 char_u buf2[NUMBUFLEN];
7992 int def = 1;
7993 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007994 char_u *typestr;
7995 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007997 message = get_tv_string_chk(&argvars[0]);
7998 if (message == NULL)
7999 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008000 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008002 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8003 if (buttons == NULL)
8004 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008005 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008007 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008008 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008010 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8011 if (typestr == NULL)
8012 error = TRUE;
8013 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008015 switch (TOUPPER_ASC(*typestr))
8016 {
8017 case 'E': type = VIM_ERROR; break;
8018 case 'Q': type = VIM_QUESTION; break;
8019 case 'I': type = VIM_INFO; break;
8020 case 'W': type = VIM_WARNING; break;
8021 case 'G': type = VIM_GENERIC; break;
8022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 }
8024 }
8025 }
8026 }
8027
8028 if (buttons == NULL || *buttons == NUL)
8029 buttons = (char_u *)_("&Ok");
8030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008031 if (error)
8032 rettv->vval.v_number = 0;
8033 else
8034 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 def, NULL);
8036#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008037 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038#endif
8039}
8040
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008041/*
8042 * "copy()" function
8043 */
8044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 typval_T *argvars;
8047 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008048{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008049 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051
8052/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008053 * "count()" function
8054 */
8055 static void
8056f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008057 typval_T *argvars;
8058 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008059{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008060 long n = 0;
8061 int ic = FALSE;
8062
Bram Moolenaare9a41262005-01-15 22:18:47 +00008063 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008064 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008065 listitem_T *li;
8066 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008067 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008068
Bram Moolenaare9a41262005-01-15 22:18:47 +00008069 if ((l = argvars[0].vval.v_list) != NULL)
8070 {
8071 li = l->lv_first;
8072 if (argvars[2].v_type != VAR_UNKNOWN)
8073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008074 int error = FALSE;
8075
8076 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008077 if (argvars[3].v_type != VAR_UNKNOWN)
8078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008079 idx = get_tv_number_chk(&argvars[3], &error);
8080 if (!error)
8081 {
8082 li = list_find(l, idx);
8083 if (li == NULL)
8084 EMSGN(_(e_listidx), idx);
8085 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008087 if (error)
8088 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008089 }
8090
8091 for ( ; li != NULL; li = li->li_next)
8092 if (tv_equal(&li->li_tv, &argvars[1], ic))
8093 ++n;
8094 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008095 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008096 else if (argvars[0].v_type == VAR_DICT)
8097 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 int todo;
8099 dict_T *d;
8100 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008101
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008102 if ((d = argvars[0].vval.v_dict) != NULL)
8103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008104 int error = FALSE;
8105
Bram Moolenaare9a41262005-01-15 22:18:47 +00008106 if (argvars[2].v_type != VAR_UNKNOWN)
8107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008108 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008109 if (argvars[3].v_type != VAR_UNKNOWN)
8110 EMSG(_(e_invarg));
8111 }
8112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008113 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008114 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008115 {
8116 if (!HASHITEM_EMPTY(hi))
8117 {
8118 --todo;
8119 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8120 ++n;
8121 }
8122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008123 }
8124 }
8125 else
8126 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008127 rettv->vval.v_number = n;
8128}
8129
8130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8132 *
8133 * Checks the existence of a cscope connection.
8134 */
8135/*ARGSUSED*/
8136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 typval_T *argvars;
8139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140{
8141#ifdef FEAT_CSCOPE
8142 int num = 0;
8143 char_u *dbpath = NULL;
8144 char_u *prepend = NULL;
8145 char_u buf[NUMBUFLEN];
8146
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008147 if (argvars[0].v_type != VAR_UNKNOWN
8148 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008150 num = (int)get_tv_number(&argvars[0]);
8151 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008152 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008153 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 }
8155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159#endif
8160}
8161
8162/*
8163 * "cursor(lnum, col)" function
8164 *
8165 * Moves the cursor to the specified line and column
8166 */
8167/*ARGSUSED*/
8168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 typval_T *argvars;
8171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 long line, col;
8174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008175 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 col = get_tv_number_chk(&argvars[1], NULL);
8177 if (line < 0 || col < 0)
8178 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (line > 0)
8180 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (col > 0)
8182 curwin->w_cursor.col = col - 1;
8183#ifdef FEAT_VIRTUALEDIT
8184 curwin->w_cursor.coladd = 0;
8185#endif
8186
8187 /* Make sure the cursor is in a valid position. */
8188 check_cursor();
8189#ifdef FEAT_MBYTE
8190 /* Correct cursor for multi-byte character. */
8191 if (has_mbyte)
8192 mb_adjust_cursor();
8193#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008194
8195 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196}
8197
8198/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008199 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 */
8201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008202f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 typval_T *argvars;
8204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008206 int noref = 0;
8207
8208 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008209 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008210 if (noref < 0 || noref > 1)
8211 EMSG(_(e_invarg));
8212 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008213 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214}
8215
8216/*
8217 * "delete()" function
8218 */
8219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008220f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 typval_T *argvars;
8222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
8224 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230/*
8231 * "did_filetype()" function
8232 */
8233/*ARGSUSED*/
8234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008236 typval_T *argvars;
8237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008240 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243#endif
8244}
8245
8246/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008247 * "diff_filler()" function
8248 */
8249/*ARGSUSED*/
8250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008252 typval_T *argvars;
8253 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008254{
8255#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008256 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008257#endif
8258}
8259
8260/*
8261 * "diff_hlID()" function
8262 */
8263/*ARGSUSED*/
8264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008265f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008266 typval_T *argvars;
8267 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008268{
8269#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008270 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008271 static linenr_T prev_lnum = 0;
8272 static int changedtick = 0;
8273 static int fnum = 0;
8274 static int change_start = 0;
8275 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008276 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008277 int filler_lines;
8278 int col;
8279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008280 if (lnum < 0) /* ignore type error in {lnum} arg */
8281 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008282 if (lnum != prev_lnum
8283 || changedtick != curbuf->b_changedtick
8284 || fnum != curbuf->b_fnum)
8285 {
8286 /* New line, buffer, change: need to get the values. */
8287 filler_lines = diff_check(curwin, lnum);
8288 if (filler_lines < 0)
8289 {
8290 if (filler_lines == -1)
8291 {
8292 change_start = MAXCOL;
8293 change_end = -1;
8294 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8295 hlID = HLF_ADD; /* added line */
8296 else
8297 hlID = HLF_CHD; /* changed line */
8298 }
8299 else
8300 hlID = HLF_ADD; /* added line */
8301 }
8302 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008303 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008304 prev_lnum = lnum;
8305 changedtick = curbuf->b_changedtick;
8306 fnum = curbuf->b_fnum;
8307 }
8308
8309 if (hlID == HLF_CHD || hlID == HLF_TXD)
8310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008312 if (col >= change_start && col <= change_end)
8313 hlID = HLF_TXD; /* changed text */
8314 else
8315 hlID = HLF_CHD; /* changed line */
8316 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008317 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008318#endif
8319}
8320
8321/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008322 * "empty({expr})" function
8323 */
8324 static void
8325f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008326 typval_T *argvars;
8327 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008328{
8329 int n;
8330
8331 switch (argvars[0].v_type)
8332 {
8333 case VAR_STRING:
8334 case VAR_FUNC:
8335 n = argvars[0].vval.v_string == NULL
8336 || *argvars[0].vval.v_string == NUL;
8337 break;
8338 case VAR_NUMBER:
8339 n = argvars[0].vval.v_number == 0;
8340 break;
8341 case VAR_LIST:
8342 n = argvars[0].vval.v_list == NULL
8343 || argvars[0].vval.v_list->lv_first == NULL;
8344 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 case VAR_DICT:
8346 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008348 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008349 default:
8350 EMSG2(_(e_intern2), "f_empty()");
8351 n = 0;
8352 }
8353
8354 rettv->vval.v_number = n;
8355}
8356
8357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 * "escape({string}, {chars})" function
8359 */
8360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 typval_T *argvars;
8363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364{
8365 char_u buf[NUMBUFLEN];
8366
Bram Moolenaar758711c2005-02-02 23:11:38 +00008367 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8368 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008369 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370}
8371
8372/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008373 * "eval()" function
8374 */
8375/*ARGSUSED*/
8376 static void
8377f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008378 typval_T *argvars;
8379 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008380{
8381 char_u *s;
8382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008383 s = get_tv_string_chk(&argvars[0]);
8384 if (s != NULL)
8385 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008387 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8388 {
8389 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008390 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008391 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008392 else if (*s != NUL)
8393 EMSG(_(e_trailing));
8394}
8395
8396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 * "eventhandler()" function
8398 */
8399/*ARGSUSED*/
8400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 typval_T *argvars;
8403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008405 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406}
8407
8408/*
8409 * "executable()" function
8410 */
8411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 typval_T *argvars;
8414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008416 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417}
8418
8419/*
8420 * "exists()" function
8421 */
8422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008424 typval_T *argvars;
8425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
8427 char_u *p;
8428 char_u *name;
8429 int n = FALSE;
8430 int len = 0;
8431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008432 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 if (*p == '$') /* environment variable */
8434 {
8435 /* first try "normal" environment variables (fast) */
8436 if (mch_getenv(p + 1) != NULL)
8437 n = TRUE;
8438 else
8439 {
8440 /* try expanding things like $VIM and ${HOME} */
8441 p = expand_env_save(p);
8442 if (p != NULL && *p != '$')
8443 n = TRUE;
8444 vim_free(p);
8445 }
8446 }
8447 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 else if (*p == '*') /* internal or user defined function */
8450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008451 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 }
8453 else if (*p == ':')
8454 {
8455 n = cmd_exists(p + 1);
8456 }
8457 else if (*p == '#')
8458 {
8459#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008460 if (p[1] == '#')
8461 n = autocmd_supported(p + 2);
8462 else
8463 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464#endif
8465 }
8466 else /* internal variable */
8467 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008468 char_u *tofree;
8469 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008471 /* get_name_len() takes care of expanding curly braces */
8472 name = p;
8473 len = get_name_len(&p, &tofree, TRUE, FALSE);
8474 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008476 if (tofree != NULL)
8477 name = tofree;
8478 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8479 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008481 /* handle d.key, l[idx], f(expr) */
8482 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8483 if (n)
8484 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 }
8486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008488 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492}
8493
8494/*
8495 * "expand()" function
8496 */
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 char_u *s;
8503 int len;
8504 char_u *errormsg;
8505 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8506 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008507 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509 rettv->v_type = VAR_STRING;
8510 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 if (*s == '%' || *s == '#' || *s == '<')
8512 {
8513 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 --emsg_off;
8516 }
8517 else
8518 {
8519 /* When the optional second argument is non-zero, don't remove matches
8520 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008521 if (argvars[1].v_type != VAR_UNKNOWN
8522 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008524 if (!error)
8525 {
8526 ExpandInit(&xpc);
8527 xpc.xp_context = EXPAND_FILES;
8528 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8529 ExpandCleanup(&xpc);
8530 }
8531 else
8532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 }
8534}
8535
8536/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008537 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008538 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008539 */
8540 static void
8541f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *argvars;
8543 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008544{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008545 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008546 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008547 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 list_T *l1, *l2;
8549 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008550 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008552
Bram Moolenaare9a41262005-01-15 22:18:47 +00008553 l1 = argvars[0].vval.v_list;
8554 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008555 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8556 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557 {
8558 if (argvars[2].v_type != VAR_UNKNOWN)
8559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008560 before = get_tv_number_chk(&argvars[2], &error);
8561 if (error)
8562 return; /* type error; errmsg already given */
8563
Bram Moolenaar758711c2005-02-02 23:11:38 +00008564 if (before == l1->lv_len)
8565 item = NULL;
8566 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008568 item = list_find(l1, before);
8569 if (item == NULL)
8570 {
8571 EMSGN(_(e_listidx), before);
8572 return;
8573 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 }
8575 }
8576 else
8577 item = NULL;
8578 list_extend(l1, l2, item);
8579
Bram Moolenaare9a41262005-01-15 22:18:47 +00008580 copy_tv(&argvars[0], rettv);
8581 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8584 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008585 dict_T *d1, *d2;
8586 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587 char_u *action;
8588 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008589 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008590 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008591
8592 d1 = argvars[0].vval.v_dict;
8593 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008594 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8595 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008596 {
8597 /* Check the third argument. */
8598 if (argvars[2].v_type != VAR_UNKNOWN)
8599 {
8600 static char *(av[]) = {"keep", "force", "error"};
8601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008602 action = get_tv_string_chk(&argvars[2]);
8603 if (action == NULL)
8604 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 for (i = 0; i < 3; ++i)
8606 if (STRCMP(action, av[i]) == 0)
8607 break;
8608 if (i == 3)
8609 {
8610 EMSGN(_(e_invarg2), action);
8611 return;
8612 }
8613 }
8614 else
8615 action = (char_u *)"force";
8616
8617 /* Go over all entries in the second dict and add them to the
8618 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 todo = d2->dv_hashtab.ht_used;
8620 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008622 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008624 --todo;
8625 di1 = dict_find(d1, hi2->hi_key, -1);
8626 if (di1 == NULL)
8627 {
8628 di1 = dictitem_copy(HI2DI(hi2));
8629 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8630 dictitem_free(di1);
8631 }
8632 else if (*action == 'e')
8633 {
8634 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8635 break;
8636 }
8637 else if (*action == 'f')
8638 {
8639 clear_tv(&di1->di_tv);
8640 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008642 }
8643 }
8644
Bram Moolenaare9a41262005-01-15 22:18:47 +00008645 copy_tv(&argvars[0], rettv);
8646 }
8647 }
8648 else
8649 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008650}
8651
8652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 * "filereadable()" function
8654 */
8655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008656f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 typval_T *argvars;
8658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 FILE *fd;
8661 char_u *p;
8662 int n;
8663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8666 {
8667 n = TRUE;
8668 fclose(fd);
8669 }
8670 else
8671 n = FALSE;
8672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
8676/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008677 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 * rights to write into.
8679 */
8680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 typval_T *argvars;
8683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008685 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686}
8687
Bram Moolenaar33570922005-01-25 22:26:29 +00008688static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008689
8690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008691findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008694 int dir;
8695{
8696#ifdef FEAT_SEARCHPATH
8697 char_u *fname;
8698 char_u *fresult = NULL;
8699 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8700 char_u *p;
8701 char_u pathbuf[NUMBUFLEN];
8702 int count = 1;
8703 int first = TRUE;
8704
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008705 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008706
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008709 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8710 if (p == NULL)
8711 count = -1; /* error */
8712 else
8713 {
8714 if (*p != NUL)
8715 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008717 if (argvars[2].v_type != VAR_UNKNOWN)
8718 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8719 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008720 }
8721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008724 do
8725 {
8726 vim_free(fresult);
8727 fresult = find_file_in_path_option(first ? fname : NULL,
8728 first ? (int)STRLEN(fname) : 0,
8729 0, first, path, dir, NULL);
8730 first = FALSE;
8731 } while (--count > 0 && fresult != NULL);
8732 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008735#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008737#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008739}
8740
Bram Moolenaar33570922005-01-25 22:26:29 +00008741static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8742static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008743
8744/*
8745 * Implementation of map() and filter().
8746 */
8747 static void
8748filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 typval_T *argvars;
8750 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008751 int map;
8752{
8753 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 listitem_T *li, *nli;
8756 list_T *l = NULL;
8757 dictitem_T *di;
8758 hashtab_T *ht;
8759 hashitem_T *hi;
8760 dict_T *d = NULL;
8761 typval_T save_val;
8762 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008763 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008764 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008765 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8766
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008767
8768 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008769 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008770 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008771 if ((l = argvars[0].vval.v_list) == NULL
8772 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008773 return;
8774 }
8775 else if (argvars[0].v_type == VAR_DICT)
8776 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008777 if ((d = argvars[0].vval.v_dict) == NULL
8778 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008779 return;
8780 }
8781 else
8782 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008783 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 return;
8785 }
8786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787 expr = get_tv_string_buf_chk(&argvars[1], buf);
8788 /* On type errors, the preceding call has already displayed an error
8789 * message. Avoid a misleading error message for an empty string that
8790 * was not passed as argument. */
8791 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008793 prepare_vimvar(VV_VAL, &save_val);
8794 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008798 prepare_vimvar(VV_KEY, &save_key);
8799 vimvars[VV_KEY].vv_type = VAR_STRING;
8800
8801 ht = &d->dv_hashtab;
8802 hash_lock(ht);
8803 todo = ht->ht_used;
8804 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806 if (!HASHITEM_EMPTY(hi))
8807 {
8808 --todo;
8809 di = HI2DI(hi);
8810 if (tv_check_lock(di->di_tv.v_lock, msg))
8811 break;
8812 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8813 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8814 break;
8815 if (!map && rem)
8816 dictitem_remove(d, di);
8817 clear_tv(&vimvars[VV_KEY].vv_tv);
8818 }
8819 }
8820 hash_unlock(ht);
8821
8822 restore_vimvar(VV_KEY, &save_key);
8823 }
8824 else
8825 {
8826 for (li = l->lv_first; li != NULL; li = nli)
8827 {
8828 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008829 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008830 nli = li->li_next;
8831 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008832 break;
8833 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008834 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008835 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008836 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008838 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008840
8841 copy_tv(&argvars[0], rettv);
8842}
8843
8844 static int
8845filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008846 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008847 char_u *expr;
8848 int map;
8849 int *remp;
8850{
Bram Moolenaar33570922005-01-25 22:26:29 +00008851 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 char_u *s;
8853
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008855 s = expr;
8856 if (eval1(&s, &rettv, TRUE) == FAIL)
8857 return FAIL;
8858 if (*s != NUL) /* check for trailing chars after expr */
8859 {
8860 EMSG2(_(e_invexpr2), s);
8861 return FAIL;
8862 }
8863 if (map)
8864 {
8865 /* map(): replace the list item value */
8866 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008867 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008868 *tv = rettv;
8869 }
8870 else
8871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 int error = FALSE;
8873
Bram Moolenaare9a41262005-01-15 22:18:47 +00008874 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008875 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008877 /* On type error, nothing has been removed; return FAIL to stop the
8878 * loop. The error message was given by get_tv_number_chk(). */
8879 if (error)
8880 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008881 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008883 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008884}
8885
8886/*
8887 * "filter()" function
8888 */
8889 static void
8890f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008891 typval_T *argvars;
8892 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008893{
8894 filter_map(argvars, rettv, FALSE);
8895}
8896
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008898 * "finddir({fname}[, {path}[, {count}]])" function
8899 */
8900 static void
8901f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *argvars;
8903 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008904{
8905 findfilendir(argvars, rettv, TRUE);
8906}
8907
8908/*
8909 * "findfile({fname}[, {path}[, {count}]])" function
8910 */
8911 static void
8912f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008913 typval_T *argvars;
8914 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008915{
8916 findfilendir(argvars, rettv, FALSE);
8917}
8918
8919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 * "fnamemodify({fname}, {mods})" function
8921 */
8922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *argvars;
8925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 char_u *fname;
8928 char_u *mods;
8929 int usedlen = 0;
8930 int len;
8931 char_u *fbuf = NULL;
8932 char_u buf[NUMBUFLEN];
8933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 fname = get_tv_string_chk(&argvars[0]);
8935 mods = get_tv_string_buf_chk(&argvars[1], buf);
8936 if (fname == NULL || mods == NULL)
8937 fname = NULL;
8938 else
8939 {
8940 len = (int)STRLEN(fname);
8941 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 vim_free(fbuf);
8950}
8951
Bram Moolenaar33570922005-01-25 22:26:29 +00008952static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954/*
8955 * "foldclosed()" function
8956 */
8957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 typval_T *argvars;
8960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 int end;
8962{
8963#ifdef FEAT_FOLDING
8964 linenr_T lnum;
8965 linenr_T first, last;
8966
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8969 {
8970 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8971 {
8972 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 return;
8977 }
8978 }
8979#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981}
8982
8983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008984 * "foldclosed()" function
8985 */
8986 static void
8987f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 typval_T *argvars;
8989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008990{
8991 foldclosed_both(argvars, rettv, FALSE);
8992}
8993
8994/*
8995 * "foldclosedend()" function
8996 */
8997 static void
8998f_foldclosedend(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 foldclosed_both(argvars, rettv, TRUE);
9003}
9004
9005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 * "foldlevel()" function
9007 */
9008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 typval_T *argvars;
9011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013#ifdef FEAT_FOLDING
9014 linenr_T lnum;
9015
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 else
9020#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022}
9023
9024/*
9025 * "foldtext()" function
9026 */
9027/*ARGSUSED*/
9028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009030 typval_T *argvars;
9031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033#ifdef FEAT_FOLDING
9034 linenr_T lnum;
9035 char_u *s;
9036 char_u *r;
9037 int len;
9038 char *txt;
9039#endif
9040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->v_type = VAR_STRING;
9042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009044 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9045 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9046 <= curbuf->b_ml.ml_line_count
9047 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 {
9049 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009050 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9051 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 {
9053 if (!linewhite(lnum))
9054 break;
9055 ++lnum;
9056 }
9057
9058 /* Find interesting text in this line. */
9059 s = skipwhite(ml_get(lnum));
9060 /* skip C comment-start */
9061 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009064 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009065 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009066 {
9067 s = skipwhite(ml_get(lnum + 1));
9068 if (*s == '*')
9069 s = skipwhite(s + 1);
9070 }
9071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 txt = _("+-%s%3ld lines: ");
9073 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009074 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 + 20 /* for %3ld */
9076 + STRLEN(s))); /* concatenated */
9077 if (r != NULL)
9078 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9080 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9081 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 len = (int)STRLEN(r);
9083 STRCAT(r, s);
9084 /* remove 'foldmarker' and 'commentstring' */
9085 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 }
9088 }
9089#endif
9090}
9091
9092/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009093 * "foldtextresult(lnum)" function
9094 */
9095/*ARGSUSED*/
9096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009100{
9101#ifdef FEAT_FOLDING
9102 linenr_T lnum;
9103 char_u *text;
9104 char_u buf[51];
9105 foldinfo_T foldinfo;
9106 int fold_count;
9107#endif
9108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 rettv->v_type = VAR_STRING;
9110 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009111#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 /* treat illegal types and illegal string values for {lnum} the same */
9114 if (lnum < 0)
9115 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009116 fold_count = foldedCount(curwin, lnum, &foldinfo);
9117 if (fold_count > 0)
9118 {
9119 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9120 &foldinfo, buf);
9121 if (text == buf)
9122 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009124 }
9125#endif
9126}
9127
9128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 * "foreground()" function
9130 */
9131/*ARGSUSED*/
9132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 typval_T *argvars;
9135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#ifdef FEAT_GUI
9139 if (gui.in_use)
9140 gui_mch_set_foreground();
9141#else
9142# ifdef WIN32
9143 win32_set_foreground();
9144# endif
9145#endif
9146}
9147
9148/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009149 * "function()" function
9150 */
9151/*ARGSUSED*/
9152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009154 typval_T *argvars;
9155 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009156{
9157 char_u *s;
9158
Bram Moolenaara7043832005-01-21 11:56:39 +00009159 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009161 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009162 EMSG2(_(e_invarg2), s);
9163 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009164 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009165 else
9166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_string = vim_strsave(s);
9168 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009169 }
9170}
9171
9172/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009173 * "garbagecollect()" function
9174 */
9175/*ARGSUSED*/
9176 static void
9177f_garbagecollect(argvars, rettv)
9178 typval_T *argvars;
9179 typval_T *rettv;
9180{
9181 garbage_collect();
9182}
9183
9184/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009185 * "get()" function
9186 */
9187 static void
9188f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009189 typval_T *argvars;
9190 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009191{
Bram Moolenaar33570922005-01-25 22:26:29 +00009192 listitem_T *li;
9193 list_T *l;
9194 dictitem_T *di;
9195 dict_T *d;
9196 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009197
Bram Moolenaare9a41262005-01-15 22:18:47 +00009198 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009199 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009200 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009202 int error = FALSE;
9203
9204 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9205 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009206 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009209 else if (argvars[0].v_type == VAR_DICT)
9210 {
9211 if ((d = argvars[0].vval.v_dict) != NULL)
9212 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009213 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009214 if (di != NULL)
9215 tv = &di->di_tv;
9216 }
9217 }
9218 else
9219 EMSG2(_(e_listdictarg), "get()");
9220
9221 if (tv == NULL)
9222 {
9223 if (argvars[2].v_type == VAR_UNKNOWN)
9224 rettv->vval.v_number = 0;
9225 else
9226 copy_tv(&argvars[2], rettv);
9227 }
9228 else
9229 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009230}
9231
Bram Moolenaar342337a2005-07-21 21:11:17 +00009232static 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 +00009233
9234/*
9235 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009236 * Return a range (from start to end) of lines in rettv from the specified
9237 * buffer.
9238 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009239 */
9240 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009241get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009242 buf_T *buf;
9243 linenr_T start;
9244 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009245 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009246 typval_T *rettv;
9247{
9248 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009249 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009250
Bram Moolenaar342337a2005-07-21 21:11:17 +00009251 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009252 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009253 l = list_alloc();
9254 if (l == NULL)
9255 return;
9256
9257 rettv->vval.v_list = l;
9258 rettv->v_type = VAR_LIST;
9259 ++l->lv_refcount;
9260 }
9261 else
9262 rettv->vval.v_number = 0;
9263
9264 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9265 return;
9266
9267 if (!retlist)
9268 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009269 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9270 p = ml_get_buf(buf, start, FALSE);
9271 else
9272 p = (char_u *)"";
9273
9274 rettv->v_type = VAR_STRING;
9275 rettv->vval.v_string = vim_strsave(p);
9276 }
9277 else
9278 {
9279 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009280 return;
9281
9282 if (start < 1)
9283 start = 1;
9284 if (end > buf->b_ml.ml_line_count)
9285 end = buf->b_ml.ml_line_count;
9286 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009287 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9288 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009289 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009290 }
9291}
9292
9293/*
9294 * "getbufline()" function
9295 */
9296 static void
9297f_getbufline(argvars, rettv)
9298 typval_T *argvars;
9299 typval_T *rettv;
9300{
9301 linenr_T lnum;
9302 linenr_T end;
9303 buf_T *buf;
9304
9305 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9306 ++emsg_off;
9307 buf = get_buf_tv(&argvars[0]);
9308 --emsg_off;
9309
Bram Moolenaar661b1822005-07-28 22:36:45 +00009310 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009311 if (argvars[2].v_type == VAR_UNKNOWN)
9312 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009313 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009314 end = get_tv_lnum_buf(&argvars[2], buf);
9315
Bram Moolenaar342337a2005-07-21 21:11:17 +00009316 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009317}
9318
Bram Moolenaar0d660222005-01-07 21:51:51 +00009319/*
9320 * "getbufvar()" function
9321 */
9322 static void
9323f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009324 typval_T *argvars;
9325 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009326{
9327 buf_T *buf;
9328 buf_T *save_curbuf;
9329 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009330 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9333 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009334 ++emsg_off;
9335 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336
9337 rettv->v_type = VAR_STRING;
9338 rettv->vval.v_string = NULL;
9339
9340 if (buf != NULL && varname != NULL)
9341 {
9342 if (*varname == '&') /* buffer-local-option */
9343 {
9344 /* set curbuf to be our buf, temporarily */
9345 save_curbuf = curbuf;
9346 curbuf = buf;
9347
9348 get_option_tv(&varname, rettv, TRUE);
9349
9350 /* restore previous notion of curbuf */
9351 curbuf = save_curbuf;
9352 }
9353 else
9354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 if (*varname == NUL)
9356 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9357 * scope prefix before the NUL byte is required by
9358 * find_var_in_ht(). */
9359 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009360 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009361 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009362 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009364 }
9365 }
9366
9367 --emsg_off;
9368}
9369
9370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 * "getchar()" function
9372 */
9373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009375 typval_T *argvars;
9376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377{
9378 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
9381 ++no_mapping;
9382 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009383 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 /* getchar(): blocking wait. */
9385 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 /* getchar(1): only check if char avail */
9388 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009389 else if (error || vpeekc() == NUL)
9390 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 n = 0;
9392 else
9393 /* getchar(0) and char avail: return char */
9394 n = safe_vgetc();
9395 --no_mapping;
9396 --allow_keys;
9397
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 if (IS_SPECIAL(n) || mod_mask != 0)
9400 {
9401 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9402 int i = 0;
9403
9404 /* Turn a special key into three bytes, plus modifier. */
9405 if (mod_mask != 0)
9406 {
9407 temp[i++] = K_SPECIAL;
9408 temp[i++] = KS_MODIFIER;
9409 temp[i++] = mod_mask;
9410 }
9411 if (IS_SPECIAL(n))
9412 {
9413 temp[i++] = K_SPECIAL;
9414 temp[i++] = K_SECOND(n);
9415 temp[i++] = K_THIRD(n);
9416 }
9417#ifdef FEAT_MBYTE
9418 else if (has_mbyte)
9419 i += (*mb_char2bytes)(n, temp + i);
9420#endif
9421 else
9422 temp[i++] = n;
9423 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->v_type = VAR_STRING;
9425 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427}
9428
9429/*
9430 * "getcharmod()" function
9431 */
9432/*ARGSUSED*/
9433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009435 typval_T *argvars;
9436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
9440
9441/*
9442 * "getcmdline()" function
9443 */
9444/*ARGSUSED*/
9445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009447 typval_T *argvars;
9448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 rettv->v_type = VAR_STRING;
9451 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452}
9453
9454/*
9455 * "getcmdpos()" function
9456 */
9457/*ARGSUSED*/
9458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464}
9465
9466/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009467 * "getcmdtype()" function
9468 */
9469/*ARGSUSED*/
9470 static void
9471f_getcmdtype(argvars, rettv)
9472 typval_T *argvars;
9473 typval_T *rettv;
9474{
9475 rettv->v_type = VAR_STRING;
9476 rettv->vval.v_string = alloc(2);
9477 if (rettv->vval.v_string != NULL)
9478 {
9479 rettv->vval.v_string[0] = get_cmdline_type();
9480 rettv->vval.v_string[1] = NUL;
9481 }
9482}
9483
9484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 * "getcwd()" function
9486 */
9487/*ARGSUSED*/
9488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009490 typval_T *argvars;
9491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
9493 char_u cwd[MAXPATHL];
9494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 else
9499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009502 if (rettv->vval.v_string != NULL)
9503 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504#endif
9505 }
9506}
9507
9508/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009509 * "getfontname()" function
9510 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009511/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009514 typval_T *argvars;
9515 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009516{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 rettv->v_type = VAR_STRING;
9518 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009519#ifdef FEAT_GUI
9520 if (gui.in_use)
9521 {
9522 GuiFont font;
9523 char_u *name = NULL;
9524
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009525 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009526 {
9527 /* Get the "Normal" font. Either the name saved by
9528 * hl_set_font_name() or from the font ID. */
9529 font = gui.norm_font;
9530 name = hl_get_font_name();
9531 }
9532 else
9533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009535 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9536 return;
9537 font = gui_mch_get_font(name, FALSE);
9538 if (font == NOFONT)
9539 return; /* Invalid font name, return empty string. */
9540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009542 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009543 gui_mch_free_font(font);
9544 }
9545#endif
9546}
9547
9548/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009549 * "getfperm({fname})" function
9550 */
9551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 typval_T *argvars;
9554 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009555{
9556 char_u *fname;
9557 struct stat st;
9558 char_u *perm = NULL;
9559 char_u flags[] = "rwx";
9560 int i;
9561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009563
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009565 if (mch_stat((char *)fname, &st) >= 0)
9566 {
9567 perm = vim_strsave((char_u *)"---------");
9568 if (perm != NULL)
9569 {
9570 for (i = 0; i < 9; i++)
9571 {
9572 if (st.st_mode & (1 << (8 - i)))
9573 perm[i] = flags[i % 3];
9574 }
9575 }
9576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009578}
9579
9580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 * "getfsize({fname})" function
9582 */
9583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009585 typval_T *argvars;
9586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 char_u *fname;
9589 struct stat st;
9590
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594
9595 if (mch_stat((char *)fname, &st) >= 0)
9596 {
9597 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 }
9602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604}
9605
9606/*
9607 * "getftime({fname})" function
9608 */
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 char_u *fname;
9615 struct stat st;
9616
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618
9619 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623}
9624
9625/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009626 * "getftype({fname})" function
9627 */
9628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 typval_T *argvars;
9631 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009632{
9633 char_u *fname;
9634 struct stat st;
9635 char_u *type = NULL;
9636 char *t;
9637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009641 if (mch_lstat((char *)fname, &st) >= 0)
9642 {
9643#ifdef S_ISREG
9644 if (S_ISREG(st.st_mode))
9645 t = "file";
9646 else if (S_ISDIR(st.st_mode))
9647 t = "dir";
9648# ifdef S_ISLNK
9649 else if (S_ISLNK(st.st_mode))
9650 t = "link";
9651# endif
9652# ifdef S_ISBLK
9653 else if (S_ISBLK(st.st_mode))
9654 t = "bdev";
9655# endif
9656# ifdef S_ISCHR
9657 else if (S_ISCHR(st.st_mode))
9658 t = "cdev";
9659# endif
9660# ifdef S_ISFIFO
9661 else if (S_ISFIFO(st.st_mode))
9662 t = "fifo";
9663# endif
9664# ifdef S_ISSOCK
9665 else if (S_ISSOCK(st.st_mode))
9666 t = "fifo";
9667# endif
9668 else
9669 t = "other";
9670#else
9671# ifdef S_IFMT
9672 switch (st.st_mode & S_IFMT)
9673 {
9674 case S_IFREG: t = "file"; break;
9675 case S_IFDIR: t = "dir"; break;
9676# ifdef S_IFLNK
9677 case S_IFLNK: t = "link"; break;
9678# endif
9679# ifdef S_IFBLK
9680 case S_IFBLK: t = "bdev"; break;
9681# endif
9682# ifdef S_IFCHR
9683 case S_IFCHR: t = "cdev"; break;
9684# endif
9685# ifdef S_IFIFO
9686 case S_IFIFO: t = "fifo"; break;
9687# endif
9688# ifdef S_IFSOCK
9689 case S_IFSOCK: t = "socket"; break;
9690# endif
9691 default: t = "other";
9692 }
9693# else
9694 if (mch_isdir(fname))
9695 t = "dir";
9696 else
9697 t = "file";
9698# endif
9699#endif
9700 type = vim_strsave((char_u *)t);
9701 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009703}
9704
9705/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009706 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009707 */
9708 static void
9709f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 typval_T *argvars;
9711 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009712{
9713 linenr_T lnum;
9714 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009715 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009716
9717 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009718 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009719 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009720 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009721 retlist = FALSE;
9722 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009723 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009724 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009725 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009726 retlist = TRUE;
9727 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009728
Bram Moolenaar342337a2005-07-21 21:11:17 +00009729 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009730}
9731
9732/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009733 * "getqflist()" function
9734 */
9735/*ARGSUSED*/
9736 static void
9737f_getqflist(argvars, rettv)
9738 typval_T *argvars;
9739 typval_T *rettv;
9740{
9741#ifdef FEAT_QUICKFIX
9742 list_T *l;
9743#endif
9744
9745 rettv->vval.v_number = FALSE;
9746#ifdef FEAT_QUICKFIX
9747 l = list_alloc();
9748 if (l != NULL)
9749 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009750 rettv->vval.v_list = l;
9751 rettv->v_type = VAR_LIST;
9752 ++l->lv_refcount;
9753 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009754 }
9755#endif
9756}
9757
9758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 * "getreg()" function
9760 */
9761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009763 typval_T *argvars;
9764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765{
9766 char_u *strregname;
9767 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009768 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009771 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009773 strregname = get_tv_string_chk(&argvars[0]);
9774 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009775 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009776 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009779 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 regname = (strregname == NULL ? '"' : *strregname);
9781 if (regname == 0)
9782 regname = '"';
9783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 rettv->vval.v_string = error ? NULL :
9786 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787}
9788
9789/*
9790 * "getregtype()" function
9791 */
9792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009794 typval_T *argvars;
9795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796{
9797 char_u *strregname;
9798 int regname;
9799 char_u buf[NUMBUFLEN + 2];
9800 long reglen = 0;
9801
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009802 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 {
9804 strregname = get_tv_string_chk(&argvars[0]);
9805 if (strregname == NULL) /* type error; errmsg already given */
9806 {
9807 rettv->v_type = VAR_STRING;
9808 rettv->vval.v_string = NULL;
9809 return;
9810 }
9811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 else
9813 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009814 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
9816 regname = (strregname == NULL ? '"' : *strregname);
9817 if (regname == 0)
9818 regname = '"';
9819
9820 buf[0] = NUL;
9821 buf[1] = NUL;
9822 switch (get_reg_type(regname, &reglen))
9823 {
9824 case MLINE: buf[0] = 'V'; break;
9825 case MCHAR: buf[0] = 'v'; break;
9826#ifdef FEAT_VISUAL
9827 case MBLOCK:
9828 buf[0] = Ctrl_V;
9829 sprintf((char *)buf + 1, "%ld", reglen + 1);
9830 break;
9831#endif
9832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 rettv->v_type = VAR_STRING;
9834 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835}
9836
9837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 * "getwinposx()" function
9839 */
9840/*ARGSUSED*/
9841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 typval_T *argvars;
9844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847#ifdef FEAT_GUI
9848 if (gui.in_use)
9849 {
9850 int x, y;
9851
9852 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 }
9855#endif
9856}
9857
9858/*
9859 * "getwinposy()" function
9860 */
9861/*ARGSUSED*/
9862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868#ifdef FEAT_GUI
9869 if (gui.in_use)
9870 {
9871 int x, y;
9872
9873 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 }
9876#endif
9877}
9878
Bram Moolenaara40058a2005-07-11 22:42:07 +00009879static win_T *find_win_by_nr __ARGS((typval_T *vp));
9880
9881 static win_T *
9882find_win_by_nr(vp)
9883 typval_T *vp;
9884{
9885#ifdef FEAT_WINDOWS
9886 win_T *wp;
9887#endif
9888 int nr;
9889
9890 nr = get_tv_number_chk(vp, NULL);
9891
9892#ifdef FEAT_WINDOWS
9893 if (nr < 0)
9894 return NULL;
9895 if (nr == 0)
9896 return curwin;
9897
9898 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9899 if (--nr <= 0)
9900 break;
9901 return wp;
9902#else
9903 if (nr == 0 || nr == 1)
9904 return curwin;
9905 return NULL;
9906#endif
9907}
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909/*
9910 * "getwinvar()" function
9911 */
9912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009913f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 typval_T *argvars;
9915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916{
9917 win_T *win, *oldcurwin;
9918 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 varname = get_tv_string_chk(&argvars[1]);
9923 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925 rettv->v_type = VAR_STRING;
9926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
9928 if (win != NULL && varname != NULL)
9929 {
9930 if (*varname == '&') /* window-local-option */
9931 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009932 /* Set curwin to be our win, temporarily. Also set curbuf, so
9933 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 oldcurwin = curwin;
9935 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009936 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
9940 /* restore previous notion of curwin */
9941 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009942 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 }
9944 else
9945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 if (*varname == NUL)
9947 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9948 * scope prefix before the NUL byte is required by
9949 * find_var_in_ht(). */
9950 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009952 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009954 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 }
9956 }
9957
9958 --emsg_off;
9959}
9960
9961/*
9962 * "glob()" function
9963 */
9964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009966 typval_T *argvars;
9967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
9969 expand_T xpc;
9970
9971 ExpandInit(&xpc);
9972 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 rettv->v_type = VAR_STRING;
9974 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9976 ExpandCleanup(&xpc);
9977}
9978
9979/*
9980 * "globpath()" function
9981 */
9982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *argvars;
9985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
9987 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009988 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009991 if (file == NULL)
9992 rettv->vval.v_string = NULL;
9993 else
9994 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
9996
9997/*
9998 * "has()" function
9999 */
10000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 typval_T *argvars;
10003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 int i;
10006 char_u *name;
10007 int n = FALSE;
10008 static char *(has_list[]) =
10009 {
10010#ifdef AMIGA
10011 "amiga",
10012# ifdef FEAT_ARP
10013 "arp",
10014# endif
10015#endif
10016#ifdef __BEOS__
10017 "beos",
10018#endif
10019#ifdef MSDOS
10020# ifdef DJGPP
10021 "dos32",
10022# else
10023 "dos16",
10024# endif
10025#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010026#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 "mac",
10028#endif
10029#if defined(MACOS_X_UNIX)
10030 "macunix",
10031#endif
10032#ifdef OS2
10033 "os2",
10034#endif
10035#ifdef __QNX__
10036 "qnx",
10037#endif
10038#ifdef RISCOS
10039 "riscos",
10040#endif
10041#ifdef UNIX
10042 "unix",
10043#endif
10044#ifdef VMS
10045 "vms",
10046#endif
10047#ifdef WIN16
10048 "win16",
10049#endif
10050#ifdef WIN32
10051 "win32",
10052#endif
10053#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10054 "win32unix",
10055#endif
10056#ifdef WIN64
10057 "win64",
10058#endif
10059#ifdef EBCDIC
10060 "ebcdic",
10061#endif
10062#ifndef CASE_INSENSITIVE_FILENAME
10063 "fname_case",
10064#endif
10065#ifdef FEAT_ARABIC
10066 "arabic",
10067#endif
10068#ifdef FEAT_AUTOCMD
10069 "autocmd",
10070#endif
10071#ifdef FEAT_BEVAL
10072 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010073# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10074 "balloon_multiline",
10075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076#endif
10077#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10078 "builtin_terms",
10079# ifdef ALL_BUILTIN_TCAPS
10080 "all_builtin_terms",
10081# endif
10082#endif
10083#ifdef FEAT_BYTEOFF
10084 "byte_offset",
10085#endif
10086#ifdef FEAT_CINDENT
10087 "cindent",
10088#endif
10089#ifdef FEAT_CLIENTSERVER
10090 "clientserver",
10091#endif
10092#ifdef FEAT_CLIPBOARD
10093 "clipboard",
10094#endif
10095#ifdef FEAT_CMDL_COMPL
10096 "cmdline_compl",
10097#endif
10098#ifdef FEAT_CMDHIST
10099 "cmdline_hist",
10100#endif
10101#ifdef FEAT_COMMENTS
10102 "comments",
10103#endif
10104#ifdef FEAT_CRYPT
10105 "cryptv",
10106#endif
10107#ifdef FEAT_CSCOPE
10108 "cscope",
10109#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010110#ifdef CURSOR_SHAPE
10111 "cursorshape",
10112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113#ifdef DEBUG
10114 "debug",
10115#endif
10116#ifdef FEAT_CON_DIALOG
10117 "dialog_con",
10118#endif
10119#ifdef FEAT_GUI_DIALOG
10120 "dialog_gui",
10121#endif
10122#ifdef FEAT_DIFF
10123 "diff",
10124#endif
10125#ifdef FEAT_DIGRAPHS
10126 "digraphs",
10127#endif
10128#ifdef FEAT_DND
10129 "dnd",
10130#endif
10131#ifdef FEAT_EMACS_TAGS
10132 "emacs_tags",
10133#endif
10134 "eval", /* always present, of course! */
10135#ifdef FEAT_EX_EXTRA
10136 "ex_extra",
10137#endif
10138#ifdef FEAT_SEARCH_EXTRA
10139 "extra_search",
10140#endif
10141#ifdef FEAT_FKMAP
10142 "farsi",
10143#endif
10144#ifdef FEAT_SEARCHPATH
10145 "file_in_path",
10146#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010147#if defined(UNIX) && !defined(USE_SYSTEM)
10148 "filterpipe",
10149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150#ifdef FEAT_FIND_ID
10151 "find_in_path",
10152#endif
10153#ifdef FEAT_FOLDING
10154 "folding",
10155#endif
10156#ifdef FEAT_FOOTER
10157 "footer",
10158#endif
10159#if !defined(USE_SYSTEM) && defined(UNIX)
10160 "fork",
10161#endif
10162#ifdef FEAT_GETTEXT
10163 "gettext",
10164#endif
10165#ifdef FEAT_GUI
10166 "gui",
10167#endif
10168#ifdef FEAT_GUI_ATHENA
10169# ifdef FEAT_GUI_NEXTAW
10170 "gui_neXtaw",
10171# else
10172 "gui_athena",
10173# endif
10174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175#ifdef FEAT_GUI_GTK
10176 "gui_gtk",
10177# ifdef HAVE_GTK2
10178 "gui_gtk2",
10179# endif
10180#endif
10181#ifdef FEAT_GUI_MAC
10182 "gui_mac",
10183#endif
10184#ifdef FEAT_GUI_MOTIF
10185 "gui_motif",
10186#endif
10187#ifdef FEAT_GUI_PHOTON
10188 "gui_photon",
10189#endif
10190#ifdef FEAT_GUI_W16
10191 "gui_win16",
10192#endif
10193#ifdef FEAT_GUI_W32
10194 "gui_win32",
10195#endif
10196#ifdef FEAT_HANGULIN
10197 "hangul_input",
10198#endif
10199#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10200 "iconv",
10201#endif
10202#ifdef FEAT_INS_EXPAND
10203 "insert_expand",
10204#endif
10205#ifdef FEAT_JUMPLIST
10206 "jumplist",
10207#endif
10208#ifdef FEAT_KEYMAP
10209 "keymap",
10210#endif
10211#ifdef FEAT_LANGMAP
10212 "langmap",
10213#endif
10214#ifdef FEAT_LIBCALL
10215 "libcall",
10216#endif
10217#ifdef FEAT_LINEBREAK
10218 "linebreak",
10219#endif
10220#ifdef FEAT_LISP
10221 "lispindent",
10222#endif
10223#ifdef FEAT_LISTCMDS
10224 "listcmds",
10225#endif
10226#ifdef FEAT_LOCALMAP
10227 "localmap",
10228#endif
10229#ifdef FEAT_MENU
10230 "menu",
10231#endif
10232#ifdef FEAT_SESSION
10233 "mksession",
10234#endif
10235#ifdef FEAT_MODIFY_FNAME
10236 "modify_fname",
10237#endif
10238#ifdef FEAT_MOUSE
10239 "mouse",
10240#endif
10241#ifdef FEAT_MOUSESHAPE
10242 "mouseshape",
10243#endif
10244#if defined(UNIX) || defined(VMS)
10245# ifdef FEAT_MOUSE_DEC
10246 "mouse_dec",
10247# endif
10248# ifdef FEAT_MOUSE_GPM
10249 "mouse_gpm",
10250# endif
10251# ifdef FEAT_MOUSE_JSB
10252 "mouse_jsbterm",
10253# endif
10254# ifdef FEAT_MOUSE_NET
10255 "mouse_netterm",
10256# endif
10257# ifdef FEAT_MOUSE_PTERM
10258 "mouse_pterm",
10259# endif
10260# ifdef FEAT_MOUSE_XTERM
10261 "mouse_xterm",
10262# endif
10263#endif
10264#ifdef FEAT_MBYTE
10265 "multi_byte",
10266#endif
10267#ifdef FEAT_MBYTE_IME
10268 "multi_byte_ime",
10269#endif
10270#ifdef FEAT_MULTI_LANG
10271 "multi_lang",
10272#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010273#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010274#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010275 "mzscheme",
10276#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278#ifdef FEAT_OLE
10279 "ole",
10280#endif
10281#ifdef FEAT_OSFILETYPE
10282 "osfiletype",
10283#endif
10284#ifdef FEAT_PATH_EXTRA
10285 "path_extra",
10286#endif
10287#ifdef FEAT_PERL
10288#ifndef DYNAMIC_PERL
10289 "perl",
10290#endif
10291#endif
10292#ifdef FEAT_PYTHON
10293#ifndef DYNAMIC_PYTHON
10294 "python",
10295#endif
10296#endif
10297#ifdef FEAT_POSTSCRIPT
10298 "postscript",
10299#endif
10300#ifdef FEAT_PRINTER
10301 "printer",
10302#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010303#ifdef FEAT_PROFILE
10304 "profile",
10305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306#ifdef FEAT_QUICKFIX
10307 "quickfix",
10308#endif
10309#ifdef FEAT_RIGHTLEFT
10310 "rightleft",
10311#endif
10312#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10313 "ruby",
10314#endif
10315#ifdef FEAT_SCROLLBIND
10316 "scrollbind",
10317#endif
10318#ifdef FEAT_CMDL_INFO
10319 "showcmd",
10320 "cmdline_info",
10321#endif
10322#ifdef FEAT_SIGNS
10323 "signs",
10324#endif
10325#ifdef FEAT_SMARTINDENT
10326 "smartindent",
10327#endif
10328#ifdef FEAT_SNIFF
10329 "sniff",
10330#endif
10331#ifdef FEAT_STL_OPT
10332 "statusline",
10333#endif
10334#ifdef FEAT_SUN_WORKSHOP
10335 "sun_workshop",
10336#endif
10337#ifdef FEAT_NETBEANS_INTG
10338 "netbeans_intg",
10339#endif
10340#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010341 "spell",
10342#endif
10343#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 "syntax",
10345#endif
10346#if defined(USE_SYSTEM) || !defined(UNIX)
10347 "system",
10348#endif
10349#ifdef FEAT_TAG_BINS
10350 "tag_binary",
10351#endif
10352#ifdef FEAT_TAG_OLDSTATIC
10353 "tag_old_static",
10354#endif
10355#ifdef FEAT_TAG_ANYWHITE
10356 "tag_any_white",
10357#endif
10358#ifdef FEAT_TCL
10359# ifndef DYNAMIC_TCL
10360 "tcl",
10361# endif
10362#endif
10363#ifdef TERMINFO
10364 "terminfo",
10365#endif
10366#ifdef FEAT_TERMRESPONSE
10367 "termresponse",
10368#endif
10369#ifdef FEAT_TEXTOBJ
10370 "textobjects",
10371#endif
10372#ifdef HAVE_TGETENT
10373 "tgetent",
10374#endif
10375#ifdef FEAT_TITLE
10376 "title",
10377#endif
10378#ifdef FEAT_TOOLBAR
10379 "toolbar",
10380#endif
10381#ifdef FEAT_USR_CMDS
10382 "user-commands", /* was accidentally included in 5.4 */
10383 "user_commands",
10384#endif
10385#ifdef FEAT_VIMINFO
10386 "viminfo",
10387#endif
10388#ifdef FEAT_VERTSPLIT
10389 "vertsplit",
10390#endif
10391#ifdef FEAT_VIRTUALEDIT
10392 "virtualedit",
10393#endif
10394#ifdef FEAT_VISUAL
10395 "visual",
10396#endif
10397#ifdef FEAT_VISUALEXTRA
10398 "visualextra",
10399#endif
10400#ifdef FEAT_VREPLACE
10401 "vreplace",
10402#endif
10403#ifdef FEAT_WILDIGN
10404 "wildignore",
10405#endif
10406#ifdef FEAT_WILDMENU
10407 "wildmenu",
10408#endif
10409#ifdef FEAT_WINDOWS
10410 "windows",
10411#endif
10412#ifdef FEAT_WAK
10413 "winaltkeys",
10414#endif
10415#ifdef FEAT_WRITEBACKUP
10416 "writebackup",
10417#endif
10418#ifdef FEAT_XIM
10419 "xim",
10420#endif
10421#ifdef FEAT_XFONTSET
10422 "xfontset",
10423#endif
10424#ifdef USE_XSMP
10425 "xsmp",
10426#endif
10427#ifdef USE_XSMP_INTERACT
10428 "xsmp_interact",
10429#endif
10430#ifdef FEAT_XCLIPBOARD
10431 "xterm_clipboard",
10432#endif
10433#ifdef FEAT_XTERM_SAVE
10434 "xterm_save",
10435#endif
10436#if defined(UNIX) && defined(FEAT_X11)
10437 "X11",
10438#endif
10439 NULL
10440 };
10441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010442 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 for (i = 0; has_list[i] != NULL; ++i)
10444 if (STRICMP(name, has_list[i]) == 0)
10445 {
10446 n = TRUE;
10447 break;
10448 }
10449
10450 if (n == FALSE)
10451 {
10452 if (STRNICMP(name, "patch", 5) == 0)
10453 n = has_patch(atoi((char *)name + 5));
10454 else if (STRICMP(name, "vim_starting") == 0)
10455 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010456#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10457 else if (STRICMP(name, "balloon_multiline") == 0)
10458 n = multiline_balloon_available();
10459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460#ifdef DYNAMIC_TCL
10461 else if (STRICMP(name, "tcl") == 0)
10462 n = tcl_enabled(FALSE);
10463#endif
10464#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10465 else if (STRICMP(name, "iconv") == 0)
10466 n = iconv_enabled(FALSE);
10467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010468#ifdef DYNAMIC_MZSCHEME
10469 else if (STRICMP(name, "mzscheme") == 0)
10470 n = mzscheme_enabled(FALSE);
10471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472#ifdef DYNAMIC_RUBY
10473 else if (STRICMP(name, "ruby") == 0)
10474 n = ruby_enabled(FALSE);
10475#endif
10476#ifdef DYNAMIC_PYTHON
10477 else if (STRICMP(name, "python") == 0)
10478 n = python_enabled(FALSE);
10479#endif
10480#ifdef DYNAMIC_PERL
10481 else if (STRICMP(name, "perl") == 0)
10482 n = perl_enabled(FALSE);
10483#endif
10484#ifdef FEAT_GUI
10485 else if (STRICMP(name, "gui_running") == 0)
10486 n = (gui.in_use || gui.starting);
10487# ifdef FEAT_GUI_W32
10488 else if (STRICMP(name, "gui_win32s") == 0)
10489 n = gui_is_win32s();
10490# endif
10491# ifdef FEAT_BROWSE
10492 else if (STRICMP(name, "browse") == 0)
10493 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10494# endif
10495#endif
10496#ifdef FEAT_SYN_HL
10497 else if (STRICMP(name, "syntax_items") == 0)
10498 n = syntax_present(curbuf);
10499#endif
10500#if defined(WIN3264)
10501 else if (STRICMP(name, "win95") == 0)
10502 n = mch_windows95();
10503#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010504#ifdef FEAT_NETBEANS_INTG
10505 else if (STRICMP(name, "netbeans_enabled") == 0)
10506 n = usingNetbeans;
10507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 }
10509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511}
10512
10513/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010514 * "has_key()" function
10515 */
10516 static void
10517f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *argvars;
10519 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010520{
10521 rettv->vval.v_number = 0;
10522 if (argvars[0].v_type != VAR_DICT)
10523 {
10524 EMSG(_(e_dictreq));
10525 return;
10526 }
10527 if (argvars[0].vval.v_dict == NULL)
10528 return;
10529
10530 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010531 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010532}
10533
10534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 * "hasmapto()" function
10536 */
10537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010539 typval_T *argvars;
10540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541{
10542 char_u *name;
10543 char_u *mode;
10544 char_u buf[NUMBUFLEN];
10545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010547 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 mode = (char_u *)"nvo";
10549 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551
10552 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556}
10557
10558/*
10559 * "histadd()" function
10560 */
10561/*ARGSUSED*/
10562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010563f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010564 typval_T *argvars;
10565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566{
10567#ifdef FEAT_CMDHIST
10568 int histype;
10569 char_u *str;
10570 char_u buf[NUMBUFLEN];
10571#endif
10572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 if (check_restricted() || check_secure())
10575 return;
10576#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10578 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 if (histype >= 0)
10580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 if (*str != NUL)
10583 {
10584 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010585 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 return;
10587 }
10588 }
10589#endif
10590}
10591
10592/*
10593 * "histdel()" function
10594 */
10595/*ARGSUSED*/
10596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010597f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010598 typval_T *argvars;
10599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
10601#ifdef FEAT_CMDHIST
10602 int n;
10603 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10607 if (str == NULL)
10608 n = 0;
10609 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010612 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 else
10617 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619 get_tv_string_buf(&argvars[1], buf));
10620 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623#endif
10624}
10625
10626/*
10627 * "histget()" function
10628 */
10629/*ARGSUSED*/
10630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *argvars;
10633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
10635#ifdef FEAT_CMDHIST
10636 int type;
10637 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010638 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10641 if (str == NULL)
10642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 {
10645 type = get_histtype(str);
10646 if (argvars[1].v_type == VAR_UNKNOWN)
10647 idx = get_history_idx(type);
10648 else
10649 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10650 /* -1 on type error */
10651 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657}
10658
10659/*
10660 * "histnr()" function
10661 */
10662/*ARGSUSED*/
10663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010665 typval_T *argvars;
10666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667{
10668 int i;
10669
10670#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671 char_u *history = get_tv_string_chk(&argvars[0]);
10672
10673 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 if (i >= HIST_CMD && i < HIST_COUNT)
10675 i = get_history_idx(i);
10676 else
10677#endif
10678 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680}
10681
10682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 * "highlightID(name)" function
10684 */
10685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010687 typval_T *argvars;
10688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691}
10692
10693/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010694 * "highlight_exists()" function
10695 */
10696 static void
10697f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010698 typval_T *argvars;
10699 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010700{
10701 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10702}
10703
10704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 * "hostname()" function
10706 */
10707/*ARGSUSED*/
10708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010710 typval_T *argvars;
10711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
10713 char_u hostname[256];
10714
10715 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010716 rettv->v_type = VAR_STRING;
10717 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718}
10719
10720/*
10721 * iconv() function
10722 */
10723/*ARGSUSED*/
10724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 typval_T *argvars;
10727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729#ifdef FEAT_MBYTE
10730 char_u buf1[NUMBUFLEN];
10731 char_u buf2[NUMBUFLEN];
10732 char_u *from, *to, *str;
10733 vimconv_T vimconv;
10734#endif
10735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->v_type = VAR_STRING;
10737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738
10739#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010740 str = get_tv_string(&argvars[0]);
10741 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10742 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 vimconv.vc_type = CONV_NONE;
10744 convert_setup(&vimconv, from, to);
10745
10746 /* If the encodings are equal, no conversion needed. */
10747 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
10752 convert_setup(&vimconv, NULL, NULL);
10753 vim_free(from);
10754 vim_free(to);
10755#endif
10756}
10757
10758/*
10759 * "indent()" function
10760 */
10761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010763 typval_T *argvars;
10764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765{
10766 linenr_T lnum;
10767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773}
10774
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010775/*
10776 * "index()" function
10777 */
10778 static void
10779f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010780 typval_T *argvars;
10781 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010782{
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 list_T *l;
10784 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010785 long idx = 0;
10786 int ic = FALSE;
10787
10788 rettv->vval.v_number = -1;
10789 if (argvars[0].v_type != VAR_LIST)
10790 {
10791 EMSG(_(e_listreq));
10792 return;
10793 }
10794 l = argvars[0].vval.v_list;
10795 if (l != NULL)
10796 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010797 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010800 int error = FALSE;
10801
Bram Moolenaar758711c2005-02-02 23:11:38 +000010802 /* Start at specified item. Use the cached index that list_find()
10803 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010804 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010805 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010806 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010807 ic = get_tv_number_chk(&argvars[3], &error);
10808 if (error)
10809 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010810 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010811
Bram Moolenaar758711c2005-02-02 23:11:38 +000010812 for ( ; item != NULL; item = item->li_next, ++idx)
10813 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010814 {
10815 rettv->vval.v_number = idx;
10816 break;
10817 }
10818 }
10819}
10820
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821static int inputsecret_flag = 0;
10822
10823/*
10824 * "input()" function
10825 * Also handles inputsecret() when inputsecret is set.
10826 */
10827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 typval_T *argvars;
10830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 char_u *p = NULL;
10834 int c;
10835 char_u buf[NUMBUFLEN];
10836 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010838 int xp_type = EXPAND_NOTHING;
10839 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010841 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842
10843#ifdef NO_CONSOLE_INPUT
10844 /* While starting up, there is no place to enter text. */
10845 if (no_console_input())
10846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 return;
10849 }
10850#endif
10851
10852 cmd_silent = FALSE; /* Want to see the prompt. */
10853 if (prompt != NULL)
10854 {
10855 /* Only the part of the message after the last NL is considered as
10856 * prompt for the command line */
10857 p = vim_strrchr(prompt, '\n');
10858 if (p == NULL)
10859 p = prompt;
10860 else
10861 {
10862 ++p;
10863 c = *p;
10864 *p = NUL;
10865 msg_start();
10866 msg_clr_eos();
10867 msg_puts_attr(prompt, echo_attr);
10868 msg_didout = FALSE;
10869 msg_starthere();
10870 *p = c;
10871 }
10872 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 if (argvars[1].v_type != VAR_UNKNOWN)
10875 {
10876 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10877 if (defstr != NULL)
10878 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879
Bram Moolenaar4463f292005-09-25 22:20:24 +000010880 if (argvars[2].v_type != VAR_UNKNOWN)
10881 {
10882 char_u *xp_name;
10883 int xp_namelen;
10884 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010885
Bram Moolenaar4463f292005-09-25 22:20:24 +000010886 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010887
Bram Moolenaar4463f292005-09-25 22:20:24 +000010888 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10889 if (xp_name == NULL)
10890 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010891
Bram Moolenaar4463f292005-09-25 22:20:24 +000010892 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010893
Bram Moolenaar4463f292005-09-25 22:20:24 +000010894 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10895 &xp_arg) == FAIL)
10896 return;
10897 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010898 }
10899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 if (defstr != NULL)
10901 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010902 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10903 xp_type, xp_arg);
10904
10905 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 /* since the user typed this, no need to wait for return */
10908 need_wait_return = FALSE;
10909 msg_didout = FALSE;
10910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 cmd_silent = cmd_silent_save;
10912}
10913
10914/*
10915 * "inputdialog()" function
10916 */
10917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010919 typval_T *argvars;
10920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921{
10922#if defined(FEAT_GUI_TEXTDIALOG)
10923 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10924 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10925 {
10926 char_u *message;
10927 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010928 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 message = get_tv_string_chk(&argvars[0]);
10931 if (argvars[1].v_type != VAR_UNKNOWN
10932 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010933 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 else
10935 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 if (message != NULL && defstr != NULL
10937 && do_dialog(VIM_QUESTION, NULL, message,
10938 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 else
10941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 if (message != NULL && defstr != NULL
10943 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010944 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010945 rettv->vval.v_string = vim_strsave(
10946 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010950 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 }
10952 else
10953#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955}
10956
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000010957/*
10958 * "inputlist()" function
10959 */
10960 static void
10961f_inputlist(argvars, rettv)
10962 typval_T *argvars;
10963 typval_T *rettv;
10964{
10965 listitem_T *li;
10966 int selected;
10967 int mouse_used;
10968
10969 rettv->vval.v_number = 0;
10970#ifdef NO_CONSOLE_INPUT
10971 /* While starting up, there is no place to enter text. */
10972 if (no_console_input())
10973 return;
10974#endif
10975 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
10976 {
10977 EMSG2(_(e_listarg), "inputlist()");
10978 return;
10979 }
10980
10981 msg_start();
10982 lines_left = Rows; /* avoid more prompt */
10983 msg_scroll = TRUE;
10984 msg_clr_eos();
10985
10986 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
10987 {
10988 msg_puts(get_tv_string(&li->li_tv));
10989 msg_putchar('\n');
10990 }
10991
10992 /* Ask for choice. */
10993 selected = prompt_for_number(&mouse_used);
10994 if (mouse_used)
10995 selected -= lines_left;
10996
10997 rettv->vval.v_number = selected;
10998}
10999
11000
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11002
11003/*
11004 * "inputrestore()" function
11005 */
11006/*ARGSUSED*/
11007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011009 typval_T *argvars;
11010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011{
11012 if (ga_userinput.ga_len > 0)
11013 {
11014 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11016 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 }
11019 else if (p_verbose > 1)
11020 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011021 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 }
11024}
11025
11026/*
11027 * "inputsave()" function
11028 */
11029/*ARGSUSED*/
11030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011031f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011032 typval_T *argvars;
11033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034{
11035 /* Add an entry to the stack of typehead storage. */
11036 if (ga_grow(&ga_userinput, 1) == OK)
11037 {
11038 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11039 + ga_userinput.ga_len);
11040 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 }
11043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045}
11046
11047/*
11048 * "inputsecret()" function
11049 */
11050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011052 typval_T *argvars;
11053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054{
11055 ++cmdline_star;
11056 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 --cmdline_star;
11059 --inputsecret_flag;
11060}
11061
11062/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011063 * "insert()" function
11064 */
11065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011067 typval_T *argvars;
11068 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011069{
11070 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011071 listitem_T *item;
11072 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011074
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011075 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011076 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011077 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011078 else if ((l = argvars[0].vval.v_list) != NULL
11079 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011080 {
11081 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011082 before = get_tv_number_chk(&argvars[2], &error);
11083 if (error)
11084 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011085
Bram Moolenaar758711c2005-02-02 23:11:38 +000011086 if (before == l->lv_len)
11087 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011088 else
11089 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011090 item = list_find(l, before);
11091 if (item == NULL)
11092 {
11093 EMSGN(_(e_listidx), before);
11094 l = NULL;
11095 }
11096 }
11097 if (l != NULL)
11098 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011099 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011100 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011101 }
11102 }
11103}
11104
11105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 * "isdirectory()" function
11107 */
11108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011110 typval_T *argvars;
11111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114}
11115
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011116/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011117 * "islocked()" function
11118 */
11119 static void
11120f_islocked(argvars, rettv)
11121 typval_T *argvars;
11122 typval_T *rettv;
11123{
11124 lval_T lv;
11125 char_u *end;
11126 dictitem_T *di;
11127
11128 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011129 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11130 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011131 if (end != NULL && lv.ll_name != NULL)
11132 {
11133 if (*end != NUL)
11134 EMSG(_(e_trailing));
11135 else
11136 {
11137 if (lv.ll_tv == NULL)
11138 {
11139 if (check_changedtick(lv.ll_name))
11140 rettv->vval.v_number = 1; /* always locked */
11141 else
11142 {
11143 di = find_var(lv.ll_name, NULL);
11144 if (di != NULL)
11145 {
11146 /* Consider a variable locked when:
11147 * 1. the variable itself is locked
11148 * 2. the value of the variable is locked.
11149 * 3. the List or Dict value is locked.
11150 */
11151 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11152 || tv_islocked(&di->di_tv));
11153 }
11154 }
11155 }
11156 else if (lv.ll_range)
11157 EMSG(_("E745: Range not allowed"));
11158 else if (lv.ll_newkey != NULL)
11159 EMSG2(_(e_dictkey), lv.ll_newkey);
11160 else if (lv.ll_list != NULL)
11161 /* List item. */
11162 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11163 else
11164 /* Dictionary item. */
11165 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11166 }
11167 }
11168
11169 clear_lval(&lv);
11170}
11171
Bram Moolenaar33570922005-01-25 22:26:29 +000011172static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011173
11174/*
11175 * Turn a dict into a list:
11176 * "what" == 0: list of keys
11177 * "what" == 1: list of values
11178 * "what" == 2: list of items
11179 */
11180 static void
11181dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011182 typval_T *argvars;
11183 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011184 int what;
11185{
Bram Moolenaar33570922005-01-25 22:26:29 +000011186 list_T *l;
11187 list_T *l2;
11188 dictitem_T *di;
11189 hashitem_T *hi;
11190 listitem_T *li;
11191 listitem_T *li2;
11192 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011193 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011194
11195 rettv->vval.v_number = 0;
11196 if (argvars[0].v_type != VAR_DICT)
11197 {
11198 EMSG(_(e_dictreq));
11199 return;
11200 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011201 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011202 return;
11203
11204 l = list_alloc();
11205 if (l == NULL)
11206 return;
11207 rettv->v_type = VAR_LIST;
11208 rettv->vval.v_list = l;
11209 ++l->lv_refcount;
11210
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 todo = d->dv_hashtab.ht_used;
11212 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011214 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011215 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011216 --todo;
11217 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011218
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011219 li = listitem_alloc();
11220 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011221 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011222 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011223
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011224 if (what == 0)
11225 {
11226 /* keys() */
11227 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011228 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011229 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11230 }
11231 else if (what == 1)
11232 {
11233 /* values() */
11234 copy_tv(&di->di_tv, &li->li_tv);
11235 }
11236 else
11237 {
11238 /* items() */
11239 l2 = list_alloc();
11240 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011241 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011242 li->li_tv.vval.v_list = l2;
11243 if (l2 == NULL)
11244 break;
11245 ++l2->lv_refcount;
11246
11247 li2 = listitem_alloc();
11248 if (li2 == NULL)
11249 break;
11250 list_append(l2, li2);
11251 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011252 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011253 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11254
11255 li2 = listitem_alloc();
11256 if (li2 == NULL)
11257 break;
11258 list_append(l2, li2);
11259 copy_tv(&di->di_tv, &li2->li_tv);
11260 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011261 }
11262 }
11263}
11264
11265/*
11266 * "items(dict)" function
11267 */
11268 static void
11269f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 typval_T *argvars;
11271 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011272{
11273 dict_list(argvars, rettv, 2);
11274}
11275
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011277 * "join()" function
11278 */
11279 static void
11280f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011283{
11284 garray_T ga;
11285 char_u *sep;
11286
11287 rettv->vval.v_number = 0;
11288 if (argvars[0].v_type != VAR_LIST)
11289 {
11290 EMSG(_(e_listreq));
11291 return;
11292 }
11293 if (argvars[0].vval.v_list == NULL)
11294 return;
11295 if (argvars[1].v_type == VAR_UNKNOWN)
11296 sep = (char_u *)" ";
11297 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011299
11300 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301
11302 if (sep != NULL)
11303 {
11304 ga_init2(&ga, (int)sizeof(char), 80);
11305 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11306 ga_append(&ga, NUL);
11307 rettv->vval.v_string = (char_u *)ga.ga_data;
11308 }
11309 else
11310 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011311}
11312
11313/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011314 * "keys()" function
11315 */
11316 static void
11317f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011318 typval_T *argvars;
11319 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011320{
11321 dict_list(argvars, rettv, 0);
11322}
11323
11324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 * "last_buffer_nr()" function.
11326 */
11327/*ARGSUSED*/
11328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011330 typval_T *argvars;
11331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332{
11333 int n = 0;
11334 buf_T *buf;
11335
11336 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11337 if (n < buf->b_fnum)
11338 n = buf->b_fnum;
11339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011341}
11342
11343/*
11344 * "len()" function
11345 */
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *argvars;
11349 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011350{
11351 switch (argvars[0].v_type)
11352 {
11353 case VAR_STRING:
11354 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->vval.v_number = (varnumber_T)STRLEN(
11356 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011357 break;
11358 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011360 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011361 case VAR_DICT:
11362 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11363 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011364 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011365 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011366 break;
11367 }
11368}
11369
Bram Moolenaar33570922005-01-25 22:26:29 +000011370static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011371
11372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011374 typval_T *argvars;
11375 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011376 int type;
11377{
11378#ifdef FEAT_LIBCALL
11379 char_u *string_in;
11380 char_u **string_result;
11381 int nr_result;
11382#endif
11383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011385 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011387 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011389
11390 if (check_restricted() || check_secure())
11391 return;
11392
11393#ifdef FEAT_LIBCALL
11394 /* The first two args must be strings, otherwise its meaningless */
11395 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11396 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011397 string_in = NULL;
11398 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011399 string_in = argvars[2].vval.v_string;
11400 if (type == VAR_NUMBER)
11401 string_result = NULL;
11402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011404 if (mch_libcall(argvars[0].vval.v_string,
11405 argvars[1].vval.v_string,
11406 string_in,
11407 argvars[2].vval.v_number,
11408 string_result,
11409 &nr_result) == OK
11410 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011412 }
11413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414}
11415
11416/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011417 * "libcall()" function
11418 */
11419 static void
11420f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011421 typval_T *argvars;
11422 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011423{
11424 libcall_common(argvars, rettv, VAR_STRING);
11425}
11426
11427/*
11428 * "libcallnr()" function
11429 */
11430 static void
11431f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011432 typval_T *argvars;
11433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011434{
11435 libcall_common(argvars, rettv, VAR_NUMBER);
11436}
11437
11438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 * "line(string)" function
11440 */
11441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011443 typval_T *argvars;
11444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445{
11446 linenr_T lnum = 0;
11447 pos_T *fp;
11448
11449 fp = var2fpos(&argvars[0], TRUE);
11450 if (fp != NULL)
11451 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453}
11454
11455/*
11456 * "line2byte(lnum)" function
11457 */
11458/*ARGSUSED*/
11459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 typval_T *argvars;
11462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463{
11464#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466#else
11467 linenr_T lnum;
11468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11474 if (rettv->vval.v_number >= 0)
11475 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476#endif
11477}
11478
11479/*
11480 * "lispindent(lnum)" function
11481 */
11482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011484 typval_T *argvars;
11485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
11487#ifdef FEAT_LISP
11488 pos_T pos;
11489 linenr_T lnum;
11490
11491 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11494 {
11495 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 curwin->w_cursor = pos;
11498 }
11499 else
11500#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502}
11503
11504/*
11505 * "localtime()" function
11506 */
11507/*ARGSUSED*/
11508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011510 typval_T *argvars;
11511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514}
11515
Bram Moolenaar33570922005-01-25 22:26:29 +000011516static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517
11518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011520 typval_T *argvars;
11521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 int exact;
11523{
11524 char_u *keys;
11525 char_u *which;
11526 char_u buf[NUMBUFLEN];
11527 char_u *keys_buf = NULL;
11528 char_u *rhs;
11529 int mode;
11530 garray_T ga;
11531
11532 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 rettv->v_type = VAR_STRING;
11534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 if (*keys == NUL)
11538 return;
11539
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011540 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011541 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 else
11543 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 if (which == NULL)
11545 return;
11546
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547 mode = get_map_mode(&which, 0);
11548
11549 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011550 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 vim_free(keys_buf);
11552 if (rhs != NULL)
11553 {
11554 ga_init(&ga);
11555 ga.ga_itemsize = 1;
11556 ga.ga_growsize = 40;
11557
11558 while (*rhs != NUL)
11559 ga_concat(&ga, str2special(&rhs, FALSE));
11560
11561 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 }
11564}
11565
11566/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011567 * "map()" function
11568 */
11569 static void
11570f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011573{
11574 filter_map(argvars, rettv, TRUE);
11575}
11576
11577/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011578 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 */
11580 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011581f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011582 typval_T *argvars;
11583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011585 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586}
11587
11588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011589 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 */
11591 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011592f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011593 typval_T *argvars;
11594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011596 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597}
11598
Bram Moolenaar33570922005-01-25 22:26:29 +000011599static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600
11601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011603 typval_T *argvars;
11604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 int type;
11606{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011607 char_u *str = NULL;
11608 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 char_u *pat;
11610 regmatch_T regmatch;
11611 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011612 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 char_u *save_cpo;
11614 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011615 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011616 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 list_T *l = NULL;
11618 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011619 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011620 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621
11622 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11623 save_cpo = p_cpo;
11624 p_cpo = (char_u *)"";
11625
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011626 rettv->vval.v_number = -1;
11627 if (type == 3)
11628 {
11629 /* return empty list when there are no matches */
11630 if ((rettv->vval.v_list = list_alloc()) == NULL)
11631 goto theend;
11632 rettv->v_type = VAR_LIST;
11633 ++rettv->vval.v_list->lv_refcount;
11634 }
11635 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 {
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 Moolenaar071d4272004-06-13 20:20:40 +000011640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011641 if (argvars[0].v_type == VAR_LIST)
11642 {
11643 if ((l = argvars[0].vval.v_list) == NULL)
11644 goto theend;
11645 li = l->lv_first;
11646 }
11647 else
11648 expr = str = get_tv_string(&argvars[0]);
11649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011650 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11651 if (pat == NULL)
11652 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011653
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011654 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011656 int error = FALSE;
11657
11658 start = get_tv_number_chk(&argvars[2], &error);
11659 if (error)
11660 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011661 if (l != NULL)
11662 {
11663 li = list_find(l, start);
11664 if (li == NULL)
11665 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011666 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011667 }
11668 else
11669 {
11670 if (start < 0)
11671 start = 0;
11672 if (start > (long)STRLEN(str))
11673 goto theend;
11674 str += start;
11675 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011676
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011677 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 nth = get_tv_number_chk(&argvars[3], &error);
11679 if (error)
11680 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 }
11682
11683 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11684 if (regmatch.regprog != NULL)
11685 {
11686 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011687
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011688 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011689 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011690 if (l != NULL)
11691 {
11692 if (li == NULL)
11693 {
11694 match = FALSE;
11695 break;
11696 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011697 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011698 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011699 if (str == NULL)
11700 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011701 }
11702
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011703 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011704
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011705 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011707 if (l == NULL && !match)
11708 break;
11709
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011711 if (l != NULL)
11712 {
11713 li = li->li_next;
11714 ++idx;
11715 }
11716 else
11717 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011718#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011719 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011720#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011721 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011722#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011723 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011724 }
11725
11726 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011728 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011729 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011730 int i;
11731
11732 /* return list with matched string and submatches */
11733 for (i = 0; i < NSUBEXP; ++i)
11734 {
11735 if (regmatch.endp[i] == NULL)
11736 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011737 if (list_append_string(rettv->vval.v_list,
11738 regmatch.startp[i],
11739 (int)(regmatch.endp[i] - regmatch.startp[i]))
11740 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011741 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011742 }
11743 }
11744 else if (type == 2)
11745 {
11746 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011747 if (l != NULL)
11748 copy_tv(&li->li_tv, rettv);
11749 else
11750 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011752 }
11753 else if (l != NULL)
11754 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 else
11756 {
11757 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 (varnumber_T)(regmatch.startp[0] - str);
11760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 }
11765 }
11766 vim_free(regmatch.regprog);
11767 }
11768
11769theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011770 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 p_cpo = save_cpo;
11772}
11773
11774/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011775 * "match()" function
11776 */
11777 static void
11778f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011779 typval_T *argvars;
11780 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011781{
11782 find_some_match(argvars, rettv, 1);
11783}
11784
11785/*
11786 * "matchend()" function
11787 */
11788 static void
11789f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011790 typval_T *argvars;
11791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011792{
11793 find_some_match(argvars, rettv, 0);
11794}
11795
11796/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011797 * "matchlist()" function
11798 */
11799 static void
11800f_matchlist(argvars, rettv)
11801 typval_T *argvars;
11802 typval_T *rettv;
11803{
11804 find_some_match(argvars, rettv, 3);
11805}
11806
11807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011808 * "matchstr()" function
11809 */
11810 static void
11811f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011812 typval_T *argvars;
11813 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011814{
11815 find_some_match(argvars, rettv, 2);
11816}
11817
Bram Moolenaar33570922005-01-25 22:26:29 +000011818static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011819
11820 static void
11821max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011822 typval_T *argvars;
11823 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011824 int domax;
11825{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011826 long n = 0;
11827 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011829
11830 if (argvars[0].v_type == VAR_LIST)
11831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011832 list_T *l;
11833 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011834
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011835 l = argvars[0].vval.v_list;
11836 if (l != NULL)
11837 {
11838 li = l->lv_first;
11839 if (li != NULL)
11840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011841 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011842 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011843 {
11844 li = li->li_next;
11845 if (li == NULL)
11846 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011848 if (domax ? i > n : i < n)
11849 n = i;
11850 }
11851 }
11852 }
11853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011854 else if (argvars[0].v_type == VAR_DICT)
11855 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011857 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011859 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011860
11861 d = argvars[0].vval.v_dict;
11862 if (d != NULL)
11863 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 todo = d->dv_hashtab.ht_used;
11865 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011866 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011867 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011869 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011870 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011871 if (first)
11872 {
11873 n = i;
11874 first = FALSE;
11875 }
11876 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011877 n = i;
11878 }
11879 }
11880 }
11881 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011882 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011883 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011884 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011885}
11886
11887/*
11888 * "max()" function
11889 */
11890 static void
11891f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011892 typval_T *argvars;
11893 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011894{
11895 max_min(argvars, rettv, TRUE);
11896}
11897
11898/*
11899 * "min()" function
11900 */
11901 static void
11902f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011903 typval_T *argvars;
11904 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011905{
11906 max_min(argvars, rettv, FALSE);
11907}
11908
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011909static int mkdir_recurse __ARGS((char_u *dir, int prot));
11910
11911/*
11912 * Create the directory in which "dir" is located, and higher levels when
11913 * needed.
11914 */
11915 static int
11916mkdir_recurse(dir, prot)
11917 char_u *dir;
11918 int prot;
11919{
11920 char_u *p;
11921 char_u *updir;
11922 int r = FAIL;
11923
11924 /* Get end of directory name in "dir".
11925 * We're done when it's "/" or "c:/". */
11926 p = gettail_sep(dir);
11927 if (p <= get_past_head(dir))
11928 return OK;
11929
11930 /* If the directory exists we're done. Otherwise: create it.*/
11931 updir = vim_strnsave(dir, (int)(p - dir));
11932 if (updir == NULL)
11933 return FAIL;
11934 if (mch_isdir(updir))
11935 r = OK;
11936 else if (mkdir_recurse(updir, prot) == OK)
11937 r = vim_mkdir_emsg(updir, prot);
11938 vim_free(updir);
11939 return r;
11940}
11941
11942#ifdef vim_mkdir
11943/*
11944 * "mkdir()" function
11945 */
11946 static void
11947f_mkdir(argvars, rettv)
11948 typval_T *argvars;
11949 typval_T *rettv;
11950{
11951 char_u *dir;
11952 char_u buf[NUMBUFLEN];
11953 int prot = 0755;
11954
11955 rettv->vval.v_number = FAIL;
11956 if (check_restricted() || check_secure())
11957 return;
11958
11959 dir = get_tv_string_buf(&argvars[0], buf);
11960 if (argvars[1].v_type != VAR_UNKNOWN)
11961 {
11962 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 prot = get_tv_number_chk(&argvars[2], NULL);
11964 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011965 mkdir_recurse(dir, prot);
11966 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011968}
11969#endif
11970
Bram Moolenaar0d660222005-01-07 21:51:51 +000011971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 * "mode()" function
11973 */
11974/*ARGSUSED*/
11975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011977 typval_T *argvars;
11978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979{
11980 char_u buf[2];
11981
11982#ifdef FEAT_VISUAL
11983 if (VIsual_active)
11984 {
11985 if (VIsual_select)
11986 buf[0] = VIsual_mode + 's' - 'v';
11987 else
11988 buf[0] = VIsual_mode;
11989 }
11990 else
11991#endif
11992 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11993 buf[0] = 'r';
11994 else if (State & INSERT)
11995 {
11996 if (State & REPLACE_FLAG)
11997 buf[0] = 'R';
11998 else
11999 buf[0] = 'i';
12000 }
12001 else if (State & CMDLINE)
12002 buf[0] = 'c';
12003 else
12004 buf[0] = 'n';
12005
12006 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 rettv->vval.v_string = vim_strsave(buf);
12008 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009}
12010
12011/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012012 * "nextnonblank()" function
12013 */
12014 static void
12015f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012016 typval_T *argvars;
12017 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012018{
12019 linenr_T lnum;
12020
12021 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012023 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012024 {
12025 lnum = 0;
12026 break;
12027 }
12028 if (*skipwhite(ml_get(lnum)) != NUL)
12029 break;
12030 }
12031 rettv->vval.v_number = lnum;
12032}
12033
12034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 * "nr2char()" function
12036 */
12037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012039 typval_T *argvars;
12040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041{
12042 char_u buf[NUMBUFLEN];
12043
12044#ifdef FEAT_MBYTE
12045 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 else
12048#endif
12049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 buf[1] = NUL;
12052 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 rettv->v_type = VAR_STRING;
12054 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012055}
12056
12057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012058 * "prevnonblank()" function
12059 */
12060 static void
12061f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012062 typval_T *argvars;
12063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012064{
12065 linenr_T lnum;
12066
12067 lnum = get_tv_lnum(argvars);
12068 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12069 lnum = 0;
12070 else
12071 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12072 --lnum;
12073 rettv->vval.v_number = lnum;
12074}
12075
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012076#ifdef HAVE_STDARG_H
12077/* This dummy va_list is here because:
12078 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12079 * - locally in the function results in a "used before set" warning
12080 * - using va_start() to initialize it gives "function with fixed args" error */
12081static va_list ap;
12082#endif
12083
Bram Moolenaar8c711452005-01-14 21:53:12 +000012084/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012085 * "printf()" function
12086 */
12087 static void
12088f_printf(argvars, rettv)
12089 typval_T *argvars;
12090 typval_T *rettv;
12091{
12092 rettv->v_type = VAR_STRING;
12093 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012094#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012095 {
12096 char_u buf[NUMBUFLEN];
12097 int len;
12098 char_u *s;
12099 int saved_did_emsg = did_emsg;
12100 char *fmt;
12101
12102 /* Get the required length, allocate the buffer and do it for real. */
12103 did_emsg = FALSE;
12104 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012105 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012106 if (!did_emsg)
12107 {
12108 s = alloc(len + 1);
12109 if (s != NULL)
12110 {
12111 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012112 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012113 }
12114 }
12115 did_emsg |= saved_did_emsg;
12116 }
12117#endif
12118}
12119
12120/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012121 * "range()" function
12122 */
12123 static void
12124f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012125 typval_T *argvars;
12126 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012127{
12128 long start;
12129 long end;
12130 long stride = 1;
12131 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012133 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012135 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012136 if (argvars[1].v_type == VAR_UNKNOWN)
12137 {
12138 end = start - 1;
12139 start = 0;
12140 }
12141 else
12142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012143 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012144 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012145 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012146 }
12147
12148 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012149 if (error)
12150 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012151 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012152 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012153 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012154 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012155 else
12156 {
12157 l = list_alloc();
12158 if (l != NULL)
12159 {
12160 rettv->v_type = VAR_LIST;
12161 rettv->vval.v_list = l;
12162 ++l->lv_refcount;
12163
12164 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012165 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012166 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012167 }
12168 }
12169}
12170
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012171/*
12172 * "readfile()" function
12173 */
12174 static void
12175f_readfile(argvars, rettv)
12176 typval_T *argvars;
12177 typval_T *rettv;
12178{
12179 int binary = FALSE;
12180 char_u *fname;
12181 FILE *fd;
12182 list_T *l;
12183 listitem_T *li;
12184#define FREAD_SIZE 200 /* optimized for text lines */
12185 char_u buf[FREAD_SIZE];
12186 int readlen; /* size of last fread() */
12187 int buflen; /* nr of valid chars in buf[] */
12188 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12189 int tolist; /* first byte in buf[] still to be put in list */
12190 int chop; /* how many CR to chop off */
12191 char_u *prev = NULL; /* previously read bytes, if any */
12192 int prevlen = 0; /* length of "prev" if not NULL */
12193 char_u *s;
12194 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012195 long maxline = MAXLNUM;
12196 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012197
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012198 if (argvars[1].v_type != VAR_UNKNOWN)
12199 {
12200 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12201 binary = TRUE;
12202 if (argvars[2].v_type != VAR_UNKNOWN)
12203 maxline = get_tv_number(&argvars[2]);
12204 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012205
12206 l = list_alloc();
12207 if (l == NULL)
12208 return;
12209 rettv->v_type = VAR_LIST;
12210 rettv->vval.v_list = l;
12211 l->lv_refcount = 1;
12212
12213 /* Always open the file in binary mode, library functions have a mind of
12214 * their own about CR-LF conversion. */
12215 fname = get_tv_string(&argvars[0]);
12216 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12217 {
12218 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12219 return;
12220 }
12221
12222 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012223 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012224 {
12225 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12226 buflen = filtd + readlen;
12227 tolist = 0;
12228 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12229 {
12230 if (buf[filtd] == '\n' || readlen <= 0)
12231 {
12232 /* Only when in binary mode add an empty list item when the
12233 * last line ends in a '\n'. */
12234 if (!binary && readlen == 0 && filtd == 0)
12235 break;
12236
12237 /* Found end-of-line or end-of-file: add a text line to the
12238 * list. */
12239 chop = 0;
12240 if (!binary)
12241 while (filtd - chop - 1 >= tolist
12242 && buf[filtd - chop - 1] == '\r')
12243 ++chop;
12244 len = filtd - tolist - chop;
12245 if (prev == NULL)
12246 s = vim_strnsave(buf + tolist, len);
12247 else
12248 {
12249 s = alloc((unsigned)(prevlen + len + 1));
12250 if (s != NULL)
12251 {
12252 mch_memmove(s, prev, prevlen);
12253 vim_free(prev);
12254 prev = NULL;
12255 mch_memmove(s + prevlen, buf + tolist, len);
12256 s[prevlen + len] = NUL;
12257 }
12258 }
12259 tolist = filtd + 1;
12260
12261 li = listitem_alloc();
12262 if (li == NULL)
12263 {
12264 vim_free(s);
12265 break;
12266 }
12267 li->li_tv.v_type = VAR_STRING;
12268 li->li_tv.v_lock = 0;
12269 li->li_tv.vval.v_string = s;
12270 list_append(l, li);
12271
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012272 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012273 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012274 if (readlen <= 0)
12275 break;
12276 }
12277 else if (buf[filtd] == NUL)
12278 buf[filtd] = '\n';
12279 }
12280 if (readlen <= 0)
12281 break;
12282
12283 if (tolist == 0)
12284 {
12285 /* "buf" is full, need to move text to an allocated buffer */
12286 if (prev == NULL)
12287 {
12288 prev = vim_strnsave(buf, buflen);
12289 prevlen = buflen;
12290 }
12291 else
12292 {
12293 s = alloc((unsigned)(prevlen + buflen));
12294 if (s != NULL)
12295 {
12296 mch_memmove(s, prev, prevlen);
12297 mch_memmove(s + prevlen, buf, buflen);
12298 vim_free(prev);
12299 prev = s;
12300 prevlen += buflen;
12301 }
12302 }
12303 filtd = 0;
12304 }
12305 else
12306 {
12307 mch_memmove(buf, buf + tolist, buflen - tolist);
12308 filtd -= tolist;
12309 }
12310 }
12311
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012312 /*
12313 * For a negative line count use only the lines at the end of the file,
12314 * free the rest.
12315 */
12316 if (maxline < 0)
12317 while (cnt > -maxline)
12318 {
12319 listitem_remove(l, l->lv_first);
12320 --cnt;
12321 }
12322
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012323 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012324 fclose(fd);
12325}
12326
12327
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12329static void make_connection __ARGS((void));
12330static int check_connection __ARGS((void));
12331
12332 static void
12333make_connection()
12334{
12335 if (X_DISPLAY == NULL
12336# ifdef FEAT_GUI
12337 && !gui.in_use
12338# endif
12339 )
12340 {
12341 x_force_connect = TRUE;
12342 setup_term_clip();
12343 x_force_connect = FALSE;
12344 }
12345}
12346
12347 static int
12348check_connection()
12349{
12350 make_connection();
12351 if (X_DISPLAY == NULL)
12352 {
12353 EMSG(_("E240: No connection to Vim server"));
12354 return FAIL;
12355 }
12356 return OK;
12357}
12358#endif
12359
12360#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012361static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012362
12363 static void
12364remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012365 typval_T *argvars;
12366 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012367 int expr;
12368{
12369 char_u *server_name;
12370 char_u *keys;
12371 char_u *r = NULL;
12372 char_u buf[NUMBUFLEN];
12373# ifdef WIN32
12374 HWND w;
12375# else
12376 Window w;
12377# endif
12378
12379 if (check_restricted() || check_secure())
12380 return;
12381
12382# ifdef FEAT_X11
12383 if (check_connection() == FAIL)
12384 return;
12385# endif
12386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012387 server_name = get_tv_string_chk(&argvars[0]);
12388 if (server_name == NULL)
12389 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012390 keys = get_tv_string_buf(&argvars[1], buf);
12391# ifdef WIN32
12392 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12393# else
12394 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12395 < 0)
12396# endif
12397 {
12398 if (r != NULL)
12399 EMSG(r); /* sending worked but evaluation failed */
12400 else
12401 EMSG2(_("E241: Unable to send to %s"), server_name);
12402 return;
12403 }
12404
12405 rettv->vval.v_string = r;
12406
12407 if (argvars[2].v_type != VAR_UNKNOWN)
12408 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012409 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012410 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012411 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012412
12413 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012414 v.di_tv.v_type = VAR_STRING;
12415 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012416 idvar = get_tv_string_chk(&argvars[2]);
12417 if (idvar != NULL)
12418 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012419 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012420 }
12421}
12422#endif
12423
12424/*
12425 * "remote_expr()" function
12426 */
12427/*ARGSUSED*/
12428 static void
12429f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 typval_T *argvars;
12431 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012432{
12433 rettv->v_type = VAR_STRING;
12434 rettv->vval.v_string = NULL;
12435#ifdef FEAT_CLIENTSERVER
12436 remote_common(argvars, rettv, TRUE);
12437#endif
12438}
12439
12440/*
12441 * "remote_foreground()" function
12442 */
12443/*ARGSUSED*/
12444 static void
12445f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012446 typval_T *argvars;
12447 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012448{
12449 rettv->vval.v_number = 0;
12450#ifdef FEAT_CLIENTSERVER
12451# ifdef WIN32
12452 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012453 {
12454 char_u *server_name = get_tv_string_chk(&argvars[0]);
12455
12456 if (server_name != NULL)
12457 serverForeground(server_name);
12458 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012459# else
12460 /* Send a foreground() expression to the server. */
12461 argvars[1].v_type = VAR_STRING;
12462 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12463 argvars[2].v_type = VAR_UNKNOWN;
12464 remote_common(argvars, rettv, TRUE);
12465 vim_free(argvars[1].vval.v_string);
12466# endif
12467#endif
12468}
12469
12470/*ARGSUSED*/
12471 static void
12472f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012473 typval_T *argvars;
12474 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012475{
12476#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012477 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012478 char_u *s = NULL;
12479# ifdef WIN32
12480 int n = 0;
12481# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012482 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012483
12484 if (check_restricted() || check_secure())
12485 {
12486 rettv->vval.v_number = -1;
12487 return;
12488 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012489 serverid = get_tv_string_chk(&argvars[0]);
12490 if (serverid == NULL)
12491 {
12492 rettv->vval.v_number = -1;
12493 return; /* type error; errmsg already given */
12494 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012496 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497 if (n == 0)
12498 rettv->vval.v_number = -1;
12499 else
12500 {
12501 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12502 rettv->vval.v_number = (s != NULL);
12503 }
12504# else
12505 rettv->vval.v_number = 0;
12506 if (check_connection() == FAIL)
12507 return;
12508
12509 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511# endif
12512
12513 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012515 char_u *retvar;
12516
Bram Moolenaar33570922005-01-25 22:26:29 +000012517 v.di_tv.v_type = VAR_STRING;
12518 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012519 retvar = get_tv_string_chk(&argvars[1]);
12520 if (retvar != NULL)
12521 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012522 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523 }
12524#else
12525 rettv->vval.v_number = -1;
12526#endif
12527}
12528
12529/*ARGSUSED*/
12530 static void
12531f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012532 typval_T *argvars;
12533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534{
12535 char_u *r = NULL;
12536
12537#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012538 char_u *serverid = get_tv_string_chk(&argvars[0]);
12539
12540 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012541 {
12542# ifdef WIN32
12543 /* The server's HWND is encoded in the 'id' parameter */
12544 int n = 0;
12545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012546 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547 if (n != 0)
12548 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12549 if (r == NULL)
12550# else
12551 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012552 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553# endif
12554 EMSG(_("E277: Unable to read a server reply"));
12555 }
12556#endif
12557 rettv->v_type = VAR_STRING;
12558 rettv->vval.v_string = r;
12559}
12560
12561/*
12562 * "remote_send()" function
12563 */
12564/*ARGSUSED*/
12565 static void
12566f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012567 typval_T *argvars;
12568 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012569{
12570 rettv->v_type = VAR_STRING;
12571 rettv->vval.v_string = NULL;
12572#ifdef FEAT_CLIENTSERVER
12573 remote_common(argvars, rettv, FALSE);
12574#endif
12575}
12576
12577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012578 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012579 */
12580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012581f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 typval_T *argvars;
12583 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012584{
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 list_T *l;
12586 listitem_T *item, *item2;
12587 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012588 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012589 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012590 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012591 dict_T *d;
12592 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012593
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012594 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012595 if (argvars[0].v_type == VAR_DICT)
12596 {
12597 if (argvars[2].v_type != VAR_UNKNOWN)
12598 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012599 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012600 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602 key = get_tv_string_chk(&argvars[1]);
12603 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605 di = dict_find(d, key, -1);
12606 if (di == NULL)
12607 EMSG2(_(e_dictkey), key);
12608 else
12609 {
12610 *rettv = di->di_tv;
12611 init_tv(&di->di_tv);
12612 dictitem_remove(d, di);
12613 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012614 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012615 }
12616 }
12617 else if (argvars[0].v_type != VAR_LIST)
12618 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012619 else if ((l = argvars[0].vval.v_list) != NULL
12620 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012622 int error = FALSE;
12623
12624 idx = get_tv_number_chk(&argvars[1], &error);
12625 if (error)
12626 ; /* type error: do nothing, errmsg already given */
12627 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012628 EMSGN(_(e_listidx), idx);
12629 else
12630 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012631 if (argvars[2].v_type == VAR_UNKNOWN)
12632 {
12633 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012634 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012635 *rettv = item->li_tv;
12636 vim_free(item);
12637 }
12638 else
12639 {
12640 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 end = get_tv_number_chk(&argvars[2], &error);
12642 if (error)
12643 ; /* type error: do nothing */
12644 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012645 EMSGN(_(e_listidx), end);
12646 else
12647 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012648 int cnt = 0;
12649
12650 for (li = item; li != NULL; li = li->li_next)
12651 {
12652 ++cnt;
12653 if (li == item2)
12654 break;
12655 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012656 if (li == NULL) /* didn't find "item2" after "item" */
12657 EMSG(_(e_invrange));
12658 else
12659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012660 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012661 l = list_alloc();
12662 if (l != NULL)
12663 {
12664 rettv->v_type = VAR_LIST;
12665 rettv->vval.v_list = l;
12666 l->lv_first = item;
12667 l->lv_last = item2;
12668 l->lv_refcount = 1;
12669 item->li_prev = NULL;
12670 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012671 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012672 }
12673 }
12674 }
12675 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012676 }
12677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678}
12679
12680/*
12681 * "rename({from}, {to})" function
12682 */
12683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012685 typval_T *argvars;
12686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687{
12688 char_u buf[NUMBUFLEN];
12689
12690 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12694 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695}
12696
12697/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012698 * "repeat()" function
12699 */
12700/*ARGSUSED*/
12701 static void
12702f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012703 typval_T *argvars;
12704 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012705{
12706 char_u *p;
12707 int n;
12708 int slen;
12709 int len;
12710 char_u *r;
12711 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012712 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012713
12714 n = get_tv_number(&argvars[1]);
12715 if (argvars[0].v_type == VAR_LIST)
12716 {
12717 l = list_alloc();
12718 if (l != NULL && argvars[0].vval.v_list != NULL)
12719 {
12720 l->lv_refcount = 1;
12721 while (n-- > 0)
12722 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12723 break;
12724 }
12725 rettv->v_type = VAR_LIST;
12726 rettv->vval.v_list = l;
12727 }
12728 else
12729 {
12730 p = get_tv_string(&argvars[0]);
12731 rettv->v_type = VAR_STRING;
12732 rettv->vval.v_string = NULL;
12733
12734 slen = (int)STRLEN(p);
12735 len = slen * n;
12736 if (len <= 0)
12737 return;
12738
12739 r = alloc(len + 1);
12740 if (r != NULL)
12741 {
12742 for (i = 0; i < n; i++)
12743 mch_memmove(r + i * slen, p, (size_t)slen);
12744 r[len] = NUL;
12745 }
12746
12747 rettv->vval.v_string = r;
12748 }
12749}
12750
12751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 * "resolve()" function
12753 */
12754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012756 typval_T *argvars;
12757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758{
12759 char_u *p;
12760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762#ifdef FEAT_SHORTCUT
12763 {
12764 char_u *v = NULL;
12765
12766 v = mch_resolve_shortcut(p);
12767 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 }
12772#else
12773# ifdef HAVE_READLINK
12774 {
12775 char_u buf[MAXPATHL + 1];
12776 char_u *cpy;
12777 int len;
12778 char_u *remain = NULL;
12779 char_u *q;
12780 int is_relative_to_current = FALSE;
12781 int has_trailing_pathsep = FALSE;
12782 int limit = 100;
12783
12784 p = vim_strsave(p);
12785
12786 if (p[0] == '.' && (vim_ispathsep(p[1])
12787 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12788 is_relative_to_current = TRUE;
12789
12790 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012791 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 has_trailing_pathsep = TRUE;
12793
12794 q = getnextcomp(p);
12795 if (*q != NUL)
12796 {
12797 /* Separate the first path component in "p", and keep the
12798 * remainder (beginning with the path separator). */
12799 remain = vim_strsave(q - 1);
12800 q[-1] = NUL;
12801 }
12802
12803 for (;;)
12804 {
12805 for (;;)
12806 {
12807 len = readlink((char *)p, (char *)buf, MAXPATHL);
12808 if (len <= 0)
12809 break;
12810 buf[len] = NUL;
12811
12812 if (limit-- == 0)
12813 {
12814 vim_free(p);
12815 vim_free(remain);
12816 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 goto fail;
12819 }
12820
12821 /* Ensure that the result will have a trailing path separator
12822 * if the argument has one. */
12823 if (remain == NULL && has_trailing_pathsep)
12824 add_pathsep(buf);
12825
12826 /* Separate the first path component in the link value and
12827 * concatenate the remainders. */
12828 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12829 if (*q != NUL)
12830 {
12831 if (remain == NULL)
12832 remain = vim_strsave(q - 1);
12833 else
12834 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012835 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 if (cpy != NULL)
12837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 vim_free(remain);
12839 remain = cpy;
12840 }
12841 }
12842 q[-1] = NUL;
12843 }
12844
12845 q = gettail(p);
12846 if (q > p && *q == NUL)
12847 {
12848 /* Ignore trailing path separator. */
12849 q[-1] = NUL;
12850 q = gettail(p);
12851 }
12852 if (q > p && !mch_isFullName(buf))
12853 {
12854 /* symlink is relative to directory of argument */
12855 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12856 if (cpy != NULL)
12857 {
12858 STRCPY(cpy, p);
12859 STRCPY(gettail(cpy), buf);
12860 vim_free(p);
12861 p = cpy;
12862 }
12863 }
12864 else
12865 {
12866 vim_free(p);
12867 p = vim_strsave(buf);
12868 }
12869 }
12870
12871 if (remain == NULL)
12872 break;
12873
12874 /* Append the first path component of "remain" to "p". */
12875 q = getnextcomp(remain + 1);
12876 len = q - remain - (*q != NUL);
12877 cpy = vim_strnsave(p, STRLEN(p) + len);
12878 if (cpy != NULL)
12879 {
12880 STRNCAT(cpy, remain, len);
12881 vim_free(p);
12882 p = cpy;
12883 }
12884 /* Shorten "remain". */
12885 if (*q != NUL)
12886 STRCPY(remain, q - 1);
12887 else
12888 {
12889 vim_free(remain);
12890 remain = NULL;
12891 }
12892 }
12893
12894 /* If the result is a relative path name, make it explicitly relative to
12895 * the current directory if and only if the argument had this form. */
12896 if (!vim_ispathsep(*p))
12897 {
12898 if (is_relative_to_current
12899 && *p != NUL
12900 && !(p[0] == '.'
12901 && (p[1] == NUL
12902 || vim_ispathsep(p[1])
12903 || (p[1] == '.'
12904 && (p[2] == NUL
12905 || vim_ispathsep(p[2]))))))
12906 {
12907 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012908 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909 if (cpy != NULL)
12910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 vim_free(p);
12912 p = cpy;
12913 }
12914 }
12915 else if (!is_relative_to_current)
12916 {
12917 /* Strip leading "./". */
12918 q = p;
12919 while (q[0] == '.' && vim_ispathsep(q[1]))
12920 q += 2;
12921 if (q > p)
12922 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12923 }
12924 }
12925
12926 /* Ensure that the result will have no trailing path separator
12927 * if the argument had none. But keep "/" or "//". */
12928 if (!has_trailing_pathsep)
12929 {
12930 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012931 if (after_pathsep(p, q))
12932 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933 }
12934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012935 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 }
12937# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939# endif
12940#endif
12941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943
12944#ifdef HAVE_READLINK
12945fail:
12946#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948}
12949
12950/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012951 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 */
12953 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012954f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012955 typval_T *argvars;
12956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957{
Bram Moolenaar33570922005-01-25 22:26:29 +000012958 list_T *l;
12959 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960
Bram Moolenaar0d660222005-01-07 21:51:51 +000012961 rettv->vval.v_number = 0;
12962 if (argvars[0].v_type != VAR_LIST)
12963 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012964 else if ((l = argvars[0].vval.v_list) != NULL
12965 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012966 {
12967 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012968 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012969 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012970 while (li != NULL)
12971 {
12972 ni = li->li_prev;
12973 list_append(l, li);
12974 li = ni;
12975 }
12976 rettv->vval.v_list = l;
12977 rettv->v_type = VAR_LIST;
12978 ++l->lv_refcount;
12979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980}
12981
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012982#define SP_NOMOVE 1 /* don't move cursor */
12983#define SP_REPEAT 2 /* repeat to find outer pair */
12984#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012985#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012986
Bram Moolenaar33570922005-01-25 22:26:29 +000012987static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012988
12989/*
12990 * Get flags for a search function.
12991 * Possibly sets "p_ws".
12992 * Returns BACKWARD, FORWARD or zero (for an error).
12993 */
12994 static int
12995get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012996 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012997 int *flagsp;
12998{
12999 int dir = FORWARD;
13000 char_u *flags;
13001 char_u nbuf[NUMBUFLEN];
13002 int mask;
13003
13004 if (varp->v_type != VAR_UNKNOWN)
13005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013006 flags = get_tv_string_buf_chk(varp, nbuf);
13007 if (flags == NULL)
13008 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013009 while (*flags != NUL)
13010 {
13011 switch (*flags)
13012 {
13013 case 'b': dir = BACKWARD; break;
13014 case 'w': p_ws = TRUE; break;
13015 case 'W': p_ws = FALSE; break;
13016 default: mask = 0;
13017 if (flagsp != NULL)
13018 switch (*flags)
13019 {
13020 case 'n': mask = SP_NOMOVE; break;
13021 case 'r': mask = SP_REPEAT; break;
13022 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013023 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013024 }
13025 if (mask == 0)
13026 {
13027 EMSG2(_(e_invarg2), flags);
13028 dir = 0;
13029 }
13030 else
13031 *flagsp |= mask;
13032 }
13033 if (dir == 0)
13034 break;
13035 ++flags;
13036 }
13037 }
13038 return dir;
13039}
13040
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041/*
13042 * "search()" function
13043 */
13044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013045f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013046 typval_T *argvars;
13047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048{
13049 char_u *pat;
13050 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013051 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 int save_p_ws = p_ws;
13053 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013054 int flags = 0;
13055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013059 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13060 if (dir == 0)
13061 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013062 /*
13063 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13064 * Check to make sure only those flags are set.
13065 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13066 * flags cannot be set. Check for that condition also.
13067 */
13068 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13069 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013072 goto theend;
13073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013075 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13077 SEARCH_KEEP, RE_SEARCH) != FAIL)
13078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013080 if (flags & SP_SETPCMARK)
13081 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082 curwin->w_cursor = pos;
13083 /* "/$" will put the cursor after the end of the line, may need to
13084 * correct that here */
13085 check_cursor();
13086 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013087
13088 /* If 'n' flag is used: restore cursor position. */
13089 if (flags & SP_NOMOVE)
13090 curwin->w_cursor = save_cursor;
13091theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 p_ws = save_p_ws;
13093}
13094
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013096 * "searchdecl()" function
13097 */
13098 static void
13099f_searchdecl(argvars, rettv)
13100 typval_T *argvars;
13101 typval_T *rettv;
13102{
13103 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013104 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013105 int error = FALSE;
13106 char_u *name;
13107
13108 rettv->vval.v_number = 1; /* default: FAIL */
13109
13110 name = get_tv_string_chk(&argvars[0]);
13111 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013112 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013113 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013114 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13115 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13116 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013117 if (!error && name != NULL)
13118 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013119 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013120}
13121
13122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 * "searchpair()" function
13124 */
13125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013127 typval_T *argvars;
13128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129{
13130 char_u *spat, *mpat, *epat;
13131 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 int dir;
13134 int flags = 0;
13135 char_u nbuf1[NUMBUFLEN];
13136 char_u nbuf2[NUMBUFLEN];
13137 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013142 spat = get_tv_string_chk(&argvars[0]);
13143 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13144 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13145 if (spat == NULL || mpat == NULL || epat == NULL)
13146 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 /* Handle the optional fourth argument: flags */
13149 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013150 if (dir == 0)
13151 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013152 /*
13153 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13154 */
13155 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13156 {
13157 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13158 goto theend;
13159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160
13161 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013162 if (argvars[3].v_type == VAR_UNKNOWN
13163 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 skip = (char_u *)"";
13165 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013166 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13167 if (skip == NULL)
13168 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013170 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13171
13172theend:
13173 p_ws = save_p_ws;
13174}
13175
13176/*
13177 * Search for a start/middle/end thing.
13178 * Used by searchpair(), see its documentation for the details.
13179 * Returns 0 or -1 for no match,
13180 */
13181 long
13182do_searchpair(spat, mpat, epat, dir, skip, flags)
13183 char_u *spat; /* start pattern */
13184 char_u *mpat; /* middle pattern */
13185 char_u *epat; /* end pattern */
13186 int dir; /* BACKWARD or FORWARD */
13187 char_u *skip; /* skip expression */
13188 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13189{
13190 char_u *save_cpo;
13191 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13192 long retval = 0;
13193 pos_T pos;
13194 pos_T firstpos;
13195 pos_T foundpos;
13196 pos_T save_cursor;
13197 pos_T save_pos;
13198 int n;
13199 int r;
13200 int nest = 1;
13201 int err;
13202
13203 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13204 save_cpo = p_cpo;
13205 p_cpo = (char_u *)"";
13206
13207 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13208 * start/middle/end (pat3, for the top pair). */
13209 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13210 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13211 if (pat2 == NULL || pat3 == NULL)
13212 goto theend;
13213 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13214 if (*mpat == NUL)
13215 STRCPY(pat3, pat2);
13216 else
13217 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13218 spat, epat, mpat);
13219
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 save_cursor = curwin->w_cursor;
13221 pos = curwin->w_cursor;
13222 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013223 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 pat = pat3;
13225 for (;;)
13226 {
13227 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13228 SEARCH_KEEP, RE_SEARCH);
13229 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13230 /* didn't find it or found the first match again: FAIL */
13231 break;
13232
13233 if (firstpos.lnum == 0)
13234 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013235 if (equalpos(pos, foundpos))
13236 {
13237 /* Found the same position again. Can happen with a pattern that
13238 * has "\zs" at the end and searching backwards. Advance one
13239 * character and try again. */
13240 if (dir == BACKWARD)
13241 decl(&pos);
13242 else
13243 incl(&pos);
13244 }
13245 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246
13247 /* If the skip pattern matches, ignore this match. */
13248 if (*skip != NUL)
13249 {
13250 save_pos = curwin->w_cursor;
13251 curwin->w_cursor = pos;
13252 r = eval_to_bool(skip, &err, NULL, FALSE);
13253 curwin->w_cursor = save_pos;
13254 if (err)
13255 {
13256 /* Evaluating {skip} caused an error, break here. */
13257 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013258 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 break;
13260 }
13261 if (r)
13262 continue;
13263 }
13264
13265 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13266 {
13267 /* Found end when searching backwards or start when searching
13268 * forward: nested pair. */
13269 ++nest;
13270 pat = pat2; /* nested, don't search for middle */
13271 }
13272 else
13273 {
13274 /* Found end when searching forward or start when searching
13275 * backward: end of (nested) pair; or found middle in outer pair. */
13276 if (--nest == 1)
13277 pat = pat3; /* outer level, search for middle */
13278 }
13279
13280 if (nest == 0)
13281 {
13282 /* Found the match: return matchcount or line number. */
13283 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013284 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013286 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013287 if (flags & SP_SETPCMARK)
13288 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 curwin->w_cursor = pos;
13290 if (!(flags & SP_REPEAT))
13291 break;
13292 nest = 1; /* search for next unmatched */
13293 }
13294 }
13295
13296 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013297 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 curwin->w_cursor = save_cursor;
13299
13300theend:
13301 vim_free(pat2);
13302 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013304
13305 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306}
13307
Bram Moolenaar0d660222005-01-07 21:51:51 +000013308/*ARGSUSED*/
13309 static void
13310f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 typval_T *argvars;
13312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013314#ifdef FEAT_CLIENTSERVER
13315 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013316 char_u *server = get_tv_string_chk(&argvars[0]);
13317 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318
Bram Moolenaar0d660222005-01-07 21:51:51 +000013319 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013320 if (server == NULL || reply == NULL)
13321 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322 if (check_restricted() || check_secure())
13323 return;
13324# ifdef FEAT_X11
13325 if (check_connection() == FAIL)
13326 return;
13327# endif
13328
13329 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013331 EMSG(_("E258: Unable to send to client"));
13332 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013334 rettv->vval.v_number = 0;
13335#else
13336 rettv->vval.v_number = -1;
13337#endif
13338}
13339
13340/*ARGSUSED*/
13341 static void
13342f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013343 typval_T *argvars;
13344 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013345{
13346 char_u *r = NULL;
13347
13348#ifdef FEAT_CLIENTSERVER
13349# ifdef WIN32
13350 r = serverGetVimNames();
13351# else
13352 make_connection();
13353 if (X_DISPLAY != NULL)
13354 r = serverGetVimNames(X_DISPLAY);
13355# endif
13356#endif
13357 rettv->v_type = VAR_STRING;
13358 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359}
13360
13361/*
13362 * "setbufvar()" function
13363 */
13364/*ARGSUSED*/
13365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013366f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 typval_T *argvars;
13368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
13370 buf_T *buf;
13371#ifdef FEAT_AUTOCMD
13372 aco_save_T aco;
13373#else
13374 buf_T *save_curbuf;
13375#endif
13376 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 char_u nbuf[NUMBUFLEN];
13379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013380 rettv->vval.v_number = 0;
13381
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 if (check_restricted() || check_secure())
13383 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13385 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013386 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 varp = &argvars[2];
13388
13389 if (buf != NULL && varname != NULL && varp != NULL)
13390 {
13391 /* set curbuf to be our buf, temporarily */
13392#ifdef FEAT_AUTOCMD
13393 aucmd_prepbuf(&aco, buf);
13394#else
13395 save_curbuf = curbuf;
13396 curbuf = buf;
13397#endif
13398
13399 if (*varname == '&')
13400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013401 long numval;
13402 char_u *strval;
13403 int error = FALSE;
13404
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013406 numval = get_tv_number_chk(varp, &error);
13407 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013408 if (!error && strval != NULL)
13409 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 }
13411 else
13412 {
13413 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13414 if (bufvarname != NULL)
13415 {
13416 STRCPY(bufvarname, "b:");
13417 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013418 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 vim_free(bufvarname);
13420 }
13421 }
13422
13423 /* reset notion of buffer */
13424#ifdef FEAT_AUTOCMD
13425 aucmd_restbuf(&aco);
13426#else
13427 curbuf = save_curbuf;
13428#endif
13429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430}
13431
13432/*
13433 * "setcmdpos()" function
13434 */
13435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013436f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 typval_T *argvars;
13438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 int pos = (int)get_tv_number(&argvars[0]) - 1;
13441
13442 if (pos >= 0)
13443 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444}
13445
13446/*
13447 * "setline()" function
13448 */
13449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 typval_T *argvars;
13452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453{
13454 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013455 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013456 list_T *l = NULL;
13457 listitem_T *li = NULL;
13458 long added = 0;
13459 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013461 lnum = get_tv_lnum(&argvars[0]);
13462 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013464 l = argvars[1].vval.v_list;
13465 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013467 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013468 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013469
13470 rettv->vval.v_number = 0; /* OK */
13471 for (;;)
13472 {
13473 if (l != NULL)
13474 {
13475 /* list argument, get next string */
13476 if (li == NULL)
13477 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013479 li = li->li_next;
13480 }
13481
13482 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013484 break;
13485 if (lnum <= curbuf->b_ml.ml_line_count)
13486 {
13487 /* existing line, replace it */
13488 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13489 {
13490 changed_bytes(lnum, 0);
13491 check_cursor_col();
13492 rettv->vval.v_number = 0; /* OK */
13493 }
13494 }
13495 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13496 {
13497 /* lnum is one past the last line, append the line */
13498 ++added;
13499 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13500 rettv->vval.v_number = 0; /* OK */
13501 }
13502
13503 if (l == NULL) /* only one string argument */
13504 break;
13505 ++lnum;
13506 }
13507
13508 if (added > 0)
13509 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510}
13511
13512/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013513 * "setqflist()" function
13514 */
13515/*ARGSUSED*/
13516 static void
13517f_setqflist(argvars, rettv)
13518 typval_T *argvars;
13519 typval_T *rettv;
13520{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013521 char_u *act;
13522 int action = ' ';
13523
Bram Moolenaar2641f772005-03-25 21:58:17 +000013524 rettv->vval.v_number = -1;
13525
13526#ifdef FEAT_QUICKFIX
13527 if (argvars[0].v_type != VAR_LIST)
13528 EMSG(_(e_listreq));
13529 else
13530 {
13531 list_T *l = argvars[0].vval.v_list;
13532
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013533 if (argvars[1].v_type == VAR_STRING)
13534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013535 act = get_tv_string_chk(&argvars[1]);
13536 if (act == NULL)
13537 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013538 if (*act == 'a' || *act == 'r')
13539 action = *act;
13540 }
13541
13542 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013543 rettv->vval.v_number = 0;
13544 }
13545#endif
13546}
13547
13548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 * "setreg()" function
13550 */
13551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013553 typval_T *argvars;
13554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555{
13556 int regname;
13557 char_u *strregname;
13558 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013559 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 int append;
13561 char_u yank_type;
13562 long block_len;
13563
13564 block_len = -1;
13565 yank_type = MAUTO;
13566 append = FALSE;
13567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013568 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013571 if (strregname == NULL)
13572 return; /* type error; errmsg already given */
13573 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 if (regname == 0 || regname == '@')
13575 regname = '"';
13576 else if (regname == '=')
13577 return;
13578
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 stropt = get_tv_string_chk(&argvars[2]);
13582 if (stropt == NULL)
13583 return; /* type error */
13584 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 switch (*stropt)
13586 {
13587 case 'a': case 'A': /* append */
13588 append = TRUE;
13589 break;
13590 case 'v': case 'c': /* character-wise selection */
13591 yank_type = MCHAR;
13592 break;
13593 case 'V': case 'l': /* line-wise selection */
13594 yank_type = MLINE;
13595 break;
13596#ifdef FEAT_VISUAL
13597 case 'b': case Ctrl_V: /* block-wise selection */
13598 yank_type = MBLOCK;
13599 if (VIM_ISDIGIT(stropt[1]))
13600 {
13601 ++stropt;
13602 block_len = getdigits(&stropt) - 1;
13603 --stropt;
13604 }
13605 break;
13606#endif
13607 }
13608 }
13609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013610 strval = get_tv_string_chk(&argvars[1]);
13611 if (strval != NULL)
13612 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
13617
13618/*
13619 * "setwinvar(expr)" function
13620 */
13621/*ARGSUSED*/
13622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013624 typval_T *argvars;
13625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626{
13627 win_T *win;
13628#ifdef FEAT_WINDOWS
13629 win_T *save_curwin;
13630#endif
13631 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 char_u nbuf[NUMBUFLEN];
13634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 rettv->vval.v_number = 0;
13636
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 if (check_restricted() || check_secure())
13638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013640 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 varp = &argvars[2];
13642
13643 if (win != NULL && varname != NULL && varp != NULL)
13644 {
13645#ifdef FEAT_WINDOWS
13646 /* set curwin to be our win, temporarily */
13647 save_curwin = curwin;
13648 curwin = win;
13649 curbuf = curwin->w_buffer;
13650#endif
13651
13652 if (*varname == '&')
13653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013654 long numval;
13655 char_u *strval;
13656 int error = FALSE;
13657
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 numval = get_tv_number_chk(varp, &error);
13660 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661 if (!error && strval != NULL)
13662 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 }
13664 else
13665 {
13666 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13667 if (winvarname != NULL)
13668 {
13669 STRCPY(winvarname, "w:");
13670 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013671 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 vim_free(winvarname);
13673 }
13674 }
13675
13676#ifdef FEAT_WINDOWS
13677 /* Restore current window, if it's still valid (autocomands can make
13678 * it invalid). */
13679 if (win_valid(save_curwin))
13680 {
13681 curwin = save_curwin;
13682 curbuf = curwin->w_buffer;
13683 }
13684#endif
13685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686}
13687
13688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013689 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 */
13691 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013693 typval_T *argvars;
13694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013696 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697
Bram Moolenaar0d660222005-01-07 21:51:51 +000013698 p = get_tv_string(&argvars[0]);
13699 rettv->vval.v_string = vim_strsave(p);
13700 simplify_filename(rettv->vval.v_string); /* simplify in place */
13701 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702}
13703
Bram Moolenaar0d660222005-01-07 21:51:51 +000013704static int
13705#ifdef __BORLANDC__
13706 _RTLENTRYF
13707#endif
13708 item_compare __ARGS((const void *s1, const void *s2));
13709static int
13710#ifdef __BORLANDC__
13711 _RTLENTRYF
13712#endif
13713 item_compare2 __ARGS((const void *s1, const void *s2));
13714
13715static int item_compare_ic;
13716static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013718#define ITEM_COMPARE_FAIL 999
13719
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013721 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013723 static int
13724#ifdef __BORLANDC__
13725_RTLENTRYF
13726#endif
13727item_compare(s1, s2)
13728 const void *s1;
13729 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013731 char_u *p1, *p2;
13732 char_u *tofree1, *tofree2;
13733 int res;
13734 char_u numbuf1[NUMBUFLEN];
13735 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736
Bram Moolenaar33570922005-01-25 22:26:29 +000013737 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13738 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013739 if (item_compare_ic)
13740 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013742 res = STRCMP(p1, p2);
13743 vim_free(tofree1);
13744 vim_free(tofree2);
13745 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746}
13747
13748 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013749#ifdef __BORLANDC__
13750_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013752item_compare2(s1, s2)
13753 const void *s1;
13754 const void *s2;
13755{
13756 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013757 typval_T rettv;
13758 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013759 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013761 /* shortcut after failure in previous call; compare all items equal */
13762 if (item_compare_func_err)
13763 return 0;
13764
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013765 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13766 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013767 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13768 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013769
13770 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13771 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013772 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013773 clear_tv(&argv[0]);
13774 clear_tv(&argv[1]);
13775
13776 if (res == FAIL)
13777 res = ITEM_COMPARE_FAIL;
13778 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779 /* return value has wrong type */
13780 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13781 if (item_compare_func_err)
13782 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013783 clear_tv(&rettv);
13784 return res;
13785}
13786
13787/*
13788 * "sort({list})" function
13789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013791f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 typval_T *argvars;
13793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794{
Bram Moolenaar33570922005-01-25 22:26:29 +000013795 list_T *l;
13796 listitem_T *li;
13797 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013798 long len;
13799 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800
Bram Moolenaar0d660222005-01-07 21:51:51 +000013801 rettv->vval.v_number = 0;
13802 if (argvars[0].v_type != VAR_LIST)
13803 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 else
13805 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013806 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013807 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808 return;
13809 rettv->vval.v_list = l;
13810 rettv->v_type = VAR_LIST;
13811 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812
Bram Moolenaar0d660222005-01-07 21:51:51 +000013813 len = list_len(l);
13814 if (len <= 1)
13815 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816
Bram Moolenaar0d660222005-01-07 21:51:51 +000013817 item_compare_ic = FALSE;
13818 item_compare_func = NULL;
13819 if (argvars[1].v_type != VAR_UNKNOWN)
13820 {
13821 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013823 else
13824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 int error = FALSE;
13826
13827 i = get_tv_number_chk(&argvars[1], &error);
13828 if (error)
13829 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013830 if (i == 1)
13831 item_compare_ic = TRUE;
13832 else
13833 item_compare_func = get_tv_string(&argvars[1]);
13834 }
13835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836
Bram Moolenaar0d660222005-01-07 21:51:51 +000013837 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013838 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013839 if (ptrs == NULL)
13840 return;
13841 i = 0;
13842 for (li = l->lv_first; li != NULL; li = li->li_next)
13843 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013846 /* test the compare function */
13847 if (item_compare_func != NULL
13848 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13849 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013850 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013852 {
13853 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013854 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013855 item_compare_func == NULL ? item_compare : item_compare2);
13856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 if (!item_compare_func_err)
13858 {
13859 /* Clear the List and append the items in the sorted order. */
13860 l->lv_first = l->lv_last = NULL;
13861 l->lv_len = 0;
13862 for (i = 0; i < len; ++i)
13863 list_append(l, ptrs[i]);
13864 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865 }
13866
13867 vim_free(ptrs);
13868 }
13869}
13870
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013871/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013872 * "soundfold({word})" function
13873 */
13874 static void
13875f_soundfold(argvars, rettv)
13876 typval_T *argvars;
13877 typval_T *rettv;
13878{
13879 char_u *s;
13880
13881 rettv->v_type = VAR_STRING;
13882 s = get_tv_string(&argvars[0]);
13883#ifdef FEAT_SYN_HL
13884 rettv->vval.v_string = eval_soundfold(s);
13885#else
13886 rettv->vval.v_string = vim_strsave(s);
13887#endif
13888}
13889
13890/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013891 * "spellbadword()" function
13892 */
13893/* ARGSUSED */
13894 static void
13895f_spellbadword(argvars, rettv)
13896 typval_T *argvars;
13897 typval_T *rettv;
13898{
Bram Moolenaar4463f292005-09-25 22:20:24 +000013899 char_u *word = (char_u *)"";
13900#ifdef FEAT_SYN_HL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013901 int len = 0;
13902 hlf_T attr = HLF_COUNT;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013903 list_T *l;
13904#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013905
Bram Moolenaar4463f292005-09-25 22:20:24 +000013906 l = list_alloc();
13907 if (l == NULL)
13908 return;
13909 rettv->v_type = VAR_LIST;
13910 rettv->vval.v_list = l;
13911 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013912
13913#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000013914 if (argvars[0].v_type == VAR_UNKNOWN)
13915 {
13916 /* Find the start and length of the badly spelled word. */
13917 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
13918 if (len != 0)
13919 word = ml_get_cursor();
13920 }
13921 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13922 {
13923 char_u *str = get_tv_string_chk(&argvars[0]);
13924 int capcol = -1;
13925
13926 if (str != NULL)
13927 {
13928 /* Check the argument for spelling. */
13929 while (*str != NUL)
13930 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013931 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013932 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013933 {
13934 word = str;
13935 break;
13936 }
13937 str += len;
13938 }
13939 }
13940 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013941#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000013942
13943 list_append_string(l, word, len);
13944 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013945 attr == HLF_SPB ? "bad" :
13946 attr == HLF_SPR ? "rare" :
13947 attr == HLF_SPL ? "local" :
13948 attr == HLF_SPC ? "caps" :
13949 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013950}
13951
13952/*
13953 * "spellsuggest()" function
13954 */
13955 static void
13956f_spellsuggest(argvars, rettv)
13957 typval_T *argvars;
13958 typval_T *rettv;
13959{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013960 list_T *l;
13961#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013962 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013963 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013964 int maxcount;
13965 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013966 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013967 listitem_T *li;
13968 int need_capital = FALSE;
13969#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013970
13971 l = list_alloc();
13972 if (l == NULL)
13973 return;
13974 rettv->v_type = VAR_LIST;
13975 rettv->vval.v_list = l;
13976 ++l->lv_refcount;
13977
13978#ifdef FEAT_SYN_HL
13979 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13980 {
13981 str = get_tv_string(&argvars[0]);
13982 if (argvars[1].v_type != VAR_UNKNOWN)
13983 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013984 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013985 if (maxcount <= 0)
13986 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013987 if (argvars[2].v_type != VAR_UNKNOWN)
13988 {
13989 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
13990 if (typeerr)
13991 return;
13992 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013993 }
13994 else
13995 maxcount = 25;
13996
Bram Moolenaar4770d092006-01-12 23:22:24 +000013997 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013998
13999 for (i = 0; i < ga.ga_len; ++i)
14000 {
14001 str = ((char_u **)ga.ga_data)[i];
14002
14003 li = listitem_alloc();
14004 if (li == NULL)
14005 vim_free(str);
14006 else
14007 {
14008 li->li_tv.v_type = VAR_STRING;
14009 li->li_tv.v_lock = 0;
14010 li->li_tv.vval.v_string = str;
14011 list_append(l, li);
14012 }
14013 }
14014 ga_clear(&ga);
14015 }
14016#endif
14017}
14018
Bram Moolenaar0d660222005-01-07 21:51:51 +000014019 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014020f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014021 typval_T *argvars;
14022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014023{
14024 char_u *str;
14025 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014026 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014027 regmatch_T regmatch;
14028 char_u patbuf[NUMBUFLEN];
14029 char_u *save_cpo;
14030 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014031 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014032 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014033 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014034 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014035
14036 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14037 save_cpo = p_cpo;
14038 p_cpo = (char_u *)"";
14039
14040 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014041 if (argvars[1].v_type != VAR_UNKNOWN)
14042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014043 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14044 if (pat == NULL)
14045 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014048 }
14049 if (pat == NULL || *pat == NUL)
14050 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014051
14052 l = list_alloc();
14053 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014055 rettv->v_type = VAR_LIST;
14056 rettv->vval.v_list = l;
14057 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014058 if (typeerr)
14059 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060
Bram Moolenaar0d660222005-01-07 21:51:51 +000014061 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14062 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014064 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014065 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014067 if (*str == NUL)
14068 match = FALSE; /* empty item at the end */
14069 else
14070 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 if (match)
14072 end = regmatch.startp[0];
14073 else
14074 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014075 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14076 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014077 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014078 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014079 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014080 }
14081 if (!match)
14082 break;
14083 /* Advance to just after the match. */
14084 if (regmatch.endp[0] > str)
14085 col = 0;
14086 else
14087 {
14088 /* Don't get stuck at the same match. */
14089#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014090 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014091#else
14092 col = 1;
14093#endif
14094 }
14095 str = regmatch.endp[0];
14096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100
Bram Moolenaar0d660222005-01-07 21:51:51 +000014101 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102}
14103
14104#ifdef HAVE_STRFTIME
14105/*
14106 * "strftime({format}[, {time}])" function
14107 */
14108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014109f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 typval_T *argvars;
14111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112{
14113 char_u result_buf[256];
14114 struct tm *curtime;
14115 time_t seconds;
14116 char_u *p;
14117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014120 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014121 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 seconds = time(NULL);
14123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014124 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 curtime = localtime(&seconds);
14126 /* MSVC returns NULL for an invalid value of seconds. */
14127 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014128 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 else
14130 {
14131# ifdef FEAT_MBYTE
14132 vimconv_T conv;
14133 char_u *enc;
14134
14135 conv.vc_type = CONV_NONE;
14136 enc = enc_locale();
14137 convert_setup(&conv, p_enc, enc);
14138 if (conv.vc_type != CONV_NONE)
14139 p = string_convert(&conv, p, NULL);
14140# endif
14141 if (p != NULL)
14142 (void)strftime((char *)result_buf, sizeof(result_buf),
14143 (char *)p, curtime);
14144 else
14145 result_buf[0] = NUL;
14146
14147# ifdef FEAT_MBYTE
14148 if (conv.vc_type != CONV_NONE)
14149 vim_free(p);
14150 convert_setup(&conv, enc, p_enc);
14151 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 else
14154# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156
14157# ifdef FEAT_MBYTE
14158 /* Release conversion descriptors */
14159 convert_setup(&conv, NULL, NULL);
14160 vim_free(enc);
14161# endif
14162 }
14163}
14164#endif
14165
14166/*
14167 * "stridx()" function
14168 */
14169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014170f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014171 typval_T *argvars;
14172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173{
14174 char_u buf[NUMBUFLEN];
14175 char_u *needle;
14176 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014177 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014179 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014181 needle = get_tv_string_chk(&argvars[1]);
14182 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 if (needle == NULL || haystack == NULL)
14185 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186
Bram Moolenaar33570922005-01-25 22:26:29 +000014187 if (argvars[2].v_type != VAR_UNKNOWN)
14188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 int error = FALSE;
14190
14191 start_idx = get_tv_number_chk(&argvars[2], &error);
14192 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014194 if (start_idx >= 0)
14195 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014196 }
14197
14198 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14199 if (pos != NULL)
14200 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201}
14202
14203/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014204 * "string()" function
14205 */
14206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014207f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014208 typval_T *argvars;
14209 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014210{
14211 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014212 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014214 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014215 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014216 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218}
14219
14220/*
14221 * "strlen()" function
14222 */
14223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014224f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014225 typval_T *argvars;
14226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 rettv->vval.v_number = (varnumber_T)(STRLEN(
14229 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230}
14231
14232/*
14233 * "strpart()" function
14234 */
14235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014236f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014237 typval_T *argvars;
14238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239{
14240 char_u *p;
14241 int n;
14242 int len;
14243 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014244 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014246 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 slen = (int)STRLEN(p);
14248
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249 n = get_tv_number_chk(&argvars[1], &error);
14250 if (error)
14251 len = 0;
14252 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014253 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 else
14255 len = slen - n; /* default len: all bytes that are available. */
14256
14257 /*
14258 * Only return the overlap between the specified part and the actual
14259 * string.
14260 */
14261 if (n < 0)
14262 {
14263 len += n;
14264 n = 0;
14265 }
14266 else if (n > slen)
14267 n = slen;
14268 if (len < 0)
14269 len = 0;
14270 else if (n + len > slen)
14271 len = slen - n;
14272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273 rettv->v_type = VAR_STRING;
14274 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275}
14276
14277/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014278 * "strridx()" function
14279 */
14280 static void
14281f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014282 typval_T *argvars;
14283 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014284{
14285 char_u buf[NUMBUFLEN];
14286 char_u *needle;
14287 char_u *haystack;
14288 char_u *rest;
14289 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014290 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014292 needle = get_tv_string_chk(&argvars[1]);
14293 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014294 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295
14296 rettv->vval.v_number = -1;
14297 if (needle == NULL || haystack == NULL)
14298 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014299 if (argvars[2].v_type != VAR_UNKNOWN)
14300 {
14301 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014303 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014305 }
14306 else
14307 end_idx = haystack_len;
14308
Bram Moolenaar0d660222005-01-07 21:51:51 +000014309 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014310 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014311 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014312 lastmatch = haystack + end_idx;
14313 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014314 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014315 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316 for (rest = haystack; *rest != '\0'; ++rest)
14317 {
14318 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014319 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014320 break;
14321 lastmatch = rest;
14322 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014323 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324
14325 if (lastmatch == NULL)
14326 rettv->vval.v_number = -1;
14327 else
14328 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14329}
14330
14331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 * "strtrans()" function
14333 */
14334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014335f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014336 typval_T *argvars;
14337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339 rettv->v_type = VAR_STRING;
14340 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341}
14342
14343/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014344 * "submatch()" function
14345 */
14346 static void
14347f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014348 typval_T *argvars;
14349 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014350{
14351 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 rettv->vval.v_string =
14353 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014354}
14355
14356/*
14357 * "substitute()" function
14358 */
14359 static void
14360f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 typval_T *argvars;
14362 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014363{
14364 char_u patbuf[NUMBUFLEN];
14365 char_u subbuf[NUMBUFLEN];
14366 char_u flagsbuf[NUMBUFLEN];
14367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014368 char_u *str = get_tv_string_chk(&argvars[0]);
14369 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14370 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14371 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14372
Bram Moolenaar0d660222005-01-07 21:51:51 +000014373 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14375 rettv->vval.v_string = NULL;
14376 else
14377 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014378}
14379
14380/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014381 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 */
14383/*ARGSUSED*/
14384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014386 typval_T *argvars;
14387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388{
14389 int id = 0;
14390#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014391 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 long col;
14393 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014394 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 lnum = get_tv_lnum(argvars); /* -1 on type error */
14397 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14398 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014400 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014401 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014402 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403#endif
14404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406}
14407
14408/*
14409 * "synIDattr(id, what [, mode])" function
14410 */
14411/*ARGSUSED*/
14412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014414 typval_T *argvars;
14415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416{
14417 char_u *p = NULL;
14418#ifdef FEAT_SYN_HL
14419 int id;
14420 char_u *what;
14421 char_u *mode;
14422 char_u modebuf[NUMBUFLEN];
14423 int modec;
14424
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 id = get_tv_number(&argvars[0]);
14426 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014427 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 modec = TOLOWER_ASC(mode[0]);
14431 if (modec != 't' && modec != 'c'
14432#ifdef FEAT_GUI
14433 && modec != 'g'
14434#endif
14435 )
14436 modec = 0; /* replace invalid with current */
14437 }
14438 else
14439 {
14440#ifdef FEAT_GUI
14441 if (gui.in_use)
14442 modec = 'g';
14443 else
14444#endif
14445 if (t_colors > 1)
14446 modec = 'c';
14447 else
14448 modec = 't';
14449 }
14450
14451
14452 switch (TOLOWER_ASC(what[0]))
14453 {
14454 case 'b':
14455 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14456 p = highlight_color(id, what, modec);
14457 else /* bold */
14458 p = highlight_has_attr(id, HL_BOLD, modec);
14459 break;
14460
14461 case 'f': /* fg[#] */
14462 p = highlight_color(id, what, modec);
14463 break;
14464
14465 case 'i':
14466 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14467 p = highlight_has_attr(id, HL_INVERSE, modec);
14468 else /* italic */
14469 p = highlight_has_attr(id, HL_ITALIC, modec);
14470 break;
14471
14472 case 'n': /* name */
14473 p = get_highlight_name(NULL, id - 1);
14474 break;
14475
14476 case 'r': /* reverse */
14477 p = highlight_has_attr(id, HL_INVERSE, modec);
14478 break;
14479
14480 case 's': /* standout */
14481 p = highlight_has_attr(id, HL_STANDOUT, modec);
14482 break;
14483
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014484 case 'u':
14485 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14486 /* underline */
14487 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14488 else
14489 /* undercurl */
14490 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 break;
14492 }
14493
14494 if (p != NULL)
14495 p = vim_strsave(p);
14496#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014497 rettv->v_type = VAR_STRING;
14498 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499}
14500
14501/*
14502 * "synIDtrans(id)" function
14503 */
14504/*ARGSUSED*/
14505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014507 typval_T *argvars;
14508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509{
14510 int id;
14511
14512#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014513 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514
14515 if (id > 0)
14516 id = syn_get_final_id(id);
14517 else
14518#endif
14519 id = 0;
14520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014521 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522}
14523
14524/*
14525 * "system()" function
14526 */
14527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 typval_T *argvars;
14530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014532 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014534 char_u *infile = NULL;
14535 char_u buf[NUMBUFLEN];
14536 int err = FALSE;
14537 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014539 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014540 {
14541 /*
14542 * Write the string to a temp file, to be used for input of the shell
14543 * command.
14544 */
14545 if ((infile = vim_tempname('i')) == NULL)
14546 {
14547 EMSG(_(e_notmp));
14548 return;
14549 }
14550
14551 fd = mch_fopen((char *)infile, WRITEBIN);
14552 if (fd == NULL)
14553 {
14554 EMSG2(_(e_notopen), infile);
14555 goto done;
14556 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 p = get_tv_string_buf_chk(&argvars[1], buf);
14558 if (p == NULL)
14559 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014560 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14561 err = TRUE;
14562 if (fclose(fd) != 0)
14563 err = TRUE;
14564 if (err)
14565 {
14566 EMSG(_("E677: Error writing temp file"));
14567 goto done;
14568 }
14569 }
14570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014571 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014572
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573#ifdef USE_CR
14574 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014575 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 {
14577 char_u *s;
14578
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014579 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 {
14581 if (*s == CAR)
14582 *s = NL;
14583 }
14584 }
14585#else
14586# ifdef USE_CRNL
14587 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014588 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 {
14590 char_u *s, *d;
14591
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014592 d = res;
14593 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 {
14595 if (s[0] == CAR && s[1] == NL)
14596 ++s;
14597 *d++ = *s;
14598 }
14599 *d = NUL;
14600 }
14601# endif
14602#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014603
14604done:
14605 if (infile != NULL)
14606 {
14607 mch_remove(infile);
14608 vim_free(infile);
14609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014610 rettv->v_type = VAR_STRING;
14611 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612}
14613
14614/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014615 * "tagfiles()" function
14616 */
14617/*ARGSUSED*/
14618 static void
14619f_tagfiles(argvars, rettv)
14620 typval_T *argvars;
14621 typval_T *rettv;
14622{
14623 char_u fname[MAXPATHL + 1];
14624 list_T *l;
14625
14626 l = list_alloc();
14627 if (l == NULL)
14628 {
14629 rettv->vval.v_number = 0;
14630 return;
14631 }
14632 rettv->vval.v_list = l;
14633 rettv->v_type = VAR_LIST;
14634 ++l->lv_refcount;
14635
14636 get_tagfname(TRUE, NULL);
14637 for (;;)
14638 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014639 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014640 break;
14641}
14642
14643/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014644 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014645 */
14646 static void
14647f_taglist(argvars, rettv)
14648 typval_T *argvars;
14649 typval_T *rettv;
14650{
14651 char_u *tag_pattern;
14652 list_T *l;
14653
14654 tag_pattern = get_tv_string(&argvars[0]);
14655
14656 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014657 if (*tag_pattern == NUL)
14658 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014659
14660 l = list_alloc();
14661 if (l != NULL)
14662 {
14663 if (get_tags(l, tag_pattern) != FAIL)
14664 {
14665 rettv->vval.v_list = l;
14666 rettv->v_type = VAR_LIST;
14667 ++l->lv_refcount;
14668 }
14669 else
14670 list_free(l);
14671 }
14672}
14673
14674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 * "tempname()" function
14676 */
14677/*ARGSUSED*/
14678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 typval_T *argvars;
14681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682{
14683 static int x = 'A';
14684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685 rettv->v_type = VAR_STRING;
14686 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687
14688 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14689 * names. Skip 'I' and 'O', they are used for shell redirection. */
14690 do
14691 {
14692 if (x == 'Z')
14693 x = '0';
14694 else if (x == '9')
14695 x = 'A';
14696 else
14697 {
14698#ifdef EBCDIC
14699 if (x == 'I')
14700 x = 'J';
14701 else if (x == 'R')
14702 x = 'S';
14703 else
14704#endif
14705 ++x;
14706 }
14707 } while (x == 'I' || x == 'O');
14708}
14709
14710/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014711 * "test(list)" function: Just checking the walls...
14712 */
14713/*ARGSUSED*/
14714 static void
14715f_test(argvars, rettv)
14716 typval_T *argvars;
14717 typval_T *rettv;
14718{
14719 /* Used for unit testing. Change the code below to your liking. */
14720#if 0
14721 listitem_T *li;
14722 list_T *l;
14723 char_u *bad, *good;
14724
14725 if (argvars[0].v_type != VAR_LIST)
14726 return;
14727 l = argvars[0].vval.v_list;
14728 if (l == NULL)
14729 return;
14730 li = l->lv_first;
14731 if (li == NULL)
14732 return;
14733 bad = get_tv_string(&li->li_tv);
14734 li = li->li_next;
14735 if (li == NULL)
14736 return;
14737 good = get_tv_string(&li->li_tv);
14738 rettv->vval.v_number = test_edit_score(bad, good);
14739#endif
14740}
14741
14742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 * "tolower(string)" function
14744 */
14745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014747 typval_T *argvars;
14748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749{
14750 char_u *p;
14751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014752 p = vim_strsave(get_tv_string(&argvars[0]));
14753 rettv->v_type = VAR_STRING;
14754 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755
14756 if (p != NULL)
14757 while (*p != NUL)
14758 {
14759#ifdef FEAT_MBYTE
14760 int l;
14761
14762 if (enc_utf8)
14763 {
14764 int c, lc;
14765
14766 c = utf_ptr2char(p);
14767 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014768 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 /* TODO: reallocate string when byte count changes. */
14770 if (utf_char2len(lc) == l)
14771 utf_char2bytes(lc, p);
14772 p += l;
14773 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014774 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775 p += l; /* skip multi-byte character */
14776 else
14777#endif
14778 {
14779 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14780 ++p;
14781 }
14782 }
14783}
14784
14785/*
14786 * "toupper(string)" function
14787 */
14788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014789f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 typval_T *argvars;
14791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795}
14796
14797/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014798 * "tr(string, fromstr, tostr)" function
14799 */
14800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014801f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014802 typval_T *argvars;
14803 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014804{
14805 char_u *instr;
14806 char_u *fromstr;
14807 char_u *tostr;
14808 char_u *p;
14809#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014810 int inlen;
14811 int fromlen;
14812 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014813 int idx;
14814 char_u *cpstr;
14815 int cplen;
14816 int first = TRUE;
14817#endif
14818 char_u buf[NUMBUFLEN];
14819 char_u buf2[NUMBUFLEN];
14820 garray_T ga;
14821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14824 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014825
14826 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->v_type = VAR_STRING;
14828 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014829 if (fromstr == NULL || tostr == NULL)
14830 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014831 ga_init2(&ga, (int)sizeof(char), 80);
14832
14833#ifdef FEAT_MBYTE
14834 if (!has_mbyte)
14835#endif
14836 /* not multi-byte: fromstr and tostr must be the same length */
14837 if (STRLEN(fromstr) != STRLEN(tostr))
14838 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014839#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014840error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014841#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014842 EMSG2(_(e_invarg2), fromstr);
14843 ga_clear(&ga);
14844 return;
14845 }
14846
14847 /* fromstr and tostr have to contain the same number of chars */
14848 while (*instr != NUL)
14849 {
14850#ifdef FEAT_MBYTE
14851 if (has_mbyte)
14852 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014853 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014854 cpstr = instr;
14855 cplen = inlen;
14856 idx = 0;
14857 for (p = fromstr; *p != NUL; p += fromlen)
14858 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014859 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014860 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14861 {
14862 for (p = tostr; *p != NUL; p += tolen)
14863 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014864 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014865 if (idx-- == 0)
14866 {
14867 cplen = tolen;
14868 cpstr = p;
14869 break;
14870 }
14871 }
14872 if (*p == NUL) /* tostr is shorter than fromstr */
14873 goto error;
14874 break;
14875 }
14876 ++idx;
14877 }
14878
14879 if (first && cpstr == instr)
14880 {
14881 /* Check that fromstr and tostr have the same number of
14882 * (multi-byte) characters. Done only once when a character
14883 * of instr doesn't appear in fromstr. */
14884 first = FALSE;
14885 for (p = tostr; *p != NUL; p += tolen)
14886 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014887 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014888 --idx;
14889 }
14890 if (idx != 0)
14891 goto error;
14892 }
14893
14894 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014895 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014896 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014897
14898 instr += inlen;
14899 }
14900 else
14901#endif
14902 {
14903 /* When not using multi-byte chars we can do it faster. */
14904 p = vim_strchr(fromstr, *instr);
14905 if (p != NULL)
14906 ga_append(&ga, tostr[p - fromstr]);
14907 else
14908 ga_append(&ga, *instr);
14909 ++instr;
14910 }
14911 }
14912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014914}
14915
14916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 * "type(expr)" function
14918 */
14919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014920f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014921 typval_T *argvars;
14922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014924 int n;
14925
14926 switch (argvars[0].v_type)
14927 {
14928 case VAR_NUMBER: n = 0; break;
14929 case VAR_STRING: n = 1; break;
14930 case VAR_FUNC: n = 2; break;
14931 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014932 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014933 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14934 }
14935 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936}
14937
14938/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014939 * "values(dict)" function
14940 */
14941 static void
14942f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014943 typval_T *argvars;
14944 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014945{
14946 dict_list(argvars, rettv, 1);
14947}
14948
14949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 * "virtcol(string)" function
14951 */
14952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 typval_T *argvars;
14955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956{
14957 colnr_T vcol = 0;
14958 pos_T *fp;
14959
14960 fp = var2fpos(&argvars[0], FALSE);
14961 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14962 {
14963 getvvcol(curwin, fp, NULL, NULL, &vcol);
14964 ++vcol;
14965 }
14966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968}
14969
14970/*
14971 * "visualmode()" function
14972 */
14973/*ARGSUSED*/
14974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014975f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *argvars;
14977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978{
14979#ifdef FEAT_VISUAL
14980 char_u str[2];
14981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 str[0] = curbuf->b_visual_mode_eval;
14984 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014985 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986
14987 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014988 if ((argvars[0].v_type == VAR_NUMBER
14989 && argvars[0].vval.v_number != 0)
14990 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014991 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 curbuf->b_visual_mode_eval = NUL;
14993#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995#endif
14996}
14997
14998/*
14999 * "winbufnr(nr)" function
15000 */
15001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002f_winbufnr(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 win_T *wp;
15007
15008 wp = find_win_by_nr(&argvars[0]);
15009 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013}
15014
15015/*
15016 * "wincol()" function
15017 */
15018/*ARGSUSED*/
15019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015020f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015021 typval_T *argvars;
15022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023{
15024 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015025 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026}
15027
15028/*
15029 * "winheight(nr)" function
15030 */
15031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015033 typval_T *argvars;
15034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035{
15036 win_T *wp;
15037
15038 wp = find_win_by_nr(&argvars[0]);
15039 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043}
15044
15045/*
15046 * "winline()" function
15047 */
15048/*ARGSUSED*/
15049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 typval_T *argvars;
15052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053{
15054 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056}
15057
15058/*
15059 * "winnr()" function
15060 */
15061/* ARGSUSED */
15062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015064 typval_T *argvars;
15065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066{
15067 int nr = 1;
15068#ifdef FEAT_WINDOWS
15069 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015070 win_T *twin = curwin;
15071 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015073 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015075 arg = get_tv_string_chk(&argvars[0]);
15076 if (arg == NULL)
15077 nr = 0; /* type error; errmsg already given */
15078 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015079 twin = lastwin;
15080 else if (STRCMP(arg, "#") == 0)
15081 {
15082 twin = prevwin;
15083 if (prevwin == NULL)
15084 nr = 0;
15085 }
15086 else
15087 {
15088 EMSG2(_(e_invexpr2), arg);
15089 nr = 0;
15090 }
15091 }
15092
15093 if (nr > 0)
15094 for (wp = firstwin; wp != twin; wp = wp->w_next)
15095 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098}
15099
15100/*
15101 * "winrestcmd()" function
15102 */
15103/* ARGSUSED */
15104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015105f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015106 typval_T *argvars;
15107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108{
15109#ifdef FEAT_WINDOWS
15110 win_T *wp;
15111 int winnr = 1;
15112 garray_T ga;
15113 char_u buf[50];
15114
15115 ga_init2(&ga, (int)sizeof(char), 70);
15116 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15117 {
15118 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15119 ga_concat(&ga, buf);
15120# ifdef FEAT_VERTSPLIT
15121 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15122 ga_concat(&ga, buf);
15123# endif
15124 ++winnr;
15125 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015126 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133}
15134
15135/*
15136 * "winwidth(nr)" function
15137 */
15138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015140 typval_T *argvars;
15141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142{
15143 win_T *wp;
15144
15145 wp = find_win_by_nr(&argvars[0]);
15146 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015147 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 else
15149#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015150 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153#endif
15154}
15155
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015157 * "writefile()" function
15158 */
15159 static void
15160f_writefile(argvars, rettv)
15161 typval_T *argvars;
15162 typval_T *rettv;
15163{
15164 int binary = FALSE;
15165 char_u *fname;
15166 FILE *fd;
15167 listitem_T *li;
15168 char_u *s;
15169 int ret = 0;
15170 int c;
15171
15172 if (argvars[0].v_type != VAR_LIST)
15173 {
15174 EMSG2(_(e_listarg), "writefile()");
15175 return;
15176 }
15177 if (argvars[0].vval.v_list == NULL)
15178 return;
15179
15180 if (argvars[2].v_type != VAR_UNKNOWN
15181 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15182 binary = TRUE;
15183
15184 /* Always open the file in binary mode, library functions have a mind of
15185 * their own about CR-LF conversion. */
15186 fname = get_tv_string(&argvars[1]);
15187 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15188 {
15189 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15190 ret = -1;
15191 }
15192 else
15193 {
15194 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15195 li = li->li_next)
15196 {
15197 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15198 {
15199 if (*s == '\n')
15200 c = putc(NUL, fd);
15201 else
15202 c = putc(*s, fd);
15203 if (c == EOF)
15204 {
15205 ret = -1;
15206 break;
15207 }
15208 }
15209 if (!binary || li->li_next != NULL)
15210 if (putc('\n', fd) == EOF)
15211 {
15212 ret = -1;
15213 break;
15214 }
15215 if (ret < 0)
15216 {
15217 EMSG(_(e_write));
15218 break;
15219 }
15220 }
15221 fclose(fd);
15222 }
15223
15224 rettv->vval.v_number = ret;
15225}
15226
15227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 * Translate a String variable into a position.
15229 */
15230 static pos_T *
15231var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 int lnum; /* TRUE when $ is last line */
15234{
15235 char_u *name;
15236 static pos_T pos;
15237 pos_T *pp;
15238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015239 name = get_tv_string_chk(varp);
15240 if (name == NULL)
15241 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242 if (name[0] == '.') /* cursor */
15243 return &curwin->w_cursor;
15244 if (name[0] == '\'') /* mark */
15245 {
15246 pp = getmark(name[1], FALSE);
15247 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15248 return NULL;
15249 return pp;
15250 }
15251 if (name[0] == '$') /* last column or line */
15252 {
15253 if (lnum)
15254 {
15255 pos.lnum = curbuf->b_ml.ml_line_count;
15256 pos.col = 0;
15257 }
15258 else
15259 {
15260 pos.lnum = curwin->w_cursor.lnum;
15261 pos.col = (colnr_T)STRLEN(ml_get_curline());
15262 }
15263 return &pos;
15264 }
15265 return NULL;
15266}
15267
15268/*
15269 * Get the length of an environment variable name.
15270 * Advance "arg" to the first character after the name.
15271 * Return 0 for error.
15272 */
15273 static int
15274get_env_len(arg)
15275 char_u **arg;
15276{
15277 char_u *p;
15278 int len;
15279
15280 for (p = *arg; vim_isIDc(*p); ++p)
15281 ;
15282 if (p == *arg) /* no name found */
15283 return 0;
15284
15285 len = (int)(p - *arg);
15286 *arg = p;
15287 return len;
15288}
15289
15290/*
15291 * Get the length of the name of a function or internal variable.
15292 * "arg" is advanced to the first non-white character after the name.
15293 * Return 0 if something is wrong.
15294 */
15295 static int
15296get_id_len(arg)
15297 char_u **arg;
15298{
15299 char_u *p;
15300 int len;
15301
15302 /* Find the end of the name. */
15303 for (p = *arg; eval_isnamec(*p); ++p)
15304 ;
15305 if (p == *arg) /* no name found */
15306 return 0;
15307
15308 len = (int)(p - *arg);
15309 *arg = skipwhite(p);
15310
15311 return len;
15312}
15313
15314/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015315 * Get the length of the name of a variable or function.
15316 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015318 * Return -1 if curly braces expansion failed.
15319 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 * If the name contains 'magic' {}'s, expand them and return the
15321 * expanded name in an allocated string via 'alias' - caller must free.
15322 */
15323 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015324get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 char_u **arg;
15326 char_u **alias;
15327 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015328 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329{
15330 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 char_u *p;
15332 char_u *expr_start;
15333 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334
15335 *alias = NULL; /* default to no alias */
15336
15337 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15338 && (*arg)[2] == (int)KE_SNR)
15339 {
15340 /* hard coded <SNR>, already translated */
15341 *arg += 3;
15342 return get_id_len(arg) + 3;
15343 }
15344 len = eval_fname_script(*arg);
15345 if (len > 0)
15346 {
15347 /* literal "<SID>", "s:" or "<SNR>" */
15348 *arg += len;
15349 }
15350
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015352 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015354 p = find_name_end(*arg, &expr_start, &expr_end,
15355 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 if (expr_start != NULL)
15357 {
15358 char_u *temp_string;
15359
15360 if (!evaluate)
15361 {
15362 len += (int)(p - *arg);
15363 *arg = skipwhite(p);
15364 return len;
15365 }
15366
15367 /*
15368 * Include any <SID> etc in the expanded string:
15369 * Thus the -len here.
15370 */
15371 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15372 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 *alias = temp_string;
15375 *arg = skipwhite(p);
15376 return (int)STRLEN(temp_string);
15377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378
15379 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015380 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 EMSG2(_(e_invexpr2), *arg);
15382
15383 return len;
15384}
15385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386/*
15387 * Find the end of a variable or function name, taking care of magic braces.
15388 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15389 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015390 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 * Return a pointer to just after the name. Equal to "arg" if there is no
15392 * valid name.
15393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015395find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 char_u *arg;
15397 char_u **expr_start;
15398 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015399 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 int mb_nest = 0;
15402 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 char_u *p;
15404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407 *expr_start = NULL;
15408 *expr_end = NULL;
15409 }
15410
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015411 /* Quick check for valid starting character. */
15412 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15413 return arg;
15414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 for (p = arg; *p != NUL
15416 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015417 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015418 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015420 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015421 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015422 if (*p == '\'')
15423 {
15424 /* skip over 'string' to avoid counting [ and ] inside it. */
15425 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15426 ;
15427 if (*p == NUL)
15428 break;
15429 }
15430 else if (*p == '"')
15431 {
15432 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15433 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15434 if (*p == '\\' && p[1] != NUL)
15435 ++p;
15436 if (*p == NUL)
15437 break;
15438 }
15439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015442 if (*p == '[')
15443 ++br_nest;
15444 else if (*p == ']')
15445 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015448 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 if (*p == '{')
15451 {
15452 mb_nest++;
15453 if (expr_start != NULL && *expr_start == NULL)
15454 *expr_start = p;
15455 }
15456 else if (*p == '}')
15457 {
15458 mb_nest--;
15459 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15460 *expr_end = p;
15461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 }
15464
15465 return p;
15466}
15467
15468/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015469 * Expands out the 'magic' {}'s in a variable/function name.
15470 * Note that this can call itself recursively, to deal with
15471 * constructs like foo{bar}{baz}{bam}
15472 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15473 * "in_start" ^
15474 * "expr_start" ^
15475 * "expr_end" ^
15476 * "in_end" ^
15477 *
15478 * Returns a new allocated string, which the caller must free.
15479 * Returns NULL for failure.
15480 */
15481 static char_u *
15482make_expanded_name(in_start, expr_start, expr_end, in_end)
15483 char_u *in_start;
15484 char_u *expr_start;
15485 char_u *expr_end;
15486 char_u *in_end;
15487{
15488 char_u c1;
15489 char_u *retval = NULL;
15490 char_u *temp_result;
15491 char_u *nextcmd = NULL;
15492
15493 if (expr_end == NULL || in_end == NULL)
15494 return NULL;
15495 *expr_start = NUL;
15496 *expr_end = NUL;
15497 c1 = *in_end;
15498 *in_end = NUL;
15499
15500 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15501 if (temp_result != NULL && nextcmd == NULL)
15502 {
15503 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15504 + (in_end - expr_end) + 1));
15505 if (retval != NULL)
15506 {
15507 STRCPY(retval, in_start);
15508 STRCAT(retval, temp_result);
15509 STRCAT(retval, expr_end + 1);
15510 }
15511 }
15512 vim_free(temp_result);
15513
15514 *in_end = c1; /* put char back for error messages */
15515 *expr_start = '{';
15516 *expr_end = '}';
15517
15518 if (retval != NULL)
15519 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015520 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015521 if (expr_start != NULL)
15522 {
15523 /* Further expansion! */
15524 temp_result = make_expanded_name(retval, expr_start,
15525 expr_end, temp_result);
15526 vim_free(retval);
15527 retval = temp_result;
15528 }
15529 }
15530
15531 return retval;
15532}
15533
15534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015536 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 */
15538 static int
15539eval_isnamec(c)
15540 int c;
15541{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015542 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15543}
15544
15545/*
15546 * Return TRUE if character "c" can be used as the first character in a
15547 * variable or function name (excluding '{' and '}').
15548 */
15549 static int
15550eval_isnamec1(c)
15551 int c;
15552{
15553 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554}
15555
15556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 * Set number v: variable to "val".
15558 */
15559 void
15560set_vim_var_nr(idx, val)
15561 int idx;
15562 long val;
15563{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015564 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565}
15566
15567/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015568 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569 */
15570 long
15571get_vim_var_nr(idx)
15572 int idx;
15573{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015574 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575}
15576
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015577#if defined(FEAT_AUTOCMD) || defined(PROTO)
15578/*
15579 * Get string v: variable value. Uses a static buffer, can only be used once.
15580 */
15581 char_u *
15582get_vim_var_str(idx)
15583 int idx;
15584{
15585 return get_tv_string(&vimvars[idx].vv_tv);
15586}
15587#endif
15588
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589/*
15590 * Set v:count, v:count1 and v:prevcount.
15591 */
15592 void
15593set_vcount(count, count1)
15594 long count;
15595 long count1;
15596{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015597 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15598 vimvars[VV_COUNT].vv_nr = count;
15599 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600}
15601
15602/*
15603 * Set string v: variable to a copy of "val".
15604 */
15605 void
15606set_vim_var_string(idx, val, len)
15607 int idx;
15608 char_u *val;
15609 int len; /* length of "val" to use or -1 (whole string) */
15610{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015611 /* Need to do this (at least) once, since we can't initialize a union.
15612 * Will always be invoked when "v:progname" is set. */
15613 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15614
Bram Moolenaare9a41262005-01-15 22:18:47 +000015615 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015617 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015619 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015621 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622}
15623
15624/*
15625 * Set v:register if needed.
15626 */
15627 void
15628set_reg_var(c)
15629 int c;
15630{
15631 char_u regname;
15632
15633 if (c == 0 || c == ' ')
15634 regname = '"';
15635 else
15636 regname = c;
15637 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015638 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 set_vim_var_string(VV_REG, &regname, 1);
15640}
15641
15642/*
15643 * Get or set v:exception. If "oldval" == NULL, return the current value.
15644 * Otherwise, restore the value to "oldval" and return NULL.
15645 * Must always be called in pairs to save and restore v:exception! Does not
15646 * take care of memory allocations.
15647 */
15648 char_u *
15649v_exception(oldval)
15650 char_u *oldval;
15651{
15652 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015653 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654
Bram Moolenaare9a41262005-01-15 22:18:47 +000015655 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656 return NULL;
15657}
15658
15659/*
15660 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15661 * Otherwise, restore the value to "oldval" and return NULL.
15662 * Must always be called in pairs to save and restore v:throwpoint! Does not
15663 * take care of memory allocations.
15664 */
15665 char_u *
15666v_throwpoint(oldval)
15667 char_u *oldval;
15668{
15669 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015670 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671
Bram Moolenaare9a41262005-01-15 22:18:47 +000015672 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 return NULL;
15674}
15675
15676#if defined(FEAT_AUTOCMD) || defined(PROTO)
15677/*
15678 * Set v:cmdarg.
15679 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15680 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15681 * Must always be called in pairs!
15682 */
15683 char_u *
15684set_cmdarg(eap, oldarg)
15685 exarg_T *eap;
15686 char_u *oldarg;
15687{
15688 char_u *oldval;
15689 char_u *newval;
15690 unsigned len;
15691
Bram Moolenaare9a41262005-01-15 22:18:47 +000015692 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015693 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015695 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015696 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015697 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 }
15699
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015700 if (eap->force_bin == FORCE_BIN)
15701 len = 6;
15702 else if (eap->force_bin == FORCE_NOBIN)
15703 len = 8;
15704 else
15705 len = 0;
15706 if (eap->force_ff != 0)
15707 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15708# ifdef FEAT_MBYTE
15709 if (eap->force_enc != 0)
15710 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015711 if (eap->bad_char != 0)
15712 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015713# endif
15714
15715 newval = alloc(len + 1);
15716 if (newval == NULL)
15717 return NULL;
15718
15719 if (eap->force_bin == FORCE_BIN)
15720 sprintf((char *)newval, " ++bin");
15721 else if (eap->force_bin == FORCE_NOBIN)
15722 sprintf((char *)newval, " ++nobin");
15723 else
15724 *newval = NUL;
15725 if (eap->force_ff != 0)
15726 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15727 eap->cmd + eap->force_ff);
15728# ifdef FEAT_MBYTE
15729 if (eap->force_enc != 0)
15730 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15731 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015732 if (eap->bad_char != 0)
15733 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
15734 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015735# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015736 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015737 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738}
15739#endif
15740
15741/*
15742 * Get the value of internal variable "name".
15743 * Return OK or FAIL.
15744 */
15745 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015746get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 char_u *name;
15748 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015749 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015750 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751{
15752 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015753 typval_T *tv = NULL;
15754 typval_T atv;
15755 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757
15758 /* truncate the name, so that we can use strcmp() */
15759 cc = name[len];
15760 name[len] = NUL;
15761
15762 /*
15763 * Check for "b:changedtick".
15764 */
15765 if (STRCMP(name, "b:changedtick") == 0)
15766 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015767 atv.v_type = VAR_NUMBER;
15768 atv.vval.v_number = curbuf->b_changedtick;
15769 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 }
15771
15772 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 * Check for user-defined variables.
15774 */
15775 else
15776 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015777 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015779 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 }
15781
Bram Moolenaare9a41262005-01-15 22:18:47 +000015782 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015784 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 ret = FAIL;
15787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015789 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790
15791 name[len] = cc;
15792
15793 return ret;
15794}
15795
15796/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015797 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15798 * Also handle function call with Funcref variable: func(expr)
15799 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15800 */
15801 static int
15802handle_subscript(arg, rettv, evaluate, verbose)
15803 char_u **arg;
15804 typval_T *rettv;
15805 int evaluate; /* do more than finding the end */
15806 int verbose; /* give error messages */
15807{
15808 int ret = OK;
15809 dict_T *selfdict = NULL;
15810 char_u *s;
15811 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015812 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015813
15814 while (ret == OK
15815 && (**arg == '['
15816 || (**arg == '.' && rettv->v_type == VAR_DICT)
15817 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15818 && !vim_iswhite(*(*arg - 1)))
15819 {
15820 if (**arg == '(')
15821 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015822 /* need to copy the funcref so that we can clear rettv */
15823 functv = *rettv;
15824 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015825
15826 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015827 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015828 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015829 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15830 &len, evaluate, selfdict);
15831
15832 /* Clear the funcref afterwards, so that deleting it while
15833 * evaluating the arguments is possible (see test55). */
15834 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015835
15836 /* Stop the expression evaluation when immediately aborting on
15837 * error, or when an interrupt occurred or an exception was thrown
15838 * but not caught. */
15839 if (aborting())
15840 {
15841 if (ret == OK)
15842 clear_tv(rettv);
15843 ret = FAIL;
15844 }
15845 dict_unref(selfdict);
15846 selfdict = NULL;
15847 }
15848 else /* **arg == '[' || **arg == '.' */
15849 {
15850 dict_unref(selfdict);
15851 if (rettv->v_type == VAR_DICT)
15852 {
15853 selfdict = rettv->vval.v_dict;
15854 if (selfdict != NULL)
15855 ++selfdict->dv_refcount;
15856 }
15857 else
15858 selfdict = NULL;
15859 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15860 {
15861 clear_tv(rettv);
15862 ret = FAIL;
15863 }
15864 }
15865 }
15866 dict_unref(selfdict);
15867 return ret;
15868}
15869
15870/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015871 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15872 * value).
15873 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015874 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015876{
Bram Moolenaar33570922005-01-25 22:26:29 +000015877 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015878}
15879
15880/*
15881 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 * The string "s" must have been allocated, it is consumed.
15883 * Return NULL for out of memory, the variable otherwise.
15884 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015885 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015886alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 char_u *s;
15888{
Bram Moolenaar33570922005-01-25 22:26:29 +000015889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891 rettv = alloc_tv();
15892 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015894 rettv->v_type = VAR_STRING;
15895 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 }
15897 else
15898 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015899 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900}
15901
15902/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015903 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015905 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908{
15909 if (varp != NULL)
15910 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015911 switch (varp->v_type)
15912 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015914 func_unref(varp->vval.v_string);
15915 /*FALLTHROUGH*/
15916 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015917 vim_free(varp->vval.v_string);
15918 break;
15919 case VAR_LIST:
15920 list_unref(varp->vval.v_list);
15921 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015922 case VAR_DICT:
15923 dict_unref(varp->vval.v_dict);
15924 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015925 case VAR_NUMBER:
15926 case VAR_UNKNOWN:
15927 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015928 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015929 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930 break;
15931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 vim_free(varp);
15933 }
15934}
15935
15936/*
15937 * Free the memory for a variable value and set the value to NULL or 0.
15938 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015939 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015940clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015941 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942{
15943 if (varp != NULL)
15944 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015945 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015947 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015948 func_unref(varp->vval.v_string);
15949 /*FALLTHROUGH*/
15950 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015951 vim_free(varp->vval.v_string);
15952 varp->vval.v_string = NULL;
15953 break;
15954 case VAR_LIST:
15955 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015956 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015957 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015958 case VAR_DICT:
15959 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015960 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015961 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015962 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015963 varp->vval.v_number = 0;
15964 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015965 case VAR_UNKNOWN:
15966 break;
15967 default:
15968 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015970 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971 }
15972}
15973
15974/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015975 * Set the value of a variable to NULL without freeing items.
15976 */
15977 static void
15978init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015979 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015980{
15981 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015982 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015983}
15984
15985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 * Get the number value of a variable.
15987 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015988 * For incompatible types, return 0.
15989 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15990 * caller of incompatible types: it sets *denote to TRUE if "denote"
15991 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 */
15993 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015994get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015997 int error = FALSE;
15998
15999 return get_tv_number_chk(varp, &error); /* return 0L on error */
16000}
16001
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016002 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016003get_tv_number_chk(varp, denote)
16004 typval_T *varp;
16005 int *denote;
16006{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016007 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016009 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016011 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016012 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016013 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016014 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016015 break;
16016 case VAR_STRING:
16017 if (varp->vval.v_string != NULL)
16018 vim_str2nr(varp->vval.v_string, NULL, NULL,
16019 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016021 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016022 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016023 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016024 case VAR_DICT:
16025 EMSG(_("E728: Using a Dictionary as a number"));
16026 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016027 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016028 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016029 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 if (denote == NULL) /* useful for values that must be unsigned */
16032 n = -1;
16033 else
16034 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016035 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036}
16037
16038/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016039 * Get the lnum from the first argument.
16040 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 */
16043 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016044get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046{
Bram Moolenaar33570922005-01-25 22:26:29 +000016047 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 linenr_T lnum;
16049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016050 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 if (lnum == 0) /* no valid number, try using line() */
16052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016053 rettv.v_type = VAR_NUMBER;
16054 f_line(argvars, &rettv);
16055 lnum = rettv.vval.v_number;
16056 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 }
16058 return lnum;
16059}
16060
16061/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016062 * Get the lnum from the first argument.
16063 * Also accepts "$", then "buf" is used.
16064 * Returns 0 on error.
16065 */
16066 static linenr_T
16067get_tv_lnum_buf(argvars, buf)
16068 typval_T *argvars;
16069 buf_T *buf;
16070{
16071 if (argvars[0].v_type == VAR_STRING
16072 && argvars[0].vval.v_string != NULL
16073 && argvars[0].vval.v_string[0] == '$'
16074 && buf != NULL)
16075 return buf->b_ml.ml_line_count;
16076 return get_tv_number_chk(&argvars[0], NULL);
16077}
16078
16079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 * Get the string value of a variable.
16081 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016082 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16083 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 * If the String variable has never been set, return an empty string.
16085 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016086 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16087 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 */
16089 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016090get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016091 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092{
16093 static char_u mybuf[NUMBUFLEN];
16094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016095 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096}
16097
16098 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016099get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016100 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016101 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016103 char_u *res = get_tv_string_buf_chk(varp, buf);
16104
16105 return res != NULL ? res : (char_u *)"";
16106}
16107
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016108 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016109get_tv_string_chk(varp)
16110 typval_T *varp;
16111{
16112 static char_u mybuf[NUMBUFLEN];
16113
16114 return get_tv_string_buf_chk(varp, mybuf);
16115}
16116
16117 static char_u *
16118get_tv_string_buf_chk(varp, buf)
16119 typval_T *varp;
16120 char_u *buf;
16121{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016122 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 case VAR_NUMBER:
16125 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16126 return buf;
16127 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016128 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016129 break;
16130 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016131 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016132 break;
16133 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016134 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016135 break;
16136 case VAR_STRING:
16137 if (varp->vval.v_string != NULL)
16138 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016139 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016140 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016141 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145}
16146
16147/*
16148 * Find variable "name" in the list of variables.
16149 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016150 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016151 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016152 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016154 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016155find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016157 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161
Bram Moolenaara7043832005-01-21 11:56:39 +000016162 ht = find_var_ht(name, &varname);
16163 if (htp != NULL)
16164 *htp = ht;
16165 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016167 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168}
16169
16170/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016172 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016174 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016175find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016177 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016178 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016179{
Bram Moolenaar33570922005-01-25 22:26:29 +000016180 hashitem_T *hi;
16181
16182 if (*varname == NUL)
16183 {
16184 /* Must be something like "s:", otherwise "ht" would be NULL. */
16185 switch (varname[-2])
16186 {
16187 case 's': return &SCRIPT_SV(current_SID).sv_var;
16188 case 'g': return &globvars_var;
16189 case 'v': return &vimvars_var;
16190 case 'b': return &curbuf->b_bufvar;
16191 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016192 case 'l': return current_funccal == NULL
16193 ? NULL : &current_funccal->l_vars_var;
16194 case 'a': return current_funccal == NULL
16195 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016196 }
16197 return NULL;
16198 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016199
16200 hi = hash_find(ht, varname);
16201 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016202 {
16203 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016204 * worked find the variable again. Don't auto-load a script if it was
16205 * loaded already, otherwise it would be loaded every time when
16206 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016207 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016208 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016209 hi = hash_find(ht, varname);
16210 if (HASHITEM_EMPTY(hi))
16211 return NULL;
16212 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016214}
16215
16216/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016217 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016218 * Set "varname" to the start of name without ':'.
16219 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016220 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016221find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 char_u *name;
16223 char_u **varname;
16224{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016225 hashitem_T *hi;
16226
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 if (name[1] != ':')
16228 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016229 /* The name must not start with a colon or #. */
16230 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 return NULL;
16232 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016233
16234 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016235 hi = hash_find(&compat_hashtab, name);
16236 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016237 return &compat_hashtab;
16238
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016240 return &globvarht; /* global variable */
16241 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 }
16243 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016244 if (*name == 'g') /* global variable */
16245 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016246 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16247 */
16248 if (vim_strchr(name + 2, ':') != NULL
16249 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016250 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016255 if (*name == 'v') /* v: variable */
16256 return &vimvarht;
16257 if (*name == 'a' && current_funccal != NULL) /* function argument */
16258 return &current_funccal->l_avars.dv_hashtab;
16259 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16260 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 if (*name == 's' /* script variable */
16262 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16263 return &SCRIPT_VARS(current_SID);
16264 return NULL;
16265}
16266
16267/*
16268 * Get the string value of a (global/local) variable.
16269 * Returns NULL when it doesn't exist.
16270 */
16271 char_u *
16272get_var_value(name)
16273 char_u *name;
16274{
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276
Bram Moolenaara7043832005-01-21 11:56:39 +000016277 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278 if (v == NULL)
16279 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281}
16282
16283/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016284 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 * sourcing this script and when executing functions defined in the script.
16286 */
16287 void
16288new_script_vars(id)
16289 scid_T id;
16290{
Bram Moolenaara7043832005-01-21 11:56:39 +000016291 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 hashtab_T *ht;
16293 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016294
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16296 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016297 /* Re-allocating ga_data means that an ht_array pointing to
16298 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016300 for (i = 1; i <= ga_scripts.ga_len; ++i)
16301 {
16302 ht = &SCRIPT_VARS(i);
16303 if (ht->ht_mask == HT_INIT_SIZE - 1)
16304 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016305 sv = &SCRIPT_SV(i);
16306 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016307 }
16308
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309 while (ga_scripts.ga_len < id)
16310 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016311 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16312 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 }
16315 }
16316}
16317
16318/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16320 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 */
16322 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016323init_var_dict(dict, dict_var)
16324 dict_T *dict;
16325 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326{
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 hash_init(&dict->dv_hashtab);
16328 dict->dv_refcount = 99999;
16329 dict_var->di_tv.vval.v_dict = dict;
16330 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016331 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016332 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16333 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334}
16335
16336/*
16337 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016338 * Frees all allocated variables and the value they contain.
16339 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 */
16341 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016342vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 hashtab_T *ht;
16344{
16345 vars_clear_ext(ht, TRUE);
16346}
16347
16348/*
16349 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16350 */
16351 static void
16352vars_clear_ext(ht, free_val)
16353 hashtab_T *ht;
16354 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355{
Bram Moolenaara7043832005-01-21 11:56:39 +000016356 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016357 hashitem_T *hi;
16358 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359
Bram Moolenaar33570922005-01-25 22:26:29 +000016360 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016361 todo = ht->ht_used;
16362 for (hi = ht->ht_array; todo > 0; ++hi)
16363 {
16364 if (!HASHITEM_EMPTY(hi))
16365 {
16366 --todo;
16367
Bram Moolenaar33570922005-01-25 22:26:29 +000016368 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016369 * ht_array might change then. hash_clear() takes care of it
16370 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 v = HI2DI(hi);
16372 if (free_val)
16373 clear_tv(&v->di_tv);
16374 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16375 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016376 }
16377 }
16378 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016379 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380}
16381
Bram Moolenaara7043832005-01-21 11:56:39 +000016382/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016383 * Delete a variable from hashtab "ht" at item "hi".
16384 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016387delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 hashtab_T *ht;
16389 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390{
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016392
16393 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 clear_tv(&di->di_tv);
16395 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396}
16397
16398/*
16399 * List the value of one internal variable.
16400 */
16401 static void
16402list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016403 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 char_u *prefix;
16405{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016406 char_u *tofree;
16407 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016408 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016409
Bram Moolenaar33570922005-01-25 22:26:29 +000016410 s = echo_string(&v->di_tv, &tofree, numbuf);
16411 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016412 s == NULL ? (char_u *)"" : s);
16413 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414}
16415
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 static void
16417list_one_var_a(prefix, name, type, string)
16418 char_u *prefix;
16419 char_u *name;
16420 int type;
16421 char_u *string;
16422{
16423 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16424 if (name != NULL) /* "a:" vars don't have a name stored */
16425 msg_puts(name);
16426 msg_putchar(' ');
16427 msg_advance(22);
16428 if (type == VAR_NUMBER)
16429 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016430 else if (type == VAR_FUNC)
16431 msg_putchar('*');
16432 else if (type == VAR_LIST)
16433 {
16434 msg_putchar('[');
16435 if (*string == '[')
16436 ++string;
16437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016438 else if (type == VAR_DICT)
16439 {
16440 msg_putchar('{');
16441 if (*string == '{')
16442 ++string;
16443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 else
16445 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016446
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016448
16449 if (type == VAR_FUNC)
16450 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451}
16452
16453/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 * If the variable already exists, the value is updated.
16456 * Otherwise the variable is created.
16457 */
16458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016462 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463{
Bram Moolenaar33570922005-01-25 22:26:29 +000016464 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016467 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016470 {
16471 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16472 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16473 ? name[2] : name[0]))
16474 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016475 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016476 return;
16477 }
16478 if (function_exists(name))
16479 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016480 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016481 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016482 return;
16483 }
16484 }
16485
Bram Moolenaara7043832005-01-21 11:56:39 +000016486 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016487 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016488 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016489 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016490 return;
16491 }
16492
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016493 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016494 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016496 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016497 if (var_check_ro(v->di_flags, name)
16498 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016499 return;
16500 if (v->di_tv.v_type != tv->v_type
16501 && !((v->di_tv.v_type == VAR_STRING
16502 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016503 && (tv->v_type == VAR_STRING
16504 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016505 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016506 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016507 return;
16508 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016509
16510 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016511 * Handle setting internal v: variables separately: we don't change
16512 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 */
16514 if (ht == &vimvarht)
16515 {
16516 if (v->di_tv.v_type == VAR_STRING)
16517 {
16518 vim_free(v->di_tv.vval.v_string);
16519 if (copy || tv->v_type != VAR_STRING)
16520 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16521 else
16522 {
16523 /* Take over the string to avoid an extra alloc/free. */
16524 v->di_tv.vval.v_string = tv->vval.v_string;
16525 tv->vval.v_string = NULL;
16526 }
16527 }
16528 else if (v->di_tv.v_type != VAR_NUMBER)
16529 EMSG2(_(e_intern2), "set_var()");
16530 else
16531 v->di_tv.vval.v_number = get_tv_number(tv);
16532 return;
16533 }
16534
16535 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 }
16537 else /* add a new variable */
16538 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016539 /* Make sure the variable name is valid. */
16540 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016541 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16542 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016543 {
16544 EMSG2(_(e_illvar), varname);
16545 return;
16546 }
16547
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016548 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16549 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016550 if (v == NULL)
16551 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016553 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016555 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 return;
16557 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016558 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016561 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016562 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016563 else
16564 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016565 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016566 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016567 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569}
16570
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016571/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016572 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16573 * Also give an error message.
16574 */
16575 static int
16576var_check_ro(flags, name)
16577 int flags;
16578 char_u *name;
16579{
16580 if (flags & DI_FLAGS_RO)
16581 {
16582 EMSG2(_(e_readonlyvar), name);
16583 return TRUE;
16584 }
16585 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16586 {
16587 EMSG2(_(e_readonlysbx), name);
16588 return TRUE;
16589 }
16590 return FALSE;
16591}
16592
16593/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016594 * Return TRUE if typeval "tv" is set to be locked (immutable).
16595 * Also give an error message, using "name".
16596 */
16597 static int
16598tv_check_lock(lock, name)
16599 int lock;
16600 char_u *name;
16601{
16602 if (lock & VAR_LOCKED)
16603 {
16604 EMSG2(_("E741: Value is locked: %s"),
16605 name == NULL ? (char_u *)_("Unknown") : name);
16606 return TRUE;
16607 }
16608 if (lock & VAR_FIXED)
16609 {
16610 EMSG2(_("E742: Cannot change value of %s"),
16611 name == NULL ? (char_u *)_("Unknown") : name);
16612 return TRUE;
16613 }
16614 return FALSE;
16615}
16616
16617/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016618 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016619 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016620 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016623copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016624 typval_T *from;
16625 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016627 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016628 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016629 switch (from->v_type)
16630 {
16631 case VAR_NUMBER:
16632 to->vval.v_number = from->vval.v_number;
16633 break;
16634 case VAR_STRING:
16635 case VAR_FUNC:
16636 if (from->vval.v_string == NULL)
16637 to->vval.v_string = NULL;
16638 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016639 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016640 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016641 if (from->v_type == VAR_FUNC)
16642 func_ref(to->vval.v_string);
16643 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016644 break;
16645 case VAR_LIST:
16646 if (from->vval.v_list == NULL)
16647 to->vval.v_list = NULL;
16648 else
16649 {
16650 to->vval.v_list = from->vval.v_list;
16651 ++to->vval.v_list->lv_refcount;
16652 }
16653 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016654 case VAR_DICT:
16655 if (from->vval.v_dict == NULL)
16656 to->vval.v_dict = NULL;
16657 else
16658 {
16659 to->vval.v_dict = from->vval.v_dict;
16660 ++to->vval.v_dict->dv_refcount;
16661 }
16662 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016663 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016664 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016665 break;
16666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667}
16668
16669/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016670 * Make a copy of an item.
16671 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016672 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16673 * reference to an already copied list/dict can be used.
16674 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016675 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016676 static int
16677item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016678 typval_T *from;
16679 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016680 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016681 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016682{
16683 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016684 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016685
Bram Moolenaar33570922005-01-25 22:26:29 +000016686 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016687 {
16688 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016689 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016690 }
16691 ++recurse;
16692
16693 switch (from->v_type)
16694 {
16695 case VAR_NUMBER:
16696 case VAR_STRING:
16697 case VAR_FUNC:
16698 copy_tv(from, to);
16699 break;
16700 case VAR_LIST:
16701 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016702 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016703 if (from->vval.v_list == NULL)
16704 to->vval.v_list = NULL;
16705 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16706 {
16707 /* use the copy made earlier */
16708 to->vval.v_list = from->vval.v_list->lv_copylist;
16709 ++to->vval.v_list->lv_refcount;
16710 }
16711 else
16712 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16713 if (to->vval.v_list == NULL)
16714 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016715 break;
16716 case VAR_DICT:
16717 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016718 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016719 if (from->vval.v_dict == NULL)
16720 to->vval.v_dict = NULL;
16721 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16722 {
16723 /* use the copy made earlier */
16724 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16725 ++to->vval.v_dict->dv_refcount;
16726 }
16727 else
16728 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16729 if (to->vval.v_dict == NULL)
16730 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016731 break;
16732 default:
16733 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016734 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016735 }
16736 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016737 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016738}
16739
16740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 * ":echo expr1 ..." print each argument separated with a space, add a
16742 * newline at the end.
16743 * ":echon expr1 ..." print each argument plain.
16744 */
16745 void
16746ex_echo(eap)
16747 exarg_T *eap;
16748{
16749 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016750 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016751 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 char_u *p;
16753 int needclr = TRUE;
16754 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016755 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756
16757 if (eap->skip)
16758 ++emsg_skip;
16759 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16760 {
16761 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 {
16764 /*
16765 * Report the invalid expression unless the expression evaluation
16766 * has been cancelled due to an aborting error, an interrupt, or an
16767 * exception.
16768 */
16769 if (!aborting())
16770 EMSG2(_(e_invexpr2), p);
16771 break;
16772 }
16773 if (!eap->skip)
16774 {
16775 if (atstart)
16776 {
16777 atstart = FALSE;
16778 /* Call msg_start() after eval1(), evaluating the expression
16779 * may cause a message to appear. */
16780 if (eap->cmdidx == CMD_echo)
16781 msg_start();
16782 }
16783 else if (eap->cmdidx == CMD_echo)
16784 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016785 p = echo_string(&rettv, &tofree, numbuf);
16786 if (p != NULL)
16787 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016789 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016791 if (*p != TAB && needclr)
16792 {
16793 /* remove any text still there from the command */
16794 msg_clr_eos();
16795 needclr = FALSE;
16796 }
16797 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 }
16799 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016800 {
16801#ifdef FEAT_MBYTE
16802 if (has_mbyte)
16803 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016804 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016805
16806 (void)msg_outtrans_len_attr(p, i, echo_attr);
16807 p += i - 1;
16808 }
16809 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016811 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016814 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016816 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 arg = skipwhite(arg);
16818 }
16819 eap->nextcmd = check_nextcmd(arg);
16820
16821 if (eap->skip)
16822 --emsg_skip;
16823 else
16824 {
16825 /* remove text that may still be there from the command */
16826 if (needclr)
16827 msg_clr_eos();
16828 if (eap->cmdidx == CMD_echo)
16829 msg_end();
16830 }
16831}
16832
16833/*
16834 * ":echohl {name}".
16835 */
16836 void
16837ex_echohl(eap)
16838 exarg_T *eap;
16839{
16840 int id;
16841
16842 id = syn_name2id(eap->arg);
16843 if (id == 0)
16844 echo_attr = 0;
16845 else
16846 echo_attr = syn_id2attr(id);
16847}
16848
16849/*
16850 * ":execute expr1 ..." execute the result of an expression.
16851 * ":echomsg expr1 ..." Print a message
16852 * ":echoerr expr1 ..." Print an error
16853 * Each gets spaces around each argument and a newline at the end for
16854 * echo commands
16855 */
16856 void
16857ex_execute(eap)
16858 exarg_T *eap;
16859{
16860 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016861 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 int ret = OK;
16863 char_u *p;
16864 garray_T ga;
16865 int len;
16866 int save_did_emsg;
16867
16868 ga_init2(&ga, 1, 80);
16869
16870 if (eap->skip)
16871 ++emsg_skip;
16872 while (*arg != NUL && *arg != '|' && *arg != '\n')
16873 {
16874 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016875 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 {
16877 /*
16878 * Report the invalid expression unless the expression evaluation
16879 * has been cancelled due to an aborting error, an interrupt, or an
16880 * exception.
16881 */
16882 if (!aborting())
16883 EMSG2(_(e_invexpr2), p);
16884 ret = FAIL;
16885 break;
16886 }
16887
16888 if (!eap->skip)
16889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016890 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 len = (int)STRLEN(p);
16892 if (ga_grow(&ga, len + 2) == FAIL)
16893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 ret = FAIL;
16896 break;
16897 }
16898 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 ga.ga_len += len;
16902 }
16903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016904 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 arg = skipwhite(arg);
16906 }
16907
16908 if (ret != FAIL && ga.ga_data != NULL)
16909 {
16910 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000016911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000016913 out_flush();
16914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 else if (eap->cmdidx == CMD_echoerr)
16916 {
16917 /* We don't want to abort following commands, restore did_emsg. */
16918 save_did_emsg = did_emsg;
16919 EMSG((char_u *)ga.ga_data);
16920 if (!force_abort)
16921 did_emsg = save_did_emsg;
16922 }
16923 else if (eap->cmdidx == CMD_execute)
16924 do_cmdline((char_u *)ga.ga_data,
16925 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16926 }
16927
16928 ga_clear(&ga);
16929
16930 if (eap->skip)
16931 --emsg_skip;
16932
16933 eap->nextcmd = check_nextcmd(arg);
16934}
16935
16936/*
16937 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16938 * "arg" points to the "&" or '+' when called, to "option" when returning.
16939 * Returns NULL when no option name found. Otherwise pointer to the char
16940 * after the option name.
16941 */
16942 static char_u *
16943find_option_end(arg, opt_flags)
16944 char_u **arg;
16945 int *opt_flags;
16946{
16947 char_u *p = *arg;
16948
16949 ++p;
16950 if (*p == 'g' && p[1] == ':')
16951 {
16952 *opt_flags = OPT_GLOBAL;
16953 p += 2;
16954 }
16955 else if (*p == 'l' && p[1] == ':')
16956 {
16957 *opt_flags = OPT_LOCAL;
16958 p += 2;
16959 }
16960 else
16961 *opt_flags = 0;
16962
16963 if (!ASCII_ISALPHA(*p))
16964 return NULL;
16965 *arg = p;
16966
16967 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16968 p += 4; /* termcap option */
16969 else
16970 while (ASCII_ISALPHA(*p))
16971 ++p;
16972 return p;
16973}
16974
16975/*
16976 * ":function"
16977 */
16978 void
16979ex_function(eap)
16980 exarg_T *eap;
16981{
16982 char_u *theline;
16983 int j;
16984 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 char_u *name = NULL;
16987 char_u *p;
16988 char_u *arg;
16989 garray_T newargs;
16990 garray_T newlines;
16991 int varargs = FALSE;
16992 int mustend = FALSE;
16993 int flags = 0;
16994 ufunc_T *fp;
16995 int indent;
16996 int nesting;
16997 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016998 dictitem_T *v;
16999 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017000 static int func_nr = 0; /* number for nameless function */
17001 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017002 hashtab_T *ht;
17003 int todo;
17004 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005
17006 /*
17007 * ":function" without argument: list functions.
17008 */
17009 if (ends_excmd(*eap->arg))
17010 {
17011 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017012 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017013 todo = func_hashtab.ht_used;
17014 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017015 {
17016 if (!HASHITEM_EMPTY(hi))
17017 {
17018 --todo;
17019 fp = HI2UF(hi);
17020 if (!isdigit(*fp->uf_name))
17021 list_func_head(fp, FALSE);
17022 }
17023 }
17024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 eap->nextcmd = check_nextcmd(eap->arg);
17026 return;
17027 }
17028
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017029 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017030 * ":function /pat": list functions matching pattern.
17031 */
17032 if (*eap->arg == '/')
17033 {
17034 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17035 if (!eap->skip)
17036 {
17037 regmatch_T regmatch;
17038
17039 c = *p;
17040 *p = NUL;
17041 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17042 *p = c;
17043 if (regmatch.regprog != NULL)
17044 {
17045 regmatch.rm_ic = p_ic;
17046
17047 todo = func_hashtab.ht_used;
17048 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17049 {
17050 if (!HASHITEM_EMPTY(hi))
17051 {
17052 --todo;
17053 fp = HI2UF(hi);
17054 if (!isdigit(*fp->uf_name)
17055 && vim_regexec(&regmatch, fp->uf_name, 0))
17056 list_func_head(fp, FALSE);
17057 }
17058 }
17059 }
17060 }
17061 if (*p == '/')
17062 ++p;
17063 eap->nextcmd = check_nextcmd(p);
17064 return;
17065 }
17066
17067 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017068 * Get the function name. There are these situations:
17069 * func normal function name
17070 * "name" == func, "fudi.fd_dict" == NULL
17071 * dict.func new dictionary entry
17072 * "name" == NULL, "fudi.fd_dict" set,
17073 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17074 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017075 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017076 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17077 * dict.func existing dict entry that's not a Funcref
17078 * "name" == NULL, "fudi.fd_dict" set,
17079 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017082 name = trans_function_name(&p, eap->skip, 0, &fudi);
17083 paren = (vim_strchr(p, '(') != NULL);
17084 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 {
17086 /*
17087 * Return on an invalid expression in braces, unless the expression
17088 * evaluation has been cancelled due to an aborting error, an
17089 * interrupt, or an exception.
17090 */
17091 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017093 if (!eap->skip && fudi.fd_newkey != NULL)
17094 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017095 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 else
17099 eap->skip = TRUE;
17100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 /* An error in a function call during evaluation of an expression in magic
17102 * braces should not cause the function not to be defined. */
17103 saved_did_emsg = did_emsg;
17104 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105
17106 /*
17107 * ":function func" with only function name: list function.
17108 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017109 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 {
17111 if (!ends_excmd(*skipwhite(p)))
17112 {
17113 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017114 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 }
17116 eap->nextcmd = check_nextcmd(p);
17117 if (eap->nextcmd != NULL)
17118 *p = NUL;
17119 if (!eap->skip && !got_int)
17120 {
17121 fp = find_func(name);
17122 if (fp != NULL)
17123 {
17124 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017125 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 {
17127 msg_putchar('\n');
17128 msg_outnum((long)(j + 1));
17129 if (j < 9)
17130 msg_putchar(' ');
17131 if (j < 99)
17132 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017133 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 out_flush(); /* show a line at a time */
17135 ui_breakcheck();
17136 }
17137 if (!got_int)
17138 {
17139 msg_putchar('\n');
17140 msg_puts((char_u *)" endfunction");
17141 }
17142 }
17143 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017144 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017146 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 }
17148
17149 /*
17150 * ":function name(arg1, arg2)" Define function.
17151 */
17152 p = skipwhite(p);
17153 if (*p != '(')
17154 {
17155 if (!eap->skip)
17156 {
17157 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017158 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 }
17160 /* attempt to continue by skipping some text */
17161 if (vim_strchr(p, '(') != NULL)
17162 p = vim_strchr(p, '(');
17163 }
17164 p = skipwhite(p + 1);
17165
17166 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17167 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17168
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017169 if (!eap->skip)
17170 {
17171 /* Check the name of the function. */
17172 if (name != NULL)
17173 arg = name;
17174 else
17175 arg = fudi.fd_newkey;
17176 if (arg != NULL)
17177 {
17178 if (*arg == K_SPECIAL)
17179 j = 3;
17180 else
17181 j = 0;
17182 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17183 : eval_isnamec(arg[j])))
17184 ++j;
17185 if (arg[j] != NUL)
17186 emsg_funcname(_(e_invarg2), arg);
17187 }
17188 }
17189
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 /*
17191 * Isolate the arguments: "arg1, arg2, ...)"
17192 */
17193 while (*p != ')')
17194 {
17195 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17196 {
17197 varargs = TRUE;
17198 p += 3;
17199 mustend = TRUE;
17200 }
17201 else
17202 {
17203 arg = p;
17204 while (ASCII_ISALNUM(*p) || *p == '_')
17205 ++p;
17206 if (arg == p || isdigit(*arg)
17207 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17208 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17209 {
17210 if (!eap->skip)
17211 EMSG2(_("E125: Illegal argument: %s"), arg);
17212 break;
17213 }
17214 if (ga_grow(&newargs, 1) == FAIL)
17215 goto erret;
17216 c = *p;
17217 *p = NUL;
17218 arg = vim_strsave(arg);
17219 if (arg == NULL)
17220 goto erret;
17221 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17222 *p = c;
17223 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 if (*p == ',')
17225 ++p;
17226 else
17227 mustend = TRUE;
17228 }
17229 p = skipwhite(p);
17230 if (mustend && *p != ')')
17231 {
17232 if (!eap->skip)
17233 EMSG2(_(e_invarg2), eap->arg);
17234 break;
17235 }
17236 }
17237 ++p; /* skip the ')' */
17238
Bram Moolenaare9a41262005-01-15 22:18:47 +000017239 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 for (;;)
17241 {
17242 p = skipwhite(p);
17243 if (STRNCMP(p, "range", 5) == 0)
17244 {
17245 flags |= FC_RANGE;
17246 p += 5;
17247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017248 else if (STRNCMP(p, "dict", 4) == 0)
17249 {
17250 flags |= FC_DICT;
17251 p += 4;
17252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 else if (STRNCMP(p, "abort", 5) == 0)
17254 {
17255 flags |= FC_ABORT;
17256 p += 5;
17257 }
17258 else
17259 break;
17260 }
17261
17262 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17263 EMSG(_(e_trailing));
17264
17265 /*
17266 * Read the body of the function, until ":endfunction" is found.
17267 */
17268 if (KeyTyped)
17269 {
17270 /* Check if the function already exists, don't let the user type the
17271 * whole function before telling him it doesn't work! For a script we
17272 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017273 if (!eap->skip && !eap->forceit)
17274 {
17275 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17276 EMSG(_(e_funcdict));
17277 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017278 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017281 if (!eap->skip && did_emsg)
17282 goto erret;
17283
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 msg_putchar('\n'); /* don't overwrite the function name */
17285 cmdline_row = msg_row;
17286 }
17287
17288 indent = 2;
17289 nesting = 0;
17290 for (;;)
17291 {
17292 msg_scroll = TRUE;
17293 need_wait_return = FALSE;
17294 if (eap->getline == NULL)
17295 theline = getcmdline(':', 0L, indent);
17296 else
17297 theline = eap->getline(':', eap->cookie, indent);
17298 if (KeyTyped)
17299 lines_left = Rows - 1;
17300 if (theline == NULL)
17301 {
17302 EMSG(_("E126: Missing :endfunction"));
17303 goto erret;
17304 }
17305
17306 if (skip_until != NULL)
17307 {
17308 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17309 * don't check for ":endfunc". */
17310 if (STRCMP(theline, skip_until) == 0)
17311 {
17312 vim_free(skip_until);
17313 skip_until = NULL;
17314 }
17315 }
17316 else
17317 {
17318 /* skip ':' and blanks*/
17319 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17320 ;
17321
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017322 /* Check for "endfunction". */
17323 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 {
17325 vim_free(theline);
17326 break;
17327 }
17328
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017329 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 * at "end". */
17331 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17332 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017333 else if (STRNCMP(p, "if", 2) == 0
17334 || STRNCMP(p, "wh", 2) == 0
17335 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 || STRNCMP(p, "try", 3) == 0)
17337 indent += 2;
17338
17339 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017340 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017342 if (*p == '!')
17343 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 p += eval_fname_script(p);
17345 if (ASCII_ISALPHA(*p))
17346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017347 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 if (*skipwhite(p) == '(')
17349 {
17350 ++nesting;
17351 indent += 2;
17352 }
17353 }
17354 }
17355
17356 /* Check for ":append" or ":insert". */
17357 p = skip_range(p, NULL);
17358 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17359 || (p[0] == 'i'
17360 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17361 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17362 skip_until = vim_strsave((char_u *)".");
17363
17364 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17365 arg = skipwhite(skiptowhite(p));
17366 if (arg[0] == '<' && arg[1] =='<'
17367 && ((p[0] == 'p' && p[1] == 'y'
17368 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17369 || (p[0] == 'p' && p[1] == 'e'
17370 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17371 || (p[0] == 't' && p[1] == 'c'
17372 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17373 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17374 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017375 || (p[0] == 'm' && p[1] == 'z'
17376 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 ))
17378 {
17379 /* ":python <<" continues until a dot, like ":append" */
17380 p = skipwhite(arg + 2);
17381 if (*p == NUL)
17382 skip_until = vim_strsave((char_u *)".");
17383 else
17384 skip_until = vim_strsave(p);
17385 }
17386 }
17387
17388 /* Add the line to the function. */
17389 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017390 {
17391 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017393 }
17394
17395 /* Copy the line to newly allocated memory. get_one_sourceline()
17396 * allocates 250 bytes per line, this saves 80% on average. The cost
17397 * is an extra alloc/free. */
17398 p = vim_strsave(theline);
17399 if (p != NULL)
17400 {
17401 vim_free(theline);
17402 theline = p;
17403 }
17404
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17406 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 }
17408
17409 /* Don't define the function when skipping commands or when an error was
17410 * detected. */
17411 if (eap->skip || did_emsg)
17412 goto erret;
17413
17414 /*
17415 * If there are no errors, add the function
17416 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017417 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017418 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017419 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017420 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017421 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017422 emsg_funcname("E707: Function name conflicts with variable: %s",
17423 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017424 goto erret;
17425 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017426
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017427 fp = find_func(name);
17428 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017430 if (!eap->forceit)
17431 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017432 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017433 goto erret;
17434 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017435 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017436 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017437 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017438 name);
17439 goto erret;
17440 }
17441 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017442 ga_clear_strings(&(fp->uf_args));
17443 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017444 vim_free(name);
17445 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 }
17448 else
17449 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017450 char numbuf[20];
17451
17452 fp = NULL;
17453 if (fudi.fd_newkey == NULL && !eap->forceit)
17454 {
17455 EMSG(_(e_funcdict));
17456 goto erret;
17457 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017458 if (fudi.fd_di == NULL)
17459 {
17460 /* Can't add a function to a locked dictionary */
17461 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17462 goto erret;
17463 }
17464 /* Can't change an existing function if it is locked */
17465 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17466 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017467
17468 /* Give the function a sequential number. Can only be used with a
17469 * Funcref! */
17470 vim_free(name);
17471 sprintf(numbuf, "%d", ++func_nr);
17472 name = vim_strsave((char_u *)numbuf);
17473 if (name == NULL)
17474 goto erret;
17475 }
17476
17477 if (fp == NULL)
17478 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017479 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017480 {
17481 int slen, plen;
17482 char_u *scriptname;
17483
17484 /* Check that the autoload name matches the script name. */
17485 j = FAIL;
17486 if (sourcing_name != NULL)
17487 {
17488 scriptname = autoload_name(name);
17489 if (scriptname != NULL)
17490 {
17491 p = vim_strchr(scriptname, '/');
17492 plen = STRLEN(p);
17493 slen = STRLEN(sourcing_name);
17494 if (slen > plen && fnamecmp(p,
17495 sourcing_name + slen - plen) == 0)
17496 j = OK;
17497 vim_free(scriptname);
17498 }
17499 }
17500 if (j == FAIL)
17501 {
17502 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17503 goto erret;
17504 }
17505 }
17506
17507 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 if (fp == NULL)
17509 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017510
17511 if (fudi.fd_dict != NULL)
17512 {
17513 if (fudi.fd_di == NULL)
17514 {
17515 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017516 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017517 if (fudi.fd_di == NULL)
17518 {
17519 vim_free(fp);
17520 goto erret;
17521 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017522 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17523 {
17524 vim_free(fudi.fd_di);
17525 goto erret;
17526 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017527 }
17528 else
17529 /* overwrite existing dict entry */
17530 clear_tv(&fudi.fd_di->di_tv);
17531 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017532 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017533 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017534 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017535 }
17536
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017538 STRCPY(fp->uf_name, name);
17539 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017541 fp->uf_args = newargs;
17542 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017543#ifdef FEAT_PROFILE
17544 fp->uf_tml_count = NULL;
17545 fp->uf_tml_total = NULL;
17546 fp->uf_tml_self = NULL;
17547 fp->uf_profiling = FALSE;
17548 if (prof_def_func())
17549 func_do_profile(fp);
17550#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017551 fp->uf_varargs = varargs;
17552 fp->uf_flags = flags;
17553 fp->uf_calls = 0;
17554 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017555 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556
17557erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 ga_clear_strings(&newargs);
17559 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017560ret_free:
17561 vim_free(skip_until);
17562 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565}
17566
17567/*
17568 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017569 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017571 * flags:
17572 * TFN_INT: internal function name OK
17573 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 * Advances "pp" to just after the function name (if no error).
17575 */
17576 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017577trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 char_u **pp;
17579 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017580 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017581 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017583 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 char_u *start;
17585 char_u *end;
17586 int lead;
17587 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017589 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017590
17591 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017592 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017594
17595 /* Check for hard coded <SNR>: already translated function ID (from a user
17596 * command). */
17597 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17598 && (*pp)[2] == (int)KE_SNR)
17599 {
17600 *pp += 3;
17601 len = get_id_len(pp) + 3;
17602 return vim_strnsave(start, len);
17603 }
17604
17605 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17606 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017608 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017610
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017611 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17612 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017613 if (end == start)
17614 {
17615 if (!skip)
17616 EMSG(_("E129: Function name required"));
17617 goto theend;
17618 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017619 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017620 {
17621 /*
17622 * Report an invalid expression in braces, unless the expression
17623 * evaluation has been cancelled due to an aborting error, an
17624 * interrupt, or an exception.
17625 */
17626 if (!aborting())
17627 {
17628 if (end != NULL)
17629 EMSG2(_(e_invarg2), start);
17630 }
17631 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017632 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017633 goto theend;
17634 }
17635
17636 if (lv.ll_tv != NULL)
17637 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017638 if (fdp != NULL)
17639 {
17640 fdp->fd_dict = lv.ll_dict;
17641 fdp->fd_newkey = lv.ll_newkey;
17642 lv.ll_newkey = NULL;
17643 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017644 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017645 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17646 {
17647 name = vim_strsave(lv.ll_tv->vval.v_string);
17648 *pp = end;
17649 }
17650 else
17651 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017652 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17653 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017654 EMSG(_(e_funcref));
17655 else
17656 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017657 name = NULL;
17658 }
17659 goto theend;
17660 }
17661
17662 if (lv.ll_name == NULL)
17663 {
17664 /* Error found, but continue after the function name. */
17665 *pp = end;
17666 goto theend;
17667 }
17668
17669 if (lv.ll_exp_name != NULL)
17670 len = STRLEN(lv.ll_exp_name);
17671 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017672 {
17673 if (lead == 2) /* skip over "s:" */
17674 lv.ll_name += 2;
17675 len = (int)(end - lv.ll_name);
17676 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017677
17678 /*
17679 * Copy the function name to allocated memory.
17680 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17681 * Accept <SNR>123_name() outside a script.
17682 */
17683 if (skip)
17684 lead = 0; /* do nothing */
17685 else if (lead > 0)
17686 {
17687 lead = 3;
17688 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17689 {
17690 if (current_SID <= 0)
17691 {
17692 EMSG(_(e_usingsid));
17693 goto theend;
17694 }
17695 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17696 lead += (int)STRLEN(sid_buf);
17697 }
17698 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017699 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017700 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017701 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017702 goto theend;
17703 }
17704 name = alloc((unsigned)(len + lead + 1));
17705 if (name != NULL)
17706 {
17707 if (lead > 0)
17708 {
17709 name[0] = K_SPECIAL;
17710 name[1] = KS_EXTRA;
17711 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017712 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017713 STRCPY(name + 3, sid_buf);
17714 }
17715 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17716 name[len + lead] = NUL;
17717 }
17718 *pp = end;
17719
17720theend:
17721 clear_lval(&lv);
17722 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723}
17724
17725/*
17726 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17727 * Return 2 if "p" starts with "s:".
17728 * Return 0 otherwise.
17729 */
17730 static int
17731eval_fname_script(p)
17732 char_u *p;
17733{
17734 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17735 || STRNICMP(p + 1, "SNR>", 4) == 0))
17736 return 5;
17737 if (p[0] == 's' && p[1] == ':')
17738 return 2;
17739 return 0;
17740}
17741
17742/*
17743 * Return TRUE if "p" starts with "<SID>" or "s:".
17744 * Only works if eval_fname_script() returned non-zero for "p"!
17745 */
17746 static int
17747eval_fname_sid(p)
17748 char_u *p;
17749{
17750 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17751}
17752
17753/*
17754 * List the head of the function: "name(arg1, arg2)".
17755 */
17756 static void
17757list_func_head(fp, indent)
17758 ufunc_T *fp;
17759 int indent;
17760{
17761 int j;
17762
17763 msg_start();
17764 if (indent)
17765 MSG_PUTS(" ");
17766 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017767 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 {
17769 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017770 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 }
17772 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017773 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017775 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 {
17777 if (j)
17778 MSG_PUTS(", ");
17779 msg_puts(FUNCARG(fp, j));
17780 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017781 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 {
17783 if (j)
17784 MSG_PUTS(", ");
17785 MSG_PUTS("...");
17786 }
17787 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017788 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017789 if (p_verbose > 0)
17790 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791}
17792
17793/*
17794 * Find a function by name, return pointer to it in ufuncs.
17795 * Return NULL for unknown function.
17796 */
17797 static ufunc_T *
17798find_func(name)
17799 char_u *name;
17800{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017801 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017803 hi = hash_find(&func_hashtab, name);
17804 if (!HASHITEM_EMPTY(hi))
17805 return HI2UF(hi);
17806 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807}
17808
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017809#if defined(EXITFREE) || defined(PROTO)
17810 void
17811free_all_functions()
17812{
17813 hashitem_T *hi;
17814
17815 /* Need to start all over every time, because func_free() may change the
17816 * hash table. */
17817 while (func_hashtab.ht_used > 0)
17818 for (hi = func_hashtab.ht_array; ; ++hi)
17819 if (!HASHITEM_EMPTY(hi))
17820 {
17821 func_free(HI2UF(hi));
17822 break;
17823 }
17824}
17825#endif
17826
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017827/*
17828 * Return TRUE if a function "name" exists.
17829 */
17830 static int
17831function_exists(name)
17832 char_u *name;
17833{
17834 char_u *p = name;
17835 int n = FALSE;
17836
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017837 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017838 if (p != NULL)
17839 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017840 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017841 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017842 else
17843 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017844 vim_free(p);
17845 }
17846 return n;
17847}
17848
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017849/*
17850 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017851 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017852 */
17853 static int
17854builtin_function(name)
17855 char_u *name;
17856{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017857 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17858 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017859}
17860
Bram Moolenaar05159a02005-02-26 23:04:13 +000017861#if defined(FEAT_PROFILE) || defined(PROTO)
17862/*
17863 * Start profiling function "fp".
17864 */
17865 static void
17866func_do_profile(fp)
17867 ufunc_T *fp;
17868{
17869 fp->uf_tm_count = 0;
17870 profile_zero(&fp->uf_tm_self);
17871 profile_zero(&fp->uf_tm_total);
17872 if (fp->uf_tml_count == NULL)
17873 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17874 (sizeof(int) * fp->uf_lines.ga_len));
17875 if (fp->uf_tml_total == NULL)
17876 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17877 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17878 if (fp->uf_tml_self == NULL)
17879 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17880 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17881 fp->uf_tml_idx = -1;
17882 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17883 || fp->uf_tml_self == NULL)
17884 return; /* out of memory */
17885
17886 fp->uf_profiling = TRUE;
17887}
17888
17889/*
17890 * Dump the profiling results for all functions in file "fd".
17891 */
17892 void
17893func_dump_profile(fd)
17894 FILE *fd;
17895{
17896 hashitem_T *hi;
17897 int todo;
17898 ufunc_T *fp;
17899 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017900 ufunc_T **sorttab;
17901 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017902
17903 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017904 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17905
Bram Moolenaar05159a02005-02-26 23:04:13 +000017906 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17907 {
17908 if (!HASHITEM_EMPTY(hi))
17909 {
17910 --todo;
17911 fp = HI2UF(hi);
17912 if (fp->uf_profiling)
17913 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017914 if (sorttab != NULL)
17915 sorttab[st_len++] = fp;
17916
Bram Moolenaar05159a02005-02-26 23:04:13 +000017917 if (fp->uf_name[0] == K_SPECIAL)
17918 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17919 else
17920 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17921 if (fp->uf_tm_count == 1)
17922 fprintf(fd, "Called 1 time\n");
17923 else
17924 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17925 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17926 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17927 fprintf(fd, "\n");
17928 fprintf(fd, "count total (s) self (s)\n");
17929
17930 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17931 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017932 prof_func_line(fd, fp->uf_tml_count[i],
17933 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017934 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17935 }
17936 fprintf(fd, "\n");
17937 }
17938 }
17939 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017940
17941 if (sorttab != NULL && st_len > 0)
17942 {
17943 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17944 prof_total_cmp);
17945 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17946 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17947 prof_self_cmp);
17948 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17949 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017950}
Bram Moolenaar73830342005-02-28 22:48:19 +000017951
17952 static void
17953prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17954 FILE *fd;
17955 ufunc_T **sorttab;
17956 int st_len;
17957 char *title;
17958 int prefer_self; /* when equal print only self time */
17959{
17960 int i;
17961 ufunc_T *fp;
17962
17963 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17964 fprintf(fd, "count total (s) self (s) function\n");
17965 for (i = 0; i < 20 && i < st_len; ++i)
17966 {
17967 fp = sorttab[i];
17968 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17969 prefer_self);
17970 if (fp->uf_name[0] == K_SPECIAL)
17971 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17972 else
17973 fprintf(fd, " %s()\n", fp->uf_name);
17974 }
17975 fprintf(fd, "\n");
17976}
17977
17978/*
17979 * Print the count and times for one function or function line.
17980 */
17981 static void
17982prof_func_line(fd, count, total, self, prefer_self)
17983 FILE *fd;
17984 int count;
17985 proftime_T *total;
17986 proftime_T *self;
17987 int prefer_self; /* when equal print only self time */
17988{
17989 if (count > 0)
17990 {
17991 fprintf(fd, "%5d ", count);
17992 if (prefer_self && profile_equal(total, self))
17993 fprintf(fd, " ");
17994 else
17995 fprintf(fd, "%s ", profile_msg(total));
17996 if (!prefer_self && profile_equal(total, self))
17997 fprintf(fd, " ");
17998 else
17999 fprintf(fd, "%s ", profile_msg(self));
18000 }
18001 else
18002 fprintf(fd, " ");
18003}
18004
18005/*
18006 * Compare function for total time sorting.
18007 */
18008 static int
18009#ifdef __BORLANDC__
18010_RTLENTRYF
18011#endif
18012prof_total_cmp(s1, s2)
18013 const void *s1;
18014 const void *s2;
18015{
18016 ufunc_T *p1, *p2;
18017
18018 p1 = *(ufunc_T **)s1;
18019 p2 = *(ufunc_T **)s2;
18020 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18021}
18022
18023/*
18024 * Compare function for self time sorting.
18025 */
18026 static int
18027#ifdef __BORLANDC__
18028_RTLENTRYF
18029#endif
18030prof_self_cmp(s1, s2)
18031 const void *s1;
18032 const void *s2;
18033{
18034 ufunc_T *p1, *p2;
18035
18036 p1 = *(ufunc_T **)s1;
18037 p2 = *(ufunc_T **)s2;
18038 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18039}
18040
Bram Moolenaar05159a02005-02-26 23:04:13 +000018041#endif
18042
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018043/* The names of packages that once were loaded is remembered. */
18044static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18045
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018046/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018047 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018048 * Return TRUE if a package was loaded.
18049 */
18050 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018051script_autoload(name, reload)
18052 char_u *name;
18053 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018054{
18055 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018056 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018057 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018058 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018059
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018060 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018061 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018062 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018063 return FALSE;
18064
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018065 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018066
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018067 /* Find the name in the list of previously loaded package names. Skip
18068 * "autoload/", it's always the same. */
18069 for (i = 0; i < ga_loaded.ga_len; ++i)
18070 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18071 break;
18072 if (!reload && i < ga_loaded.ga_len)
18073 ret = FALSE; /* was loaded already */
18074 else
18075 {
18076 /* Remember the name if it wasn't loaded already. */
18077 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18078 {
18079 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18080 tofree = NULL;
18081 }
18082
18083 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018084 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018085 ret = TRUE;
18086 }
18087
18088 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018089 return ret;
18090}
18091
18092/*
18093 * Return the autoload script name for a function or variable name.
18094 * Returns NULL when out of memory.
18095 */
18096 static char_u *
18097autoload_name(name)
18098 char_u *name;
18099{
18100 char_u *p;
18101 char_u *scriptname;
18102
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018103 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018104 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18105 if (scriptname == NULL)
18106 return FALSE;
18107 STRCPY(scriptname, "autoload/");
18108 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018109 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018110 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018111 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018112 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018113 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018114}
18115
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18117
18118/*
18119 * Function given to ExpandGeneric() to obtain the list of user defined
18120 * function names.
18121 */
18122 char_u *
18123get_user_func_name(xp, idx)
18124 expand_T *xp;
18125 int idx;
18126{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018127 static long_u done;
18128 static hashitem_T *hi;
18129 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130
18131 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018133 done = 0;
18134 hi = func_hashtab.ht_array;
18135 }
18136 if (done < func_hashtab.ht_used)
18137 {
18138 if (done++ > 0)
18139 ++hi;
18140 while (HASHITEM_EMPTY(hi))
18141 ++hi;
18142 fp = HI2UF(hi);
18143
18144 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18145 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146
18147 cat_func_name(IObuff, fp);
18148 if (xp->xp_context != EXPAND_USER_FUNC)
18149 {
18150 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018151 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 STRCAT(IObuff, ")");
18153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 return IObuff;
18155 }
18156 return NULL;
18157}
18158
18159#endif /* FEAT_CMDL_COMPL */
18160
18161/*
18162 * Copy the function name of "fp" to buffer "buf".
18163 * "buf" must be able to hold the function name plus three bytes.
18164 * Takes care of script-local function names.
18165 */
18166 static void
18167cat_func_name(buf, fp)
18168 char_u *buf;
18169 ufunc_T *fp;
18170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018171 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 {
18173 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018174 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 }
18176 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018177 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178}
18179
18180/*
18181 * ":delfunction {name}"
18182 */
18183 void
18184ex_delfunction(eap)
18185 exarg_T *eap;
18186{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018187 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 char_u *p;
18189 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018190 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191
18192 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018193 name = trans_function_name(&p, eap->skip, 0, &fudi);
18194 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018196 {
18197 if (fudi.fd_dict != NULL && !eap->skip)
18198 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 if (!ends_excmd(*skipwhite(p)))
18202 {
18203 vim_free(name);
18204 EMSG(_(e_trailing));
18205 return;
18206 }
18207 eap->nextcmd = check_nextcmd(p);
18208 if (eap->nextcmd != NULL)
18209 *p = NUL;
18210
18211 if (!eap->skip)
18212 fp = find_func(name);
18213 vim_free(name);
18214
18215 if (!eap->skip)
18216 {
18217 if (fp == NULL)
18218 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018219 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 return;
18221 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018222 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 {
18224 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18225 return;
18226 }
18227
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018228 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018230 /* Delete the dict item that refers to the function, it will
18231 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018232 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018234 else
18235 func_free(fp);
18236 }
18237}
18238
18239/*
18240 * Free a function and remove it from the list of functions.
18241 */
18242 static void
18243func_free(fp)
18244 ufunc_T *fp;
18245{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018246 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018247
18248 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018249 ga_clear_strings(&(fp->uf_args));
18250 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018251#ifdef FEAT_PROFILE
18252 vim_free(fp->uf_tml_count);
18253 vim_free(fp->uf_tml_total);
18254 vim_free(fp->uf_tml_self);
18255#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018256
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018257 /* remove the function from the function hashtable */
18258 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18259 if (HASHITEM_EMPTY(hi))
18260 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018261 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018262 hash_remove(&func_hashtab, hi);
18263
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018264 vim_free(fp);
18265}
18266
18267/*
18268 * Unreference a Function: decrement the reference count and free it when it
18269 * becomes zero. Only for numbered functions.
18270 */
18271 static void
18272func_unref(name)
18273 char_u *name;
18274{
18275 ufunc_T *fp;
18276
18277 if (name != NULL && isdigit(*name))
18278 {
18279 fp = find_func(name);
18280 if (fp == NULL)
18281 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018282 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018283 {
18284 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018285 * when "uf_calls" becomes zero. */
18286 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018287 func_free(fp);
18288 }
18289 }
18290}
18291
18292/*
18293 * Count a reference to a Function.
18294 */
18295 static void
18296func_ref(name)
18297 char_u *name;
18298{
18299 ufunc_T *fp;
18300
18301 if (name != NULL && isdigit(*name))
18302 {
18303 fp = find_func(name);
18304 if (fp == NULL)
18305 EMSG2(_(e_intern2), "func_ref()");
18306 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018307 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308 }
18309}
18310
18311/*
18312 * Call a user function.
18313 */
18314 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018315call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 ufunc_T *fp; /* pointer to function */
18317 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018318 typval_T *argvars; /* arguments */
18319 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 linenr_T firstline; /* first line of range */
18321 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018322 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323{
Bram Moolenaar33570922005-01-25 22:26:29 +000018324 char_u *save_sourcing_name;
18325 linenr_T save_sourcing_lnum;
18326 scid_T save_current_SID;
18327 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018328 int save_did_emsg;
18329 static int depth = 0;
18330 dictitem_T *v;
18331 int fixvar_idx = 0; /* index in fixvar[] */
18332 int i;
18333 int ai;
18334 char_u numbuf[NUMBUFLEN];
18335 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018336#ifdef FEAT_PROFILE
18337 proftime_T wait_start;
18338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339
18340 /* If depth of calling is getting too high, don't execute the function */
18341 if (depth >= p_mfd)
18342 {
18343 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018344 rettv->v_type = VAR_NUMBER;
18345 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 return;
18347 }
18348 ++depth;
18349
18350 line_breakcheck(); /* check for CTRL-C hit */
18351
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018352 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018353 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018355 fc.rettv = rettv;
18356 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 fc.linenr = 0;
18358 fc.returned = FALSE;
18359 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018361 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 fc.dbg_tick = debug_tick;
18363
Bram Moolenaar33570922005-01-25 22:26:29 +000018364 /*
18365 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18366 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18367 * each argument variable and saves a lot of time.
18368 */
18369 /*
18370 * Init l: variables.
18371 */
18372 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018373 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018374 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018375 /* Set l:self to "selfdict". */
18376 v = &fc.fixvar[fixvar_idx++].var;
18377 STRCPY(v->di_key, "self");
18378 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18379 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18380 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018381 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018382 v->di_tv.vval.v_dict = selfdict;
18383 ++selfdict->dv_refcount;
18384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018385
Bram Moolenaar33570922005-01-25 22:26:29 +000018386 /*
18387 * Init a: variables.
18388 * Set a:0 to "argcount".
18389 * Set a:000 to a list with room for the "..." arguments.
18390 */
18391 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18392 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018393 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 v = &fc.fixvar[fixvar_idx++].var;
18395 STRCPY(v->di_key, "000");
18396 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18397 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18398 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018399 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018400 v->di_tv.vval.v_list = &fc.l_varlist;
18401 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18402 fc.l_varlist.lv_refcount = 99999;
18403
18404 /*
18405 * Set a:firstline to "firstline" and a:lastline to "lastline".
18406 * Set a:name to named arguments.
18407 * Set a:N to the "..." arguments.
18408 */
18409 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18410 (varnumber_T)firstline);
18411 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18412 (varnumber_T)lastline);
18413 for (i = 0; i < argcount; ++i)
18414 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018415 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018416 if (ai < 0)
18417 /* named argument a:name */
18418 name = FUNCARG(fp, i);
18419 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018420 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018421 /* "..." argument a:1, a:2, etc. */
18422 sprintf((char *)numbuf, "%d", ai + 1);
18423 name = numbuf;
18424 }
18425 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18426 {
18427 v = &fc.fixvar[fixvar_idx++].var;
18428 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18429 }
18430 else
18431 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018432 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18433 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018434 if (v == NULL)
18435 break;
18436 v->di_flags = DI_FLAGS_RO;
18437 }
18438 STRCPY(v->di_key, name);
18439 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18440
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018441 /* Note: the values are copied directly to avoid alloc/free.
18442 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018443 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018444 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018445
18446 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18447 {
18448 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18449 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018450 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018451 }
18452 }
18453
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454 /* Don't redraw while executing the function. */
18455 ++RedrawingDisabled;
18456 save_sourcing_name = sourcing_name;
18457 save_sourcing_lnum = sourcing_lnum;
18458 sourcing_lnum = 1;
18459 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018460 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 if (sourcing_name != NULL)
18462 {
18463 if (save_sourcing_name != NULL
18464 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18465 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18466 else
18467 STRCPY(sourcing_name, "function ");
18468 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18469
18470 if (p_verbose >= 12)
18471 {
18472 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018473 verbose_enter_scroll();
18474
Bram Moolenaar555b2802005-05-19 21:08:39 +000018475 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 if (p_verbose >= 14)
18477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018479 char_u numbuf[NUMBUFLEN];
18480 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481
18482 msg_puts((char_u *)"(");
18483 for (i = 0; i < argcount; ++i)
18484 {
18485 if (i > 0)
18486 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018487 if (argvars[i].v_type == VAR_NUMBER)
18488 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 else
18490 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018491 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018492 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018494 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 }
18496 }
18497 msg_puts((char_u *)")");
18498 }
18499 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018500
18501 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502 --no_wait_return;
18503 }
18504 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018505#ifdef FEAT_PROFILE
18506 if (do_profiling)
18507 {
18508 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18509 func_do_profile(fp);
18510 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018511 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018512 {
18513 ++fp->uf_tm_count;
18514 profile_start(&fp->uf_tm_start);
18515 profile_zero(&fp->uf_tm_children);
18516 }
18517 script_prof_save(&wait_start);
18518 }
18519#endif
18520
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018522 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 save_did_emsg = did_emsg;
18524 did_emsg = FALSE;
18525
18526 /* call do_cmdline() to execute the lines */
18527 do_cmdline(NULL, get_func_line, (void *)&fc,
18528 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18529
18530 --RedrawingDisabled;
18531
18532 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018533 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018535 clear_tv(rettv);
18536 rettv->v_type = VAR_NUMBER;
18537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 }
18539
Bram Moolenaar05159a02005-02-26 23:04:13 +000018540#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018541 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018542 {
18543 profile_end(&fp->uf_tm_start);
18544 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18545 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18546 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18547 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018548 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018549 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018550 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18551 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018552 }
18553 }
18554#endif
18555
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 /* when being verbose, mention the return value */
18557 if (p_verbose >= 12)
18558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018560 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018563 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018564 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018565 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18566 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018567 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018569 char_u buf[MSG_BUF_LEN];
18570 char_u numbuf[NUMBUFLEN];
18571 char_u *tofree;
18572
Bram Moolenaar555b2802005-05-19 21:08:39 +000018573 /* The value may be very long. Skip the middle part, so that we
18574 * have some idea how it starts and ends. smsg() would always
18575 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018576 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018577 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018578 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018579 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 }
18581 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018582
18583 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 --no_wait_return;
18585 }
18586
18587 vim_free(sourcing_name);
18588 sourcing_name = save_sourcing_name;
18589 sourcing_lnum = save_sourcing_lnum;
18590 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018591#ifdef FEAT_PROFILE
18592 if (do_profiling)
18593 script_prof_restore(&wait_start);
18594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595
18596 if (p_verbose >= 12 && sourcing_name != NULL)
18597 {
18598 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018599 verbose_enter_scroll();
18600
Bram Moolenaar555b2802005-05-19 21:08:39 +000018601 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018603
18604 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 --no_wait_return;
18606 }
18607
18608 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018609 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610
Bram Moolenaar33570922005-01-25 22:26:29 +000018611 /* The a: variables typevals were not alloced, only free the allocated
18612 * variables. */
18613 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18614
18615 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 --depth;
18617}
18618
18619/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018620 * Add a number variable "name" to dict "dp" with value "nr".
18621 */
18622 static void
18623add_nr_var(dp, v, name, nr)
18624 dict_T *dp;
18625 dictitem_T *v;
18626 char *name;
18627 varnumber_T nr;
18628{
18629 STRCPY(v->di_key, name);
18630 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18631 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18632 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018633 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 v->di_tv.vval.v_number = nr;
18635}
18636
18637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 * ":return [expr]"
18639 */
18640 void
18641ex_return(eap)
18642 exarg_T *eap;
18643{
18644 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018645 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 int returning = FALSE;
18647
18648 if (current_funccal == NULL)
18649 {
18650 EMSG(_("E133: :return not inside a function"));
18651 return;
18652 }
18653
18654 if (eap->skip)
18655 ++emsg_skip;
18656
18657 eap->nextcmd = NULL;
18658 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 {
18661 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018662 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 }
18666 /* It's safer to return also on error. */
18667 else if (!eap->skip)
18668 {
18669 /*
18670 * Return unless the expression evaluation has been cancelled due to an
18671 * aborting error, an interrupt, or an exception.
18672 */
18673 if (!aborting())
18674 returning = do_return(eap, FALSE, TRUE, NULL);
18675 }
18676
18677 /* When skipping or the return gets pending, advance to the next command
18678 * in this line (!returning). Otherwise, ignore the rest of the line.
18679 * Following lines will be ignored by get_func_line(). */
18680 if (returning)
18681 eap->nextcmd = NULL;
18682 else if (eap->nextcmd == NULL) /* no argument */
18683 eap->nextcmd = check_nextcmd(arg);
18684
18685 if (eap->skip)
18686 --emsg_skip;
18687}
18688
18689/*
18690 * Return from a function. Possibly makes the return pending. Also called
18691 * for a pending return at the ":endtry" or after returning from an extra
18692 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018693 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 * FALSE when the return gets pending.
18696 */
18697 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 exarg_T *eap;
18700 int reanimate;
18701 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018702 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703{
18704 int idx;
18705 struct condstack *cstack = eap->cstack;
18706
18707 if (reanimate)
18708 /* Undo the return. */
18709 current_funccal->returned = FALSE;
18710
18711 /*
18712 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18713 * not in its finally clause (which then is to be executed next) is found.
18714 * In this case, make the ":return" pending for execution at the ":endtry".
18715 * Otherwise, return normally.
18716 */
18717 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18718 if (idx >= 0)
18719 {
18720 cstack->cs_pending[idx] = CSTP_RETURN;
18721
18722 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723 /* A pending return again gets pending. "rettv" points to an
18724 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 else
18728 {
18729 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018730 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018732 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018734 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 {
18736 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018737 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018738 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 else
18740 EMSG(_(e_outofmem));
18741 }
18742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018743 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744
18745 if (reanimate)
18746 {
18747 /* The pending return value could be overwritten by a ":return"
18748 * without argument in a finally clause; reset the default
18749 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 current_funccal->rettv->v_type = VAR_NUMBER;
18751 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 }
18753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018754 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755 }
18756 else
18757 {
18758 current_funccal->returned = TRUE;
18759
18760 /* If the return is carried out now, store the return value. For
18761 * a return immediately after reanimation, the value is already
18762 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018763 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018765 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 }
18770 }
18771
18772 return idx < 0;
18773}
18774
18775/*
18776 * Free the variable with a pending return value.
18777 */
18778 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018779discard_pending_return(rettv)
18780 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781{
Bram Moolenaar33570922005-01-25 22:26:29 +000018782 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783}
18784
18785/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 * is an allocated string. Used by report_pending() for verbose messages.
18788 */
18789 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790get_return_cmd(rettv)
18791 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018793 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018794 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018795 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018797 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018799 if (s == NULL)
18800 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801
18802 STRCPY(IObuff, ":return ");
18803 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18804 if (STRLEN(s) + 8 >= IOSIZE)
18805 STRCPY(IObuff + IOSIZE - 4, "...");
18806 vim_free(tofree);
18807 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808}
18809
18810/*
18811 * Get next function line.
18812 * Called by do_cmdline() to get the next line.
18813 * Returns allocated string, or NULL for end of function.
18814 */
18815/* ARGSUSED */
18816 char_u *
18817get_func_line(c, cookie, indent)
18818 int c; /* not used */
18819 void *cookie;
18820 int indent; /* not used */
18821{
Bram Moolenaar33570922005-01-25 22:26:29 +000018822 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018823 ufunc_T *fp = fcp->func;
18824 char_u *retval;
18825 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826
18827 /* If breakpoints have been added/deleted need to check for it. */
18828 if (fcp->dbg_tick != debug_tick)
18829 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018830 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 sourcing_lnum);
18832 fcp->dbg_tick = debug_tick;
18833 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018834#ifdef FEAT_PROFILE
18835 if (do_profiling)
18836 func_line_end(cookie);
18837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838
Bram Moolenaar05159a02005-02-26 23:04:13 +000018839 gap = &fp->uf_lines;
18840 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 retval = NULL;
18842 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18843 retval = NULL;
18844 else
18845 {
18846 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18847 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018848#ifdef FEAT_PROFILE
18849 if (do_profiling)
18850 func_line_start(cookie);
18851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 }
18853
18854 /* Did we encounter a breakpoint? */
18855 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18856 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018857 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018859 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 sourcing_lnum);
18861 fcp->dbg_tick = debug_tick;
18862 }
18863
18864 return retval;
18865}
18866
Bram Moolenaar05159a02005-02-26 23:04:13 +000018867#if defined(FEAT_PROFILE) || defined(PROTO)
18868/*
18869 * Called when starting to read a function line.
18870 * "sourcing_lnum" must be correct!
18871 * When skipping lines it may not actually be executed, but we won't find out
18872 * until later and we need to store the time now.
18873 */
18874 void
18875func_line_start(cookie)
18876 void *cookie;
18877{
18878 funccall_T *fcp = (funccall_T *)cookie;
18879 ufunc_T *fp = fcp->func;
18880
18881 if (fp->uf_profiling && sourcing_lnum >= 1
18882 && sourcing_lnum <= fp->uf_lines.ga_len)
18883 {
18884 fp->uf_tml_idx = sourcing_lnum - 1;
18885 fp->uf_tml_execed = FALSE;
18886 profile_start(&fp->uf_tml_start);
18887 profile_zero(&fp->uf_tml_children);
18888 profile_get_wait(&fp->uf_tml_wait);
18889 }
18890}
18891
18892/*
18893 * Called when actually executing a function line.
18894 */
18895 void
18896func_line_exec(cookie)
18897 void *cookie;
18898{
18899 funccall_T *fcp = (funccall_T *)cookie;
18900 ufunc_T *fp = fcp->func;
18901
18902 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18903 fp->uf_tml_execed = TRUE;
18904}
18905
18906/*
18907 * Called when done with a function line.
18908 */
18909 void
18910func_line_end(cookie)
18911 void *cookie;
18912{
18913 funccall_T *fcp = (funccall_T *)cookie;
18914 ufunc_T *fp = fcp->func;
18915
18916 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18917 {
18918 if (fp->uf_tml_execed)
18919 {
18920 ++fp->uf_tml_count[fp->uf_tml_idx];
18921 profile_end(&fp->uf_tml_start);
18922 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18923 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18924 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18925 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18926 }
18927 fp->uf_tml_idx = -1;
18928 }
18929}
18930#endif
18931
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932/*
18933 * Return TRUE if the currently active function should be ended, because a
18934 * return was encountered or an error occured. Used inside a ":while".
18935 */
18936 int
18937func_has_ended(cookie)
18938 void *cookie;
18939{
Bram Moolenaar33570922005-01-25 22:26:29 +000018940 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941
18942 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18943 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018944 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 || fcp->returned);
18946}
18947
18948/*
18949 * return TRUE if cookie indicates a function which "abort"s on errors.
18950 */
18951 int
18952func_has_abort(cookie)
18953 void *cookie;
18954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018955 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956}
18957
18958#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18959typedef enum
18960{
18961 VAR_FLAVOUR_DEFAULT,
18962 VAR_FLAVOUR_SESSION,
18963 VAR_FLAVOUR_VIMINFO
18964} var_flavour_T;
18965
18966static var_flavour_T var_flavour __ARGS((char_u *varname));
18967
18968 static var_flavour_T
18969var_flavour(varname)
18970 char_u *varname;
18971{
18972 char_u *p = varname;
18973
18974 if (ASCII_ISUPPER(*p))
18975 {
18976 while (*(++p))
18977 if (ASCII_ISLOWER(*p))
18978 return VAR_FLAVOUR_SESSION;
18979 return VAR_FLAVOUR_VIMINFO;
18980 }
18981 else
18982 return VAR_FLAVOUR_DEFAULT;
18983}
18984#endif
18985
18986#if defined(FEAT_VIMINFO) || defined(PROTO)
18987/*
18988 * Restore global vars that start with a capital from the viminfo file
18989 */
18990 int
18991read_viminfo_varlist(virp, writing)
18992 vir_T *virp;
18993 int writing;
18994{
18995 char_u *tab;
18996 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018997 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998
18999 if (!writing && (find_viminfo_parameter('!') != NULL))
19000 {
19001 tab = vim_strchr(virp->vir_line + 1, '\t');
19002 if (tab != NULL)
19003 {
19004 *tab++ = '\0'; /* isolate the variable name */
19005 if (*tab == 'S') /* string var */
19006 is_string = TRUE;
19007
19008 tab = vim_strchr(tab, '\t');
19009 if (tab != NULL)
19010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 if (is_string)
19012 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019013 tv.v_type = VAR_STRING;
19014 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 }
19017 else
19018 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019019 tv.v_type = VAR_NUMBER;
19020 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019022 set_var(virp->vir_line + 1, &tv, FALSE);
19023 if (is_string)
19024 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025 }
19026 }
19027 }
19028
19029 return viminfo_readline(virp);
19030}
19031
19032/*
19033 * Write global vars that start with a capital to the viminfo file
19034 */
19035 void
19036write_viminfo_varlist(fp)
19037 FILE *fp;
19038{
Bram Moolenaar33570922005-01-25 22:26:29 +000019039 hashitem_T *hi;
19040 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019041 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019042 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019043 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019045 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046
19047 if (find_viminfo_parameter('!') == NULL)
19048 return;
19049
19050 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019051
Bram Moolenaar33570922005-01-25 22:26:29 +000019052 todo = globvarht.ht_used;
19053 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019055 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019057 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019058 this_var = HI2DI(hi);
19059 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019060 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019061 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019062 {
19063 case VAR_STRING: s = "STR"; break;
19064 case VAR_NUMBER: s = "NUM"; break;
19065 default: continue;
19066 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019067 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019068 p = echo_string(&this_var->di_tv, &tofree, numbuf);
19069 if (p != NULL)
19070 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019071 vim_free(tofree);
19072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 }
19074 }
19075}
19076#endif
19077
19078#if defined(FEAT_SESSION) || defined(PROTO)
19079 int
19080store_session_globals(fd)
19081 FILE *fd;
19082{
Bram Moolenaar33570922005-01-25 22:26:29 +000019083 hashitem_T *hi;
19084 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019085 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 char_u *p, *t;
19087
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 todo = globvarht.ht_used;
19089 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019091 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019093 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019094 this_var = HI2DI(hi);
19095 if ((this_var->di_tv.v_type == VAR_NUMBER
19096 || this_var->di_tv.v_type == VAR_STRING)
19097 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019098 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019099 /* Escape special characters with a backslash. Turn a LF and
19100 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019101 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019102 (char_u *)"\\\"\n\r");
19103 if (p == NULL) /* out of memory */
19104 break;
19105 for (t = p; *t != NUL; ++t)
19106 if (*t == '\n')
19107 *t = 'n';
19108 else if (*t == '\r')
19109 *t = 'r';
19110 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019111 this_var->di_key,
19112 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19113 : ' ',
19114 p,
19115 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19116 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019117 || put_eol(fd) == FAIL)
19118 {
19119 vim_free(p);
19120 return FAIL;
19121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 vim_free(p);
19123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 }
19125 }
19126 return OK;
19127}
19128#endif
19129
Bram Moolenaar661b1822005-07-28 22:36:45 +000019130/*
19131 * Display script name where an item was last set.
19132 * Should only be invoked when 'verbose' is non-zero.
19133 */
19134 void
19135last_set_msg(scriptID)
19136 scid_T scriptID;
19137{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019138 char_u *p;
19139
Bram Moolenaar661b1822005-07-28 22:36:45 +000019140 if (scriptID != 0)
19141 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019142 p = home_replace_save(NULL, get_scriptname(scriptID));
19143 if (p != NULL)
19144 {
19145 verbose_enter();
19146 MSG_PUTS(_("\n\tLast set from "));
19147 MSG_PUTS(p);
19148 vim_free(p);
19149 verbose_leave();
19150 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019151 }
19152}
19153
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154#endif /* FEAT_EVAL */
19155
19156#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19157
19158
19159#ifdef WIN3264
19160/*
19161 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19162 */
19163static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19164static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19165static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19166
19167/*
19168 * Get the short pathname of a file.
19169 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19170 */
19171 static int
19172get_short_pathname(fnamep, bufp, fnamelen)
19173 char_u **fnamep;
19174 char_u **bufp;
19175 int *fnamelen;
19176{
19177 int l,len;
19178 char_u *newbuf;
19179
19180 len = *fnamelen;
19181
19182 l = GetShortPathName(*fnamep, *fnamep, len);
19183 if (l > len - 1)
19184 {
19185 /* If that doesn't work (not enough space), then save the string
19186 * and try again with a new buffer big enough
19187 */
19188 newbuf = vim_strnsave(*fnamep, l);
19189 if (newbuf == NULL)
19190 return 0;
19191
19192 vim_free(*bufp);
19193 *fnamep = *bufp = newbuf;
19194
19195 l = GetShortPathName(*fnamep,*fnamep,l+1);
19196
19197 /* Really should always succeed, as the buffer is big enough */
19198 }
19199
19200 *fnamelen = l;
19201 return 1;
19202}
19203
19204/*
19205 * Create a short path name. Returns the length of the buffer it needs.
19206 * Doesn't copy over the end of the buffer passed in.
19207 */
19208 static int
19209shortpath_for_invalid_fname(fname, bufp, fnamelen)
19210 char_u **fname;
19211 char_u **bufp;
19212 int *fnamelen;
19213{
19214 char_u *s, *p, *pbuf2, *pbuf3;
19215 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019216 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217
19218 /* Make a copy */
19219 len2 = *fnamelen;
19220 pbuf2 = vim_strnsave(*fname, len2);
19221 pbuf3 = NULL;
19222
19223 s = pbuf2 + len2 - 1; /* Find the end */
19224 slen = 1;
19225 plen = len2;
19226
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019227 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 {
19229 --s;
19230 ++slen;
19231 --plen;
19232 }
19233
19234 do
19235 {
19236 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019237 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238 {
19239 --s;
19240 ++slen;
19241 --plen;
19242 }
19243 if (s <= pbuf2)
19244 break;
19245
19246 /* Remeber the character that is about to be blatted */
19247 ch = *s;
19248 *s = 0; /* get_short_pathname requires a null-terminated string */
19249
19250 /* Try it in situ */
19251 p = pbuf2;
19252 if (!get_short_pathname(&p, &pbuf3, &plen))
19253 {
19254 vim_free(pbuf2);
19255 return -1;
19256 }
19257 *s = ch; /* Preserve the string */
19258 } while (plen == 0);
19259
19260 if (plen > 0)
19261 {
19262 /* Remeber the length of the new string. */
19263 *fnamelen = len = plen + slen;
19264 vim_free(*bufp);
19265 if (len > len2)
19266 {
19267 /* If there's not enough space in the currently allocated string,
19268 * then copy it to a buffer big enough.
19269 */
19270 *fname= *bufp = vim_strnsave(p, len);
19271 if (*fname == NULL)
19272 return -1;
19273 }
19274 else
19275 {
19276 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19277 *fname = *bufp = pbuf2;
19278 if (p != pbuf2)
19279 strncpy(*fname, p, plen);
19280 pbuf2 = NULL;
19281 }
19282 /* Concat the next bit */
19283 strncpy(*fname + plen, s, slen);
19284 (*fname)[len] = '\0';
19285 }
19286 vim_free(pbuf3);
19287 vim_free(pbuf2);
19288 return 0;
19289}
19290
19291/*
19292 * Get a pathname for a partial path.
19293 */
19294 static int
19295shortpath_for_partial(fnamep, bufp, fnamelen)
19296 char_u **fnamep;
19297 char_u **bufp;
19298 int *fnamelen;
19299{
19300 int sepcount, len, tflen;
19301 char_u *p;
19302 char_u *pbuf, *tfname;
19303 int hasTilde;
19304
19305 /* Count up the path seperators from the RHS.. so we know which part
19306 * of the path to return.
19307 */
19308 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019309 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 if (vim_ispathsep(*p))
19311 ++sepcount;
19312
19313 /* Need full path first (use expand_env() to remove a "~/") */
19314 hasTilde = (**fnamep == '~');
19315 if (hasTilde)
19316 pbuf = tfname = expand_env_save(*fnamep);
19317 else
19318 pbuf = tfname = FullName_save(*fnamep, FALSE);
19319
19320 len = tflen = STRLEN(tfname);
19321
19322 if (!get_short_pathname(&tfname, &pbuf, &len))
19323 return -1;
19324
19325 if (len == 0)
19326 {
19327 /* Don't have a valid filename, so shorten the rest of the
19328 * path if we can. This CAN give us invalid 8.3 filenames, but
19329 * there's not a lot of point in guessing what it might be.
19330 */
19331 len = tflen;
19332 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19333 return -1;
19334 }
19335
19336 /* Count the paths backward to find the beginning of the desired string. */
19337 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019338 {
19339#ifdef FEAT_MBYTE
19340 if (has_mbyte)
19341 p -= mb_head_off(tfname, p);
19342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 if (vim_ispathsep(*p))
19344 {
19345 if (sepcount == 0 || (hasTilde && sepcount == 1))
19346 break;
19347 else
19348 sepcount --;
19349 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 if (hasTilde)
19352 {
19353 --p;
19354 if (p >= tfname)
19355 *p = '~';
19356 else
19357 return -1;
19358 }
19359 else
19360 ++p;
19361
19362 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19363 vim_free(*bufp);
19364 *fnamelen = (int)STRLEN(p);
19365 *bufp = pbuf;
19366 *fnamep = p;
19367
19368 return 0;
19369}
19370#endif /* WIN3264 */
19371
19372/*
19373 * Adjust a filename, according to a string of modifiers.
19374 * *fnamep must be NUL terminated when called. When returning, the length is
19375 * determined by *fnamelen.
19376 * Returns valid flags.
19377 * When there is an error, *fnamep is set to NULL.
19378 */
19379 int
19380modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19381 char_u *src; /* string with modifiers */
19382 int *usedlen; /* characters after src that are used */
19383 char_u **fnamep; /* file name so far */
19384 char_u **bufp; /* buffer for allocated file name or NULL */
19385 int *fnamelen; /* length of fnamep */
19386{
19387 int valid = 0;
19388 char_u *tail;
19389 char_u *s, *p, *pbuf;
19390 char_u dirname[MAXPATHL];
19391 int c;
19392 int has_fullname = 0;
19393#ifdef WIN3264
19394 int has_shortname = 0;
19395#endif
19396
19397repeat:
19398 /* ":p" - full path/file_name */
19399 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19400 {
19401 has_fullname = 1;
19402
19403 valid |= VALID_PATH;
19404 *usedlen += 2;
19405
19406 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19407 if ((*fnamep)[0] == '~'
19408#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19409 && ((*fnamep)[1] == '/'
19410# ifdef BACKSLASH_IN_FILENAME
19411 || (*fnamep)[1] == '\\'
19412# endif
19413 || (*fnamep)[1] == NUL)
19414
19415#endif
19416 )
19417 {
19418 *fnamep = expand_env_save(*fnamep);
19419 vim_free(*bufp); /* free any allocated file name */
19420 *bufp = *fnamep;
19421 if (*fnamep == NULL)
19422 return -1;
19423 }
19424
19425 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019426 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 {
19428 if (vim_ispathsep(*p)
19429 && p[1] == '.'
19430 && (p[2] == NUL
19431 || vim_ispathsep(p[2])
19432 || (p[2] == '.'
19433 && (p[3] == NUL || vim_ispathsep(p[3])))))
19434 break;
19435 }
19436
19437 /* FullName_save() is slow, don't use it when not needed. */
19438 if (*p != NUL || !vim_isAbsName(*fnamep))
19439 {
19440 *fnamep = FullName_save(*fnamep, *p != NUL);
19441 vim_free(*bufp); /* free any allocated file name */
19442 *bufp = *fnamep;
19443 if (*fnamep == NULL)
19444 return -1;
19445 }
19446
19447 /* Append a path separator to a directory. */
19448 if (mch_isdir(*fnamep))
19449 {
19450 /* Make room for one or two extra characters. */
19451 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19452 vim_free(*bufp); /* free any allocated file name */
19453 *bufp = *fnamep;
19454 if (*fnamep == NULL)
19455 return -1;
19456 add_pathsep(*fnamep);
19457 }
19458 }
19459
19460 /* ":." - path relative to the current directory */
19461 /* ":~" - path relative to the home directory */
19462 /* ":8" - shortname path - postponed till after */
19463 while (src[*usedlen] == ':'
19464 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19465 {
19466 *usedlen += 2;
19467 if (c == '8')
19468 {
19469#ifdef WIN3264
19470 has_shortname = 1; /* Postpone this. */
19471#endif
19472 continue;
19473 }
19474 pbuf = NULL;
19475 /* Need full path first (use expand_env() to remove a "~/") */
19476 if (!has_fullname)
19477 {
19478 if (c == '.' && **fnamep == '~')
19479 p = pbuf = expand_env_save(*fnamep);
19480 else
19481 p = pbuf = FullName_save(*fnamep, FALSE);
19482 }
19483 else
19484 p = *fnamep;
19485
19486 has_fullname = 0;
19487
19488 if (p != NULL)
19489 {
19490 if (c == '.')
19491 {
19492 mch_dirname(dirname, MAXPATHL);
19493 s = shorten_fname(p, dirname);
19494 if (s != NULL)
19495 {
19496 *fnamep = s;
19497 if (pbuf != NULL)
19498 {
19499 vim_free(*bufp); /* free any allocated file name */
19500 *bufp = pbuf;
19501 pbuf = NULL;
19502 }
19503 }
19504 }
19505 else
19506 {
19507 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19508 /* Only replace it when it starts with '~' */
19509 if (*dirname == '~')
19510 {
19511 s = vim_strsave(dirname);
19512 if (s != NULL)
19513 {
19514 *fnamep = s;
19515 vim_free(*bufp);
19516 *bufp = s;
19517 }
19518 }
19519 }
19520 vim_free(pbuf);
19521 }
19522 }
19523
19524 tail = gettail(*fnamep);
19525 *fnamelen = (int)STRLEN(*fnamep);
19526
19527 /* ":h" - head, remove "/file_name", can be repeated */
19528 /* Don't remove the first "/" or "c:\" */
19529 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19530 {
19531 valid |= VALID_HEAD;
19532 *usedlen += 2;
19533 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019534 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 --tail;
19536 *fnamelen = (int)(tail - *fnamep);
19537#ifdef VMS
19538 if (*fnamelen > 0)
19539 *fnamelen += 1; /* the path separator is part of the path */
19540#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019541 while (tail > s && !after_pathsep(s, tail))
19542 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 }
19544
19545 /* ":8" - shortname */
19546 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19547 {
19548 *usedlen += 2;
19549#ifdef WIN3264
19550 has_shortname = 1;
19551#endif
19552 }
19553
19554#ifdef WIN3264
19555 /* Check shortname after we have done 'heads' and before we do 'tails'
19556 */
19557 if (has_shortname)
19558 {
19559 pbuf = NULL;
19560 /* Copy the string if it is shortened by :h */
19561 if (*fnamelen < (int)STRLEN(*fnamep))
19562 {
19563 p = vim_strnsave(*fnamep, *fnamelen);
19564 if (p == 0)
19565 return -1;
19566 vim_free(*bufp);
19567 *bufp = *fnamep = p;
19568 }
19569
19570 /* Split into two implementations - makes it easier. First is where
19571 * there isn't a full name already, second is where there is.
19572 */
19573 if (!has_fullname && !vim_isAbsName(*fnamep))
19574 {
19575 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19576 return -1;
19577 }
19578 else
19579 {
19580 int l;
19581
19582 /* Simple case, already have the full-name
19583 * Nearly always shorter, so try first time. */
19584 l = *fnamelen;
19585 if (!get_short_pathname(fnamep, bufp, &l))
19586 return -1;
19587
19588 if (l == 0)
19589 {
19590 /* Couldn't find the filename.. search the paths.
19591 */
19592 l = *fnamelen;
19593 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19594 return -1;
19595 }
19596 *fnamelen = l;
19597 }
19598 }
19599#endif /* WIN3264 */
19600
19601 /* ":t" - tail, just the basename */
19602 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19603 {
19604 *usedlen += 2;
19605 *fnamelen -= (int)(tail - *fnamep);
19606 *fnamep = tail;
19607 }
19608
19609 /* ":e" - extension, can be repeated */
19610 /* ":r" - root, without extension, can be repeated */
19611 while (src[*usedlen] == ':'
19612 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19613 {
19614 /* find a '.' in the tail:
19615 * - for second :e: before the current fname
19616 * - otherwise: The last '.'
19617 */
19618 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19619 s = *fnamep - 2;
19620 else
19621 s = *fnamep + *fnamelen - 1;
19622 for ( ; s > tail; --s)
19623 if (s[0] == '.')
19624 break;
19625 if (src[*usedlen + 1] == 'e') /* :e */
19626 {
19627 if (s > tail)
19628 {
19629 *fnamelen += (int)(*fnamep - (s + 1));
19630 *fnamep = s + 1;
19631#ifdef VMS
19632 /* cut version from the extension */
19633 s = *fnamep + *fnamelen - 1;
19634 for ( ; s > *fnamep; --s)
19635 if (s[0] == ';')
19636 break;
19637 if (s > *fnamep)
19638 *fnamelen = s - *fnamep;
19639#endif
19640 }
19641 else if (*fnamep <= tail)
19642 *fnamelen = 0;
19643 }
19644 else /* :r */
19645 {
19646 if (s > tail) /* remove one extension */
19647 *fnamelen = (int)(s - *fnamep);
19648 }
19649 *usedlen += 2;
19650 }
19651
19652 /* ":s?pat?foo?" - substitute */
19653 /* ":gs?pat?foo?" - global substitute */
19654 if (src[*usedlen] == ':'
19655 && (src[*usedlen + 1] == 's'
19656 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19657 {
19658 char_u *str;
19659 char_u *pat;
19660 char_u *sub;
19661 int sep;
19662 char_u *flags;
19663 int didit = FALSE;
19664
19665 flags = (char_u *)"";
19666 s = src + *usedlen + 2;
19667 if (src[*usedlen + 1] == 'g')
19668 {
19669 flags = (char_u *)"g";
19670 ++s;
19671 }
19672
19673 sep = *s++;
19674 if (sep)
19675 {
19676 /* find end of pattern */
19677 p = vim_strchr(s, sep);
19678 if (p != NULL)
19679 {
19680 pat = vim_strnsave(s, (int)(p - s));
19681 if (pat != NULL)
19682 {
19683 s = p + 1;
19684 /* find end of substitution */
19685 p = vim_strchr(s, sep);
19686 if (p != NULL)
19687 {
19688 sub = vim_strnsave(s, (int)(p - s));
19689 str = vim_strnsave(*fnamep, *fnamelen);
19690 if (sub != NULL && str != NULL)
19691 {
19692 *usedlen = (int)(p + 1 - src);
19693 s = do_string_sub(str, pat, sub, flags);
19694 if (s != NULL)
19695 {
19696 *fnamep = s;
19697 *fnamelen = (int)STRLEN(s);
19698 vim_free(*bufp);
19699 *bufp = s;
19700 didit = TRUE;
19701 }
19702 }
19703 vim_free(sub);
19704 vim_free(str);
19705 }
19706 vim_free(pat);
19707 }
19708 }
19709 /* after using ":s", repeat all the modifiers */
19710 if (didit)
19711 goto repeat;
19712 }
19713 }
19714
19715 return valid;
19716}
19717
19718/*
19719 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19720 * "flags" can be "g" to do a global substitute.
19721 * Returns an allocated string, NULL for error.
19722 */
19723 char_u *
19724do_string_sub(str, pat, sub, flags)
19725 char_u *str;
19726 char_u *pat;
19727 char_u *sub;
19728 char_u *flags;
19729{
19730 int sublen;
19731 regmatch_T regmatch;
19732 int i;
19733 int do_all;
19734 char_u *tail;
19735 garray_T ga;
19736 char_u *ret;
19737 char_u *save_cpo;
19738
19739 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19740 save_cpo = p_cpo;
19741 p_cpo = (char_u *)"";
19742
19743 ga_init2(&ga, 1, 200);
19744
19745 do_all = (flags[0] == 'g');
19746
19747 regmatch.rm_ic = p_ic;
19748 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19749 if (regmatch.regprog != NULL)
19750 {
19751 tail = str;
19752 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19753 {
19754 /*
19755 * Get some space for a temporary buffer to do the substitution
19756 * into. It will contain:
19757 * - The text up to where the match is.
19758 * - The substituted text.
19759 * - The text after the match.
19760 */
19761 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19762 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19763 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19764 {
19765 ga_clear(&ga);
19766 break;
19767 }
19768
19769 /* copy the text up to where the match is */
19770 i = (int)(regmatch.startp[0] - tail);
19771 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19772 /* add the substituted text */
19773 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19774 + ga.ga_len + i, TRUE, TRUE, FALSE);
19775 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 /* avoid getting stuck on a match with an empty string */
19777 if (tail == regmatch.endp[0])
19778 {
19779 if (*tail == NUL)
19780 break;
19781 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19782 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 }
19784 else
19785 {
19786 tail = regmatch.endp[0];
19787 if (*tail == NUL)
19788 break;
19789 }
19790 if (!do_all)
19791 break;
19792 }
19793
19794 if (ga.ga_data != NULL)
19795 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19796
19797 vim_free(regmatch.regprog);
19798 }
19799
19800 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19801 ga_clear(&ga);
19802 p_cpo = save_cpo;
19803
19804 return ret;
19805}
19806
19807#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */