blob: eef865a4bddadecef5f305cf27268ca81a0b5b84 [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));
657static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void init_tv __ARGS((typval_T *varp));
659static long get_tv_number __ARGS((typval_T *varp));
660static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000661static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static char_u *get_tv_string __ARGS((typval_T *varp));
663static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000666static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
668static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
669static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
670static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
671static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
672static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
673static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000674static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000676static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
678static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
679static int eval_fname_script __ARGS((char_u *p));
680static int eval_fname_sid __ARGS((char_u *p));
681static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static ufunc_T *find_func __ARGS((char_u *name));
683static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000684static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000685#ifdef FEAT_PROFILE
686static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000687static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
688static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
689static int
690# ifdef __BORLANDC__
691 _RTLENTRYF
692# endif
693 prof_total_cmp __ARGS((const void *s1, const void *s2));
694static int
695# ifdef __BORLANDC__
696 _RTLENTRYF
697# endif
698 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000700static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000701static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000702static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void func_free __ARGS((ufunc_T *fp));
704static void func_unref __ARGS((char_u *name));
705static void func_ref __ARGS((char_u *name));
706static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
707static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
708
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000709/* Character used as separated in autoload function/variable names. */
710#define AUTOLOAD_CHAR '#'
711
Bram Moolenaar33570922005-01-25 22:26:29 +0000712/*
713 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000714 */
715 void
716eval_init()
717{
Bram Moolenaar33570922005-01-25 22:26:29 +0000718 int i;
719 struct vimvar *p;
720
721 init_var_dict(&globvardict, &globvars_var);
722 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000723 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000724 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000725
726 for (i = 0; i < VV_LEN; ++i)
727 {
728 p = &vimvars[i];
729 STRCPY(p->vv_di.di_key, p->vv_name);
730 if (p->vv_flags & VV_RO)
731 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
732 else if (p->vv_flags & VV_RO_SBX)
733 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
734 else
735 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000736
737 /* add to v: scope dict, unless the value is not always available */
738 if (p->vv_type != VAR_UNKNOWN)
739 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000740 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000741 /* add to compat scope dict */
742 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000743 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000744}
745
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000746#if defined(EXITFREE) || defined(PROTO)
747 void
748eval_clear()
749{
750 int i;
751 struct vimvar *p;
752
753 for (i = 0; i < VV_LEN; ++i)
754 {
755 p = &vimvars[i];
756 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000757 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000758 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000759 p->vv_di.di_tv.vval.v_string = NULL;
760 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000761 }
762 hash_clear(&vimvarht);
763 hash_clear(&compat_hashtab);
764
765 /* script-local variables */
766 for (i = 1; i <= ga_scripts.ga_len; ++i)
767 vars_clear(&SCRIPT_VARS(i));
768 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000769 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000770
771 /* global variables */
772 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000773
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000774 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000775 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000776 hash_clear(&func_hashtab);
777
778 /* unreferenced lists and dicts */
779 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000780}
781#endif
782
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * Return the name of the executed function.
785 */
786 char_u *
787func_name(cookie)
788 void *cookie;
789{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000790 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792
793/*
794 * Return the address holding the next breakpoint line for a funccall cookie.
795 */
796 linenr_T *
797func_breakpoint(cookie)
798 void *cookie;
799{
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
803/*
804 * Return the address holding the debug tick for a funccall cookie.
805 */
806 int *
807func_dbg_tick(cookie)
808 void *cookie;
809{
Bram Moolenaar33570922005-01-25 22:26:29 +0000810 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811}
812
813/*
814 * Return the nesting level for a funccall cookie.
815 */
816 int
817func_level(cookie)
818 void *cookie;
819{
Bram Moolenaar33570922005-01-25 22:26:29 +0000820 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822
823/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000824funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826/*
827 * Return TRUE when a function was ended by a ":return" command.
828 */
829 int
830current_func_returned()
831{
832 return current_funccal->returned;
833}
834
835
836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 * Set an internal variable to a string value. Creates the variable if it does
838 * not already exist.
839 */
840 void
841set_internal_string_var(name, value)
842 char_u *name;
843 char_u *value;
844{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000845 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
848 val = vim_strsave(value);
849 if (val != NULL)
850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000852 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000854 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857 }
858}
859
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000860static lval_T *redir_lval = NULL;
861static char_u *redir_endp = NULL;
862static char_u *redir_varname = NULL;
863
864/*
865 * Start recording command output to a variable
866 * Returns OK if successfully completed the setup. FAIL otherwise.
867 */
868 int
869var_redir_start(name, append)
870 char_u *name;
871 int append; /* append to an existing variable */
872{
873 int save_emsg;
874 int err;
875 typval_T tv;
876
877 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000878 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000879 {
880 EMSG(_(e_invarg));
881 return FAIL;
882 }
883
884 redir_varname = vim_strsave(name);
885 if (redir_varname == NULL)
886 return FAIL;
887
888 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
889 if (redir_lval == NULL)
890 {
891 var_redir_stop();
892 return FAIL;
893 }
894
895 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000896 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
897 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
899 {
900 if (redir_endp != NULL && *redir_endp != NUL)
901 /* Trailing characters are present after the variable name */
902 EMSG(_(e_trailing));
903 else
904 EMSG(_(e_invarg));
905 var_redir_stop();
906 return FAIL;
907 }
908
909 /* check if we can write to the variable: set it to or append an empty
910 * string */
911 save_emsg = did_emsg;
912 did_emsg = FALSE;
913 tv.v_type = VAR_STRING;
914 tv.vval.v_string = (char_u *)"";
915 if (append)
916 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
917 else
918 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
919 err = did_emsg;
920 did_emsg += save_emsg;
921 if (err)
922 {
923 var_redir_stop();
924 return FAIL;
925 }
926 if (redir_lval->ll_newkey != NULL)
927 {
928 /* Dictionary item was created, don't do it again. */
929 vim_free(redir_lval->ll_newkey);
930 redir_lval->ll_newkey = NULL;
931 }
932
933 return OK;
934}
935
936/*
937 * Append "value[len]" to the variable set by var_redir_start().
938 */
939 void
940var_redir_str(value, len)
941 char_u *value;
942 int len;
943{
944 char_u *val;
945 typval_T tv;
946 int save_emsg;
947 int err;
948
949 if (redir_lval == NULL)
950 return;
951
952 if (len == -1)
953 /* Append the entire string */
954 val = vim_strsave(value);
955 else
956 /* Append only the specified number of characters */
957 val = vim_strnsave(value, len);
958 if (val == NULL)
959 return;
960
961 tv.v_type = VAR_STRING;
962 tv.vval.v_string = val;
963
964 save_emsg = did_emsg;
965 did_emsg = FALSE;
966 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
967 err = did_emsg;
968 did_emsg += save_emsg;
969 if (err)
970 var_redir_stop();
971
972 vim_free(tv.vval.v_string);
973}
974
975/*
976 * Stop redirecting command output to a variable.
977 */
978 void
979var_redir_stop()
980{
981 if (redir_lval != NULL)
982 {
983 clear_lval(redir_lval);
984 vim_free(redir_lval);
985 redir_lval = NULL;
986 }
987 vim_free(redir_varname);
988 redir_varname = NULL;
989}
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991# if defined(FEAT_MBYTE) || defined(PROTO)
992 int
993eval_charconvert(enc_from, enc_to, fname_from, fname_to)
994 char_u *enc_from;
995 char_u *enc_to;
996 char_u *fname_from;
997 char_u *fname_to;
998{
999 int err = FALSE;
1000
1001 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1002 set_vim_var_string(VV_CC_TO, enc_to, -1);
1003 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1004 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1005 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1006 err = TRUE;
1007 set_vim_var_string(VV_CC_FROM, NULL, -1);
1008 set_vim_var_string(VV_CC_TO, NULL, -1);
1009 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1010 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1011
1012 if (err)
1013 return FAIL;
1014 return OK;
1015}
1016# endif
1017
1018# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1019 int
1020eval_printexpr(fname, args)
1021 char_u *fname;
1022 char_u *args;
1023{
1024 int err = FALSE;
1025
1026 set_vim_var_string(VV_FNAME_IN, fname, -1);
1027 set_vim_var_string(VV_CMDARG, args, -1);
1028 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1029 err = TRUE;
1030 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1031 set_vim_var_string(VV_CMDARG, NULL, -1);
1032
1033 if (err)
1034 {
1035 mch_remove(fname);
1036 return FAIL;
1037 }
1038 return OK;
1039}
1040# endif
1041
1042# if defined(FEAT_DIFF) || defined(PROTO)
1043 void
1044eval_diff(origfile, newfile, outfile)
1045 char_u *origfile;
1046 char_u *newfile;
1047 char_u *outfile;
1048{
1049 int err = FALSE;
1050
1051 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1052 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1053 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1054 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1055 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1056 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1057 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1058}
1059
1060 void
1061eval_patch(origfile, difffile, outfile)
1062 char_u *origfile;
1063 char_u *difffile;
1064 char_u *outfile;
1065{
1066 int err;
1067
1068 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1069 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1070 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1071 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1072 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1073 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1074 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1075}
1076# endif
1077
1078/*
1079 * Top level evaluation function, returning a boolean.
1080 * Sets "error" to TRUE if there was an error.
1081 * Return TRUE or FALSE.
1082 */
1083 int
1084eval_to_bool(arg, error, nextcmd, skip)
1085 char_u *arg;
1086 int *error;
1087 char_u **nextcmd;
1088 int skip; /* only parse, don't execute */
1089{
Bram Moolenaar33570922005-01-25 22:26:29 +00001090 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 int retval = FALSE;
1092
1093 if (skip)
1094 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001095 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 else
1098 {
1099 *error = FALSE;
1100 if (!skip)
1101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001102 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001103 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105 }
1106 if (skip)
1107 --emsg_skip;
1108
1109 return retval;
1110}
1111
1112/*
1113 * Top level evaluation function, returning a string. If "skip" is TRUE,
1114 * only parsing to "nextcmd" is done, without reporting errors. Return
1115 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1116 */
1117 char_u *
1118eval_to_string_skip(arg, nextcmd, skip)
1119 char_u *arg;
1120 char_u **nextcmd;
1121 int skip; /* only parse, don't execute */
1122{
Bram Moolenaar33570922005-01-25 22:26:29 +00001123 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 char_u *retval;
1125
1126 if (skip)
1127 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 retval = NULL;
1130 else
1131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001132 retval = vim_strsave(get_tv_string(&tv));
1133 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 if (skip)
1136 --emsg_skip;
1137
1138 return retval;
1139}
1140
1141/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001142 * Skip over an expression at "*pp".
1143 * Return FAIL for an error, OK otherwise.
1144 */
1145 int
1146skip_expr(pp)
1147 char_u **pp;
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001150
1151 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001153}
1154
1155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * Top level evaluation function, returning a string.
1157 * Return pointer to allocated memory, or NULL for failure.
1158 */
1159 char_u *
1160eval_to_string(arg, nextcmd)
1161 char_u *arg;
1162 char_u **nextcmd;
1163{
Bram Moolenaar33570922005-01-25 22:26:29 +00001164 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 char_u *retval;
1166
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 retval = NULL;
1169 else
1170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 retval = vim_strsave(get_tv_string(&tv));
1172 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174
1175 return retval;
1176}
1177
1178/*
1179 * Call eval_to_string() with "sandbox" set and not using local variables.
1180 */
1181 char_u *
1182eval_to_string_safe(arg, nextcmd)
1183 char_u *arg;
1184 char_u **nextcmd;
1185{
1186 char_u *retval;
1187 void *save_funccalp;
1188
1189 save_funccalp = save_funccal();
1190 ++sandbox;
1191 retval = eval_to_string(arg, nextcmd);
1192 --sandbox;
1193 restore_funccal(save_funccalp);
1194 return retval;
1195}
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197/*
1198 * Top level evaluation function, returning a number.
1199 * Evaluates "expr" silently.
1200 * Returns -1 for an error.
1201 */
1202 int
1203eval_to_number(expr)
1204 char_u *expr;
1205{
Bram Moolenaar33570922005-01-25 22:26:29 +00001206 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001208 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
1210 ++emsg_off;
1211
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 retval = -1;
1214 else
1215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001216 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 --emsg_off;
1220
1221 return retval;
1222}
1223
Bram Moolenaara40058a2005-07-11 22:42:07 +00001224/*
1225 * Prepare v: variable "idx" to be used.
1226 * Save the current typeval in "save_tv".
1227 * When not used yet add the variable to the v: hashtable.
1228 */
1229 static void
1230prepare_vimvar(idx, save_tv)
1231 int idx;
1232 typval_T *save_tv;
1233{
1234 *save_tv = vimvars[idx].vv_tv;
1235 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1236 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1237}
1238
1239/*
1240 * Restore v: variable "idx" to typeval "save_tv".
1241 * When no longer defined, remove the variable from the v: hashtable.
1242 */
1243 static void
1244restore_vimvar(idx, save_tv)
1245 int idx;
1246 typval_T *save_tv;
1247{
1248 hashitem_T *hi;
1249
1250 clear_tv(&vimvars[idx].vv_tv);
1251 vimvars[idx].vv_tv = *save_tv;
1252 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1253 {
1254 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1255 if (HASHITEM_EMPTY(hi))
1256 EMSG2(_(e_intern2), "restore_vimvar()");
1257 else
1258 hash_remove(&vimvarht, hi);
1259 }
1260}
1261
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001262#if defined(FEAT_SYN_HL) || defined(PROTO)
1263/*
1264 * Evaluate an expression to a list with suggestions.
1265 * For the "expr:" part of 'spellsuggest'.
1266 */
1267 list_T *
1268eval_spell_expr(badword, expr)
1269 char_u *badword;
1270 char_u *expr;
1271{
1272 typval_T save_val;
1273 typval_T rettv;
1274 list_T *list = NULL;
1275 char_u *p = skipwhite(expr);
1276
1277 /* Set "v:val" to the bad word. */
1278 prepare_vimvar(VV_VAL, &save_val);
1279 vimvars[VV_VAL].vv_type = VAR_STRING;
1280 vimvars[VV_VAL].vv_str = badword;
1281 if (p_verbose == 0)
1282 ++emsg_off;
1283
1284 if (eval1(&p, &rettv, TRUE) == OK)
1285 {
1286 if (rettv.v_type != VAR_LIST)
1287 clear_tv(&rettv);
1288 else
1289 list = rettv.vval.v_list;
1290 }
1291
1292 if (p_verbose == 0)
1293 --emsg_off;
1294 vimvars[VV_VAL].vv_str = NULL;
1295 restore_vimvar(VV_VAL, &save_val);
1296
1297 return list;
1298}
1299
1300/*
1301 * "list" is supposed to contain two items: a word and a number. Return the
1302 * word in "pp" and the number as the return value.
1303 * Return -1 if anything isn't right.
1304 * Used to get the good word and score from the eval_spell_expr() result.
1305 */
1306 int
1307get_spellword(list, pp)
1308 list_T *list;
1309 char_u **pp;
1310{
1311 listitem_T *li;
1312
1313 li = list->lv_first;
1314 if (li == NULL)
1315 return -1;
1316 *pp = get_tv_string(&li->li_tv);
1317
1318 li = li->li_next;
1319 if (li == NULL)
1320 return -1;
1321 return get_tv_number(&li->li_tv);
1322}
1323#endif
1324
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001325/*
1326 * Top level evaluation function,
1327 */
1328 typval_T *
1329eval_expr(arg, nextcmd)
1330 char_u *arg;
1331 char_u **nextcmd;
1332{
1333 typval_T *tv;
1334
1335 tv = (typval_T *)alloc(sizeof(typval_T));
1336 if (!tv)
1337 return NULL;
1338
1339 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1340 {
1341 vim_free(tv);
1342 return NULL;
1343 }
1344
1345 return tv;
1346}
1347
1348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1350/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001351 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001353 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001355 static int
1356call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *func;
1358 int argc;
1359 char_u **argv;
1360 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
Bram Moolenaar33570922005-01-25 22:26:29 +00001363 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 long n;
1365 int len;
1366 int i;
1367 int doesrange;
1368 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001369 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001373 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 for (i = 0; i < argc; i++)
1376 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001377 /* Pass a NULL or empty argument as an empty string */
1378 if (argv[i] == NULL || *argv[i] == NUL)
1379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001380 argvars[i].v_type = VAR_STRING;
1381 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001382 continue;
1383 }
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /* Recognize a number argument, the others must be strings. */
1386 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1387 if (len != 0 && len == (int)STRLEN(argv[i]))
1388 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001389 argvars[i].v_type = VAR_NUMBER;
1390 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 else
1393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001394 argvars[i].v_type = VAR_STRING;
1395 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397 }
1398
1399 if (safe)
1400 {
1401 save_funccalp = save_funccal();
1402 ++sandbox;
1403 }
1404
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1406 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (safe)
1410 {
1411 --sandbox;
1412 restore_funccal(save_funccalp);
1413 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 vim_free(argvars);
1415
1416 if (ret == FAIL)
1417 clear_tv(rettv);
1418
1419 return ret;
1420}
1421
1422/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001423 * Call vimL function "func" and return the result as a string.
1424 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 * Uses argv[argc] for the function arguments.
1426 */
1427 void *
1428call_func_retstr(func, argc, argv, safe)
1429 char_u *func;
1430 int argc;
1431 char_u **argv;
1432 int safe; /* use the sandbox */
1433{
1434 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001435 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001436
1437 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1438 return NULL;
1439
1440 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 return retval;
1443}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001445#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001446/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001447 * Call vimL function "func" and return the result as a number.
1448 * Returns -1 when calling the function fails.
1449 * Uses argv[argc] for the function arguments.
1450 */
1451 long
1452call_func_retnr(func, argc, argv, safe)
1453 char_u *func;
1454 int argc;
1455 char_u **argv;
1456 int safe; /* use the sandbox */
1457{
1458 typval_T rettv;
1459 long retval;
1460
1461 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1462 return -1;
1463
1464 retval = get_tv_number_chk(&rettv, NULL);
1465 clear_tv(&rettv);
1466 return retval;
1467}
1468#endif
1469
1470/*
1471 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472 * Uses argv[argc] for the function arguments.
1473 */
1474 void *
1475call_func_retlist(func, argc, argv, safe)
1476 char_u *func;
1477 int argc;
1478 char_u **argv;
1479 int safe; /* use the sandbox */
1480{
1481 typval_T rettv;
1482
1483 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1484 return NULL;
1485
1486 if (rettv.v_type != VAR_LIST)
1487 {
1488 clear_tv(&rettv);
1489 return NULL;
1490 }
1491
1492 return rettv.vval.v_list;
1493}
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495#endif
1496
1497/*
1498 * Save the current function call pointer, and set it to NULL.
1499 * Used when executing autocommands and for ":source".
1500 */
1501 void *
1502save_funccal()
1503{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001504 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 current_funccal = NULL;
1507 return (void *)fc;
1508}
1509
1510 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001511restore_funccal(vfc)
1512 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001514 funccall_T *fc = (funccall_T *)vfc;
1515
1516 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517}
1518
Bram Moolenaar05159a02005-02-26 23:04:13 +00001519#if defined(FEAT_PROFILE) || defined(PROTO)
1520/*
1521 * Prepare profiling for entering a child or something else that is not
1522 * counted for the script/function itself.
1523 * Should always be called in pair with prof_child_exit().
1524 */
1525 void
1526prof_child_enter(tm)
1527 proftime_T *tm; /* place to store waittime */
1528{
1529 funccall_T *fc = current_funccal;
1530
1531 if (fc != NULL && fc->func->uf_profiling)
1532 profile_start(&fc->prof_child);
1533 script_prof_save(tm);
1534}
1535
1536/*
1537 * Take care of time spent in a child.
1538 * Should always be called after prof_child_enter().
1539 */
1540 void
1541prof_child_exit(tm)
1542 proftime_T *tm; /* where waittime was stored */
1543{
1544 funccall_T *fc = current_funccal;
1545
1546 if (fc != NULL && fc->func->uf_profiling)
1547 {
1548 profile_end(&fc->prof_child);
1549 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1550 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1551 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1552 }
1553 script_prof_restore(tm);
1554}
1555#endif
1556
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558#ifdef FEAT_FOLDING
1559/*
1560 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1561 * it in "*cp". Doesn't give error messages.
1562 */
1563 int
1564eval_foldexpr(arg, cp)
1565 char_u *arg;
1566 int *cp;
1567{
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 int retval;
1570 char_u *s;
1571
1572 ++emsg_off;
1573 ++sandbox;
1574 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 retval = 0;
1577 else
1578 {
1579 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001580 if (tv.v_type == VAR_NUMBER)
1581 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001582 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 retval = 0;
1584 else
1585 {
1586 /* If the result is a string, check if there is a non-digit before
1587 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (!VIM_ISDIGIT(*s) && *s != '-')
1590 *cp = *s++;
1591 retval = atol((char *)s);
1592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 --emsg_off;
1596 --sandbox;
1597
1598 return retval;
1599}
1600#endif
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001603 * ":let" list all variable values
1604 * ":let var1 var2" list variable values
1605 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 * ":let var += expr" assignment command.
1607 * ":let var -= expr" assignment command.
1608 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 */
1611 void
1612ex_let(eap)
1613 exarg_T *eap;
1614{
1615 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001617 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619 int var_count = 0;
1620 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 expr = skip_var_list(arg, &var_count, &semicolon);
1624 if (expr == NULL)
1625 return;
1626 expr = vim_strchr(expr, '=');
1627 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001629 /*
1630 * ":let" without "=": list variables
1631 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 if (*arg == '[')
1633 EMSG(_(e_invarg));
1634 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 /* ":let var1 var2" */
1636 arg = list_arg_vars(eap, arg);
1637 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001640 list_glob_vars();
1641 list_buf_vars();
1642 list_win_vars();
1643 list_vim_vars();
1644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 eap->nextcmd = check_nextcmd(arg);
1646 }
1647 else
1648 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001649 op[0] = '=';
1650 op[1] = NUL;
1651 if (expr > arg)
1652 {
1653 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1654 op[0] = expr[-1]; /* +=, -= or .= */
1655 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (eap->skip)
1659 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (eap->skip)
1662 {
1663 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 --emsg_skip;
1666 }
1667 else if (i != FAIL)
1668 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673 }
1674}
1675
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676/*
1677 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1678 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 * When "nextchars" is not NULL it points to a string with characters that
1680 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1681 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 * Returns OK or FAIL;
1683 */
1684 static int
1685ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1686 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 int copy; /* copy values from "tv", don't move */
1689 int semicolon; /* from skip_var_list() */
1690 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692{
1693 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 listitem_T *item;
1697 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698
1699 if (*arg != '[')
1700 {
1701 /*
1702 * ":let var = expr" or ":for var in list"
1703 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001704 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 return FAIL;
1706 return OK;
1707 }
1708
1709 /*
1710 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1711 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001712 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 {
1714 EMSG(_(e_listreq));
1715 return FAIL;
1716 }
1717
1718 i = list_len(l);
1719 if (semicolon == 0 && var_count < i)
1720 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001721 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 return FAIL;
1723 }
1724 if (var_count - semicolon > i)
1725 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001726 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 return FAIL;
1728 }
1729
1730 item = l->lv_first;
1731 while (*arg != ']')
1732 {
1733 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001734 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 item = item->li_next;
1736 if (arg == NULL)
1737 return FAIL;
1738
1739 arg = skipwhite(arg);
1740 if (*arg == ';')
1741 {
1742 /* Put the rest of the list (may be empty) in the var after ';'.
1743 * Create a new list for this. */
1744 l = list_alloc();
1745 if (l == NULL)
1746 return FAIL;
1747 while (item != NULL)
1748 {
1749 list_append_tv(l, &item->li_tv);
1750 item = item->li_next;
1751 }
1752
1753 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001754 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 ltv.vval.v_list = l;
1756 l->lv_refcount = 1;
1757
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1759 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 clear_tv(&ltv);
1761 if (arg == NULL)
1762 return FAIL;
1763 break;
1764 }
1765 else if (*arg != ',' && *arg != ']')
1766 {
1767 EMSG2(_(e_intern2), "ex_let_vars()");
1768 return FAIL;
1769 }
1770 }
1771
1772 return OK;
1773}
1774
1775/*
1776 * Skip over assignable variable "var" or list of variables "[var, var]".
1777 * Used for ":let varvar = expr" and ":for varvar in expr".
1778 * For "[var, var]" increment "*var_count" for each variable.
1779 * for "[var, var; var]" set "semicolon".
1780 * Return NULL for an error.
1781 */
1782 static char_u *
1783skip_var_list(arg, var_count, semicolon)
1784 char_u *arg;
1785 int *var_count;
1786 int *semicolon;
1787{
1788 char_u *p, *s;
1789
1790 if (*arg == '[')
1791 {
1792 /* "[var, var]": find the matching ']'. */
1793 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001794 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 {
1796 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1797 s = skip_var_one(p);
1798 if (s == p)
1799 {
1800 EMSG2(_(e_invarg2), p);
1801 return NULL;
1802 }
1803 ++*var_count;
1804
1805 p = skipwhite(s);
1806 if (*p == ']')
1807 break;
1808 else if (*p == ';')
1809 {
1810 if (*semicolon == 1)
1811 {
1812 EMSG(_("Double ; in list of variables"));
1813 return NULL;
1814 }
1815 *semicolon = 1;
1816 }
1817 else if (*p != ',')
1818 {
1819 EMSG2(_(e_invarg2), p);
1820 return NULL;
1821 }
1822 }
1823 return p + 1;
1824 }
1825 else
1826 return skip_var_one(arg);
1827}
1828
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001829/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001830 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1831 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001832 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833 static char_u *
1834skip_var_one(arg)
1835 char_u *arg;
1836{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001837 if (*arg == '@' && arg[1] != NUL)
1838 return arg + 2;
1839 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1840 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841}
1842
Bram Moolenaara7043832005-01-21 11:56:39 +00001843/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 * List variables for hashtab "ht" with prefix "prefix".
1845 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001846 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001848list_hashtable_vars(ht, prefix, empty)
1849 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001850 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001852{
Bram Moolenaar33570922005-01-25 22:26:29 +00001853 hashitem_T *hi;
1854 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001855 int todo;
1856
1857 todo = ht->ht_used;
1858 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1859 {
1860 if (!HASHITEM_EMPTY(hi))
1861 {
1862 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001863 di = HI2DI(hi);
1864 if (empty || di->di_tv.v_type != VAR_STRING
1865 || di->di_tv.vval.v_string != NULL)
1866 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 }
1868 }
1869}
1870
1871/*
1872 * List global variables.
1873 */
1874 static void
1875list_glob_vars()
1876{
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001878}
1879
1880/*
1881 * List buffer variables.
1882 */
1883 static void
1884list_buf_vars()
1885{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001886 char_u numbuf[NUMBUFLEN];
1887
Bram Moolenaar33570922005-01-25 22:26:29 +00001888 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001889
1890 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1891 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001892}
1893
1894/*
1895 * List window variables.
1896 */
1897 static void
1898list_win_vars()
1899{
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001901}
1902
1903/*
1904 * List Vim variables.
1905 */
1906 static void
1907list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908{
Bram Moolenaar33570922005-01-25 22:26:29 +00001909 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910}
1911
1912/*
1913 * List variables in "arg".
1914 */
1915 static char_u *
1916list_arg_vars(eap, arg)
1917 exarg_T *eap;
1918 char_u *arg;
1919{
1920 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001923 char_u *name_start;
1924 char_u *arg_subsc;
1925 char_u *tofree;
1926 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927
1928 while (!ends_excmd(*arg) && !got_int)
1929 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001930 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001932 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001933 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1934 {
1935 emsg_severe = TRUE;
1936 EMSG(_(e_trailing));
1937 break;
1938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 /* get_name_len() takes care of expanding curly braces */
1943 name_start = name = arg;
1944 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1945 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001947 /* This is mainly to keep test 49 working: when expanding
1948 * curly braces fails overrule the exception error message. */
1949 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001951 emsg_severe = TRUE;
1952 EMSG2(_(e_invarg2), arg);
1953 break;
1954 }
1955 error = TRUE;
1956 }
1957 else
1958 {
1959 if (tofree != NULL)
1960 name = tofree;
1961 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 else
1964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 /* handle d.key, l[idx], f(expr) */
1966 arg_subsc = arg;
1967 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001971 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001972 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001973 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001974 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001975 case 'g': list_glob_vars(); break;
1976 case 'b': list_buf_vars(); break;
1977 case 'w': list_win_vars(); break;
1978 case 'v': list_vim_vars(); break;
1979 default:
1980 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001981 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001982 }
1983 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 {
1985 char_u numbuf[NUMBUFLEN];
1986 char_u *tf;
1987 int c;
1988 char_u *s;
1989
1990 s = echo_string(&tv, &tf, numbuf);
1991 c = *arg;
1992 *arg = NUL;
1993 list_one_var_a((char_u *)"",
1994 arg == arg_subsc ? name : name_start,
1995 tv.v_type, s == NULL ? (char_u *)"" : s);
1996 *arg = c;
1997 vim_free(tf);
1998 }
1999 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 }
2002 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003
2004 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002006
2007 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002008 }
2009
2010 return arg;
2011}
2012
2013/*
2014 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2015 * Returns a pointer to the char just after the var name.
2016 * Returns NULL if there is an error.
2017 */
2018 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002021 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025{
2026 int c1;
2027 char_u *name;
2028 char_u *p;
2029 char_u *arg_end = NULL;
2030 int len;
2031 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033
2034 /*
2035 * ":let $VAR = expr": Set environment variable.
2036 */
2037 if (*arg == '$')
2038 {
2039 /* Find the end of the name. */
2040 ++arg;
2041 name = arg;
2042 len = get_env_len(&arg);
2043 if (len == 0)
2044 EMSG2(_(e_invarg2), name - 1);
2045 else
2046 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 if (op != NULL && (*op == '+' || *op == '-'))
2048 EMSG2(_(e_letwrong), op);
2049 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 EMSG(_(e_letunexp));
2052 else
2053 {
2054 c1 = name[len];
2055 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002056 p = get_tv_string_chk(tv);
2057 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002058 {
2059 int mustfree = FALSE;
2060 char_u *s = vim_getenv(name, &mustfree);
2061
2062 if (s != NULL)
2063 {
2064 p = tofree = concat_str(s, p);
2065 if (mustfree)
2066 vim_free(s);
2067 }
2068 }
2069 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002070 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002071 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002072 if (STRICMP(name, "HOME") == 0)
2073 init_homedir();
2074 else if (didset_vim && STRICMP(name, "VIM") == 0)
2075 didset_vim = FALSE;
2076 else if (didset_vimruntime
2077 && STRICMP(name, "VIMRUNTIME") == 0)
2078 didset_vimruntime = FALSE;
2079 arg_end = arg;
2080 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002082 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 }
2084 }
2085 }
2086
2087 /*
2088 * ":let &option = expr": Set option value.
2089 * ":let &l:option = expr": Set local option value.
2090 * ":let &g:option = expr": Set global option value.
2091 */
2092 else if (*arg == '&')
2093 {
2094 /* Find the end of the name. */
2095 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002096 if (p == NULL || (endchars != NULL
2097 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 EMSG(_(e_letunexp));
2099 else
2100 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 long n;
2102 int opt_type;
2103 long numval;
2104 char_u *stringval = NULL;
2105 char_u *s;
2106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 c1 = *p;
2108 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002109
2110 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002111 s = get_tv_string_chk(tv); /* != NULL if number or string */
2112 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002113 {
2114 opt_type = get_option_value(arg, &numval,
2115 &stringval, opt_flags);
2116 if ((opt_type == 1 && *op == '.')
2117 || (opt_type == 0 && *op != '.'))
2118 EMSG2(_(e_letwrong), op);
2119 else
2120 {
2121 if (opt_type == 1) /* number */
2122 {
2123 if (*op == '+')
2124 n = numval + n;
2125 else
2126 n = numval - n;
2127 }
2128 else if (opt_type == 0 && stringval != NULL) /* string */
2129 {
2130 s = concat_str(stringval, s);
2131 vim_free(stringval);
2132 stringval = s;
2133 }
2134 }
2135 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 if (s != NULL)
2137 {
2138 set_option_value(arg, n, s, opt_flags);
2139 arg_end = p;
2140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002142 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 }
2144 }
2145
2146 /*
2147 * ":let @r = expr": Set register contents.
2148 */
2149 else if (*arg == '@')
2150 {
2151 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 if (op != NULL && (*op == '+' || *op == '-'))
2153 EMSG2(_(e_letwrong), op);
2154 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002155 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156 EMSG(_(e_letunexp));
2157 else
2158 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159 char_u *tofree = NULL;
2160 char_u *s;
2161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 p = get_tv_string_chk(tv);
2163 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002165 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 if (s != NULL)
2167 {
2168 p = tofree = concat_str(s, p);
2169 vim_free(s);
2170 }
2171 }
2172 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002173 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002175 arg_end = arg + 1;
2176 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 }
2179 }
2180
2181 /*
2182 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002185 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002187 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002189 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2193 EMSG(_(e_letunexp));
2194 else
2195 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 arg_end = p;
2198 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 }
2202
2203 else
2204 EMSG2(_(e_invarg2), arg);
2205
2206 return arg_end;
2207}
2208
2209/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002210 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2211 */
2212 static int
2213check_changedtick(arg)
2214 char_u *arg;
2215{
2216 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2217 {
2218 EMSG2(_(e_readonlyvar), arg);
2219 return TRUE;
2220 }
2221 return FALSE;
2222}
2223
2224/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 * Get an lval: variable, Dict item or List item that can be assigned a value
2226 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2227 * "name.key", "name.key[expr]" etc.
2228 * Indexing only works if "name" is an existing List or Dictionary.
2229 * "name" points to the start of the name.
2230 * If "rettv" is not NULL it points to the value to be assigned.
2231 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2232 * wrong; must end in space or cmd separator.
2233 *
2234 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002235 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 */
2238 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002239get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002241 typval_T *rettv;
2242 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 int unlet;
2244 int skip;
2245 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002246 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 char_u *expr_start, *expr_end;
2250 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002251 dictitem_T *v;
2252 typval_T var1;
2253 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002255 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002256 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002258 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002261 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262
2263 if (skip)
2264 {
2265 /* When skipping just find the end of the name. */
2266 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002267 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 }
2269
2270 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002271 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 if (expr_start != NULL)
2273 {
2274 /* Don't expand the name when we already know there is an error. */
2275 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2276 && *p != '[' && *p != '.')
2277 {
2278 EMSG(_(e_trailing));
2279 return NULL;
2280 }
2281
2282 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2283 if (lp->ll_exp_name == NULL)
2284 {
2285 /* Report an invalid expression in braces, unless the
2286 * expression evaluation has been cancelled due to an
2287 * aborting error, an interrupt, or an exception. */
2288 if (!aborting() && !quiet)
2289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002290 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 EMSG2(_(e_invarg2), name);
2292 return NULL;
2293 }
2294 }
2295 lp->ll_name = lp->ll_exp_name;
2296 }
2297 else
2298 lp->ll_name = name;
2299
2300 /* Without [idx] or .key we are done. */
2301 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2302 return p;
2303
2304 cc = *p;
2305 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 if (v == NULL && !quiet)
2308 EMSG2(_(e_undefvar), lp->ll_name);
2309 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 if (v == NULL)
2311 return NULL;
2312
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 /*
2314 * Loop until no more [idx] or .key is following.
2315 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002316 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2320 && !(lp->ll_tv->v_type == VAR_DICT
2321 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 if (!quiet)
2324 EMSG(_("E689: Can only index a List or Dictionary"));
2325 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 if (!quiet)
2330 EMSG(_("E708: [:] must come last"));
2331 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002333
Bram Moolenaar8c711452005-01-14 21:53:12 +00002334 len = -1;
2335 if (*p == '.')
2336 {
2337 key = p + 1;
2338 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2339 ;
2340 if (len == 0)
2341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (!quiet)
2343 EMSG(_(e_emptykey));
2344 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 }
2346 p = key + len;
2347 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002348 else
2349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002351 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 if (*p == ':')
2353 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002354 else
2355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 empty1 = FALSE;
2357 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359 if (get_tv_string_chk(&var1) == NULL)
2360 {
2361 /* not a number or string */
2362 clear_tv(&var1);
2363 return NULL;
2364 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 }
2366
2367 /* Optionally get the second index [ :expr]. */
2368 if (*p == ':')
2369 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002374 if (!empty1)
2375 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002377 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (rettv != NULL && (rettv->v_type != VAR_LIST
2379 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 if (!quiet)
2382 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 if (!empty1)
2384 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 }
2387 p = skipwhite(p + 1);
2388 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 else
2391 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002393 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 if (!empty1)
2396 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002399 if (get_tv_string_chk(&var2) == NULL)
2400 {
2401 /* not a number or string */
2402 if (!empty1)
2403 clear_tv(&var1);
2404 clear_tv(&var2);
2405 return NULL;
2406 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002409 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002412
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 if (!quiet)
2416 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 if (!empty1)
2418 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 }
2423
2424 /* Skip to past ']'. */
2425 ++p;
2426 }
2427
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 {
2430 if (len == -1)
2431 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 if (*key == NUL)
2435 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 if (!quiet)
2437 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 }
2441 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 lp->ll_list = NULL;
2443 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002444 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002447 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 if (len == -1)
2453 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 }
2456 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 if (len == -1)
2461 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 p = NULL;
2464 break;
2465 }
2466 if (len == -1)
2467 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 }
2470 else
2471 {
2472 /*
2473 * Get the number and item for the only or first index of the List.
2474 */
2475 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 else
2478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 clear_tv(&var1);
2481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 lp->ll_list = lp->ll_tv->vval.v_list;
2484 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2485 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (!quiet)
2488 EMSGN(_(e_listidx), lp->ll_n1);
2489 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 }
2493
2494 /*
2495 * May need to find the item or absolute index for the second
2496 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * When no index given: "lp->ll_empty2" is TRUE.
2498 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 if (ni == NULL)
2508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (!quiet)
2510 EMSGN(_(e_listidx), lp->ll_n2);
2511 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 }
2515
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2517 if (lp->ll_n1 < 0)
2518 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2519 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (!quiet)
2522 EMSGN(_(e_listidx), lp->ll_n2);
2523 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002524 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002525 }
2526
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
2530
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 return p;
2532}
2533
2534/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 */
2537 static void
2538clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540{
2541 vim_free(lp->ll_exp_name);
2542 vim_free(lp->ll_newkey);
2543}
2544
2545/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002546 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 */
2550 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002551set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002556 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557{
2558 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 listitem_T *ri;
2560 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561
2562 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 cc = *endp;
2567 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568 if (op != NULL && *op != '=')
2569 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002571
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002572 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002573 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2574 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002575 {
2576 if (tv_op(&tv, rettv, op) == OK)
2577 set_var(lp->ll_name, &tv, FALSE);
2578 clear_tv(&tv);
2579 }
2580 }
2581 else
2582 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002586 else if (tv_check_lock(lp->ll_newkey == NULL
2587 ? lp->ll_tv->v_lock
2588 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2589 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 else if (lp->ll_range)
2591 {
2592 /*
2593 * Assign the List values to the list items.
2594 */
2595 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002597 if (op != NULL && *op != '=')
2598 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2599 else
2600 {
2601 clear_tv(&lp->ll_li->li_tv);
2602 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 ri = ri->li_next;
2605 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2606 break;
2607 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002610 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 ri = NULL;
2613 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 lp->ll_li = lp->ll_li->li_next;
2617 ++lp->ll_n1;
2618 }
2619 if (ri != NULL)
2620 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 else if (lp->ll_empty2
2622 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 : lp->ll_n1 != lp->ll_n2)
2624 EMSG(_("E711: List value has not enough items"));
2625 }
2626 else
2627 {
2628 /*
2629 * Assign to a List or Dictionary item.
2630 */
2631 if (lp->ll_newkey != NULL)
2632 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 if (op != NULL && *op != '=')
2634 {
2635 EMSG2(_(e_letwrong), op);
2636 return;
2637 }
2638
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002640 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (di == NULL)
2642 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002643 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2644 {
2645 vim_free(di);
2646 return;
2647 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 else if (op != NULL && *op != '=')
2651 {
2652 tv_op(lp->ll_tv, rettv, op);
2653 return;
2654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 /*
2659 * Assign the value to the variable or list item.
2660 */
2661 if (copy)
2662 copy_tv(rettv, lp->ll_tv);
2663 else
2664 {
2665 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002666 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
2669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670}
2671
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002672/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2674 * Returns OK or FAIL.
2675 */
2676 static int
2677tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002678 typval_T *tv1;
2679 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002680 char_u *op;
2681{
2682 long n;
2683 char_u numbuf[NUMBUFLEN];
2684 char_u *s;
2685
2686 /* Can't do anything with a Funcref or a Dict on the right. */
2687 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2688 {
2689 switch (tv1->v_type)
2690 {
2691 case VAR_DICT:
2692 case VAR_FUNC:
2693 break;
2694
2695 case VAR_LIST:
2696 if (*op != '+' || tv2->v_type != VAR_LIST)
2697 break;
2698 /* List += List */
2699 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2700 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2701 return OK;
2702
2703 case VAR_NUMBER:
2704 case VAR_STRING:
2705 if (tv2->v_type == VAR_LIST)
2706 break;
2707 if (*op == '+' || *op == '-')
2708 {
2709 /* nr += nr or nr -= nr*/
2710 n = get_tv_number(tv1);
2711 if (*op == '+')
2712 n += get_tv_number(tv2);
2713 else
2714 n -= get_tv_number(tv2);
2715 clear_tv(tv1);
2716 tv1->v_type = VAR_NUMBER;
2717 tv1->vval.v_number = n;
2718 }
2719 else
2720 {
2721 /* str .= str */
2722 s = get_tv_string(tv1);
2723 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2724 clear_tv(tv1);
2725 tv1->v_type = VAR_STRING;
2726 tv1->vval.v_string = s;
2727 }
2728 return OK;
2729 }
2730 }
2731
2732 EMSG2(_(e_letwrong), op);
2733 return FAIL;
2734}
2735
2736/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737 * Add a watcher to a list.
2738 */
2739 static void
2740list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002741 list_T *l;
2742 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002743{
2744 lw->lw_next = l->lv_watch;
2745 l->lv_watch = lw;
2746}
2747
2748/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002749 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002750 * No warning when it isn't found...
2751 */
2752 static void
2753list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002754 list_T *l;
2755 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002756{
Bram Moolenaar33570922005-01-25 22:26:29 +00002757 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002758
2759 lwp = &l->lv_watch;
2760 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2761 {
2762 if (lw == lwrem)
2763 {
2764 *lwp = lw->lw_next;
2765 break;
2766 }
2767 lwp = &lw->lw_next;
2768 }
2769}
2770
2771/*
2772 * Just before removing an item from a list: advance watchers to the next
2773 * item.
2774 */
2775 static void
2776list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 list_T *l;
2778 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002779{
Bram Moolenaar33570922005-01-25 22:26:29 +00002780 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002781
2782 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2783 if (lw->lw_item == item)
2784 lw->lw_item = item->li_next;
2785}
2786
2787/*
2788 * Evaluate the expression used in a ":for var in expr" command.
2789 * "arg" points to "var".
2790 * Set "*errp" to TRUE for an error, FALSE otherwise;
2791 * Return a pointer that holds the info. Null when there is an error.
2792 */
2793 void *
2794eval_for_line(arg, errp, nextcmdp, skip)
2795 char_u *arg;
2796 int *errp;
2797 char_u **nextcmdp;
2798 int skip;
2799{
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002801 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002802 typval_T tv;
2803 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804
2805 *errp = TRUE; /* default: there is an error */
2806
Bram Moolenaar33570922005-01-25 22:26:29 +00002807 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002808 if (fi == NULL)
2809 return NULL;
2810
2811 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2812 if (expr == NULL)
2813 return fi;
2814
2815 expr = skipwhite(expr);
2816 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2817 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002818 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002819 return fi;
2820 }
2821
2822 if (skip)
2823 ++emsg_skip;
2824 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2825 {
2826 *errp = FALSE;
2827 if (!skip)
2828 {
2829 l = tv.vval.v_list;
2830 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002831 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002833 clear_tv(&tv);
2834 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002835 else
2836 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002837 /* No need to increment the refcount, it's already set for the
2838 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839 fi->fi_list = l;
2840 list_add_watch(l, &fi->fi_lw);
2841 fi->fi_lw.lw_item = l->lv_first;
2842 }
2843 }
2844 }
2845 if (skip)
2846 --emsg_skip;
2847
2848 return fi;
2849}
2850
2851/*
2852 * Use the first item in a ":for" list. Advance to the next.
2853 * Assign the values to the variable (list). "arg" points to the first one.
2854 * Return TRUE when a valid item was found, FALSE when at end of list or
2855 * something wrong.
2856 */
2857 int
2858next_for_item(fi_void, arg)
2859 void *fi_void;
2860 char_u *arg;
2861{
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865
2866 item = fi->fi_lw.lw_item;
2867 if (item == NULL)
2868 result = FALSE;
2869 else
2870 {
2871 fi->fi_lw.lw_item = item->li_next;
2872 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2873 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2874 }
2875 return result;
2876}
2877
2878/*
2879 * Free the structure used to store info used by ":for".
2880 */
2881 void
2882free_for_info(fi_void)
2883 void *fi_void;
2884{
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002887 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002888 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002890 list_unref(fi->fi_list);
2891 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002892 vim_free(fi);
2893}
2894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2896
2897 void
2898set_context_for_expression(xp, arg, cmdidx)
2899 expand_T *xp;
2900 char_u *arg;
2901 cmdidx_T cmdidx;
2902{
2903 int got_eq = FALSE;
2904 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 if (cmdidx == CMD_let)
2908 {
2909 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002910 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 {
2912 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002913 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 {
2915 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002917 if (vim_iswhite(*p))
2918 break;
2919 }
2920 return;
2921 }
2922 }
2923 else
2924 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2925 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 while ((xp->xp_pattern = vim_strpbrk(arg,
2927 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2928 {
2929 c = *xp->xp_pattern;
2930 if (c == '&')
2931 {
2932 c = xp->xp_pattern[1];
2933 if (c == '&')
2934 {
2935 ++xp->xp_pattern;
2936 xp->xp_context = cmdidx != CMD_let || got_eq
2937 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2938 }
2939 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002942 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2943 xp->xp_pattern += 2;
2944
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 else if (c == '$')
2948 {
2949 /* environment variable */
2950 xp->xp_context = EXPAND_ENV_VARS;
2951 }
2952 else if (c == '=')
2953 {
2954 got_eq = TRUE;
2955 xp->xp_context = EXPAND_EXPRESSION;
2956 }
2957 else if (c == '<'
2958 && xp->xp_context == EXPAND_FUNCTIONS
2959 && vim_strchr(xp->xp_pattern, '(') == NULL)
2960 {
2961 /* Function name can start with "<SNR>" */
2962 break;
2963 }
2964 else if (cmdidx != CMD_let || got_eq)
2965 {
2966 if (c == '"') /* string */
2967 {
2968 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2969 if (c == '\\' && xp->xp_pattern[1] != NUL)
2970 ++xp->xp_pattern;
2971 xp->xp_context = EXPAND_NOTHING;
2972 }
2973 else if (c == '\'') /* literal string */
2974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002975 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2977 /* skip */ ;
2978 xp->xp_context = EXPAND_NOTHING;
2979 }
2980 else if (c == '|')
2981 {
2982 if (xp->xp_pattern[1] == '|')
2983 {
2984 ++xp->xp_pattern;
2985 xp->xp_context = EXPAND_EXPRESSION;
2986 }
2987 else
2988 xp->xp_context = EXPAND_COMMANDS;
2989 }
2990 else
2991 xp->xp_context = EXPAND_EXPRESSION;
2992 }
2993 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 /* Doesn't look like something valid, expand as an expression
2995 * anyway. */
2996 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 arg = xp->xp_pattern;
2998 if (*arg != NUL)
2999 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3000 /* skip */ ;
3001 }
3002 xp->xp_pattern = arg;
3003}
3004
3005#endif /* FEAT_CMDL_COMPL */
3006
3007/*
3008 * ":1,25call func(arg1, arg2)" function call.
3009 */
3010 void
3011ex_call(eap)
3012 exarg_T *eap;
3013{
3014 char_u *arg = eap->arg;
3015 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 linenr_T lnum;
3021 int doesrange;
3022 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003025 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3026 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 if (tofree == NULL)
3028 return;
3029
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003030 /* Increase refcount on dictionary, it could get deleted when evaluating
3031 * the arguments. */
3032 if (fudi.fd_dict != NULL)
3033 ++fudi.fd_dict->dv_refcount;
3034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3036 len = STRLEN(tofree);
3037 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
Bram Moolenaar532c7802005-01-27 14:44:31 +00003039 /* Skip white space to allow ":call func ()". Not good, but required for
3040 * backward compatibility. */
3041 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003042 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 if (*startarg != '(')
3045 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003046 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 goto end;
3048 }
3049
3050 /*
3051 * When skipping, evaluate the function once, to find the end of the
3052 * arguments.
3053 * When the function takes a range, this is discovered after the first
3054 * call, and the loop is broken.
3055 */
3056 if (eap->skip)
3057 {
3058 ++emsg_skip;
3059 lnum = eap->line2; /* do it once, also with an invalid range */
3060 }
3061 else
3062 lnum = eap->line1;
3063 for ( ; lnum <= eap->line2; ++lnum)
3064 {
3065 if (!eap->skip && eap->addr_count > 0)
3066 {
3067 curwin->w_cursor.lnum = lnum;
3068 curwin->w_cursor.col = 0;
3069 }
3070 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003072 eap->line1, eap->line2, &doesrange,
3073 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
3075 failed = TRUE;
3076 break;
3077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (doesrange || eap->skip)
3080 break;
3081 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003082 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003083 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003084 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (aborting())
3086 break;
3087 }
3088 if (eap->skip)
3089 --emsg_skip;
3090
3091 if (!failed)
3092 {
3093 /* Check for trailing illegal characters and a following command. */
3094 if (!ends_excmd(*arg))
3095 {
3096 emsg_severe = TRUE;
3097 EMSG(_(e_trailing));
3098 }
3099 else
3100 eap->nextcmd = check_nextcmd(arg);
3101 }
3102
3103end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003104 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106}
3107
3108/*
3109 * ":unlet[!] var1 ... " command.
3110 */
3111 void
3112ex_unlet(eap)
3113 exarg_T *eap;
3114{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 ex_unletlock(eap, eap->arg, 0);
3116}
3117
3118/*
3119 * ":lockvar" and ":unlockvar" commands
3120 */
3121 void
3122ex_lockvar(eap)
3123 exarg_T *eap;
3124{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003126 int deep = 2;
3127
3128 if (eap->forceit)
3129 deep = -1;
3130 else if (vim_isdigit(*arg))
3131 {
3132 deep = getdigits(&arg);
3133 arg = skipwhite(arg);
3134 }
3135
3136 ex_unletlock(eap, arg, deep);
3137}
3138
3139/*
3140 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3141 */
3142 static void
3143ex_unletlock(eap, argstart, deep)
3144 exarg_T *eap;
3145 char_u *argstart;
3146 int deep;
3147{
3148 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003151 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 do
3154 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003155 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003156 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3157 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003158 if (lv.ll_name == NULL)
3159 error = TRUE; /* error but continue parsing */
3160 if (name_end == NULL || (!vim_iswhite(*name_end)
3161 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003163 if (name_end != NULL)
3164 {
3165 emsg_severe = TRUE;
3166 EMSG(_(e_trailing));
3167 }
3168 if (!(eap->skip || error))
3169 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 break;
3171 }
3172
3173 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003174 {
3175 if (eap->cmdidx == CMD_unlet)
3176 {
3177 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3178 error = TRUE;
3179 }
3180 else
3181 {
3182 if (do_lock_var(&lv, name_end, deep,
3183 eap->cmdidx == CMD_lockvar) == FAIL)
3184 error = TRUE;
3185 }
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003188 if (!eap->skip)
3189 clear_lval(&lv);
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 arg = skipwhite(name_end);
3192 } while (!ends_excmd(*arg));
3193
3194 eap->nextcmd = check_nextcmd(arg);
3195}
3196
Bram Moolenaar8c711452005-01-14 21:53:12 +00003197 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003198do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 int forceit;
3202{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003203 int ret = OK;
3204 int cc;
3205
3206 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003208 cc = *name_end;
3209 *name_end = NUL;
3210
3211 /* Normal name or expanded name. */
3212 if (check_changedtick(lp->ll_name))
3213 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003214 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003216 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003217 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3219 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 else if (lp->ll_range)
3221 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223
3224 /* Delete a range of List items. */
3225 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3226 {
3227 li = lp->ll_li->li_next;
3228 listitem_remove(lp->ll_list, lp->ll_li);
3229 lp->ll_li = li;
3230 ++lp->ll_n1;
3231 }
3232 }
3233 else
3234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 /* unlet a List item. */
3237 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003240 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003241 }
3242
3243 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003244}
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246/*
3247 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003248 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 */
3250 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003251do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003253 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 hashtab_T *ht;
3256 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003257 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 ht = find_var_ht(name, &varname);
3260 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003262 hi = hash_find(ht, varname);
3263 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003264 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003265 if (var_check_ro(HI2DI(hi)->di_flags, name))
3266 return FAIL;
3267 delete_var(ht, hi);
3268 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003271 if (forceit)
3272 return OK;
3273 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 return FAIL;
3275}
3276
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277/*
3278 * Lock or unlock variable indicated by "lp".
3279 * "deep" is the levels to go (-1 for unlimited);
3280 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3281 */
3282 static int
3283do_lock_var(lp, name_end, deep, lock)
3284 lval_T *lp;
3285 char_u *name_end;
3286 int deep;
3287 int lock;
3288{
3289 int ret = OK;
3290 int cc;
3291 dictitem_T *di;
3292
3293 if (deep == 0) /* nothing to do */
3294 return OK;
3295
3296 if (lp->ll_tv == NULL)
3297 {
3298 cc = *name_end;
3299 *name_end = NUL;
3300
3301 /* Normal name or expanded name. */
3302 if (check_changedtick(lp->ll_name))
3303 ret = FAIL;
3304 else
3305 {
3306 di = find_var(lp->ll_name, NULL);
3307 if (di == NULL)
3308 ret = FAIL;
3309 else
3310 {
3311 if (lock)
3312 di->di_flags |= DI_FLAGS_LOCK;
3313 else
3314 di->di_flags &= ~DI_FLAGS_LOCK;
3315 item_lock(&di->di_tv, deep, lock);
3316 }
3317 }
3318 *name_end = cc;
3319 }
3320 else if (lp->ll_range)
3321 {
3322 listitem_T *li = lp->ll_li;
3323
3324 /* (un)lock a range of List items. */
3325 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3326 {
3327 item_lock(&li->li_tv, deep, lock);
3328 li = li->li_next;
3329 ++lp->ll_n1;
3330 }
3331 }
3332 else if (lp->ll_list != NULL)
3333 /* (un)lock a List item. */
3334 item_lock(&lp->ll_li->li_tv, deep, lock);
3335 else
3336 /* un(lock) a Dictionary item. */
3337 item_lock(&lp->ll_di->di_tv, deep, lock);
3338
3339 return ret;
3340}
3341
3342/*
3343 * Lock or unlock an item. "deep" is nr of levels to go.
3344 */
3345 static void
3346item_lock(tv, deep, lock)
3347 typval_T *tv;
3348 int deep;
3349 int lock;
3350{
3351 static int recurse = 0;
3352 list_T *l;
3353 listitem_T *li;
3354 dict_T *d;
3355 hashitem_T *hi;
3356 int todo;
3357
3358 if (recurse >= DICT_MAXNEST)
3359 {
3360 EMSG(_("E743: variable nested too deep for (un)lock"));
3361 return;
3362 }
3363 if (deep == 0)
3364 return;
3365 ++recurse;
3366
3367 /* lock/unlock the item itself */
3368 if (lock)
3369 tv->v_lock |= VAR_LOCKED;
3370 else
3371 tv->v_lock &= ~VAR_LOCKED;
3372
3373 switch (tv->v_type)
3374 {
3375 case VAR_LIST:
3376 if ((l = tv->vval.v_list) != NULL)
3377 {
3378 if (lock)
3379 l->lv_lock |= VAR_LOCKED;
3380 else
3381 l->lv_lock &= ~VAR_LOCKED;
3382 if (deep < 0 || deep > 1)
3383 /* recursive: lock/unlock the items the List contains */
3384 for (li = l->lv_first; li != NULL; li = li->li_next)
3385 item_lock(&li->li_tv, deep - 1, lock);
3386 }
3387 break;
3388 case VAR_DICT:
3389 if ((d = tv->vval.v_dict) != NULL)
3390 {
3391 if (lock)
3392 d->dv_lock |= VAR_LOCKED;
3393 else
3394 d->dv_lock &= ~VAR_LOCKED;
3395 if (deep < 0 || deep > 1)
3396 {
3397 /* recursive: lock/unlock the items the List contains */
3398 todo = d->dv_hashtab.ht_used;
3399 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3400 {
3401 if (!HASHITEM_EMPTY(hi))
3402 {
3403 --todo;
3404 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3405 }
3406 }
3407 }
3408 }
3409 }
3410 --recurse;
3411}
3412
Bram Moolenaara40058a2005-07-11 22:42:07 +00003413/*
3414 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3415 * it refers to a List or Dictionary that is locked.
3416 */
3417 static int
3418tv_islocked(tv)
3419 typval_T *tv;
3420{
3421 return (tv->v_lock & VAR_LOCKED)
3422 || (tv->v_type == VAR_LIST
3423 && tv->vval.v_list != NULL
3424 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3425 || (tv->v_type == VAR_DICT
3426 && tv->vval.v_dict != NULL
3427 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3428}
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3431/*
3432 * Delete all "menutrans_" variables.
3433 */
3434 void
3435del_menutrans_vars()
3436{
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003438 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 hash_lock(&globvarht);
3441 todo = globvarht.ht_used;
3442 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003443 {
3444 if (!HASHITEM_EMPTY(hi))
3445 {
3446 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003447 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3448 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003449 }
3450 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452}
3453#endif
3454
3455#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3456
3457/*
3458 * Local string buffer for the next two functions to store a variable name
3459 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3460 * get_user_var_name().
3461 */
3462
3463static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3464
3465static char_u *varnamebuf = NULL;
3466static int varnamebuflen = 0;
3467
3468/*
3469 * Function to concatenate a prefix and a variable name.
3470 */
3471 static char_u *
3472cat_prefix_varname(prefix, name)
3473 int prefix;
3474 char_u *name;
3475{
3476 int len;
3477
3478 len = (int)STRLEN(name) + 3;
3479 if (len > varnamebuflen)
3480 {
3481 vim_free(varnamebuf);
3482 len += 10; /* some additional space */
3483 varnamebuf = alloc(len);
3484 if (varnamebuf == NULL)
3485 {
3486 varnamebuflen = 0;
3487 return NULL;
3488 }
3489 varnamebuflen = len;
3490 }
3491 *varnamebuf = prefix;
3492 varnamebuf[1] = ':';
3493 STRCPY(varnamebuf + 2, name);
3494 return varnamebuf;
3495}
3496
3497/*
3498 * Function given to ExpandGeneric() to obtain the list of user defined
3499 * (global/buffer/window/built-in) variable names.
3500 */
3501/*ARGSUSED*/
3502 char_u *
3503get_user_var_name(xp, idx)
3504 expand_T *xp;
3505 int idx;
3506{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003507 static long_u gdone;
3508 static long_u bdone;
3509 static long_u wdone;
3510 static int vidx;
3511 static hashitem_T *hi;
3512 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003515 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003516
3517 /* Global variables */
3518 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003520 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003521 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003522 else
3523 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003524 while (HASHITEM_EMPTY(hi))
3525 ++hi;
3526 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3527 return cat_prefix_varname('g', hi->hi_key);
3528 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003530
3531 /* b: variables */
3532 ht = &curbuf->b_vars.dv_hashtab;
3533 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003535 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003537 else
3538 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 while (HASHITEM_EMPTY(hi))
3540 ++hi;
3541 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 return (char_u *)"b:changedtick";
3547 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003548
3549 /* w: variables */
3550 ht = &curwin->w_vars.dv_hashtab;
3551 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003553 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003554 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003555 else
3556 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003557 while (HASHITEM_EMPTY(hi))
3558 ++hi;
3559 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003561
3562 /* v: variables */
3563 if (vidx < VV_LEN)
3564 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566 vim_free(varnamebuf);
3567 varnamebuf = NULL;
3568 varnamebuflen = 0;
3569 return NULL;
3570}
3571
3572#endif /* FEAT_CMDL_COMPL */
3573
3574/*
3575 * types for expressions.
3576 */
3577typedef enum
3578{
3579 TYPE_UNKNOWN = 0
3580 , TYPE_EQUAL /* == */
3581 , TYPE_NEQUAL /* != */
3582 , TYPE_GREATER /* > */
3583 , TYPE_GEQUAL /* >= */
3584 , TYPE_SMALLER /* < */
3585 , TYPE_SEQUAL /* <= */
3586 , TYPE_MATCH /* =~ */
3587 , TYPE_NOMATCH /* !~ */
3588} exptype_T;
3589
3590/*
3591 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003592 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3594 */
3595
3596/*
3597 * Handle zero level expression.
3598 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003600 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 * Return OK or FAIL.
3602 */
3603 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003604eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u **nextcmd;
3608 int evaluate;
3609{
3610 int ret;
3611 char_u *p;
3612
3613 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003614 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 if (ret == FAIL || !ends_excmd(*p))
3616 {
3617 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003618 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /*
3620 * Report the invalid expression unless the expression evaluation has
3621 * been cancelled due to an aborting error, an interrupt, or an
3622 * exception.
3623 */
3624 if (!aborting())
3625 EMSG2(_(e_invexpr2), arg);
3626 ret = FAIL;
3627 }
3628 if (nextcmd != NULL)
3629 *nextcmd = check_nextcmd(p);
3630
3631 return ret;
3632}
3633
3634/*
3635 * Handle top level expression:
3636 * expr1 ? expr0 : expr0
3637 *
3638 * "arg" must point to the first non-white of the expression.
3639 * "arg" is advanced to the next non-white after the recognized expression.
3640 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003641 * Note: "rettv.v_lock" is not set.
3642 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * Return OK or FAIL.
3644 */
3645 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003646eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 int evaluate;
3650{
3651 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003652 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 /*
3655 * Get the first variable.
3656 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return FAIL;
3659
3660 if ((*arg)[0] == '?')
3661 {
3662 result = FALSE;
3663 if (evaluate)
3664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003665 int error = FALSE;
3666
3667 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003670 if (error)
3671 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673
3674 /*
3675 * Get the second variable.
3676 */
3677 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return FAIL;
3680
3681 /*
3682 * Check for the ":".
3683 */
3684 if ((*arg)[0] != ':')
3685 {
3686 EMSG(_("E109: Missing ':' after '?'"));
3687 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 return FAIL;
3690 }
3691
3692 /*
3693 * Get the third variable.
3694 */
3695 *arg = skipwhite(*arg + 1);
3696 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3697 {
3698 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 return FAIL;
3701 }
3702 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003703 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705
3706 return OK;
3707}
3708
3709/*
3710 * Handle first level expression:
3711 * expr2 || expr2 || expr2 logical OR
3712 *
3713 * "arg" must point to the first non-white of the expression.
3714 * "arg" is advanced to the next non-white after the recognized expression.
3715 *
3716 * Return OK or FAIL.
3717 */
3718 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003719eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 int evaluate;
3723{
Bram Moolenaar33570922005-01-25 22:26:29 +00003724 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 long result;
3726 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003727 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 /*
3730 * Get the first variable.
3731 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 return FAIL;
3734
3735 /*
3736 * Repeat until there is no following "||".
3737 */
3738 first = TRUE;
3739 result = FALSE;
3740 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3741 {
3742 if (evaluate && first)
3743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003746 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003747 if (error)
3748 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 first = FALSE;
3750 }
3751
3752 /*
3753 * Get the second variable.
3754 */
3755 *arg = skipwhite(*arg + 2);
3756 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3757 return FAIL;
3758
3759 /*
3760 * Compute the result.
3761 */
3762 if (evaluate && !result)
3763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003766 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003767 if (error)
3768 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770 if (evaluate)
3771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003772 rettv->v_type = VAR_NUMBER;
3773 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775 }
3776
3777 return OK;
3778}
3779
3780/*
3781 * Handle second level expression:
3782 * expr3 && expr3 && expr3 logical AND
3783 *
3784 * "arg" must point to the first non-white of the expression.
3785 * "arg" is advanced to the next non-white after the recognized expression.
3786 *
3787 * Return OK or FAIL.
3788 */
3789 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 int evaluate;
3794{
Bram Moolenaar33570922005-01-25 22:26:29 +00003795 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 long result;
3797 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003798 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 /*
3801 * Get the first variable.
3802 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805
3806 /*
3807 * Repeat until there is no following "&&".
3808 */
3809 first = TRUE;
3810 result = TRUE;
3811 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3812 {
3813 if (evaluate && first)
3814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003815 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003817 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003818 if (error)
3819 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 first = FALSE;
3821 }
3822
3823 /*
3824 * Get the second variable.
3825 */
3826 *arg = skipwhite(*arg + 2);
3827 if (eval4(arg, &var2, evaluate && result) == FAIL)
3828 return FAIL;
3829
3830 /*
3831 * Compute the result.
3832 */
3833 if (evaluate && result)
3834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003835 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003838 if (error)
3839 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 if (evaluate)
3842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003843 rettv->v_type = VAR_NUMBER;
3844 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 }
3847
3848 return OK;
3849}
3850
3851/*
3852 * Handle third level expression:
3853 * var1 == var2
3854 * var1 =~ var2
3855 * var1 != var2
3856 * var1 !~ var2
3857 * var1 > var2
3858 * var1 >= var2
3859 * var1 < var2
3860 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003861 * var1 is var2
3862 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 *
3864 * "arg" must point to the first non-white of the expression.
3865 * "arg" is advanced to the next non-white after the recognized expression.
3866 *
3867 * Return OK or FAIL.
3868 */
3869 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003870eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 int evaluate;
3874{
Bram Moolenaar33570922005-01-25 22:26:29 +00003875 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 char_u *p;
3877 int i;
3878 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003879 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 int len = 2;
3881 long n1, n2;
3882 char_u *s1, *s2;
3883 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3884 regmatch_T regmatch;
3885 int ic;
3886 char_u *save_cpo;
3887
3888 /*
3889 * Get the first variable.
3890 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return FAIL;
3893
3894 p = *arg;
3895 switch (p[0])
3896 {
3897 case '=': if (p[1] == '=')
3898 type = TYPE_EQUAL;
3899 else if (p[1] == '~')
3900 type = TYPE_MATCH;
3901 break;
3902 case '!': if (p[1] == '=')
3903 type = TYPE_NEQUAL;
3904 else if (p[1] == '~')
3905 type = TYPE_NOMATCH;
3906 break;
3907 case '>': if (p[1] != '=')
3908 {
3909 type = TYPE_GREATER;
3910 len = 1;
3911 }
3912 else
3913 type = TYPE_GEQUAL;
3914 break;
3915 case '<': if (p[1] != '=')
3916 {
3917 type = TYPE_SMALLER;
3918 len = 1;
3919 }
3920 else
3921 type = TYPE_SEQUAL;
3922 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003923 case 'i': if (p[1] == 's')
3924 {
3925 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3926 len = 5;
3927 if (!vim_isIDc(p[len]))
3928 {
3929 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3930 type_is = TRUE;
3931 }
3932 }
3933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935
3936 /*
3937 * If there is a comparitive operator, use it.
3938 */
3939 if (type != TYPE_UNKNOWN)
3940 {
3941 /* extra question mark appended: ignore case */
3942 if (p[len] == '?')
3943 {
3944 ic = TRUE;
3945 ++len;
3946 }
3947 /* extra '#' appended: match case */
3948 else if (p[len] == '#')
3949 {
3950 ic = FALSE;
3951 ++len;
3952 }
3953 /* nothing appened: use 'ignorecase' */
3954 else
3955 ic = p_ic;
3956
3957 /*
3958 * Get the second variable.
3959 */
3960 *arg = skipwhite(p + len);
3961 if (eval5(arg, &var2, evaluate) == FAIL)
3962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return FAIL;
3965 }
3966
3967 if (evaluate)
3968 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003969 if (type_is && rettv->v_type != var2.v_type)
3970 {
3971 /* For "is" a different type always means FALSE, for "notis"
3972 * it means TRUE. */
3973 n1 = (type == TYPE_NEQUAL);
3974 }
3975 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3976 {
3977 if (type_is)
3978 {
3979 n1 = (rettv->v_type == var2.v_type
3980 && rettv->vval.v_list == var2.vval.v_list);
3981 if (type == TYPE_NEQUAL)
3982 n1 = !n1;
3983 }
3984 else if (rettv->v_type != var2.v_type
3985 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3986 {
3987 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003988 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003989 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003990 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003991 clear_tv(rettv);
3992 clear_tv(&var2);
3993 return FAIL;
3994 }
3995 else
3996 {
3997 /* Compare two Lists for being equal or unequal. */
3998 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3999 if (type == TYPE_NEQUAL)
4000 n1 = !n1;
4001 }
4002 }
4003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004004 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4005 {
4006 if (type_is)
4007 {
4008 n1 = (rettv->v_type == var2.v_type
4009 && rettv->vval.v_dict == var2.vval.v_dict);
4010 if (type == TYPE_NEQUAL)
4011 n1 = !n1;
4012 }
4013 else if (rettv->v_type != var2.v_type
4014 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4015 {
4016 if (rettv->v_type != var2.v_type)
4017 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4018 else
4019 EMSG(_("E736: Invalid operation for Dictionary"));
4020 clear_tv(rettv);
4021 clear_tv(&var2);
4022 return FAIL;
4023 }
4024 else
4025 {
4026 /* Compare two Dictionaries for being equal or unequal. */
4027 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4028 if (type == TYPE_NEQUAL)
4029 n1 = !n1;
4030 }
4031 }
4032
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004033 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4034 {
4035 if (rettv->v_type != var2.v_type
4036 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4037 {
4038 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004039 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004040 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004041 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004042 clear_tv(rettv);
4043 clear_tv(&var2);
4044 return FAIL;
4045 }
4046 else
4047 {
4048 /* Compare two Funcrefs for being equal or unequal. */
4049 if (rettv->vval.v_string == NULL
4050 || var2.vval.v_string == NULL)
4051 n1 = FALSE;
4052 else
4053 n1 = STRCMP(rettv->vval.v_string,
4054 var2.vval.v_string) == 0;
4055 if (type == TYPE_NEQUAL)
4056 n1 = !n1;
4057 }
4058 }
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /*
4061 * If one of the two variables is a number, compare as a number.
4062 * When using "=~" or "!~", always compare as string.
4063 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004064 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 n1 = get_tv_number(rettv);
4068 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 switch (type)
4070 {
4071 case TYPE_EQUAL: n1 = (n1 == n2); break;
4072 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4073 case TYPE_GREATER: n1 = (n1 > n2); break;
4074 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4075 case TYPE_SMALLER: n1 = (n1 < n2); break;
4076 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4077 case TYPE_UNKNOWN:
4078 case TYPE_MATCH:
4079 case TYPE_NOMATCH: break; /* avoid gcc warning */
4080 }
4081 }
4082 else
4083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 s1 = get_tv_string_buf(rettv, buf1);
4085 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4087 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4088 else
4089 i = 0;
4090 n1 = FALSE;
4091 switch (type)
4092 {
4093 case TYPE_EQUAL: n1 = (i == 0); break;
4094 case TYPE_NEQUAL: n1 = (i != 0); break;
4095 case TYPE_GREATER: n1 = (i > 0); break;
4096 case TYPE_GEQUAL: n1 = (i >= 0); break;
4097 case TYPE_SMALLER: n1 = (i < 0); break;
4098 case TYPE_SEQUAL: n1 = (i <= 0); break;
4099
4100 case TYPE_MATCH:
4101 case TYPE_NOMATCH:
4102 /* avoid 'l' flag in 'cpoptions' */
4103 save_cpo = p_cpo;
4104 p_cpo = (char_u *)"";
4105 regmatch.regprog = vim_regcomp(s2,
4106 RE_MAGIC + RE_STRING);
4107 regmatch.rm_ic = ic;
4108 if (regmatch.regprog != NULL)
4109 {
4110 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4111 vim_free(regmatch.regprog);
4112 if (type == TYPE_NOMATCH)
4113 n1 = !n1;
4114 }
4115 p_cpo = save_cpo;
4116 break;
4117
4118 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4119 }
4120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 clear_tv(rettv);
4122 clear_tv(&var2);
4123 rettv->v_type = VAR_NUMBER;
4124 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 }
4127
4128 return OK;
4129}
4130
4131/*
4132 * Handle fourth level expression:
4133 * + number addition
4134 * - number subtraction
4135 * . string concatenation
4136 *
4137 * "arg" must point to the first non-white of the expression.
4138 * "arg" is advanced to the next non-white after the recognized expression.
4139 *
4140 * Return OK or FAIL.
4141 */
4142 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int evaluate;
4147{
Bram Moolenaar33570922005-01-25 22:26:29 +00004148 typval_T var2;
4149 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 int op;
4151 long n1, n2;
4152 char_u *s1, *s2;
4153 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4154 char_u *p;
4155
4156 /*
4157 * Get the first variable.
4158 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return FAIL;
4161
4162 /*
4163 * Repeat computing, until no '+', '-' or '.' is following.
4164 */
4165 for (;;)
4166 {
4167 op = **arg;
4168 if (op != '+' && op != '-' && op != '.')
4169 break;
4170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 if (op != '+' || rettv->v_type != VAR_LIST)
4172 {
4173 /* For "list + ...", an illegal use of the first operand as
4174 * a number cannot be determined before evaluating the 2nd
4175 * operand: if this is also a list, all is ok.
4176 * For "something . ...", "something - ..." or "non-list + ...",
4177 * we know that the first operand needs to be a string or number
4178 * without evaluating the 2nd operand. So check before to avoid
4179 * side effects after an error. */
4180 if (evaluate && get_tv_string_chk(rettv) == NULL)
4181 {
4182 clear_tv(rettv);
4183 return FAIL;
4184 }
4185 }
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 1);
4191 if (eval6(arg, &var2, evaluate) == FAIL)
4192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return FAIL;
4195 }
4196
4197 if (evaluate)
4198 {
4199 /*
4200 * Compute the result.
4201 */
4202 if (op == '.')
4203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4205 s2 = get_tv_string_buf_chk(&var2, buf2);
4206 if (s2 == NULL) /* type error ? */
4207 {
4208 clear_tv(rettv);
4209 clear_tv(&var2);
4210 return FAIL;
4211 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004212 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
4214 rettv->v_type = VAR_STRING;
4215 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004217 else if (op == '+' && rettv->v_type == VAR_LIST
4218 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004219 {
4220 /* concatenate Lists */
4221 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4222 &var3) == FAIL)
4223 {
4224 clear_tv(rettv);
4225 clear_tv(&var2);
4226 return FAIL;
4227 }
4228 clear_tv(rettv);
4229 *rettv = var3;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 else
4232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 int error = FALSE;
4234
4235 n1 = get_tv_number_chk(rettv, &error);
4236 if (error)
4237 {
4238 /* This can only happen for "list + non-list".
4239 * For "non-list + ..." or "something - ...", we returned
4240 * before evaluating the 2nd operand. */
4241 clear_tv(rettv);
4242 return FAIL;
4243 }
4244 n2 = get_tv_number_chk(&var2, &error);
4245 if (error)
4246 {
4247 clear_tv(rettv);
4248 clear_tv(&var2);
4249 return FAIL;
4250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (op == '+')
4253 n1 = n1 + n2;
4254 else
4255 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 rettv->v_type = VAR_NUMBER;
4257 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 }
4262 return OK;
4263}
4264
4265/*
4266 * Handle fifth level expression:
4267 * * number multiplication
4268 * / number division
4269 * % number modulo
4270 *
4271 * "arg" must point to the first non-white of the expression.
4272 * "arg" is advanced to the next non-white after the recognized expression.
4273 *
4274 * Return OK or FAIL.
4275 */
4276 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int evaluate;
4281{
Bram Moolenaar33570922005-01-25 22:26:29 +00004282 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 int op;
4284 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 /*
4288 * Get the first variable.
4289 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 return FAIL;
4292
4293 /*
4294 * Repeat computing, until no '*', '/' or '%' is following.
4295 */
4296 for (;;)
4297 {
4298 op = **arg;
4299 if (op != '*' && op != '/' && op != '%')
4300 break;
4301
4302 if (evaluate)
4303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 if (error)
4307 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309 else
4310 n1 = 0;
4311
4312 /*
4313 * Get the second variable.
4314 */
4315 *arg = skipwhite(*arg + 1);
4316 if (eval7(arg, &var2, evaluate) == FAIL)
4317 return FAIL;
4318
4319 if (evaluate)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (error)
4324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
4326 /*
4327 * Compute the result.
4328 */
4329 if (op == '*')
4330 n1 = n1 * n2;
4331 else if (op == '/')
4332 {
4333 if (n2 == 0) /* give an error message? */
4334 n1 = 0x7fffffffL;
4335 else
4336 n1 = n1 / n2;
4337 }
4338 else
4339 {
4340 if (n2 == 0) /* give an error message? */
4341 n1 = 0;
4342 else
4343 n1 = n1 % n2;
4344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 rettv->v_type = VAR_NUMBER;
4346 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 }
4349
4350 return OK;
4351}
4352
4353/*
4354 * Handle sixth level expression:
4355 * number number constant
4356 * "string" string contstant
4357 * 'string' literal string contstant
4358 * &option-name option value
4359 * @r register contents
4360 * identifier variable value
4361 * function() function call
4362 * $VAR environment variable
4363 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004364 * [expr, expr] List
4365 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 *
4367 * Also handle:
4368 * ! in front logical NOT
4369 * - in front unary minus
4370 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004371 * trailing [] subscript in String or List
4372 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 *
4374 * "arg" must point to the first non-white of the expression.
4375 * "arg" is advanced to the next non-white after the recognized expression.
4376 *
4377 * Return OK or FAIL.
4378 */
4379 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 int evaluate;
4384{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 long n;
4386 int len;
4387 char_u *s;
4388 int val;
4389 char_u *start_leader, *end_leader;
4390 int ret = OK;
4391 char_u *alias;
4392
4393 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004395 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 /*
4400 * Skip '!' and '-' characters. They are handled later.
4401 */
4402 start_leader = *arg;
4403 while (**arg == '!' || **arg == '-' || **arg == '+')
4404 *arg = skipwhite(*arg + 1);
4405 end_leader = *arg;
4406
4407 switch (**arg)
4408 {
4409 /*
4410 * Number constant.
4411 */
4412 case '0':
4413 case '1':
4414 case '2':
4415 case '3':
4416 case '4':
4417 case '5':
4418 case '6':
4419 case '7':
4420 case '8':
4421 case '9':
4422 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4423 *arg += len;
4424 if (evaluate)
4425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 rettv->v_type = VAR_NUMBER;
4427 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 break;
4430
4431 /*
4432 * String constant: "string".
4433 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 break;
4436
4437 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 break;
4442
4443 /*
4444 * List: [expr, expr]
4445 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 break;
4448
4449 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 * Dictionary: {key: val, key: val}
4451 */
4452 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4453 break;
4454
4455 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004456 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004458 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 break;
4460
4461 /*
4462 * Environment variable: $VAR.
4463 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 break;
4466
4467 /*
4468 * Register contents: @r.
4469 */
4470 case '@': ++*arg;
4471 if (evaluate)
4472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004474 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 if (**arg != NUL)
4477 ++*arg;
4478 break;
4479
4480 /*
4481 * nested expression: (expression).
4482 */
4483 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (**arg == ')')
4486 ++*arg;
4487 else if (ret == OK)
4488 {
4489 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 ret = FAIL;
4492 }
4493 break;
4494
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 break;
4497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498
4499 if (ret == NOTDONE)
4500 {
4501 /*
4502 * Must be a variable or function name.
4503 * Can also be a curly-braces kind of name: {expr}.
4504 */
4505 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004506 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 if (alias != NULL)
4508 s = alias;
4509
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004510 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004511 ret = FAIL;
4512 else
4513 {
4514 if (**arg == '(') /* recursive! */
4515 {
4516 /* If "s" is the name of a variable of type VAR_FUNC
4517 * use its contents. */
4518 s = deref_func_name(s, &len);
4519
4520 /* Invoke the function. */
4521 ret = get_func_tv(s, len, rettv, arg,
4522 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004523 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 /* Stop the expression evaluation when immediately
4525 * aborting on error, or when an interrupt occurred or
4526 * an exception was thrown but not caught. */
4527 if (aborting())
4528 {
4529 if (ret == OK)
4530 clear_tv(rettv);
4531 ret = FAIL;
4532 }
4533 }
4534 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004535 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004536 else
4537 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538 }
4539
4540 if (alias != NULL)
4541 vim_free(alias);
4542 }
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 *arg = skipwhite(*arg);
4545
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004546 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4547 * expr(expr). */
4548 if (ret == OK)
4549 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 /*
4552 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4553 */
4554 if (ret == OK && evaluate && end_leader > start_leader)
4555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004556 int error = FALSE;
4557
4558 val = get_tv_number_chk(rettv, &error);
4559 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004561 clear_tv(rettv);
4562 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004564 else
4565 {
4566 while (end_leader > start_leader)
4567 {
4568 --end_leader;
4569 if (*end_leader == '!')
4570 val = !val;
4571 else if (*end_leader == '-')
4572 val = -val;
4573 }
4574 clear_tv(rettv);
4575 rettv->v_type = VAR_NUMBER;
4576 rettv->vval.v_number = val;
4577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
4579
4580 return ret;
4581}
4582
4583/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584 * Evaluate an "[expr]" or "[expr:expr]" index.
4585 * "*arg" points to the '['.
4586 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4587 */
4588 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004589eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004591 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004593 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594{
4595 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004596 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 long len = -1;
4599 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004600 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004605 if (verbose)
4606 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 return FAIL;
4608 }
4609
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004612 /*
4613 * dict.name
4614 */
4615 key = *arg + 1;
4616 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4617 ;
4618 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 }
4622 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 /*
4625 * something[idx]
4626 *
4627 * Get the (first) variable from inside the [].
4628 */
4629 *arg = skipwhite(*arg + 1);
4630 if (**arg == ':')
4631 empty1 = TRUE;
4632 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4633 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4635 {
4636 /* not a number or string */
4637 clear_tv(&var1);
4638 return FAIL;
4639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640
4641 /*
4642 * Get the second variable from inside the [:].
4643 */
4644 if (**arg == ':')
4645 {
4646 range = TRUE;
4647 *arg = skipwhite(*arg + 1);
4648 if (**arg == ']')
4649 empty2 = TRUE;
4650 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652 if (!empty1)
4653 clear_tv(&var1);
4654 return FAIL;
4655 }
4656 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4657 {
4658 /* not a number or string */
4659 if (!empty1)
4660 clear_tv(&var1);
4661 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662 return FAIL;
4663 }
4664 }
4665
4666 /* Check for the ']'. */
4667 if (**arg != ']')
4668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004669 if (verbose)
4670 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 clear_tv(&var1);
4672 if (range)
4673 clear_tv(&var2);
4674 return FAIL;
4675 }
4676 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 }
4678
4679 if (evaluate)
4680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 n1 = 0;
4682 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 n1 = get_tv_number(&var1);
4685 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 }
4687 if (range)
4688 {
4689 if (empty2)
4690 n2 = -1;
4691 else
4692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 n2 = get_tv_number(&var2);
4694 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 }
4696 }
4697
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 {
4700 case VAR_NUMBER:
4701 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 len = (long)STRLEN(s);
4704 if (range)
4705 {
4706 /* The resulting variable is a substring. If the indexes
4707 * are out of range the result is empty. */
4708 if (n1 < 0)
4709 {
4710 n1 = len + n1;
4711 if (n1 < 0)
4712 n1 = 0;
4713 }
4714 if (n2 < 0)
4715 n2 = len + n2;
4716 else if (n2 >= len)
4717 n2 = len;
4718 if (n1 >= len || n2 < 0 || n1 > n2)
4719 s = NULL;
4720 else
4721 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4722 }
4723 else
4724 {
4725 /* The resulting variable is a string of a single
4726 * character. If the index is too big or negative the
4727 * result is empty. */
4728 if (n1 >= len || n1 < 0)
4729 s = NULL;
4730 else
4731 s = vim_strnsave(s + n1, 1);
4732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004733 clear_tv(rettv);
4734 rettv->v_type = VAR_STRING;
4735 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 break;
4737
4738 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004739 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 if (n1 < 0)
4741 n1 = len + n1;
4742 if (!empty1 && (n1 < 0 || n1 >= len))
4743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004744 if (verbose)
4745 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 return FAIL;
4747 }
4748 if (range)
4749 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004750 list_T *l;
4751 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752
4753 if (n2 < 0)
4754 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004757 if (verbose)
4758 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 return FAIL;
4760 }
4761 l = list_alloc();
4762 if (l == NULL)
4763 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 n1 <= n2; ++n1)
4766 {
4767 if (list_append_tv(l, &item->li_tv) == FAIL)
4768 {
4769 list_free(l);
4770 return FAIL;
4771 }
4772 item = item->li_next;
4773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(rettv);
4775 rettv->v_type = VAR_LIST;
4776 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004777 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 }
4779 else
4780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 clear_tv(rettv);
4784 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 }
4786 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004787
4788 case VAR_DICT:
4789 if (range)
4790 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004791 if (verbose)
4792 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 if (len == -1)
4794 clear_tv(&var1);
4795 return FAIL;
4796 }
4797 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004798 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799
4800 if (len == -1)
4801 {
4802 key = get_tv_string(&var1);
4803 if (*key == NUL)
4804 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004805 if (verbose)
4806 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807 clear_tv(&var1);
4808 return FAIL;
4809 }
4810 }
4811
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004812 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004814 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004815 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 if (len == -1)
4817 clear_tv(&var1);
4818 if (item == NULL)
4819 return FAIL;
4820
4821 copy_tv(&item->di_tv, &var1);
4822 clear_tv(rettv);
4823 *rettv = var1;
4824 }
4825 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 }
4827 }
4828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 return OK;
4830}
4831
4832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * Get an option value.
4834 * "arg" points to the '&' or '+' before the option name.
4835 * "arg" is advanced to character after the option name.
4836 * Return OK or FAIL.
4837 */
4838 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004841 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 int evaluate;
4843{
4844 char_u *option_end;
4845 long numval;
4846 char_u *stringval;
4847 int opt_type;
4848 int c;
4849 int working = (**arg == '+'); /* has("+option") */
4850 int ret = OK;
4851 int opt_flags;
4852
4853 /*
4854 * Isolate the option name and find its value.
4855 */
4856 option_end = find_option_end(arg, &opt_flags);
4857 if (option_end == NULL)
4858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 EMSG2(_("E112: Option name missing: %s"), *arg);
4861 return FAIL;
4862 }
4863
4864 if (!evaluate)
4865 {
4866 *arg = option_end;
4867 return OK;
4868 }
4869
4870 c = *option_end;
4871 *option_end = NUL;
4872 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 if (opt_type == -3) /* invalid name */
4876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 EMSG2(_("E113: Unknown option: %s"), *arg);
4879 ret = FAIL;
4880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 if (opt_type == -2) /* hidden string option */
4884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 rettv->v_type = VAR_STRING;
4886 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 else if (opt_type == -1) /* hidden number option */
4889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 rettv->v_type = VAR_NUMBER;
4891 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 else if (opt_type == 1) /* number option */
4894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 rettv->v_type = VAR_NUMBER;
4896 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898 else /* string option */
4899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 rettv->v_type = VAR_STRING;
4901 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 }
4904 else if (working && (opt_type == -2 || opt_type == -1))
4905 ret = FAIL;
4906
4907 *option_end = c; /* put back for error messages */
4908 *arg = option_end;
4909
4910 return ret;
4911}
4912
4913/*
4914 * Allocate a variable for a string constant.
4915 * Return OK or FAIL.
4916 */
4917 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 int evaluate;
4922{
4923 char_u *p;
4924 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 int extra = 0;
4926
4927 /*
4928 * Find the end of the string, skipping backslashed characters.
4929 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004930 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
4932 if (*p == '\\' && p[1] != NUL)
4933 {
4934 ++p;
4935 /* A "\<x>" form occupies at least 4 characters, and produces up
4936 * to 6 characters: reserve space for 2 extra */
4937 if (*p == '<')
4938 extra += 2;
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941
4942 if (*p != '"')
4943 {
4944 EMSG2(_("E114: Missing quote: %s"), *arg);
4945 return FAIL;
4946 }
4947
4948 /* If only parsing, set *arg and return here */
4949 if (!evaluate)
4950 {
4951 *arg = p + 1;
4952 return OK;
4953 }
4954
4955 /*
4956 * Copy the string into allocated memory, handling backslashed
4957 * characters.
4958 */
4959 name = alloc((unsigned)(p - *arg + extra));
4960 if (name == NULL)
4961 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 rettv->v_type = VAR_STRING;
4963 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 if (*p == '\\')
4968 {
4969 switch (*++p)
4970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 case 'b': *name++ = BS; ++p; break;
4972 case 'e': *name++ = ESC; ++p; break;
4973 case 'f': *name++ = FF; ++p; break;
4974 case 'n': *name++ = NL; ++p; break;
4975 case 'r': *name++ = CAR; ++p; break;
4976 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 case 'X': /* hex: "\x1", "\x12" */
4979 case 'x':
4980 case 'u': /* Unicode: "\u0023" */
4981 case 'U':
4982 if (vim_isxdigit(p[1]))
4983 {
4984 int n, nr;
4985 int c = toupper(*p);
4986
4987 if (c == 'X')
4988 n = 2;
4989 else
4990 n = 4;
4991 nr = 0;
4992 while (--n >= 0 && vim_isxdigit(p[1]))
4993 {
4994 ++p;
4995 nr = (nr << 4) + hex2nr(*p);
4996 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#ifdef FEAT_MBYTE
4999 /* For "\u" store the number according to
5000 * 'encoding'. */
5001 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else
5004#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 break;
5008
5009 /* octal: "\1", "\12", "\123" */
5010 case '0':
5011 case '1':
5012 case '2':
5013 case '3':
5014 case '4':
5015 case '5':
5016 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 case '7': *name = *p++ - '0';
5018 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 *name = (*name << 3) + *p++ - '0';
5021 if (*p >= '0' && *p <= '7')
5022 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 break;
5026
5027 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (extra != 0)
5030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 break;
5033 }
5034 /* FALLTHROUGH */
5035
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
5038 }
5039 }
5040 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 *arg = p + 1;
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 return OK;
5048}
5049
5050/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 * Return OK or FAIL.
5053 */
5054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 int evaluate;
5059{
5060 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 int reduce = 0;
5063
5064 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 */
5067 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 break;
5073 ++reduce;
5074 ++p;
5075 }
5076 }
5077
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 return FAIL;
5082 }
5083
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085 if (!evaluate)
5086 {
5087 *arg = p + 1;
5088 return OK;
5089 }
5090
5091 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093 */
5094 str = alloc((unsigned)((p - *arg) - reduce));
5095 if (str == NULL)
5096 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 rettv->v_type = VAR_STRING;
5098 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 break;
5106 ++p;
5107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 *arg = p + 1;
5112
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 return OK;
5114}
5115
5116/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 * Allocate a variable for a List and fill it from "*arg".
5118 * Return OK or FAIL.
5119 */
5120 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005123 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124 int evaluate;
5125{
Bram Moolenaar33570922005-01-25 22:26:29 +00005126 list_T *l = NULL;
5127 typval_T tv;
5128 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129
5130 if (evaluate)
5131 {
5132 l = list_alloc();
5133 if (l == NULL)
5134 return FAIL;
5135 }
5136
5137 *arg = skipwhite(*arg + 1);
5138 while (**arg != ']' && **arg != NUL)
5139 {
5140 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5141 goto failret;
5142 if (evaluate)
5143 {
5144 item = listitem_alloc();
5145 if (item != NULL)
5146 {
5147 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005148 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 list_append(l, item);
5150 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005151 else
5152 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 }
5154
5155 if (**arg == ']')
5156 break;
5157 if (**arg != ',')
5158 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 goto failret;
5161 }
5162 *arg = skipwhite(*arg + 1);
5163 }
5164
5165 if (**arg != ']')
5166 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168failret:
5169 if (evaluate)
5170 list_free(l);
5171 return FAIL;
5172 }
5173
5174 *arg = skipwhite(*arg + 1);
5175 if (evaluate)
5176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 rettv->v_type = VAR_LIST;
5178 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 ++l->lv_refcount;
5180 }
5181
5182 return OK;
5183}
5184
5185/*
5186 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005187 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005189 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190list_alloc()
5191{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005192 list_T *l;
5193
5194 l = (list_T *)alloc_clear(sizeof(list_T));
5195 if (l != NULL)
5196 {
5197 /* Prepend the list to the list of lists for garbage collection. */
5198 if (first_list != NULL)
5199 first_list->lv_used_prev = l;
5200 l->lv_used_prev = NULL;
5201 l->lv_used_next = first_list;
5202 first_list = l;
5203 }
5204 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205}
5206
5207/*
5208 * Unreference a list: decrement the reference count and free it when it
5209 * becomes zero.
5210 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005211 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005213 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005215 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5216 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217}
5218
5219/*
5220 * Free a list, including all items it points to.
5221 * Ignores the reference count.
5222 */
5223 static void
5224list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005225 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226{
Bram Moolenaar33570922005-01-25 22:26:29 +00005227 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228
Bram Moolenaard9fba312005-06-26 22:34:35 +00005229 /* Avoid that recursive reference to the list frees us again. */
5230 l->lv_refcount = DEL_REFCOUNT;
5231
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232 /* Remove the list from the list of lists for garbage collection. */
5233 if (l->lv_used_prev == NULL)
5234 first_list = l->lv_used_next;
5235 else
5236 l->lv_used_prev->lv_used_next = l->lv_used_next;
5237 if (l->lv_used_next != NULL)
5238 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5239
Bram Moolenaard9fba312005-06-26 22:34:35 +00005240 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005242 /* Remove the item before deleting it. */
5243 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 listitem_free(item);
5245 }
5246 vim_free(l);
5247}
5248
5249/*
5250 * Allocate a list item.
5251 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005252 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253listitem_alloc()
5254{
Bram Moolenaar33570922005-01-25 22:26:29 +00005255 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256}
5257
5258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005259 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 */
5261 static void
5262listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005265 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 vim_free(item);
5267}
5268
5269/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005270 * Remove a list item from a List and free it. Also clears the value.
5271 */
5272 static void
5273listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 list_T *l;
5275 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005276{
5277 list_remove(l, item, item);
5278 listitem_free(item);
5279}
5280
5281/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 * Get the number of items in a list.
5283 */
5284 static long
5285list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005286 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 if (l == NULL)
5289 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005290 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291}
5292
5293/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005294 * Return TRUE when two lists have exactly the same values.
5295 */
5296 static int
5297list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005298 list_T *l1;
5299 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 int ic; /* ignore case for strings */
5301{
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005304 if (list_len(l1) != list_len(l2))
5305 return FALSE;
5306
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005307 for (item1 = l1->lv_first, item2 = l2->lv_first;
5308 item1 != NULL && item2 != NULL;
5309 item1 = item1->li_next, item2 = item2->li_next)
5310 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5311 return FALSE;
5312 return item1 == NULL && item2 == NULL;
5313}
5314
5315/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316 * Return TRUE when two dictionaries have exactly the same key/values.
5317 */
5318 static int
5319dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005320 dict_T *d1;
5321 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005322 int ic; /* ignore case for strings */
5323{
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 hashitem_T *hi;
5325 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005326 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005327
5328 if (dict_len(d1) != dict_len(d2))
5329 return FALSE;
5330
Bram Moolenaar33570922005-01-25 22:26:29 +00005331 todo = d1->dv_hashtab.ht_used;
5332 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005334 if (!HASHITEM_EMPTY(hi))
5335 {
5336 item2 = dict_find(d2, hi->hi_key, -1);
5337 if (item2 == NULL)
5338 return FALSE;
5339 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5340 return FALSE;
5341 --todo;
5342 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005343 }
5344 return TRUE;
5345}
5346
5347/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005348 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005349 * Compares the items just like "==" would compare them, but strings and
5350 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005351 */
5352 static int
5353tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 typval_T *tv1;
5355 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005356 int ic; /* ignore case */
5357{
5358 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005360
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005361 if (tv1->v_type != tv2->v_type)
5362 return FALSE;
5363
5364 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005365 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005366 case VAR_LIST:
5367 /* recursive! */
5368 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5369
5370 case VAR_DICT:
5371 /* recursive! */
5372 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5373
5374 case VAR_FUNC:
5375 return (tv1->vval.v_string != NULL
5376 && tv2->vval.v_string != NULL
5377 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5378
5379 case VAR_NUMBER:
5380 return tv1->vval.v_number == tv2->vval.v_number;
5381
5382 case VAR_STRING:
5383 s1 = get_tv_string_buf(tv1, buf1);
5384 s2 = get_tv_string_buf(tv2, buf2);
5385 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005387
5388 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005389 return TRUE;
5390}
5391
5392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 * Locate item with index "n" in list "l" and return it.
5394 * A negative index is counted from the end; -1 is the last item.
5395 * Returns NULL when "n" is out of range.
5396 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 long n;
5401{
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 long idx;
5404
5405 if (l == NULL)
5406 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005407
5408 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005410 n = l->lv_len + n;
5411
5412 /* Check for index out of range. */
5413 if (n < 0 || n >= l->lv_len)
5414 return NULL;
5415
5416 /* When there is a cached index may start search from there. */
5417 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005419 if (n < l->lv_idx / 2)
5420 {
5421 /* closest to the start of the list */
5422 item = l->lv_first;
5423 idx = 0;
5424 }
5425 else if (n > (l->lv_idx + l->lv_len) / 2)
5426 {
5427 /* closest to the end of the list */
5428 item = l->lv_last;
5429 idx = l->lv_len - 1;
5430 }
5431 else
5432 {
5433 /* closest to the cached index */
5434 item = l->lv_idx_item;
5435 idx = l->lv_idx;
5436 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 }
5438 else
5439 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 if (n < l->lv_len / 2)
5441 {
5442 /* closest to the start of the list */
5443 item = l->lv_first;
5444 idx = 0;
5445 }
5446 else
5447 {
5448 /* closest to the end of the list */
5449 item = l->lv_last;
5450 idx = l->lv_len - 1;
5451 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005453
5454 while (n > idx)
5455 {
5456 /* search forward */
5457 item = item->li_next;
5458 ++idx;
5459 }
5460 while (n < idx)
5461 {
5462 /* search backward */
5463 item = item->li_prev;
5464 --idx;
5465 }
5466
5467 /* cache the used index */
5468 l->lv_idx = idx;
5469 l->lv_idx_item = item;
5470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 return item;
5472}
5473
5474/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005475 * Locate "item" list "l" and return its index.
5476 * Returns -1 when "item" is not in the list.
5477 */
5478 static long
5479list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 list_T *l;
5481 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005482{
5483 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005484 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005485
5486 if (l == NULL)
5487 return -1;
5488 idx = 0;
5489 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5490 ++idx;
5491 if (li == NULL)
5492 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005493 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005494}
5495
5496/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 * Append item "item" to the end of list "l".
5498 */
5499 static void
5500list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 list_T *l;
5502 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503{
5504 if (l->lv_last == NULL)
5505 {
5506 /* empty list */
5507 l->lv_first = item;
5508 l->lv_last = item;
5509 item->li_prev = NULL;
5510 }
5511 else
5512 {
5513 l->lv_last->li_next = item;
5514 item->li_prev = l->lv_last;
5515 l->lv_last = item;
5516 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005517 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 item->li_next = NULL;
5519}
5520
5521/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 */
5525 static int
5526list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005527 list_T *l;
5528 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005530 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531
Bram Moolenaar05159a02005-02-26 23:04:13 +00005532 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005534 copy_tv(tv, &li->li_tv);
5535 list_append(l, li);
5536 return OK;
5537}
5538
5539/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005540 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005541 * Return FAIL when out of memory.
5542 */
5543 int
5544list_append_dict(list, dict)
5545 list_T *list;
5546 dict_T *dict;
5547{
5548 listitem_T *li = listitem_alloc();
5549
5550 if (li == NULL)
5551 return FAIL;
5552 li->li_tv.v_type = VAR_DICT;
5553 li->li_tv.v_lock = 0;
5554 li->li_tv.vval.v_dict = dict;
5555 list_append(list, li);
5556 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return OK;
5558}
5559
5560/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005561 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005562 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005563 * Returns FAIL when out of memory.
5564 */
5565 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005566list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005567 list_T *l;
5568 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005569 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005570{
5571 listitem_T *li = listitem_alloc();
5572
5573 if (li == NULL)
5574 return FAIL;
5575 list_append(l, li);
5576 li->li_tv.v_type = VAR_STRING;
5577 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005578 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5579 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005580 return FAIL;
5581 return OK;
5582}
5583
5584/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005585 * Append "n" to list "l".
5586 * Returns FAIL when out of memory.
5587 */
5588 static int
5589list_append_number(l, n)
5590 list_T *l;
5591 varnumber_T n;
5592{
5593 listitem_T *li;
5594
5595 li = listitem_alloc();
5596 if (li == NULL)
5597 return FAIL;
5598 li->li_tv.v_type = VAR_NUMBER;
5599 li->li_tv.v_lock = 0;
5600 li->li_tv.vval.v_number = n;
5601 list_append(l, li);
5602 return OK;
5603}
5604
5605/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005606 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005607 * If "item" is NULL append at the end.
5608 * Return FAIL when out of memory.
5609 */
5610 static int
5611list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005612 list_T *l;
5613 typval_T *tv;
5614 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005615{
Bram Moolenaar33570922005-01-25 22:26:29 +00005616 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005617
5618 if (ni == NULL)
5619 return FAIL;
5620 copy_tv(tv, &ni->li_tv);
5621 if (item == NULL)
5622 /* Append new item at end of list. */
5623 list_append(l, ni);
5624 else
5625 {
5626 /* Insert new item before existing item. */
5627 ni->li_prev = item->li_prev;
5628 ni->li_next = item;
5629 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005630 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005631 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005632 ++l->lv_idx;
5633 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005634 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005635 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005636 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637 l->lv_idx_item = NULL;
5638 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005639 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005640 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005641 }
5642 return OK;
5643}
5644
5645/*
5646 * Extend "l1" with "l2".
5647 * If "bef" is NULL append at the end, otherwise insert before this item.
5648 * Returns FAIL when out of memory.
5649 */
5650 static int
5651list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005652 list_T *l1;
5653 list_T *l2;
5654 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005655{
Bram Moolenaar33570922005-01-25 22:26:29 +00005656 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657
5658 for (item = l2->lv_first; item != NULL; item = item->li_next)
5659 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5660 return FAIL;
5661 return OK;
5662}
5663
5664/*
5665 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5666 * Return FAIL when out of memory.
5667 */
5668 static int
5669list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005670 list_T *l1;
5671 list_T *l2;
5672 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005673{
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005675
5676 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005677 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005678 if (l == NULL)
5679 return FAIL;
5680 tv->v_type = VAR_LIST;
5681 tv->vval.v_list = l;
5682
5683 /* append all items from the second list */
5684 return list_extend(l, l2, NULL);
5685}
5686
5687/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005688 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005690 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691 * Returns NULL when out of memory.
5692 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005694list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005697 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698{
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 list_T *copy;
5700 listitem_T *item;
5701 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702
5703 if (orig == NULL)
5704 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705
5706 copy = list_alloc();
5707 if (copy != NULL)
5708 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005709 if (copyID != 0)
5710 {
5711 /* Do this before adding the items, because one of the items may
5712 * refer back to this list. */
5713 orig->lv_copyID = copyID;
5714 orig->lv_copylist = copy;
5715 }
5716 for (item = orig->lv_first; item != NULL && !got_int;
5717 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 {
5719 ni = listitem_alloc();
5720 if (ni == NULL)
5721 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005722 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005723 {
5724 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5725 {
5726 vim_free(ni);
5727 break;
5728 }
5729 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 list_append(copy, ni);
5733 }
5734 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005735 if (item != NULL)
5736 {
5737 list_unref(copy);
5738 copy = NULL;
5739 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 }
5741
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 return copy;
5743}
5744
5745/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005746 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005747 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005749 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005750list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 list_T *l;
5752 listitem_T *item;
5753 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005754{
Bram Moolenaar33570922005-01-25 22:26:29 +00005755 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005757 /* notify watchers */
5758 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005760 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005761 list_fix_watch(l, ip);
5762 if (ip == item2)
5763 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005765
5766 if (item2->li_next == NULL)
5767 l->lv_last = item->li_prev;
5768 else
5769 item2->li_next->li_prev = item->li_prev;
5770 if (item->li_prev == NULL)
5771 l->lv_first = item2->li_next;
5772 else
5773 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005774 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775}
5776
5777/*
5778 * Return an allocated string with the string representation of a list.
5779 * May return NULL.
5780 */
5781 static char_u *
5782list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005783 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784{
5785 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786
5787 if (tv->vval.v_list == NULL)
5788 return NULL;
5789 ga_init2(&ga, (int)sizeof(char), 80);
5790 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005791 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5792 {
5793 vim_free(ga.ga_data);
5794 return NULL;
5795 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 ga_append(&ga, ']');
5797 ga_append(&ga, NUL);
5798 return (char_u *)ga.ga_data;
5799}
5800
5801/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 * Join list "l" into a string in "*gap", using separator "sep".
5803 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005804 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005806 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807list_join(gap, l, sep, echo)
5808 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810 char_u *sep;
5811 int echo;
5812{
5813 int first = TRUE;
5814 char_u *tofree;
5815 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005816 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 char_u *s;
5818
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005819 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 {
5821 if (first)
5822 first = FALSE;
5823 else
5824 ga_concat(gap, sep);
5825
5826 if (echo)
5827 s = echo_string(&item->li_tv, &tofree, numbuf);
5828 else
5829 s = tv2string(&item->li_tv, &tofree, numbuf);
5830 if (s != NULL)
5831 ga_concat(gap, s);
5832 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005833 if (s == NULL)
5834 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005836 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837}
5838
5839/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005840 * Garbage collection for lists and dictionaries.
5841 *
5842 * We use reference counts to be able to free most items right away when they
5843 * are no longer used. But for composite items it's possible that it becomes
5844 * unused while the reference count is > 0: When there is a recursive
5845 * reference. Example:
5846 * :let l = [1, 2, 3]
5847 * :let d = {9: l}
5848 * :let l[1] = d
5849 *
5850 * Since this is quite unusual we handle this with garbage collection: every
5851 * once in a while find out which lists and dicts are not referenced from any
5852 * variable.
5853 *
5854 * Here is a good reference text about garbage collection (refers to Python
5855 * but it applies to all reference-counting mechanisms):
5856 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005857 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005858
5859/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005860 * Do garbage collection for lists and dicts.
5861 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005862 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005863 int
5864garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005865{
5866 dict_T *dd;
5867 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005868 int copyID = ++current_copyID;
5869 buf_T *buf;
5870 win_T *wp;
5871 int i;
5872 funccall_T *fc;
5873 int did_free = FALSE;
5874
5875 /*
5876 * 1. Go through all accessible variables and mark all lists and dicts
5877 * with copyID.
5878 */
5879 /* script-local variables */
5880 for (i = 1; i <= ga_scripts.ga_len; ++i)
5881 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5882
5883 /* buffer-local variables */
5884 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5885 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5886
5887 /* window-local variables */
5888 FOR_ALL_WINDOWS(wp)
5889 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5890
5891 /* global variables */
5892 set_ref_in_ht(&globvarht, copyID);
5893
5894 /* function-local variables */
5895 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5896 {
5897 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5898 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5899 }
5900
5901 /*
5902 * 2. Go through the list of dicts and free items without the copyID.
5903 */
5904 for (dd = first_dict; dd != NULL; )
5905 if (dd->dv_copyID != copyID)
5906 {
5907 dict_free(dd);
5908 did_free = TRUE;
5909
5910 /* restart, next dict may also have been freed */
5911 dd = first_dict;
5912 }
5913 else
5914 dd = dd->dv_used_next;
5915
5916 /*
5917 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005918 * But don't free a list that has a watcher (used in a for loop), these
5919 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005920 */
5921 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005922 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005923 {
5924 list_free(ll);
5925 did_free = TRUE;
5926
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005927 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005928 ll = first_list;
5929 }
5930 else
5931 ll = ll->lv_used_next;
5932
5933 return did_free;
5934}
5935
5936/*
5937 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5938 */
5939 static void
5940set_ref_in_ht(ht, copyID)
5941 hashtab_T *ht;
5942 int copyID;
5943{
5944 int todo;
5945 hashitem_T *hi;
5946
5947 todo = ht->ht_used;
5948 for (hi = ht->ht_array; todo > 0; ++hi)
5949 if (!HASHITEM_EMPTY(hi))
5950 {
5951 --todo;
5952 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5953 }
5954}
5955
5956/*
5957 * Mark all lists and dicts referenced through list "l" with "copyID".
5958 */
5959 static void
5960set_ref_in_list(l, copyID)
5961 list_T *l;
5962 int copyID;
5963{
5964 listitem_T *li;
5965
5966 for (li = l->lv_first; li != NULL; li = li->li_next)
5967 set_ref_in_item(&li->li_tv, copyID);
5968}
5969
5970/*
5971 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5972 */
5973 static void
5974set_ref_in_item(tv, copyID)
5975 typval_T *tv;
5976 int copyID;
5977{
5978 dict_T *dd;
5979 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005980
5981 switch (tv->v_type)
5982 {
5983 case VAR_DICT:
5984 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005985 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005986 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 /* Didn't see this dict yet. */
5988 dd->dv_copyID = copyID;
5989 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005990 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005991 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005992
5993 case VAR_LIST:
5994 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005995 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005996 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005997 /* Didn't see this list yet. */
5998 ll->lv_copyID = copyID;
5999 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006000 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006001 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006002 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006003 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006004}
6005
6006/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006007 * Allocate an empty header for a dictionary.
6008 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006009 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006010dict_alloc()
6011{
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006013
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006015 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006016 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006017 /* Add the list to the hashtable for garbage collection. */
6018 if (first_dict != NULL)
6019 first_dict->dv_used_prev = d;
6020 d->dv_used_next = first_dict;
6021 d->dv_used_prev = NULL;
6022
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006024 d->dv_lock = 0;
6025 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006026 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006027 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006028 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006029}
6030
6031/*
6032 * Unreference a Dictionary: decrement the reference count and free it when it
6033 * becomes zero.
6034 */
6035 static void
6036dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006038{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6040 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006041}
6042
6043/*
6044 * Free a Dictionary, including all items it contains.
6045 * Ignores the reference count.
6046 */
6047 static void
6048dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006050{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006051 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006053 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006054
Bram Moolenaard9fba312005-06-26 22:34:35 +00006055 /* Avoid that recursive reference to the dict frees us again. */
6056 d->dv_refcount = DEL_REFCOUNT;
6057
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006058 /* Remove the dict from the list of dicts for garbage collection. */
6059 if (d->dv_used_prev == NULL)
6060 first_dict = d->dv_used_next;
6061 else
6062 d->dv_used_prev->dv_used_next = d->dv_used_next;
6063 if (d->dv_used_next != NULL)
6064 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6065
6066 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006067 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 todo = d->dv_hashtab.ht_used;
6069 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006070 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006071 if (!HASHITEM_EMPTY(hi))
6072 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006073 /* Remove the item before deleting it, just in case there is
6074 * something recursive causing trouble. */
6075 di = HI2DI(hi);
6076 hash_remove(&d->dv_hashtab, hi);
6077 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006078 --todo;
6079 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006080 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006082 vim_free(d);
6083}
6084
6085/*
6086 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 * The "key" is copied to the new item.
6088 * Note that the value of the item "di_tv" still needs to be initialized!
6089 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006090 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006092dictitem_alloc(key)
6093 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006094{
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006096
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006098 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006101 di->di_flags = 0;
6102 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006104}
6105
6106/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107 * Make a copy of a Dictionary item.
6108 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006110dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112{
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006114
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006116 if (di != NULL)
6117 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006118 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006119 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006120 copy_tv(&org->di_tv, &di->di_tv);
6121 }
6122 return di;
6123}
6124
6125/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006126 * Remove item "item" from Dictionary "dict" and free it.
6127 */
6128 static void
6129dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 dict_T *dict;
6131 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132{
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006134
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006136 if (HASHITEM_EMPTY(hi))
6137 EMSG2(_(e_intern2), "dictitem_remove()");
6138 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 dictitem_free(item);
6141}
6142
6143/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006144 * Free a dict item. Also clears the value.
6145 */
6146 static void
6147dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006149{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006150 clear_tv(&item->di_tv);
6151 vim_free(item);
6152}
6153
6154/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006155 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6156 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006157 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006158 * Returns NULL when out of memory.
6159 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006161dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006163 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006164 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006165{
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 dict_T *copy;
6167 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006170
6171 if (orig == NULL)
6172 return NULL;
6173
6174 copy = dict_alloc();
6175 if (copy != NULL)
6176 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006177 if (copyID != 0)
6178 {
6179 orig->dv_copyID = copyID;
6180 orig->dv_copydict = copy;
6181 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006183 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006185 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006187 --todo;
6188
6189 di = dictitem_alloc(hi->hi_key);
6190 if (di == NULL)
6191 break;
6192 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006193 {
6194 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6195 copyID) == FAIL)
6196 {
6197 vim_free(di);
6198 break;
6199 }
6200 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006201 else
6202 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6203 if (dict_add(copy, di) == FAIL)
6204 {
6205 dictitem_free(di);
6206 break;
6207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006210
Bram Moolenaare9a41262005-01-15 22:18:47 +00006211 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006212 if (todo > 0)
6213 {
6214 dict_unref(copy);
6215 copy = NULL;
6216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006217 }
6218
6219 return copy;
6220}
6221
6222/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006225 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006226 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006227dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 dict_T *d;
6229 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006230{
Bram Moolenaar33570922005-01-25 22:26:29 +00006231 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006232}
6233
Bram Moolenaar8c711452005-01-14 21:53:12 +00006234/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006235 * Add a number or string entry to dictionary "d".
6236 * When "str" is NULL use number "nr", otherwise use "str".
6237 * Returns FAIL when out of memory and when key already exists.
6238 */
6239 int
6240dict_add_nr_str(d, key, nr, str)
6241 dict_T *d;
6242 char *key;
6243 long nr;
6244 char_u *str;
6245{
6246 dictitem_T *item;
6247
6248 item = dictitem_alloc((char_u *)key);
6249 if (item == NULL)
6250 return FAIL;
6251 item->di_tv.v_lock = 0;
6252 if (str == NULL)
6253 {
6254 item->di_tv.v_type = VAR_NUMBER;
6255 item->di_tv.vval.v_number = nr;
6256 }
6257 else
6258 {
6259 item->di_tv.v_type = VAR_STRING;
6260 item->di_tv.vval.v_string = vim_strsave(str);
6261 }
6262 if (dict_add(d, item) == FAIL)
6263 {
6264 dictitem_free(item);
6265 return FAIL;
6266 }
6267 return OK;
6268}
6269
6270/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006271 * Get the number of items in a Dictionary.
6272 */
6273 static long
6274dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006276{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277 if (d == NULL)
6278 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006280}
6281
6282/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283 * Find item "key[len]" in Dictionary "d".
6284 * If "len" is negative use strlen(key).
6285 * Returns NULL when not found.
6286 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290 char_u *key;
6291 int len;
6292{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006293#define AKEYLEN 200
6294 char_u buf[AKEYLEN];
6295 char_u *akey;
6296 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006298
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 if (len < 0)
6300 akey = key;
6301 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 tofree = akey = vim_strnsave(key, len);
6304 if (akey == NULL)
6305 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006306 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006307 else
6308 {
6309 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006310 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006311 akey = buf;
6312 }
6313
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006315 vim_free(tofree);
6316 if (HASHITEM_EMPTY(hi))
6317 return NULL;
6318 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319}
6320
6321/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006322 * Get a string item from a dictionary in allocated memory.
6323 * Returns NULL if the entry doesn't exist or out of memory.
6324 */
6325 char_u *
6326get_dict_string(d, key)
6327 dict_T *d;
6328 char_u *key;
6329{
6330 dictitem_T *di;
6331
6332 di = dict_find(d, key, -1);
6333 if (di == NULL)
6334 return NULL;
6335 return vim_strsave(get_tv_string(&di->di_tv));
6336}
6337
6338/*
6339 * Get a number item from a dictionary.
6340 * Returns 0 if the entry doesn't exist or out of memory.
6341 */
6342 long
6343get_dict_number(d, key)
6344 dict_T *d;
6345 char_u *key;
6346{
6347 dictitem_T *di;
6348
6349 di = dict_find(d, key, -1);
6350 if (di == NULL)
6351 return 0;
6352 return get_tv_number(&di->di_tv);
6353}
6354
6355/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356 * Return an allocated string with the string representation of a Dictionary.
6357 * May return NULL.
6358 */
6359 static char_u *
6360dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006362{
6363 garray_T ga;
6364 int first = TRUE;
6365 char_u *tofree;
6366 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006368 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006370 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 return NULL;
6374 ga_init2(&ga, (int)sizeof(char), 80);
6375 ga_append(&ga, '{');
6376
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006378 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006379 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006380 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006382 --todo;
6383
6384 if (first)
6385 first = FALSE;
6386 else
6387 ga_concat(&ga, (char_u *)", ");
6388
6389 tofree = string_quote(hi->hi_key, FALSE);
6390 if (tofree != NULL)
6391 {
6392 ga_concat(&ga, tofree);
6393 vim_free(tofree);
6394 }
6395 ga_concat(&ga, (char_u *)": ");
6396 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6397 if (s != NULL)
6398 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 if (s == NULL)
6401 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006402 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006403 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006404 if (todo > 0)
6405 {
6406 vim_free(ga.ga_data);
6407 return NULL;
6408 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409
6410 ga_append(&ga, '}');
6411 ga_append(&ga, NUL);
6412 return (char_u *)ga.ga_data;
6413}
6414
6415/*
6416 * Allocate a variable for a Dictionary and fill it from "*arg".
6417 * Return OK or FAIL. Returns NOTDONE for {expr}.
6418 */
6419 static int
6420get_dict_tv(arg, rettv, evaluate)
6421 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006423 int evaluate;
6424{
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 dict_T *d = NULL;
6426 typval_T tvkey;
6427 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006431 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006432
6433 /*
6434 * First check if it's not a curly-braces thing: {expr}.
6435 * Must do this without evaluating, otherwise a function may be called
6436 * twice. Unfortunately this means we need to call eval1() twice for the
6437 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006439 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006440 if (*start != '}')
6441 {
6442 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6443 return FAIL;
6444 if (*start == '}')
6445 return NOTDONE;
6446 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006447
6448 if (evaluate)
6449 {
6450 d = dict_alloc();
6451 if (d == NULL)
6452 return FAIL;
6453 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006454 tvkey.v_type = VAR_UNKNOWN;
6455 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006456
6457 *arg = skipwhite(*arg + 1);
6458 while (**arg != '}' && **arg != NUL)
6459 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006460 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461 goto failret;
6462 if (**arg != ':')
6463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006464 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006465 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006466 goto failret;
6467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006468 key = get_tv_string_buf_chk(&tvkey, buf);
6469 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006471 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6472 if (key != NULL)
6473 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006474 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006475 goto failret;
6476 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477
6478 *arg = skipwhite(*arg + 1);
6479 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006481 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482 goto failret;
6483 }
6484 if (evaluate)
6485 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006486 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487 if (item != NULL)
6488 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006489 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491 clear_tv(&tv);
6492 goto failret;
6493 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006494 item = dictitem_alloc(key);
6495 clear_tv(&tvkey);
6496 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006499 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 if (dict_add(d, item) == FAIL)
6501 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502 }
6503 }
6504
6505 if (**arg == '}')
6506 break;
6507 if (**arg != ',')
6508 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006509 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510 goto failret;
6511 }
6512 *arg = skipwhite(*arg + 1);
6513 }
6514
6515 if (**arg != '}')
6516 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006517 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006518failret:
6519 if (evaluate)
6520 dict_free(d);
6521 return FAIL;
6522 }
6523
6524 *arg = skipwhite(*arg + 1);
6525 if (evaluate)
6526 {
6527 rettv->v_type = VAR_DICT;
6528 rettv->vval.v_dict = d;
6529 ++d->dv_refcount;
6530 }
6531
6532 return OK;
6533}
6534
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 * Return a string with the string representation of a variable.
6537 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006539 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 * May return NULL;
6541 */
6542 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006543echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006545 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548 static int recurse = 0;
6549 char_u *r = NULL;
6550
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006553 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 *tofree = NULL;
6555 return NULL;
6556 }
6557 ++recurse;
6558
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 switch (tv->v_type)
6560 {
6561 case VAR_FUNC:
6562 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 r = tv->vval.v_string;
6564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 case VAR_LIST:
6566 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006567 r = *tofree;
6568 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006569 case VAR_DICT:
6570 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 r = *tofree;
6572 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006573 case VAR_STRING:
6574 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 *tofree = NULL;
6576 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006578 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006579 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006582
6583 --recurse;
6584 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006585}
6586
6587/*
6588 * Return a string with the string representation of a variable.
6589 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6590 * "numbuf" is used for a number.
6591 * Puts quotes around strings, so that they can be parsed back by eval().
6592 * May return NULL;
6593 */
6594 static char_u *
6595tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006596 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006597 char_u **tofree;
6598 char_u *numbuf;
6599{
6600 switch (tv->v_type)
6601 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006602 case VAR_FUNC:
6603 *tofree = string_quote(tv->vval.v_string, TRUE);
6604 return *tofree;
6605 case VAR_STRING:
6606 *tofree = string_quote(tv->vval.v_string, FALSE);
6607 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006609 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006610 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006611 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006612 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006613 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616}
6617
6618/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 * Return string "str" in ' quotes, doubling ' characters.
6620 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006621 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006622 */
6623 static char_u *
6624string_quote(str, function)
6625 char_u *str;
6626 int function;
6627{
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006629 char_u *p, *r, *s;
6630
Bram Moolenaar33570922005-01-25 22:26:29 +00006631 len = (function ? 13 : 3);
6632 if (str != NULL)
6633 {
6634 len += STRLEN(str);
6635 for (p = str; *p != NUL; mb_ptr_adv(p))
6636 if (*p == '\'')
6637 ++len;
6638 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006639 s = r = alloc(len);
6640 if (r != NULL)
6641 {
6642 if (function)
6643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006645 r += 10;
6646 }
6647 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006649 if (str != NULL)
6650 for (p = str; *p != NUL; )
6651 {
6652 if (*p == '\'')
6653 *r++ = '\'';
6654 MB_COPY_CHAR(p, r);
6655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006657 if (function)
6658 *r++ = ')';
6659 *r++ = NUL;
6660 }
6661 return s;
6662}
6663
6664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 * Get the value of an environment variable.
6666 * "arg" is pointing to the '$'. It is advanced to after the name.
6667 * If the environment variable was not set, silently assume it is empty.
6668 * Always return OK.
6669 */
6670 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006671get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 int evaluate;
6675{
6676 char_u *string = NULL;
6677 int len;
6678 int cc;
6679 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006680 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
6682 ++*arg;
6683 name = *arg;
6684 len = get_env_len(arg);
6685 if (evaluate)
6686 {
6687 if (len != 0)
6688 {
6689 cc = name[len];
6690 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006691 /* first try vim_getenv(), fast for normal environment vars */
6692 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006694 {
6695 if (!mustfree)
6696 string = vim_strsave(string);
6697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 else
6699 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006700 if (mustfree)
6701 vim_free(string);
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 /* next try expanding things like $VIM and ${HOME} */
6704 string = expand_env_save(name - 1);
6705 if (string != NULL && *string == '$')
6706 {
6707 vim_free(string);
6708 string = NULL;
6709 }
6710 }
6711 name[len] = cc;
6712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006713 rettv->v_type = VAR_STRING;
6714 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
6716
6717 return OK;
6718}
6719
6720/*
6721 * Array with names and number of arguments of all internal functions
6722 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6723 */
6724static struct fst
6725{
6726 char *f_name; /* function name */
6727 char f_min_argc; /* minimal number of arguments */
6728 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731} functions[] =
6732{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006733 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {"append", 2, 2, f_append},
6735 {"argc", 0, 0, f_argc},
6736 {"argidx", 0, 0, f_argidx},
6737 {"argv", 1, 1, f_argv},
6738 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006739 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {"bufexists", 1, 1, f_bufexists},
6741 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6742 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6743 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6744 {"buflisted", 1, 1, f_buflisted},
6745 {"bufloaded", 1, 1, f_bufloaded},
6746 {"bufname", 1, 1, f_bufname},
6747 {"bufnr", 1, 1, f_bufnr},
6748 {"bufwinnr", 1, 1, f_bufwinnr},
6749 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006750 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006751 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {"char2nr", 1, 1, f_char2nr},
6753 {"cindent", 1, 1, f_cindent},
6754 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006755#if defined(FEAT_INS_EXPAND)
6756 {"complete_add", 1, 1, f_complete_add},
6757 {"complete_check", 0, 0, f_complete_check},
6758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006760 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006761 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {"cscope_connection",0,3, f_cscope_connection},
6763 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006764 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {"delete", 1, 1, f_delete},
6766 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006767 {"diff_filler", 1, 1, f_diff_filler},
6768 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006769 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {"eventhandler", 0, 0, f_eventhandler},
6773 {"executable", 1, 1, f_executable},
6774 {"exists", 1, 1, f_exists},
6775 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006776 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6778 {"filereadable", 1, 1, f_filereadable},
6779 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006780 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006781 {"finddir", 1, 3, f_finddir},
6782 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"fnamemodify", 2, 2, f_fnamemodify},
6784 {"foldclosed", 1, 1, f_foldclosed},
6785 {"foldclosedend", 1, 1, f_foldclosedend},
6786 {"foldlevel", 1, 1, f_foldlevel},
6787 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006788 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006790 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006792 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006793 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {"getbufvar", 2, 2, f_getbufvar},
6795 {"getchar", 0, 1, f_getchar},
6796 {"getcharmod", 0, 0, f_getcharmod},
6797 {"getcmdline", 0, 0, f_getcmdline},
6798 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006799 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006801 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006802 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {"getfsize", 1, 1, f_getfsize},
6804 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006805 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006806 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006807 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006808 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {"getregtype", 0, 1, f_getregtype},
6810 {"getwinposx", 0, 0, f_getwinposx},
6811 {"getwinposy", 0, 0, f_getwinposy},
6812 {"getwinvar", 2, 2, f_getwinvar},
6813 {"glob", 1, 1, f_glob},
6814 {"globpath", 2, 2, f_globpath},
6815 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 {"hasmapto", 1, 2, f_hasmapto},
6818 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6819 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6820 {"histadd", 2, 2, f_histadd},
6821 {"histdel", 1, 2, f_histdel},
6822 {"histget", 1, 2, f_histget},
6823 {"histnr", 1, 1, f_histnr},
6824 {"hlID", 1, 1, f_hlID},
6825 {"hlexists", 1, 1, f_hlexists},
6826 {"hostname", 0, 0, f_hostname},
6827 {"iconv", 3, 3, f_iconv},
6828 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006829 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006830 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006832 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {"inputrestore", 0, 0, f_inputrestore},
6834 {"inputsave", 0, 0, f_inputsave},
6835 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006838 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006840 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006841 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006843 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {"libcall", 3, 3, f_libcall},
6845 {"libcallnr", 3, 3, f_libcallnr},
6846 {"line", 1, 1, f_line},
6847 {"line2byte", 1, 1, f_line2byte},
6848 {"lispindent", 1, 1, f_lispindent},
6849 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"maparg", 1, 2, f_maparg},
6852 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006853 {"match", 2, 4, f_match},
6854 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006855 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006856 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006857 {"max", 1, 1, f_max},
6858 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006859#ifdef vim_mkdir
6860 {"mkdir", 1, 3, f_mkdir},
6861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {"mode", 0, 0, f_mode},
6863 {"nextnonblank", 1, 1, f_nextnonblank},
6864 {"nr2char", 1, 1, f_nr2char},
6865 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006866 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006867 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006868 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {"remote_expr", 2, 3, f_remote_expr},
6870 {"remote_foreground", 1, 1, f_remote_foreground},
6871 {"remote_peek", 1, 2, f_remote_peek},
6872 {"remote_read", 1, 1, f_remote_read},
6873 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006874 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006876 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006878 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006880 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {"searchpair", 3, 5, f_searchpair},
6882 {"server2client", 2, 2, f_server2client},
6883 {"serverlist", 0, 0, f_serverlist},
6884 {"setbufvar", 3, 3, f_setbufvar},
6885 {"setcmdpos", 1, 1, f_setcmdpos},
6886 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006887 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {"setreg", 2, 3, f_setreg},
6889 {"setwinvar", 3, 3, f_setwinvar},
6890 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006891 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006892 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006893 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006894 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006895 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896#ifdef HAVE_STRFTIME
6897 {"strftime", 1, 2, f_strftime},
6898#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006899 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006900 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {"strlen", 1, 1, f_strlen},
6902 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006903 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"strtrans", 1, 1, f_strtrans},
6905 {"submatch", 1, 1, f_submatch},
6906 {"substitute", 4, 4, f_substitute},
6907 {"synID", 3, 3, f_synID},
6908 {"synIDattr", 2, 3, f_synIDattr},
6909 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006910 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006911 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006912 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006914 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {"tolower", 1, 1, f_tolower},
6916 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006917 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006919 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {"virtcol", 1, 1, f_virtcol},
6921 {"visualmode", 0, 1, f_visualmode},
6922 {"winbufnr", 1, 1, f_winbufnr},
6923 {"wincol", 0, 0, f_wincol},
6924 {"winheight", 1, 1, f_winheight},
6925 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006926 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {"winrestcmd", 0, 0, f_winrestcmd},
6928 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006929 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930};
6931
6932#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6933
6934/*
6935 * Function given to ExpandGeneric() to obtain the list of internal
6936 * or user defined function names.
6937 */
6938 char_u *
6939get_function_name(xp, idx)
6940 expand_T *xp;
6941 int idx;
6942{
6943 static int intidx = -1;
6944 char_u *name;
6945
6946 if (idx == 0)
6947 intidx = -1;
6948 if (intidx < 0)
6949 {
6950 name = get_user_func_name(xp, idx);
6951 if (name != NULL)
6952 return name;
6953 }
6954 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6955 {
6956 STRCPY(IObuff, functions[intidx].f_name);
6957 STRCAT(IObuff, "(");
6958 if (functions[intidx].f_max_argc == 0)
6959 STRCAT(IObuff, ")");
6960 return IObuff;
6961 }
6962
6963 return NULL;
6964}
6965
6966/*
6967 * Function given to ExpandGeneric() to obtain the list of internal or
6968 * user defined variable or function names.
6969 */
6970/*ARGSUSED*/
6971 char_u *
6972get_expr_name(xp, idx)
6973 expand_T *xp;
6974 int idx;
6975{
6976 static int intidx = -1;
6977 char_u *name;
6978
6979 if (idx == 0)
6980 intidx = -1;
6981 if (intidx < 0)
6982 {
6983 name = get_function_name(xp, idx);
6984 if (name != NULL)
6985 return name;
6986 }
6987 return get_user_var_name(xp, ++intidx);
6988}
6989
6990#endif /* FEAT_CMDL_COMPL */
6991
6992/*
6993 * Find internal function in table above.
6994 * Return index, or -1 if not found
6995 */
6996 static int
6997find_internal_func(name)
6998 char_u *name; /* name of the function */
6999{
7000 int first = 0;
7001 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7002 int cmp;
7003 int x;
7004
7005 /*
7006 * Find the function name in the table. Binary search.
7007 */
7008 while (first <= last)
7009 {
7010 x = first + ((unsigned)(last - first) >> 1);
7011 cmp = STRCMP(name, functions[x].f_name);
7012 if (cmp < 0)
7013 last = x - 1;
7014 else if (cmp > 0)
7015 first = x + 1;
7016 else
7017 return x;
7018 }
7019 return -1;
7020}
7021
7022/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007023 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7024 * name it contains, otherwise return "name".
7025 */
7026 static char_u *
7027deref_func_name(name, lenp)
7028 char_u *name;
7029 int *lenp;
7030{
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007032 int cc;
7033
7034 cc = name[*lenp];
7035 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007036 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007039 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007040 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007041 {
7042 *lenp = 0;
7043 return (char_u *)""; /* just in case */
7044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 *lenp = STRLEN(v->di_tv.vval.v_string);
7046 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007047 }
7048
7049 return name;
7050}
7051
7052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 * Allocate a variable for the result of a function.
7054 * Return OK or FAIL.
7055 */
7056 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007057get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7058 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 char_u *name; /* name of the function */
7060 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 char_u **arg; /* argument, pointing to the '(' */
7063 linenr_T firstline; /* first line of range */
7064 linenr_T lastline; /* last line of range */
7065 int *doesrange; /* return: function handled range */
7066 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 char_u *argp;
7070 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 int argcount = 0; /* number of arguments found */
7073
7074 /*
7075 * Get the arguments.
7076 */
7077 argp = *arg;
7078 while (argcount < MAX_FUNC_ARGS)
7079 {
7080 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7081 if (*argp == ')' || *argp == ',' || *argp == NUL)
7082 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7084 {
7085 ret = FAIL;
7086 break;
7087 }
7088 ++argcount;
7089 if (*argp != ',')
7090 break;
7091 }
7092 if (*argp == ')')
7093 ++argp;
7094 else
7095 ret = FAIL;
7096
7097 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007098 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007099 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 {
7102 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007103 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007105 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007109 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111 *arg = skipwhite(argp);
7112 return ret;
7113}
7114
7115
7116/*
7117 * Call a function with its resolved parameters
7118 * Return OK or FAIL.
7119 */
7120 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007121call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007122 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 char_u *name; /* name of the function */
7124 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 linenr_T firstline; /* first line of range */
7129 linenr_T lastline; /* last line of range */
7130 int *doesrange; /* return: function handled range */
7131 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133{
7134 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135#define ERROR_UNKNOWN 0
7136#define ERROR_TOOMANY 1
7137#define ERROR_TOOFEW 2
7138#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007139#define ERROR_DICT 4
7140#define ERROR_NONE 5
7141#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 int error = ERROR_NONE;
7143 int i;
7144 int llen;
7145 ufunc_T *fp;
7146 int cc;
7147#define FLEN_FIXED 40
7148 char_u fname_buf[FLEN_FIXED + 1];
7149 char_u *fname;
7150
7151 /*
7152 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7153 * Change <SNR>123_name() to K_SNR 123_name().
7154 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7155 */
7156 cc = name[len];
7157 name[len] = NUL;
7158 llen = eval_fname_script(name);
7159 if (llen > 0)
7160 {
7161 fname_buf[0] = K_SPECIAL;
7162 fname_buf[1] = KS_EXTRA;
7163 fname_buf[2] = (int)KE_SNR;
7164 i = 3;
7165 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7166 {
7167 if (current_SID <= 0)
7168 error = ERROR_SCRIPT;
7169 else
7170 {
7171 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7172 i = (int)STRLEN(fname_buf);
7173 }
7174 }
7175 if (i + STRLEN(name + llen) < FLEN_FIXED)
7176 {
7177 STRCPY(fname_buf + i, name + llen);
7178 fname = fname_buf;
7179 }
7180 else
7181 {
7182 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7183 if (fname == NULL)
7184 error = ERROR_OTHER;
7185 else
7186 {
7187 mch_memmove(fname, fname_buf, (size_t)i);
7188 STRCPY(fname + i, name + llen);
7189 }
7190 }
7191 }
7192 else
7193 fname = name;
7194
7195 *doesrange = FALSE;
7196
7197
7198 /* execute the function if no errors detected and executing */
7199 if (evaluate && error == ERROR_NONE)
7200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007201 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 error = ERROR_UNKNOWN;
7203
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007204 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
7206 /*
7207 * User defined function.
7208 */
7209 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007210
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007212 /* Trigger FuncUndefined event, may load the function. */
7213 if (fp == NULL
7214 && apply_autocmds(EVENT_FUNCUNDEFINED,
7215 fname, fname, TRUE, NULL)
7216 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007218 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 fp = find_func(fname);
7220 }
7221#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007222 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007223 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007224 {
7225 /* loaded a package, search for the function again */
7226 fp = find_func(fname);
7227 }
7228
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 if (fp != NULL)
7230 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007231 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007233 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007235 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007237 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 else
7240 {
7241 /*
7242 * Call the user function.
7243 * Save and restore search patterns, script variables and
7244 * redo buffer.
7245 */
7246 save_search_patterns();
7247 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007248 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007249 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007250 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007251 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7252 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7253 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007254 /* Function was unreferenced while being used, free it
7255 * now. */
7256 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 restoreRedobuff();
7258 restore_search_patterns();
7259 error = ERROR_NONE;
7260 }
7261 }
7262 }
7263 else
7264 {
7265 /*
7266 * Find the function name in the table, call its implementation.
7267 */
7268 i = find_internal_func(fname);
7269 if (i >= 0)
7270 {
7271 if (argcount < functions[i].f_min_argc)
7272 error = ERROR_TOOFEW;
7273 else if (argcount > functions[i].f_max_argc)
7274 error = ERROR_TOOMANY;
7275 else
7276 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007277 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007278 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 error = ERROR_NONE;
7280 }
7281 }
7282 }
7283 /*
7284 * The function call (or "FuncUndefined" autocommand sequence) might
7285 * have been aborted by an error, an interrupt, or an explicitly thrown
7286 * exception that has not been caught so far. This situation can be
7287 * tested for by calling aborting(). For an error in an internal
7288 * function or for the "E132" error in call_user_func(), however, the
7289 * throw point at which the "force_abort" flag (temporarily reset by
7290 * emsg()) is normally updated has not been reached yet. We need to
7291 * update that flag first to make aborting() reliable.
7292 */
7293 update_force_abort();
7294 }
7295 if (error == ERROR_NONE)
7296 ret = OK;
7297
7298 /*
7299 * Report an error unless the argument evaluation or function call has been
7300 * cancelled due to an aborting error, an interrupt, or an exception.
7301 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 if (!aborting())
7303 {
7304 switch (error)
7305 {
7306 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007307 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 break;
7309 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007310 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 break;
7312 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007313 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 name);
7315 break;
7316 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007317 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318 name);
7319 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007321 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 name);
7323 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324 }
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326
7327 name[len] = cc;
7328 if (fname != name && fname != fname_buf)
7329 vim_free(fname);
7330
7331 return ret;
7332}
7333
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007334/*
7335 * Give an error message with a function name. Handle <SNR> things.
7336 */
7337 static void
7338emsg_funcname(msg, name)
7339 char *msg;
7340 char_u *name;
7341{
7342 char_u *p;
7343
7344 if (*name == K_SPECIAL)
7345 p = concat_str((char_u *)"<SNR>", name + 3);
7346 else
7347 p = name;
7348 EMSG2(_(msg), p);
7349 if (p != name)
7350 vim_free(p);
7351}
7352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353/*********************************************
7354 * Implementation of the built-in functions
7355 */
7356
7357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007358 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 */
7360 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007361f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 typval_T *argvars;
7363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007367 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007368 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007370 if ((l = argvars[0].vval.v_list) != NULL
7371 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7372 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007373 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007374 }
7375 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007376 EMSG(_(e_listreq));
7377}
7378
7379/*
7380 * "append(lnum, string/list)" function
7381 */
7382 static void
7383f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 typval_T *argvars;
7385 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007386{
7387 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007388 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 list_T *l = NULL;
7390 listitem_T *li = NULL;
7391 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007392 long added = 0;
7393
Bram Moolenaar0d660222005-01-07 21:51:51 +00007394 lnum = get_tv_lnum(argvars);
7395 if (lnum >= 0
7396 && lnum <= curbuf->b_ml.ml_line_count
7397 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007398 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007399 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007400 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007401 l = argvars[1].vval.v_list;
7402 if (l == NULL)
7403 return;
7404 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007407 for (;;)
7408 {
7409 if (l == NULL)
7410 tv = &argvars[1]; /* append a string */
7411 else if (li == NULL)
7412 break; /* end of list */
7413 else
7414 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007415 line = get_tv_string_chk(tv);
7416 if (line == NULL) /* type error */
7417 {
7418 rettv->vval.v_number = 1; /* Failed */
7419 break;
7420 }
7421 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007422 ++added;
7423 if (l == NULL)
7424 break;
7425 li = li->li_next;
7426 }
7427
7428 appended_lines_mark(lnum, added);
7429 if (curwin->w_cursor.lnum > lnum)
7430 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007432 else
7433 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435
7436/*
7437 * "argc()" function
7438 */
7439/* ARGSUSED */
7440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 typval_T *argvars;
7443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446}
7447
7448/*
7449 * "argidx()" function
7450 */
7451/* ARGSUSED */
7452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007453f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 typval_T *argvars;
7455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007457 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458}
7459
7460/*
7461 * "argv(nr)" function
7462 */
7463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 typval_T *argvars;
7466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
7468 int idx;
7469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007474 rettv->vval.v_string = NULL;
7475 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476}
7477
7478/*
7479 * "browse(save, title, initdir, default)" function
7480 */
7481/* ARGSUSED */
7482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007483f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 typval_T *argvars;
7485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
7487#ifdef FEAT_BROWSE
7488 int save;
7489 char_u *title;
7490 char_u *initdir;
7491 char_u *defname;
7492 char_u buf[NUMBUFLEN];
7493 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007494 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007496 save = get_tv_number_chk(&argvars[0], &error);
7497 title = get_tv_string_chk(&argvars[1]);
7498 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7499 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007501 if (error || title == NULL || initdir == NULL || defname == NULL)
7502 rettv->vval.v_string = NULL;
7503 else
7504 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007505 do_browse(save ? BROWSE_SAVE : 0,
7506 title, defname, NULL, initdir, NULL, curbuf);
7507#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007509#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007510 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007511}
7512
7513/*
7514 * "browsedir(title, initdir)" function
7515 */
7516/* ARGSUSED */
7517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007518f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 typval_T *argvars;
7520 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007521{
7522#ifdef FEAT_BROWSE
7523 char_u *title;
7524 char_u *initdir;
7525 char_u buf[NUMBUFLEN];
7526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527 title = get_tv_string_chk(&argvars[0]);
7528 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007530 if (title == NULL || initdir == NULL)
7531 rettv->vval.v_string = NULL;
7532 else
7533 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007534 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539}
7540
Bram Moolenaar33570922005-01-25 22:26:29 +00007541static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543/*
7544 * Find a buffer by number or exact name.
7545 */
7546 static buf_T *
7547find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
7550 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007552 if (avar->v_type == VAR_NUMBER)
7553 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007554 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007556 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007557 if (buf == NULL)
7558 {
7559 /* No full path name match, try a match with a URL or a "nofile"
7560 * buffer, these don't use the full path. */
7561 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7562 if (buf->b_fname != NULL
7563 && (path_with_url(buf->b_fname)
7564#ifdef FEAT_QUICKFIX
7565 || bt_nofile(buf)
7566#endif
7567 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007569 break;
7570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 }
7572 return buf;
7573}
7574
7575/*
7576 * "bufexists(expr)" function
7577 */
7578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007579f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 typval_T *argvars;
7581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007583 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584}
7585
7586/*
7587 * "buflisted(expr)" function
7588 */
7589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007590f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 typval_T *argvars;
7592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
7594 buf_T *buf;
7595
7596 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598}
7599
7600/*
7601 * "bufloaded(expr)" function
7602 */
7603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 typval_T *argvars;
7606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608 buf_T *buf;
7609
7610 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007611 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612}
7613
Bram Moolenaar33570922005-01-25 22:26:29 +00007614static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007615
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616/*
7617 * Get buffer by number or pattern.
7618 */
7619 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 int save_magic;
7625 char_u *save_cpo;
7626 buf_T *buf;
7627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 if (tv->v_type == VAR_NUMBER)
7629 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007630 if (tv->v_type != VAR_STRING)
7631 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (name == NULL || *name == NUL)
7633 return curbuf;
7634 if (name[0] == '$' && name[1] == NUL)
7635 return lastbuf;
7636
7637 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7638 save_magic = p_magic;
7639 p_magic = TRUE;
7640 save_cpo = p_cpo;
7641 p_cpo = (char_u *)"";
7642
7643 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7644 TRUE, FALSE));
7645
7646 p_magic = save_magic;
7647 p_cpo = save_cpo;
7648
7649 /* If not found, try expanding the name, like done for bufexists(). */
7650 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652
7653 return buf;
7654}
7655
7656/*
7657 * "bufname(expr)" function
7658 */
7659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *argvars;
7662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664 buf_T *buf;
7665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007666 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 buf = get_buf_tv(&argvars[0]);
7669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 --emsg_off;
7675}
7676
7677/*
7678 * "bufnr(expr)" function
7679 */
7680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *argvars;
7683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 buf_T *buf;
7686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007687 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 --emsg_off;
7695}
7696
7697/*
7698 * "bufwinnr(nr)" function
7699 */
7700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 typval_T *argvars;
7703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
7705#ifdef FEAT_WINDOWS
7706 win_T *wp;
7707 int winnr = 0;
7708#endif
7709 buf_T *buf;
7710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007711 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714#ifdef FEAT_WINDOWS
7715 for (wp = firstwin; wp; wp = wp->w_next)
7716 {
7717 ++winnr;
7718 if (wp->w_buffer == buf)
7719 break;
7720 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007721 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724#endif
7725 --emsg_off;
7726}
7727
7728/*
7729 * "byte2line(byte)" function
7730 */
7731/*ARGSUSED*/
7732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007733f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007734 typval_T *argvars;
7735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739#else
7740 long boff = 0;
7741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007746 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 (linenr_T)0, &boff);
7748#endif
7749}
7750
7751/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007752 * "byteidx()" function
7753 */
7754/*ARGSUSED*/
7755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007756f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 typval_T *argvars;
7758 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007759{
7760#ifdef FEAT_MBYTE
7761 char_u *t;
7762#endif
7763 char_u *str;
7764 long idx;
7765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 str = get_tv_string_chk(&argvars[0]);
7767 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007770 return;
7771
7772#ifdef FEAT_MBYTE
7773 t = str;
7774 for ( ; idx > 0; idx--)
7775 {
7776 if (*t == NUL) /* EOL reached */
7777 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007778 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007781#else
7782 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007784#endif
7785}
7786
7787/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007788 * "call(func, arglist)" function
7789 */
7790 static void
7791f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 typval_T *argvars;
7793 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007794{
7795 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007797 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007799 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007800 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007801
7802 rettv->vval.v_number = 0;
7803 if (argvars[1].v_type != VAR_LIST)
7804 {
7805 EMSG(_(e_listreq));
7806 return;
7807 }
7808 if (argvars[1].vval.v_list == NULL)
7809 return;
7810
7811 if (argvars[0].v_type == VAR_FUNC)
7812 func = argvars[0].vval.v_string;
7813 else
7814 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007815 if (*func == NUL)
7816 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007817
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 if (argvars[2].v_type != VAR_UNKNOWN)
7819 {
7820 if (argvars[2].v_type != VAR_DICT)
7821 {
7822 EMSG(_(e_dictreq));
7823 return;
7824 }
7825 selfdict = argvars[2].vval.v_dict;
7826 }
7827
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007828 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7829 item = item->li_next)
7830 {
7831 if (argc == MAX_FUNC_ARGS)
7832 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007833 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007834 break;
7835 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007836 /* Make a copy of each argument. This is needed to be able to set
7837 * v_lock to VAR_FIXED in the copy without changing the original list.
7838 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007839 copy_tv(&item->li_tv, &argv[argc++]);
7840 }
7841
7842 if (item == NULL)
7843 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7845 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007846
7847 /* Free the arguments. */
7848 while (argc > 0)
7849 clear_tv(&argv[--argc]);
7850}
7851
7852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 * "char2nr(string)" function
7854 */
7855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 typval_T *argvars;
7858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860#ifdef FEAT_MBYTE
7861 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007862 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 else
7864#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866}
7867
7868/*
7869 * "cindent(lnum)" function
7870 */
7871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 typval_T *argvars;
7874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876#ifdef FEAT_CINDENT
7877 pos_T pos;
7878 linenr_T lnum;
7879
7880 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7883 {
7884 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 curwin->w_cursor = pos;
7887 }
7888 else
7889#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891}
7892
7893/*
7894 * "col(string)" function
7895 */
7896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007898 typval_T *argvars;
7899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900{
7901 colnr_T col = 0;
7902 pos_T *fp;
7903
7904 fp = var2fpos(&argvars[0], FALSE);
7905 if (fp != NULL)
7906 {
7907 if (fp->col == MAXCOL)
7908 {
7909 /* '> can be MAXCOL, get the length of the line then */
7910 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7911 col = STRLEN(ml_get(fp->lnum)) + 1;
7912 else
7913 col = MAXCOL;
7914 }
7915 else
7916 {
7917 col = fp->col + 1;
7918#ifdef FEAT_VIRTUALEDIT
7919 /* col(".") when the cursor is on the NUL at the end of the line
7920 * because of "coladd" can be seen as an extra column. */
7921 if (virtual_active() && fp == &curwin->w_cursor)
7922 {
7923 char_u *p = ml_get_cursor();
7924
7925 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7926 curwin->w_virtcol - curwin->w_cursor.coladd))
7927 {
7928# ifdef FEAT_MBYTE
7929 int l;
7930
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007931 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 col += l;
7933# else
7934 if (*p != NUL && p[1] == NUL)
7935 ++col;
7936# endif
7937 }
7938 }
7939#endif
7940 }
7941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943}
7944
Bram Moolenaar572cb562005-08-05 21:35:02 +00007945#if defined(FEAT_INS_EXPAND)
7946/*
7947 * "complete_add()" function
7948 */
7949/*ARGSUSED*/
7950 static void
7951f_complete_add(argvars, rettv)
7952 typval_T *argvars;
7953 typval_T *rettv;
7954{
7955 char_u *s;
7956
7957 s = get_tv_string_chk(&argvars[0]);
7958 if (s != NULL)
7959 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7960}
7961
7962/*
7963 * "complete_check()" function
7964 */
7965/*ARGSUSED*/
7966 static void
7967f_complete_check(argvars, rettv)
7968 typval_T *argvars;
7969 typval_T *rettv;
7970{
7971 int saved = RedrawingDisabled;
7972
7973 RedrawingDisabled = 0;
7974 ins_compl_check_keys(0);
7975 rettv->vval.v_number = compl_interrupted;
7976 RedrawingDisabled = saved;
7977}
7978#endif
7979
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980/*
7981 * "confirm(message, buttons[, default [, type]])" function
7982 */
7983/*ARGSUSED*/
7984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 typval_T *argvars;
7987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7990 char_u *message;
7991 char_u *buttons = NULL;
7992 char_u buf[NUMBUFLEN];
7993 char_u buf2[NUMBUFLEN];
7994 int def = 1;
7995 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007996 char_u *typestr;
7997 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007999 message = get_tv_string_chk(&argvars[0]);
8000 if (message == NULL)
8001 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008002 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008004 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8005 if (buttons == NULL)
8006 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008009 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008010 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008012 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8013 if (typestr == NULL)
8014 error = TRUE;
8015 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008017 switch (TOUPPER_ASC(*typestr))
8018 {
8019 case 'E': type = VIM_ERROR; break;
8020 case 'Q': type = VIM_QUESTION; break;
8021 case 'I': type = VIM_INFO; break;
8022 case 'W': type = VIM_WARNING; break;
8023 case 'G': type = VIM_GENERIC; break;
8024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
8026 }
8027 }
8028 }
8029
8030 if (buttons == NULL || *buttons == NUL)
8031 buttons = (char_u *)_("&Ok");
8032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008033 if (error)
8034 rettv->vval.v_number = 0;
8035 else
8036 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 def, NULL);
8038#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008039 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040#endif
8041}
8042
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008043/*
8044 * "copy()" function
8045 */
8046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 typval_T *argvars;
8049 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008051 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
8054/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008055 * "count()" function
8056 */
8057 static void
8058f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 typval_T *argvars;
8060 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008061{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008062 long n = 0;
8063 int ic = FALSE;
8064
Bram Moolenaare9a41262005-01-15 22:18:47 +00008065 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008066 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 listitem_T *li;
8068 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008069 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008070
Bram Moolenaare9a41262005-01-15 22:18:47 +00008071 if ((l = argvars[0].vval.v_list) != NULL)
8072 {
8073 li = l->lv_first;
8074 if (argvars[2].v_type != VAR_UNKNOWN)
8075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008076 int error = FALSE;
8077
8078 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008079 if (argvars[3].v_type != VAR_UNKNOWN)
8080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 idx = get_tv_number_chk(&argvars[3], &error);
8082 if (!error)
8083 {
8084 li = list_find(l, idx);
8085 if (li == NULL)
8086 EMSGN(_(e_listidx), idx);
8087 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 if (error)
8090 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008091 }
8092
8093 for ( ; li != NULL; li = li->li_next)
8094 if (tv_equal(&li->li_tv, &argvars[1], ic))
8095 ++n;
8096 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008098 else if (argvars[0].v_type == VAR_DICT)
8099 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 int todo;
8101 dict_T *d;
8102 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008103
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008104 if ((d = argvars[0].vval.v_dict) != NULL)
8105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008106 int error = FALSE;
8107
Bram Moolenaare9a41262005-01-15 22:18:47 +00008108 if (argvars[2].v_type != VAR_UNKNOWN)
8109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008110 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008111 if (argvars[3].v_type != VAR_UNKNOWN)
8112 EMSG(_(e_invarg));
8113 }
8114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008115 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008116 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008117 {
8118 if (!HASHITEM_EMPTY(hi))
8119 {
8120 --todo;
8121 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8122 ++n;
8123 }
8124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008125 }
8126 }
8127 else
8128 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008129 rettv->vval.v_number = n;
8130}
8131
8132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8134 *
8135 * Checks the existence of a cscope connection.
8136 */
8137/*ARGSUSED*/
8138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008139f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 typval_T *argvars;
8141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143#ifdef FEAT_CSCOPE
8144 int num = 0;
8145 char_u *dbpath = NULL;
8146 char_u *prepend = NULL;
8147 char_u buf[NUMBUFLEN];
8148
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 if (argvars[0].v_type != VAR_UNKNOWN
8150 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 num = (int)get_tv_number(&argvars[0]);
8153 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008154 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 }
8157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008160 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161#endif
8162}
8163
8164/*
8165 * "cursor(lnum, col)" function
8166 *
8167 * Moves the cursor to the specified line and column
8168 */
8169/*ARGSUSED*/
8170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008171f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 typval_T *argvars;
8173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
8175 long line, col;
8176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008177 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008178 col = get_tv_number_chk(&argvars[1], NULL);
8179 if (line < 0 || col < 0)
8180 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (line > 0)
8182 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 if (col > 0)
8184 curwin->w_cursor.col = col - 1;
8185#ifdef FEAT_VIRTUALEDIT
8186 curwin->w_cursor.coladd = 0;
8187#endif
8188
8189 /* Make sure the cursor is in a valid position. */
8190 check_cursor();
8191#ifdef FEAT_MBYTE
8192 /* Correct cursor for multi-byte character. */
8193 if (has_mbyte)
8194 mb_adjust_cursor();
8195#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008196
8197 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198}
8199
8200/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008201 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 */
8203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008204f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 typval_T *argvars;
8206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008208 int noref = 0;
8209
8210 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008211 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008212 if (noref < 0 || noref > 1)
8213 EMSG(_(e_invarg));
8214 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008215 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216}
8217
8218/*
8219 * "delete()" function
8220 */
8221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008223 typval_T *argvars;
8224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225{
8226 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008229 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230}
8231
8232/*
8233 * "did_filetype()" function
8234 */
8235/*ARGSUSED*/
8236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008238 typval_T *argvars;
8239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240{
8241#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245#endif
8246}
8247
8248/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008249 * "diff_filler()" function
8250 */
8251/*ARGSUSED*/
8252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 typval_T *argvars;
8255 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008256{
8257#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008258 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008259#endif
8260}
8261
8262/*
8263 * "diff_hlID()" function
8264 */
8265/*ARGSUSED*/
8266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008268 typval_T *argvars;
8269 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008270{
8271#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008273 static linenr_T prev_lnum = 0;
8274 static int changedtick = 0;
8275 static int fnum = 0;
8276 static int change_start = 0;
8277 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008278 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008279 int filler_lines;
8280 int col;
8281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008282 if (lnum < 0) /* ignore type error in {lnum} arg */
8283 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008284 if (lnum != prev_lnum
8285 || changedtick != curbuf->b_changedtick
8286 || fnum != curbuf->b_fnum)
8287 {
8288 /* New line, buffer, change: need to get the values. */
8289 filler_lines = diff_check(curwin, lnum);
8290 if (filler_lines < 0)
8291 {
8292 if (filler_lines == -1)
8293 {
8294 change_start = MAXCOL;
8295 change_end = -1;
8296 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8297 hlID = HLF_ADD; /* added line */
8298 else
8299 hlID = HLF_CHD; /* changed line */
8300 }
8301 else
8302 hlID = HLF_ADD; /* added line */
8303 }
8304 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008305 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008306 prev_lnum = lnum;
8307 changedtick = curbuf->b_changedtick;
8308 fnum = curbuf->b_fnum;
8309 }
8310
8311 if (hlID == HLF_CHD || hlID == HLF_TXD)
8312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008313 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008314 if (col >= change_start && col <= change_end)
8315 hlID = HLF_TXD; /* changed text */
8316 else
8317 hlID = HLF_CHD; /* changed line */
8318 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008319 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008320#endif
8321}
8322
8323/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008324 * "empty({expr})" function
8325 */
8326 static void
8327f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 typval_T *argvars;
8329 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008330{
8331 int n;
8332
8333 switch (argvars[0].v_type)
8334 {
8335 case VAR_STRING:
8336 case VAR_FUNC:
8337 n = argvars[0].vval.v_string == NULL
8338 || *argvars[0].vval.v_string == NUL;
8339 break;
8340 case VAR_NUMBER:
8341 n = argvars[0].vval.v_number == 0;
8342 break;
8343 case VAR_LIST:
8344 n = argvars[0].vval.v_list == NULL
8345 || argvars[0].vval.v_list->lv_first == NULL;
8346 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008347 case VAR_DICT:
8348 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008350 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008351 default:
8352 EMSG2(_(e_intern2), "f_empty()");
8353 n = 0;
8354 }
8355
8356 rettv->vval.v_number = n;
8357}
8358
8359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 * "escape({string}, {chars})" function
8361 */
8362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 typval_T *argvars;
8365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
8367 char_u buf[NUMBUFLEN];
8368
Bram Moolenaar758711c2005-02-02 23:11:38 +00008369 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8370 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372}
8373
8374/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008375 * "eval()" function
8376 */
8377/*ARGSUSED*/
8378 static void
8379f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 typval_T *argvars;
8381 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008382{
8383 char_u *s;
8384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008385 s = get_tv_string_chk(&argvars[0]);
8386 if (s != NULL)
8387 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008389 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8390 {
8391 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008392 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008393 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008394 else if (*s != NUL)
8395 EMSG(_(e_trailing));
8396}
8397
8398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 * "eventhandler()" function
8400 */
8401/*ARGSUSED*/
8402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008403f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008404 typval_T *argvars;
8405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008407 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408}
8409
8410/*
8411 * "executable()" function
8412 */
8413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008414f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 typval_T *argvars;
8416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419}
8420
8421/*
8422 * "exists()" function
8423 */
8424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 typval_T *argvars;
8427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 char_u *p;
8430 char_u *name;
8431 int n = FALSE;
8432 int len = 0;
8433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008434 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (*p == '$') /* environment variable */
8436 {
8437 /* first try "normal" environment variables (fast) */
8438 if (mch_getenv(p + 1) != NULL)
8439 n = TRUE;
8440 else
8441 {
8442 /* try expanding things like $VIM and ${HOME} */
8443 p = expand_env_save(p);
8444 if (p != NULL && *p != '$')
8445 n = TRUE;
8446 vim_free(p);
8447 }
8448 }
8449 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 else if (*p == '*') /* internal or user defined function */
8452 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008453 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 else if (*p == ':')
8456 {
8457 n = cmd_exists(p + 1);
8458 }
8459 else if (*p == '#')
8460 {
8461#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008462 if (p[1] == '#')
8463 n = autocmd_supported(p + 2);
8464 else
8465 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466#endif
8467 }
8468 else /* internal variable */
8469 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008470 char_u *tofree;
8471 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008473 /* get_name_len() takes care of expanding curly braces */
8474 name = p;
8475 len = get_name_len(&p, &tofree, TRUE, FALSE);
8476 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008478 if (tofree != NULL)
8479 name = tofree;
8480 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8481 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008483 /* handle d.key, l[idx], f(expr) */
8484 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8485 if (n)
8486 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008490 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 }
8492
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494}
8495
8496/*
8497 * "expand()" function
8498 */
8499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008500f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 typval_T *argvars;
8502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
8504 char_u *s;
8505 int len;
8506 char_u *errormsg;
8507 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8508 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008509 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511 rettv->v_type = VAR_STRING;
8512 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (*s == '%' || *s == '#' || *s == '<')
8514 {
8515 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 --emsg_off;
8518 }
8519 else
8520 {
8521 /* When the optional second argument is non-zero, don't remove matches
8522 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523 if (argvars[1].v_type != VAR_UNKNOWN
8524 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008526 if (!error)
8527 {
8528 ExpandInit(&xpc);
8529 xpc.xp_context = EXPAND_FILES;
8530 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8531 ExpandCleanup(&xpc);
8532 }
8533 else
8534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
8536}
8537
8538/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008539 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008541 */
8542 static void
8543f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008544 typval_T *argvars;
8545 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008546{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008547 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008548 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008549 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 list_T *l1, *l2;
8551 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008552 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008553 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008554
Bram Moolenaare9a41262005-01-15 22:18:47 +00008555 l1 = argvars[0].vval.v_list;
8556 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008557 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8558 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008559 {
8560 if (argvars[2].v_type != VAR_UNKNOWN)
8561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008562 before = get_tv_number_chk(&argvars[2], &error);
8563 if (error)
8564 return; /* type error; errmsg already given */
8565
Bram Moolenaar758711c2005-02-02 23:11:38 +00008566 if (before == l1->lv_len)
8567 item = NULL;
8568 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008569 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008570 item = list_find(l1, before);
8571 if (item == NULL)
8572 {
8573 EMSGN(_(e_listidx), before);
8574 return;
8575 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008576 }
8577 }
8578 else
8579 item = NULL;
8580 list_extend(l1, l2, item);
8581
Bram Moolenaare9a41262005-01-15 22:18:47 +00008582 copy_tv(&argvars[0], rettv);
8583 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008584 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008585 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8586 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008587 dict_T *d1, *d2;
8588 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008589 char_u *action;
8590 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008592 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008593
8594 d1 = argvars[0].vval.v_dict;
8595 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008596 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8597 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008598 {
8599 /* Check the third argument. */
8600 if (argvars[2].v_type != VAR_UNKNOWN)
8601 {
8602 static char *(av[]) = {"keep", "force", "error"};
8603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008604 action = get_tv_string_chk(&argvars[2]);
8605 if (action == NULL)
8606 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008607 for (i = 0; i < 3; ++i)
8608 if (STRCMP(action, av[i]) == 0)
8609 break;
8610 if (i == 3)
8611 {
8612 EMSGN(_(e_invarg2), action);
8613 return;
8614 }
8615 }
8616 else
8617 action = (char_u *)"force";
8618
8619 /* Go over all entries in the second dict and add them to the
8620 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 todo = d2->dv_hashtab.ht_used;
8622 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008624 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008625 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008626 --todo;
8627 di1 = dict_find(d1, hi2->hi_key, -1);
8628 if (di1 == NULL)
8629 {
8630 di1 = dictitem_copy(HI2DI(hi2));
8631 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8632 dictitem_free(di1);
8633 }
8634 else if (*action == 'e')
8635 {
8636 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8637 break;
8638 }
8639 else if (*action == 'f')
8640 {
8641 clear_tv(&di1->di_tv);
8642 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008644 }
8645 }
8646
Bram Moolenaare9a41262005-01-15 22:18:47 +00008647 copy_tv(&argvars[0], rettv);
8648 }
8649 }
8650 else
8651 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008652}
8653
8654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 * "filereadable()" function
8656 */
8657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 typval_T *argvars;
8660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
8662 FILE *fd;
8663 char_u *p;
8664 int n;
8665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8668 {
8669 n = TRUE;
8670 fclose(fd);
8671 }
8672 else
8673 n = FALSE;
8674
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676}
8677
8678/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008679 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 * rights to write into.
8681 */
8682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008684 typval_T *argvars;
8685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008687 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688}
8689
Bram Moolenaar33570922005-01-25 22:26:29 +00008690static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008691
8692 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008693findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 typval_T *argvars;
8695 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008696 int dir;
8697{
8698#ifdef FEAT_SEARCHPATH
8699 char_u *fname;
8700 char_u *fresult = NULL;
8701 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8702 char_u *p;
8703 char_u pathbuf[NUMBUFLEN];
8704 int count = 1;
8705 int first = TRUE;
8706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008708
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008711 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8712 if (p == NULL)
8713 count = -1; /* error */
8714 else
8715 {
8716 if (*p != NUL)
8717 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 if (argvars[2].v_type != VAR_UNKNOWN)
8720 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8721 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008722 }
8723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008724 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008726 do
8727 {
8728 vim_free(fresult);
8729 fresult = find_file_in_path_option(first ? fname : NULL,
8730 first ? (int)STRLEN(fname) : 0,
8731 0, first, path, dir, NULL);
8732 first = FALSE;
8733 } while (--count > 0 && fresult != NULL);
8734 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008735
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008737#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008739#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008741}
8742
Bram Moolenaar33570922005-01-25 22:26:29 +00008743static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8744static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008745
8746/*
8747 * Implementation of map() and filter().
8748 */
8749 static void
8750filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008751 typval_T *argvars;
8752 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008753 int map;
8754{
8755 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008756 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 listitem_T *li, *nli;
8758 list_T *l = NULL;
8759 dictitem_T *di;
8760 hashtab_T *ht;
8761 hashitem_T *hi;
8762 dict_T *d = NULL;
8763 typval_T save_val;
8764 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008765 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008766 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008767 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8768
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008769
8770 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008771 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008772 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008773 if ((l = argvars[0].vval.v_list) == NULL
8774 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008775 return;
8776 }
8777 else if (argvars[0].v_type == VAR_DICT)
8778 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008779 if ((d = argvars[0].vval.v_dict) == NULL
8780 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781 return;
8782 }
8783 else
8784 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008785 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008786 return;
8787 }
8788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008789 expr = get_tv_string_buf_chk(&argvars[1], buf);
8790 /* On type errors, the preceding call has already displayed an error
8791 * message. Avoid a misleading error message for an empty string that
8792 * was not passed as argument. */
8793 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008795 prepare_vimvar(VV_VAL, &save_val);
8796 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008798 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008800 prepare_vimvar(VV_KEY, &save_key);
8801 vimvars[VV_KEY].vv_type = VAR_STRING;
8802
8803 ht = &d->dv_hashtab;
8804 hash_lock(ht);
8805 todo = ht->ht_used;
8806 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008808 if (!HASHITEM_EMPTY(hi))
8809 {
8810 --todo;
8811 di = HI2DI(hi);
8812 if (tv_check_lock(di->di_tv.v_lock, msg))
8813 break;
8814 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8815 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8816 break;
8817 if (!map && rem)
8818 dictitem_remove(d, di);
8819 clear_tv(&vimvars[VV_KEY].vv_tv);
8820 }
8821 }
8822 hash_unlock(ht);
8823
8824 restore_vimvar(VV_KEY, &save_key);
8825 }
8826 else
8827 {
8828 for (li = l->lv_first; li != NULL; li = nli)
8829 {
8830 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008831 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008832 nli = li->li_next;
8833 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008834 break;
8835 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008836 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008837 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008838 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008840 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008842
8843 copy_tv(&argvars[0], rettv);
8844}
8845
8846 static int
8847filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008849 char_u *expr;
8850 int map;
8851 int *remp;
8852{
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854 char_u *s;
8855
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008857 s = expr;
8858 if (eval1(&s, &rettv, TRUE) == FAIL)
8859 return FAIL;
8860 if (*s != NUL) /* check for trailing chars after expr */
8861 {
8862 EMSG2(_(e_invexpr2), s);
8863 return FAIL;
8864 }
8865 if (map)
8866 {
8867 /* map(): replace the list item value */
8868 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008869 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008870 *tv = rettv;
8871 }
8872 else
8873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 int error = FALSE;
8875
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008877 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008878 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008879 /* On type error, nothing has been removed; return FAIL to stop the
8880 * loop. The error message was given by get_tv_number_chk(). */
8881 if (error)
8882 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008883 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008885 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008886}
8887
8888/*
8889 * "filter()" function
8890 */
8891 static void
8892f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 typval_T *argvars;
8894 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008895{
8896 filter_map(argvars, rettv, FALSE);
8897}
8898
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008899/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008900 * "finddir({fname}[, {path}[, {count}]])" function
8901 */
8902 static void
8903f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 typval_T *argvars;
8905 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008906{
8907 findfilendir(argvars, rettv, TRUE);
8908}
8909
8910/*
8911 * "findfile({fname}[, {path}[, {count}]])" function
8912 */
8913 static void
8914f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008915 typval_T *argvars;
8916 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008917{
8918 findfilendir(argvars, rettv, FALSE);
8919}
8920
8921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 * "fnamemodify({fname}, {mods})" function
8923 */
8924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008926 typval_T *argvars;
8927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929 char_u *fname;
8930 char_u *mods;
8931 int usedlen = 0;
8932 int len;
8933 char_u *fbuf = NULL;
8934 char_u buf[NUMBUFLEN];
8935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008936 fname = get_tv_string_chk(&argvars[0]);
8937 mods = get_tv_string_buf_chk(&argvars[1], buf);
8938 if (fname == NULL || mods == NULL)
8939 fname = NULL;
8940 else
8941 {
8942 len = (int)STRLEN(fname);
8943 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 vim_free(fbuf);
8952}
8953
Bram Moolenaar33570922005-01-25 22:26:29 +00008954static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955
8956/*
8957 * "foldclosed()" function
8958 */
8959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008961 typval_T *argvars;
8962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 int end;
8964{
8965#ifdef FEAT_FOLDING
8966 linenr_T lnum;
8967 linenr_T first, last;
8968
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8971 {
8972 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8973 {
8974 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 return;
8979 }
8980 }
8981#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983}
8984
8985/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008986 * "foldclosed()" function
8987 */
8988 static void
8989f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008990 typval_T *argvars;
8991 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008992{
8993 foldclosed_both(argvars, rettv, FALSE);
8994}
8995
8996/*
8997 * "foldclosedend()" function
8998 */
8999 static void
9000f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009003{
9004 foldclosed_both(argvars, rettv, TRUE);
9005}
9006
9007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 * "foldlevel()" function
9009 */
9010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 typval_T *argvars;
9013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
9015#ifdef FEAT_FOLDING
9016 linenr_T lnum;
9017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 else
9022#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024}
9025
9026/*
9027 * "foldtext()" function
9028 */
9029/*ARGSUSED*/
9030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009032 typval_T *argvars;
9033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034{
9035#ifdef FEAT_FOLDING
9036 linenr_T lnum;
9037 char_u *s;
9038 char_u *r;
9039 int len;
9040 char *txt;
9041#endif
9042
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->v_type = VAR_STRING;
9044 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009046 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9047 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9048 <= curbuf->b_ml.ml_line_count
9049 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 {
9051 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009052 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9053 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 {
9055 if (!linewhite(lnum))
9056 break;
9057 ++lnum;
9058 }
9059
9060 /* Find interesting text in this line. */
9061 s = skipwhite(ml_get(lnum));
9062 /* skip C comment-start */
9063 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009066 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009067 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009068 {
9069 s = skipwhite(ml_get(lnum + 1));
9070 if (*s == '*')
9071 s = skipwhite(s + 1);
9072 }
9073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 txt = _("+-%s%3ld lines: ");
9075 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009076 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 + 20 /* for %3ld */
9078 + STRLEN(s))); /* concatenated */
9079 if (r != NULL)
9080 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009081 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9082 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9083 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 len = (int)STRLEN(r);
9085 STRCAT(r, s);
9086 /* remove 'foldmarker' and 'commentstring' */
9087 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 }
9090 }
9091#endif
9092}
9093
9094/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009095 * "foldtextresult(lnum)" function
9096 */
9097/*ARGSUSED*/
9098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 typval_T *argvars;
9101 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009102{
9103#ifdef FEAT_FOLDING
9104 linenr_T lnum;
9105 char_u *text;
9106 char_u buf[51];
9107 foldinfo_T foldinfo;
9108 int fold_count;
9109#endif
9110
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->v_type = VAR_STRING;
9112 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009113#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009115 /* treat illegal types and illegal string values for {lnum} the same */
9116 if (lnum < 0)
9117 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009118 fold_count = foldedCount(curwin, lnum, &foldinfo);
9119 if (fold_count > 0)
9120 {
9121 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9122 &foldinfo, buf);
9123 if (text == buf)
9124 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009126 }
9127#endif
9128}
9129
9130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 * "foreground()" function
9132 */
9133/*ARGSUSED*/
9134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009136 typval_T *argvars;
9137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140#ifdef FEAT_GUI
9141 if (gui.in_use)
9142 gui_mch_set_foreground();
9143#else
9144# ifdef WIN32
9145 win32_set_foreground();
9146# endif
9147#endif
9148}
9149
9150/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009151 * "function()" function
9152 */
9153/*ARGSUSED*/
9154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009156 typval_T *argvars;
9157 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009158{
9159 char_u *s;
9160
Bram Moolenaara7043832005-01-21 11:56:39 +00009161 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009163 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009164 EMSG2(_(e_invarg2), s);
9165 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009166 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009167 else
9168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->vval.v_string = vim_strsave(s);
9170 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009171 }
9172}
9173
9174/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009175 * "garbagecollect()" function
9176 */
9177/*ARGSUSED*/
9178 static void
9179f_garbagecollect(argvars, rettv)
9180 typval_T *argvars;
9181 typval_T *rettv;
9182{
9183 garbage_collect();
9184}
9185
9186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009187 * "get()" function
9188 */
9189 static void
9190f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009191 typval_T *argvars;
9192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009193{
Bram Moolenaar33570922005-01-25 22:26:29 +00009194 listitem_T *li;
9195 list_T *l;
9196 dictitem_T *di;
9197 dict_T *d;
9198 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009199
Bram Moolenaare9a41262005-01-15 22:18:47 +00009200 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009201 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009202 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 int error = FALSE;
9205
9206 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9207 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009208 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009209 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009210 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009211 else if (argvars[0].v_type == VAR_DICT)
9212 {
9213 if ((d = argvars[0].vval.v_dict) != NULL)
9214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009215 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009216 if (di != NULL)
9217 tv = &di->di_tv;
9218 }
9219 }
9220 else
9221 EMSG2(_(e_listdictarg), "get()");
9222
9223 if (tv == NULL)
9224 {
9225 if (argvars[2].v_type == VAR_UNKNOWN)
9226 rettv->vval.v_number = 0;
9227 else
9228 copy_tv(&argvars[2], rettv);
9229 }
9230 else
9231 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009232}
9233
Bram Moolenaar342337a2005-07-21 21:11:17 +00009234static 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 +00009235
9236/*
9237 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009238 * Return a range (from start to end) of lines in rettv from the specified
9239 * buffer.
9240 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009241 */
9242 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009243get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009244 buf_T *buf;
9245 linenr_T start;
9246 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009247 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009248 typval_T *rettv;
9249{
9250 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009251 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009252
Bram Moolenaar342337a2005-07-21 21:11:17 +00009253 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009254 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009255 l = list_alloc();
9256 if (l == NULL)
9257 return;
9258
9259 rettv->vval.v_list = l;
9260 rettv->v_type = VAR_LIST;
9261 ++l->lv_refcount;
9262 }
9263 else
9264 rettv->vval.v_number = 0;
9265
9266 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9267 return;
9268
9269 if (!retlist)
9270 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009271 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9272 p = ml_get_buf(buf, start, FALSE);
9273 else
9274 p = (char_u *)"";
9275
9276 rettv->v_type = VAR_STRING;
9277 rettv->vval.v_string = vim_strsave(p);
9278 }
9279 else
9280 {
9281 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009282 return;
9283
9284 if (start < 1)
9285 start = 1;
9286 if (end > buf->b_ml.ml_line_count)
9287 end = buf->b_ml.ml_line_count;
9288 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009289 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9290 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009291 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009292 }
9293}
9294
9295/*
9296 * "getbufline()" function
9297 */
9298 static void
9299f_getbufline(argvars, rettv)
9300 typval_T *argvars;
9301 typval_T *rettv;
9302{
9303 linenr_T lnum;
9304 linenr_T end;
9305 buf_T *buf;
9306
9307 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9308 ++emsg_off;
9309 buf = get_buf_tv(&argvars[0]);
9310 --emsg_off;
9311
Bram Moolenaar661b1822005-07-28 22:36:45 +00009312 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009313 if (argvars[2].v_type == VAR_UNKNOWN)
9314 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009315 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009316 end = get_tv_lnum_buf(&argvars[2], buf);
9317
Bram Moolenaar342337a2005-07-21 21:11:17 +00009318 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009319}
9320
Bram Moolenaar0d660222005-01-07 21:51:51 +00009321/*
9322 * "getbufvar()" function
9323 */
9324 static void
9325f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 typval_T *argvars;
9327 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009328{
9329 buf_T *buf;
9330 buf_T *save_curbuf;
9331 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009332 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009334 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9335 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336 ++emsg_off;
9337 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009338
9339 rettv->v_type = VAR_STRING;
9340 rettv->vval.v_string = NULL;
9341
9342 if (buf != NULL && varname != NULL)
9343 {
9344 if (*varname == '&') /* buffer-local-option */
9345 {
9346 /* set curbuf to be our buf, temporarily */
9347 save_curbuf = curbuf;
9348 curbuf = buf;
9349
9350 get_option_tv(&varname, rettv, TRUE);
9351
9352 /* restore previous notion of curbuf */
9353 curbuf = save_curbuf;
9354 }
9355 else
9356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009357 if (*varname == NUL)
9358 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9359 * scope prefix before the NUL byte is required by
9360 * find_var_in_ht(). */
9361 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009362 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009363 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009364 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009366 }
9367 }
9368
9369 --emsg_off;
9370}
9371
9372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 * "getchar()" function
9374 */
9375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009377 typval_T *argvars;
9378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
9383 ++no_mapping;
9384 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009385 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 /* getchar(): blocking wait. */
9387 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 /* getchar(1): only check if char avail */
9390 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009391 else if (error || vpeekc() == NUL)
9392 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 n = 0;
9394 else
9395 /* getchar(0) and char avail: return char */
9396 n = safe_vgetc();
9397 --no_mapping;
9398 --allow_keys;
9399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 if (IS_SPECIAL(n) || mod_mask != 0)
9402 {
9403 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9404 int i = 0;
9405
9406 /* Turn a special key into three bytes, plus modifier. */
9407 if (mod_mask != 0)
9408 {
9409 temp[i++] = K_SPECIAL;
9410 temp[i++] = KS_MODIFIER;
9411 temp[i++] = mod_mask;
9412 }
9413 if (IS_SPECIAL(n))
9414 {
9415 temp[i++] = K_SPECIAL;
9416 temp[i++] = K_SECOND(n);
9417 temp[i++] = K_THIRD(n);
9418 }
9419#ifdef FEAT_MBYTE
9420 else if (has_mbyte)
9421 i += (*mb_char2bytes)(n, temp + i);
9422#endif
9423 else
9424 temp[i++] = n;
9425 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->v_type = VAR_STRING;
9427 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 }
9429}
9430
9431/*
9432 * "getcharmod()" function
9433 */
9434/*ARGSUSED*/
9435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 typval_T *argvars;
9438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441}
9442
9443/*
9444 * "getcmdline()" function
9445 */
9446/*ARGSUSED*/
9447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *argvars;
9450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->v_type = VAR_STRING;
9453 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454}
9455
9456/*
9457 * "getcmdpos()" function
9458 */
9459/*ARGSUSED*/
9460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009462 typval_T *argvars;
9463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467
9468/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009469 * "getcmdtype()" function
9470 */
9471/*ARGSUSED*/
9472 static void
9473f_getcmdtype(argvars, rettv)
9474 typval_T *argvars;
9475 typval_T *rettv;
9476{
9477 rettv->v_type = VAR_STRING;
9478 rettv->vval.v_string = alloc(2);
9479 if (rettv->vval.v_string != NULL)
9480 {
9481 rettv->vval.v_string[0] = get_cmdline_type();
9482 rettv->vval.v_string[1] = NUL;
9483 }
9484}
9485
9486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 * "getcwd()" function
9488 */
9489/*ARGSUSED*/
9490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009492 typval_T *argvars;
9493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495 char_u cwd[MAXPATHL];
9496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 else
9501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009504 if (rettv->vval.v_string != NULL)
9505 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506#endif
9507 }
9508}
9509
9510/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009511 * "getfontname()" function
9512 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009513/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 typval_T *argvars;
9517 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->v_type = VAR_STRING;
9520 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009521#ifdef FEAT_GUI
9522 if (gui.in_use)
9523 {
9524 GuiFont font;
9525 char_u *name = NULL;
9526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009527 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009528 {
9529 /* Get the "Normal" font. Either the name saved by
9530 * hl_set_font_name() or from the font ID. */
9531 font = gui.norm_font;
9532 name = hl_get_font_name();
9533 }
9534 else
9535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009537 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9538 return;
9539 font = gui_mch_get_font(name, FALSE);
9540 if (font == NOFONT)
9541 return; /* Invalid font name, return empty string. */
9542 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009544 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009545 gui_mch_free_font(font);
9546 }
9547#endif
9548}
9549
9550/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009551 * "getfperm({fname})" function
9552 */
9553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009555 typval_T *argvars;
9556 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009557{
9558 char_u *fname;
9559 struct stat st;
9560 char_u *perm = NULL;
9561 char_u flags[] = "rwx";
9562 int i;
9563
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009567 if (mch_stat((char *)fname, &st) >= 0)
9568 {
9569 perm = vim_strsave((char_u *)"---------");
9570 if (perm != NULL)
9571 {
9572 for (i = 0; i < 9; i++)
9573 {
9574 if (st.st_mode & (1 << (8 - i)))
9575 perm[i] = flags[i % 3];
9576 }
9577 }
9578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009580}
9581
9582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 * "getfsize({fname})" function
9584 */
9585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009587 typval_T *argvars;
9588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
9590 char_u *fname;
9591 struct stat st;
9592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
9597 if (mch_stat((char *)fname, &st) >= 0)
9598 {
9599 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 }
9604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606}
9607
9608/*
9609 * "getftime({fname})" function
9610 */
9611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 typval_T *argvars;
9614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 char_u *fname;
9617 struct stat st;
9618
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620
9621 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625}
9626
9627/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009628 * "getftype({fname})" function
9629 */
9630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009632 typval_T *argvars;
9633 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009634{
9635 char_u *fname;
9636 struct stat st;
9637 char_u *type = NULL;
9638 char *t;
9639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009643 if (mch_lstat((char *)fname, &st) >= 0)
9644 {
9645#ifdef S_ISREG
9646 if (S_ISREG(st.st_mode))
9647 t = "file";
9648 else if (S_ISDIR(st.st_mode))
9649 t = "dir";
9650# ifdef S_ISLNK
9651 else if (S_ISLNK(st.st_mode))
9652 t = "link";
9653# endif
9654# ifdef S_ISBLK
9655 else if (S_ISBLK(st.st_mode))
9656 t = "bdev";
9657# endif
9658# ifdef S_ISCHR
9659 else if (S_ISCHR(st.st_mode))
9660 t = "cdev";
9661# endif
9662# ifdef S_ISFIFO
9663 else if (S_ISFIFO(st.st_mode))
9664 t = "fifo";
9665# endif
9666# ifdef S_ISSOCK
9667 else if (S_ISSOCK(st.st_mode))
9668 t = "fifo";
9669# endif
9670 else
9671 t = "other";
9672#else
9673# ifdef S_IFMT
9674 switch (st.st_mode & S_IFMT)
9675 {
9676 case S_IFREG: t = "file"; break;
9677 case S_IFDIR: t = "dir"; break;
9678# ifdef S_IFLNK
9679 case S_IFLNK: t = "link"; break;
9680# endif
9681# ifdef S_IFBLK
9682 case S_IFBLK: t = "bdev"; break;
9683# endif
9684# ifdef S_IFCHR
9685 case S_IFCHR: t = "cdev"; break;
9686# endif
9687# ifdef S_IFIFO
9688 case S_IFIFO: t = "fifo"; break;
9689# endif
9690# ifdef S_IFSOCK
9691 case S_IFSOCK: t = "socket"; break;
9692# endif
9693 default: t = "other";
9694 }
9695# else
9696 if (mch_isdir(fname))
9697 t = "dir";
9698 else
9699 t = "file";
9700# endif
9701#endif
9702 type = vim_strsave((char_u *)t);
9703 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009705}
9706
9707/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009708 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009709 */
9710 static void
9711f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009712 typval_T *argvars;
9713 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009714{
9715 linenr_T lnum;
9716 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009717 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009718
9719 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009720 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009721 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009722 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009723 retlist = FALSE;
9724 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009725 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009726 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009727 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009728 retlist = TRUE;
9729 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009730
Bram Moolenaar342337a2005-07-21 21:11:17 +00009731 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009732}
9733
9734/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009735 * "getqflist()" function
9736 */
9737/*ARGSUSED*/
9738 static void
9739f_getqflist(argvars, rettv)
9740 typval_T *argvars;
9741 typval_T *rettv;
9742{
9743#ifdef FEAT_QUICKFIX
9744 list_T *l;
9745#endif
9746
9747 rettv->vval.v_number = FALSE;
9748#ifdef FEAT_QUICKFIX
9749 l = list_alloc();
9750 if (l != NULL)
9751 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009752 rettv->vval.v_list = l;
9753 rettv->v_type = VAR_LIST;
9754 ++l->lv_refcount;
9755 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009756 }
9757#endif
9758}
9759
9760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 * "getreg()" function
9762 */
9763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 typval_T *argvars;
9766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767{
9768 char_u *strregname;
9769 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009770 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009773 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 strregname = get_tv_string_chk(&argvars[0]);
9776 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009777 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009781 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 regname = (strregname == NULL ? '"' : *strregname);
9783 if (regname == 0)
9784 regname = '"';
9785
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009787 rettv->vval.v_string = error ? NULL :
9788 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789}
9790
9791/*
9792 * "getregtype()" function
9793 */
9794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009796 typval_T *argvars;
9797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798{
9799 char_u *strregname;
9800 int regname;
9801 char_u buf[NUMBUFLEN + 2];
9802 long reglen = 0;
9803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009804 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 {
9806 strregname = get_tv_string_chk(&argvars[0]);
9807 if (strregname == NULL) /* type error; errmsg already given */
9808 {
9809 rettv->v_type = VAR_STRING;
9810 rettv->vval.v_string = NULL;
9811 return;
9812 }
9813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 else
9815 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009816 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817
9818 regname = (strregname == NULL ? '"' : *strregname);
9819 if (regname == 0)
9820 regname = '"';
9821
9822 buf[0] = NUL;
9823 buf[1] = NUL;
9824 switch (get_reg_type(regname, &reglen))
9825 {
9826 case MLINE: buf[0] = 'V'; break;
9827 case MCHAR: buf[0] = 'v'; break;
9828#ifdef FEAT_VISUAL
9829 case MBLOCK:
9830 buf[0] = Ctrl_V;
9831 sprintf((char *)buf + 1, "%ld", reglen + 1);
9832 break;
9833#endif
9834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 rettv->v_type = VAR_STRING;
9836 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837}
9838
9839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 * "getwinposx()" function
9841 */
9842/*ARGSUSED*/
9843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009845 typval_T *argvars;
9846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849#ifdef FEAT_GUI
9850 if (gui.in_use)
9851 {
9852 int x, y;
9853
9854 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 }
9857#endif
9858}
9859
9860/*
9861 * "getwinposy()" function
9862 */
9863/*ARGSUSED*/
9864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009866 typval_T *argvars;
9867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870#ifdef FEAT_GUI
9871 if (gui.in_use)
9872 {
9873 int x, y;
9874
9875 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 }
9878#endif
9879}
9880
Bram Moolenaara40058a2005-07-11 22:42:07 +00009881static win_T *find_win_by_nr __ARGS((typval_T *vp));
9882
9883 static win_T *
9884find_win_by_nr(vp)
9885 typval_T *vp;
9886{
9887#ifdef FEAT_WINDOWS
9888 win_T *wp;
9889#endif
9890 int nr;
9891
9892 nr = get_tv_number_chk(vp, NULL);
9893
9894#ifdef FEAT_WINDOWS
9895 if (nr < 0)
9896 return NULL;
9897 if (nr == 0)
9898 return curwin;
9899
9900 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9901 if (--nr <= 0)
9902 break;
9903 return wp;
9904#else
9905 if (nr == 0 || nr == 1)
9906 return curwin;
9907 return NULL;
9908#endif
9909}
9910
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911/*
9912 * "getwinvar()" function
9913 */
9914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009915f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009916 typval_T *argvars;
9917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918{
9919 win_T *win, *oldcurwin;
9920 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009921 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 varname = get_tv_string_chk(&argvars[1]);
9925 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927 rettv->v_type = VAR_STRING;
9928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
9930 if (win != NULL && varname != NULL)
9931 {
9932 if (*varname == '&') /* window-local-option */
9933 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009934 /* Set curwin to be our win, temporarily. Also set curbuf, so
9935 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 oldcurwin = curwin;
9937 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009938 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
9942 /* restore previous notion of curwin */
9943 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009944 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 }
9946 else
9947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 if (*varname == NUL)
9949 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9950 * scope prefix before the NUL byte is required by
9951 * find_var_in_ht(). */
9952 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009954 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009956 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 }
9958 }
9959
9960 --emsg_off;
9961}
9962
9963/*
9964 * "glob()" function
9965 */
9966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009968 typval_T *argvars;
9969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
9971 expand_T xpc;
9972
9973 ExpandInit(&xpc);
9974 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 rettv->v_type = VAR_STRING;
9976 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9978 ExpandCleanup(&xpc);
9979}
9980
9981/*
9982 * "globpath()" function
9983 */
9984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 if (file == NULL)
9994 rettv->vval.v_string = NULL;
9995 else
9996 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997}
9998
9999/*
10000 * "has()" function
10001 */
10002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010004 typval_T *argvars;
10005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 int i;
10008 char_u *name;
10009 int n = FALSE;
10010 static char *(has_list[]) =
10011 {
10012#ifdef AMIGA
10013 "amiga",
10014# ifdef FEAT_ARP
10015 "arp",
10016# endif
10017#endif
10018#ifdef __BEOS__
10019 "beos",
10020#endif
10021#ifdef MSDOS
10022# ifdef DJGPP
10023 "dos32",
10024# else
10025 "dos16",
10026# endif
10027#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010028#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 "mac",
10030#endif
10031#if defined(MACOS_X_UNIX)
10032 "macunix",
10033#endif
10034#ifdef OS2
10035 "os2",
10036#endif
10037#ifdef __QNX__
10038 "qnx",
10039#endif
10040#ifdef RISCOS
10041 "riscos",
10042#endif
10043#ifdef UNIX
10044 "unix",
10045#endif
10046#ifdef VMS
10047 "vms",
10048#endif
10049#ifdef WIN16
10050 "win16",
10051#endif
10052#ifdef WIN32
10053 "win32",
10054#endif
10055#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10056 "win32unix",
10057#endif
10058#ifdef WIN64
10059 "win64",
10060#endif
10061#ifdef EBCDIC
10062 "ebcdic",
10063#endif
10064#ifndef CASE_INSENSITIVE_FILENAME
10065 "fname_case",
10066#endif
10067#ifdef FEAT_ARABIC
10068 "arabic",
10069#endif
10070#ifdef FEAT_AUTOCMD
10071 "autocmd",
10072#endif
10073#ifdef FEAT_BEVAL
10074 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010075# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10076 "balloon_multiline",
10077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078#endif
10079#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10080 "builtin_terms",
10081# ifdef ALL_BUILTIN_TCAPS
10082 "all_builtin_terms",
10083# endif
10084#endif
10085#ifdef FEAT_BYTEOFF
10086 "byte_offset",
10087#endif
10088#ifdef FEAT_CINDENT
10089 "cindent",
10090#endif
10091#ifdef FEAT_CLIENTSERVER
10092 "clientserver",
10093#endif
10094#ifdef FEAT_CLIPBOARD
10095 "clipboard",
10096#endif
10097#ifdef FEAT_CMDL_COMPL
10098 "cmdline_compl",
10099#endif
10100#ifdef FEAT_CMDHIST
10101 "cmdline_hist",
10102#endif
10103#ifdef FEAT_COMMENTS
10104 "comments",
10105#endif
10106#ifdef FEAT_CRYPT
10107 "cryptv",
10108#endif
10109#ifdef FEAT_CSCOPE
10110 "cscope",
10111#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010112#ifdef CURSOR_SHAPE
10113 "cursorshape",
10114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115#ifdef DEBUG
10116 "debug",
10117#endif
10118#ifdef FEAT_CON_DIALOG
10119 "dialog_con",
10120#endif
10121#ifdef FEAT_GUI_DIALOG
10122 "dialog_gui",
10123#endif
10124#ifdef FEAT_DIFF
10125 "diff",
10126#endif
10127#ifdef FEAT_DIGRAPHS
10128 "digraphs",
10129#endif
10130#ifdef FEAT_DND
10131 "dnd",
10132#endif
10133#ifdef FEAT_EMACS_TAGS
10134 "emacs_tags",
10135#endif
10136 "eval", /* always present, of course! */
10137#ifdef FEAT_EX_EXTRA
10138 "ex_extra",
10139#endif
10140#ifdef FEAT_SEARCH_EXTRA
10141 "extra_search",
10142#endif
10143#ifdef FEAT_FKMAP
10144 "farsi",
10145#endif
10146#ifdef FEAT_SEARCHPATH
10147 "file_in_path",
10148#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010149#if defined(UNIX) && !defined(USE_SYSTEM)
10150 "filterpipe",
10151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152#ifdef FEAT_FIND_ID
10153 "find_in_path",
10154#endif
10155#ifdef FEAT_FOLDING
10156 "folding",
10157#endif
10158#ifdef FEAT_FOOTER
10159 "footer",
10160#endif
10161#if !defined(USE_SYSTEM) && defined(UNIX)
10162 "fork",
10163#endif
10164#ifdef FEAT_GETTEXT
10165 "gettext",
10166#endif
10167#ifdef FEAT_GUI
10168 "gui",
10169#endif
10170#ifdef FEAT_GUI_ATHENA
10171# ifdef FEAT_GUI_NEXTAW
10172 "gui_neXtaw",
10173# else
10174 "gui_athena",
10175# endif
10176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177#ifdef FEAT_GUI_GTK
10178 "gui_gtk",
10179# ifdef HAVE_GTK2
10180 "gui_gtk2",
10181# endif
10182#endif
10183#ifdef FEAT_GUI_MAC
10184 "gui_mac",
10185#endif
10186#ifdef FEAT_GUI_MOTIF
10187 "gui_motif",
10188#endif
10189#ifdef FEAT_GUI_PHOTON
10190 "gui_photon",
10191#endif
10192#ifdef FEAT_GUI_W16
10193 "gui_win16",
10194#endif
10195#ifdef FEAT_GUI_W32
10196 "gui_win32",
10197#endif
10198#ifdef FEAT_HANGULIN
10199 "hangul_input",
10200#endif
10201#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10202 "iconv",
10203#endif
10204#ifdef FEAT_INS_EXPAND
10205 "insert_expand",
10206#endif
10207#ifdef FEAT_JUMPLIST
10208 "jumplist",
10209#endif
10210#ifdef FEAT_KEYMAP
10211 "keymap",
10212#endif
10213#ifdef FEAT_LANGMAP
10214 "langmap",
10215#endif
10216#ifdef FEAT_LIBCALL
10217 "libcall",
10218#endif
10219#ifdef FEAT_LINEBREAK
10220 "linebreak",
10221#endif
10222#ifdef FEAT_LISP
10223 "lispindent",
10224#endif
10225#ifdef FEAT_LISTCMDS
10226 "listcmds",
10227#endif
10228#ifdef FEAT_LOCALMAP
10229 "localmap",
10230#endif
10231#ifdef FEAT_MENU
10232 "menu",
10233#endif
10234#ifdef FEAT_SESSION
10235 "mksession",
10236#endif
10237#ifdef FEAT_MODIFY_FNAME
10238 "modify_fname",
10239#endif
10240#ifdef FEAT_MOUSE
10241 "mouse",
10242#endif
10243#ifdef FEAT_MOUSESHAPE
10244 "mouseshape",
10245#endif
10246#if defined(UNIX) || defined(VMS)
10247# ifdef FEAT_MOUSE_DEC
10248 "mouse_dec",
10249# endif
10250# ifdef FEAT_MOUSE_GPM
10251 "mouse_gpm",
10252# endif
10253# ifdef FEAT_MOUSE_JSB
10254 "mouse_jsbterm",
10255# endif
10256# ifdef FEAT_MOUSE_NET
10257 "mouse_netterm",
10258# endif
10259# ifdef FEAT_MOUSE_PTERM
10260 "mouse_pterm",
10261# endif
10262# ifdef FEAT_MOUSE_XTERM
10263 "mouse_xterm",
10264# endif
10265#endif
10266#ifdef FEAT_MBYTE
10267 "multi_byte",
10268#endif
10269#ifdef FEAT_MBYTE_IME
10270 "multi_byte_ime",
10271#endif
10272#ifdef FEAT_MULTI_LANG
10273 "multi_lang",
10274#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010275#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010276#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010277 "mzscheme",
10278#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280#ifdef FEAT_OLE
10281 "ole",
10282#endif
10283#ifdef FEAT_OSFILETYPE
10284 "osfiletype",
10285#endif
10286#ifdef FEAT_PATH_EXTRA
10287 "path_extra",
10288#endif
10289#ifdef FEAT_PERL
10290#ifndef DYNAMIC_PERL
10291 "perl",
10292#endif
10293#endif
10294#ifdef FEAT_PYTHON
10295#ifndef DYNAMIC_PYTHON
10296 "python",
10297#endif
10298#endif
10299#ifdef FEAT_POSTSCRIPT
10300 "postscript",
10301#endif
10302#ifdef FEAT_PRINTER
10303 "printer",
10304#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010305#ifdef FEAT_PROFILE
10306 "profile",
10307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308#ifdef FEAT_QUICKFIX
10309 "quickfix",
10310#endif
10311#ifdef FEAT_RIGHTLEFT
10312 "rightleft",
10313#endif
10314#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10315 "ruby",
10316#endif
10317#ifdef FEAT_SCROLLBIND
10318 "scrollbind",
10319#endif
10320#ifdef FEAT_CMDL_INFO
10321 "showcmd",
10322 "cmdline_info",
10323#endif
10324#ifdef FEAT_SIGNS
10325 "signs",
10326#endif
10327#ifdef FEAT_SMARTINDENT
10328 "smartindent",
10329#endif
10330#ifdef FEAT_SNIFF
10331 "sniff",
10332#endif
10333#ifdef FEAT_STL_OPT
10334 "statusline",
10335#endif
10336#ifdef FEAT_SUN_WORKSHOP
10337 "sun_workshop",
10338#endif
10339#ifdef FEAT_NETBEANS_INTG
10340 "netbeans_intg",
10341#endif
10342#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010343 "spell",
10344#endif
10345#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 "syntax",
10347#endif
10348#if defined(USE_SYSTEM) || !defined(UNIX)
10349 "system",
10350#endif
10351#ifdef FEAT_TAG_BINS
10352 "tag_binary",
10353#endif
10354#ifdef FEAT_TAG_OLDSTATIC
10355 "tag_old_static",
10356#endif
10357#ifdef FEAT_TAG_ANYWHITE
10358 "tag_any_white",
10359#endif
10360#ifdef FEAT_TCL
10361# ifndef DYNAMIC_TCL
10362 "tcl",
10363# endif
10364#endif
10365#ifdef TERMINFO
10366 "terminfo",
10367#endif
10368#ifdef FEAT_TERMRESPONSE
10369 "termresponse",
10370#endif
10371#ifdef FEAT_TEXTOBJ
10372 "textobjects",
10373#endif
10374#ifdef HAVE_TGETENT
10375 "tgetent",
10376#endif
10377#ifdef FEAT_TITLE
10378 "title",
10379#endif
10380#ifdef FEAT_TOOLBAR
10381 "toolbar",
10382#endif
10383#ifdef FEAT_USR_CMDS
10384 "user-commands", /* was accidentally included in 5.4 */
10385 "user_commands",
10386#endif
10387#ifdef FEAT_VIMINFO
10388 "viminfo",
10389#endif
10390#ifdef FEAT_VERTSPLIT
10391 "vertsplit",
10392#endif
10393#ifdef FEAT_VIRTUALEDIT
10394 "virtualedit",
10395#endif
10396#ifdef FEAT_VISUAL
10397 "visual",
10398#endif
10399#ifdef FEAT_VISUALEXTRA
10400 "visualextra",
10401#endif
10402#ifdef FEAT_VREPLACE
10403 "vreplace",
10404#endif
10405#ifdef FEAT_WILDIGN
10406 "wildignore",
10407#endif
10408#ifdef FEAT_WILDMENU
10409 "wildmenu",
10410#endif
10411#ifdef FEAT_WINDOWS
10412 "windows",
10413#endif
10414#ifdef FEAT_WAK
10415 "winaltkeys",
10416#endif
10417#ifdef FEAT_WRITEBACKUP
10418 "writebackup",
10419#endif
10420#ifdef FEAT_XIM
10421 "xim",
10422#endif
10423#ifdef FEAT_XFONTSET
10424 "xfontset",
10425#endif
10426#ifdef USE_XSMP
10427 "xsmp",
10428#endif
10429#ifdef USE_XSMP_INTERACT
10430 "xsmp_interact",
10431#endif
10432#ifdef FEAT_XCLIPBOARD
10433 "xterm_clipboard",
10434#endif
10435#ifdef FEAT_XTERM_SAVE
10436 "xterm_save",
10437#endif
10438#if defined(UNIX) && defined(FEAT_X11)
10439 "X11",
10440#endif
10441 NULL
10442 };
10443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 for (i = 0; has_list[i] != NULL; ++i)
10446 if (STRICMP(name, has_list[i]) == 0)
10447 {
10448 n = TRUE;
10449 break;
10450 }
10451
10452 if (n == FALSE)
10453 {
10454 if (STRNICMP(name, "patch", 5) == 0)
10455 n = has_patch(atoi((char *)name + 5));
10456 else if (STRICMP(name, "vim_starting") == 0)
10457 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010458#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10459 else if (STRICMP(name, "balloon_multiline") == 0)
10460 n = multiline_balloon_available();
10461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462#ifdef DYNAMIC_TCL
10463 else if (STRICMP(name, "tcl") == 0)
10464 n = tcl_enabled(FALSE);
10465#endif
10466#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10467 else if (STRICMP(name, "iconv") == 0)
10468 n = iconv_enabled(FALSE);
10469#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010470#ifdef DYNAMIC_MZSCHEME
10471 else if (STRICMP(name, "mzscheme") == 0)
10472 n = mzscheme_enabled(FALSE);
10473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474#ifdef DYNAMIC_RUBY
10475 else if (STRICMP(name, "ruby") == 0)
10476 n = ruby_enabled(FALSE);
10477#endif
10478#ifdef DYNAMIC_PYTHON
10479 else if (STRICMP(name, "python") == 0)
10480 n = python_enabled(FALSE);
10481#endif
10482#ifdef DYNAMIC_PERL
10483 else if (STRICMP(name, "perl") == 0)
10484 n = perl_enabled(FALSE);
10485#endif
10486#ifdef FEAT_GUI
10487 else if (STRICMP(name, "gui_running") == 0)
10488 n = (gui.in_use || gui.starting);
10489# ifdef FEAT_GUI_W32
10490 else if (STRICMP(name, "gui_win32s") == 0)
10491 n = gui_is_win32s();
10492# endif
10493# ifdef FEAT_BROWSE
10494 else if (STRICMP(name, "browse") == 0)
10495 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10496# endif
10497#endif
10498#ifdef FEAT_SYN_HL
10499 else if (STRICMP(name, "syntax_items") == 0)
10500 n = syntax_present(curbuf);
10501#endif
10502#if defined(WIN3264)
10503 else if (STRICMP(name, "win95") == 0)
10504 n = mch_windows95();
10505#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010506#ifdef FEAT_NETBEANS_INTG
10507 else if (STRICMP(name, "netbeans_enabled") == 0)
10508 n = usingNetbeans;
10509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 }
10511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513}
10514
10515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010516 * "has_key()" function
10517 */
10518 static void
10519f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010520 typval_T *argvars;
10521 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010522{
10523 rettv->vval.v_number = 0;
10524 if (argvars[0].v_type != VAR_DICT)
10525 {
10526 EMSG(_(e_dictreq));
10527 return;
10528 }
10529 if (argvars[0].vval.v_dict == NULL)
10530 return;
10531
10532 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010533 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010534}
10535
10536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 * "hasmapto()" function
10538 */
10539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010541 typval_T *argvars;
10542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543{
10544 char_u *name;
10545 char_u *mode;
10546 char_u buf[NUMBUFLEN];
10547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010549 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 mode = (char_u *)"nvo";
10551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553
10554 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558}
10559
10560/*
10561 * "histadd()" function
10562 */
10563/*ARGSUSED*/
10564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569#ifdef FEAT_CMDHIST
10570 int histype;
10571 char_u *str;
10572 char_u buf[NUMBUFLEN];
10573#endif
10574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 if (check_restricted() || check_secure())
10577 return;
10578#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10580 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 if (histype >= 0)
10582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 if (*str != NUL)
10585 {
10586 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 return;
10589 }
10590 }
10591#endif
10592}
10593
10594/*
10595 * "histdel()" function
10596 */
10597/*ARGSUSED*/
10598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 typval_T *argvars;
10601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
10603#ifdef FEAT_CMDHIST
10604 int n;
10605 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10609 if (str == NULL)
10610 n = 0;
10611 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010613 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010614 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 else
10619 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621 get_tv_string_buf(&argvars[1], buf));
10622 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625#endif
10626}
10627
10628/*
10629 * "histget()" function
10630 */
10631/*ARGSUSED*/
10632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010634 typval_T *argvars;
10635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636{
10637#ifdef FEAT_CMDHIST
10638 int type;
10639 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10643 if (str == NULL)
10644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 {
10647 type = get_histtype(str);
10648 if (argvars[1].v_type == VAR_UNKNOWN)
10649 idx = get_history_idx(type);
10650 else
10651 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10652 /* -1 on type error */
10653 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659}
10660
10661/*
10662 * "histnr()" function
10663 */
10664/*ARGSUSED*/
10665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010667 typval_T *argvars;
10668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669{
10670 int i;
10671
10672#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 char_u *history = get_tv_string_chk(&argvars[0]);
10674
10675 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 if (i >= HIST_CMD && i < HIST_COUNT)
10677 i = get_history_idx(i);
10678 else
10679#endif
10680 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682}
10683
10684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 * "highlightID(name)" function
10686 */
10687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010689 typval_T *argvars;
10690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693}
10694
10695/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010696 * "highlight_exists()" function
10697 */
10698 static void
10699f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010700 typval_T *argvars;
10701 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010702{
10703 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10704}
10705
10706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 * "hostname()" function
10708 */
10709/*ARGSUSED*/
10710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010712 typval_T *argvars;
10713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715 char_u hostname[256];
10716
10717 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 rettv->v_type = VAR_STRING;
10719 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720}
10721
10722/*
10723 * iconv() function
10724 */
10725/*ARGSUSED*/
10726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010728 typval_T *argvars;
10729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730{
10731#ifdef FEAT_MBYTE
10732 char_u buf1[NUMBUFLEN];
10733 char_u buf2[NUMBUFLEN];
10734 char_u *from, *to, *str;
10735 vimconv_T vimconv;
10736#endif
10737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010738 rettv->v_type = VAR_STRING;
10739 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740
10741#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742 str = get_tv_string(&argvars[0]);
10743 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10744 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 vimconv.vc_type = CONV_NONE;
10746 convert_setup(&vimconv, from, to);
10747
10748 /* If the encodings are equal, no conversion needed. */
10749 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
10754 convert_setup(&vimconv, NULL, NULL);
10755 vim_free(from);
10756 vim_free(to);
10757#endif
10758}
10759
10760/*
10761 * "indent()" function
10762 */
10763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010764f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010765 typval_T *argvars;
10766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767{
10768 linenr_T lnum;
10769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775}
10776
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010777/*
10778 * "index()" function
10779 */
10780 static void
10781f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010782 typval_T *argvars;
10783 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010784{
Bram Moolenaar33570922005-01-25 22:26:29 +000010785 list_T *l;
10786 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010787 long idx = 0;
10788 int ic = FALSE;
10789
10790 rettv->vval.v_number = -1;
10791 if (argvars[0].v_type != VAR_LIST)
10792 {
10793 EMSG(_(e_listreq));
10794 return;
10795 }
10796 l = argvars[0].vval.v_list;
10797 if (l != NULL)
10798 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010799 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010800 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010802 int error = FALSE;
10803
Bram Moolenaar758711c2005-02-02 23:11:38 +000010804 /* Start at specified item. Use the cached index that list_find()
10805 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010807 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010808 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 ic = get_tv_number_chk(&argvars[3], &error);
10810 if (error)
10811 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010812 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010813
Bram Moolenaar758711c2005-02-02 23:11:38 +000010814 for ( ; item != NULL; item = item->li_next, ++idx)
10815 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010816 {
10817 rettv->vval.v_number = idx;
10818 break;
10819 }
10820 }
10821}
10822
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823static int inputsecret_flag = 0;
10824
10825/*
10826 * "input()" function
10827 * Also handles inputsecret() when inputsecret is set.
10828 */
10829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 typval_T *argvars;
10832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010834 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 char_u *p = NULL;
10836 int c;
10837 char_u buf[NUMBUFLEN];
10838 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010839 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010840 int xp_type = EXPAND_NOTHING;
10841 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
10845#ifdef NO_CONSOLE_INPUT
10846 /* While starting up, there is no place to enter text. */
10847 if (no_console_input())
10848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 return;
10851 }
10852#endif
10853
10854 cmd_silent = FALSE; /* Want to see the prompt. */
10855 if (prompt != NULL)
10856 {
10857 /* Only the part of the message after the last NL is considered as
10858 * prompt for the command line */
10859 p = vim_strrchr(prompt, '\n');
10860 if (p == NULL)
10861 p = prompt;
10862 else
10863 {
10864 ++p;
10865 c = *p;
10866 *p = NUL;
10867 msg_start();
10868 msg_clr_eos();
10869 msg_puts_attr(prompt, echo_attr);
10870 msg_didout = FALSE;
10871 msg_starthere();
10872 *p = c;
10873 }
10874 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 if (argvars[1].v_type != VAR_UNKNOWN)
10877 {
10878 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10879 if (defstr != NULL)
10880 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881
Bram Moolenaar4463f292005-09-25 22:20:24 +000010882 if (argvars[2].v_type != VAR_UNKNOWN)
10883 {
10884 char_u *xp_name;
10885 int xp_namelen;
10886 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010887
Bram Moolenaar4463f292005-09-25 22:20:24 +000010888 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010889
Bram Moolenaar4463f292005-09-25 22:20:24 +000010890 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10891 if (xp_name == NULL)
10892 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010893
Bram Moolenaar4463f292005-09-25 22:20:24 +000010894 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010895
Bram Moolenaar4463f292005-09-25 22:20:24 +000010896 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10897 &xp_arg) == FAIL)
10898 return;
10899 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010900 }
10901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 if (defstr != NULL)
10903 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010904 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10905 xp_type, xp_arg);
10906
10907 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 /* since the user typed this, no need to wait for return */
10910 need_wait_return = FALSE;
10911 msg_didout = FALSE;
10912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 cmd_silent = cmd_silent_save;
10914}
10915
10916/*
10917 * "inputdialog()" function
10918 */
10919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010921 typval_T *argvars;
10922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923{
10924#if defined(FEAT_GUI_TEXTDIALOG)
10925 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10926 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10927 {
10928 char_u *message;
10929 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 message = get_tv_string_chk(&argvars[0]);
10933 if (argvars[1].v_type != VAR_UNKNOWN
10934 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010935 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 else
10937 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 if (message != NULL && defstr != NULL
10939 && do_dialog(VIM_QUESTION, NULL, message,
10940 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 else
10943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 if (message != NULL && defstr != NULL
10945 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010946 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 rettv->vval.v_string = vim_strsave(
10948 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010950 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010952 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 }
10954 else
10955#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010956 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957}
10958
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000010959/*
10960 * "inputlist()" function
10961 */
10962 static void
10963f_inputlist(argvars, rettv)
10964 typval_T *argvars;
10965 typval_T *rettv;
10966{
10967 listitem_T *li;
10968 int selected;
10969 int mouse_used;
10970
10971 rettv->vval.v_number = 0;
10972#ifdef NO_CONSOLE_INPUT
10973 /* While starting up, there is no place to enter text. */
10974 if (no_console_input())
10975 return;
10976#endif
10977 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
10978 {
10979 EMSG2(_(e_listarg), "inputlist()");
10980 return;
10981 }
10982
10983 msg_start();
10984 lines_left = Rows; /* avoid more prompt */
10985 msg_scroll = TRUE;
10986 msg_clr_eos();
10987
10988 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
10989 {
10990 msg_puts(get_tv_string(&li->li_tv));
10991 msg_putchar('\n');
10992 }
10993
10994 /* Ask for choice. */
10995 selected = prompt_for_number(&mouse_used);
10996 if (mouse_used)
10997 selected -= lines_left;
10998
10999 rettv->vval.v_number = selected;
11000}
11001
11002
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11004
11005/*
11006 * "inputrestore()" function
11007 */
11008/*ARGSUSED*/
11009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011011 typval_T *argvars;
11012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013{
11014 if (ga_userinput.ga_len > 0)
11015 {
11016 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11018 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 }
11021 else if (p_verbose > 1)
11022 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011023 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 }
11026}
11027
11028/*
11029 * "inputsave()" function
11030 */
11031/*ARGSUSED*/
11032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 typval_T *argvars;
11035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036{
11037 /* Add an entry to the stack of typehead storage. */
11038 if (ga_grow(&ga_userinput, 1) == OK)
11039 {
11040 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11041 + ga_userinput.ga_len);
11042 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 }
11045 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047}
11048
11049/*
11050 * "inputsecret()" function
11051 */
11052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011054 typval_T *argvars;
11055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056{
11057 ++cmdline_star;
11058 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 --cmdline_star;
11061 --inputsecret_flag;
11062}
11063
11064/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011065 * "insert()" function
11066 */
11067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011069 typval_T *argvars;
11070 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011071{
11072 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011073 listitem_T *item;
11074 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011076
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011077 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011078 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011079 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011080 else if ((l = argvars[0].vval.v_list) != NULL
11081 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011082 {
11083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 before = get_tv_number_chk(&argvars[2], &error);
11085 if (error)
11086 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011087
Bram Moolenaar758711c2005-02-02 23:11:38 +000011088 if (before == l->lv_len)
11089 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011090 else
11091 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011092 item = list_find(l, before);
11093 if (item == NULL)
11094 {
11095 EMSGN(_(e_listidx), before);
11096 l = NULL;
11097 }
11098 }
11099 if (l != NULL)
11100 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011101 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011102 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011103 }
11104 }
11105}
11106
11107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 * "isdirectory()" function
11109 */
11110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011112 typval_T *argvars;
11113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116}
11117
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011118/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011119 * "islocked()" function
11120 */
11121 static void
11122f_islocked(argvars, rettv)
11123 typval_T *argvars;
11124 typval_T *rettv;
11125{
11126 lval_T lv;
11127 char_u *end;
11128 dictitem_T *di;
11129
11130 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011131 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11132 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011133 if (end != NULL && lv.ll_name != NULL)
11134 {
11135 if (*end != NUL)
11136 EMSG(_(e_trailing));
11137 else
11138 {
11139 if (lv.ll_tv == NULL)
11140 {
11141 if (check_changedtick(lv.ll_name))
11142 rettv->vval.v_number = 1; /* always locked */
11143 else
11144 {
11145 di = find_var(lv.ll_name, NULL);
11146 if (di != NULL)
11147 {
11148 /* Consider a variable locked when:
11149 * 1. the variable itself is locked
11150 * 2. the value of the variable is locked.
11151 * 3. the List or Dict value is locked.
11152 */
11153 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11154 || tv_islocked(&di->di_tv));
11155 }
11156 }
11157 }
11158 else if (lv.ll_range)
11159 EMSG(_("E745: Range not allowed"));
11160 else if (lv.ll_newkey != NULL)
11161 EMSG2(_(e_dictkey), lv.ll_newkey);
11162 else if (lv.ll_list != NULL)
11163 /* List item. */
11164 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11165 else
11166 /* Dictionary item. */
11167 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11168 }
11169 }
11170
11171 clear_lval(&lv);
11172}
11173
Bram Moolenaar33570922005-01-25 22:26:29 +000011174static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011175
11176/*
11177 * Turn a dict into a list:
11178 * "what" == 0: list of keys
11179 * "what" == 1: list of values
11180 * "what" == 2: list of items
11181 */
11182 static void
11183dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011184 typval_T *argvars;
11185 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011186 int what;
11187{
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 list_T *l;
11189 list_T *l2;
11190 dictitem_T *di;
11191 hashitem_T *hi;
11192 listitem_T *li;
11193 listitem_T *li2;
11194 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011195 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011196
11197 rettv->vval.v_number = 0;
11198 if (argvars[0].v_type != VAR_DICT)
11199 {
11200 EMSG(_(e_dictreq));
11201 return;
11202 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011203 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011204 return;
11205
11206 l = list_alloc();
11207 if (l == NULL)
11208 return;
11209 rettv->v_type = VAR_LIST;
11210 rettv->vval.v_list = l;
11211 ++l->lv_refcount;
11212
Bram Moolenaar33570922005-01-25 22:26:29 +000011213 todo = d->dv_hashtab.ht_used;
11214 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011215 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011216 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011217 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011218 --todo;
11219 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011220
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011221 li = listitem_alloc();
11222 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011223 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011224 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011225
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011226 if (what == 0)
11227 {
11228 /* keys() */
11229 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011230 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011231 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11232 }
11233 else if (what == 1)
11234 {
11235 /* values() */
11236 copy_tv(&di->di_tv, &li->li_tv);
11237 }
11238 else
11239 {
11240 /* items() */
11241 l2 = list_alloc();
11242 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011243 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011244 li->li_tv.vval.v_list = l2;
11245 if (l2 == NULL)
11246 break;
11247 ++l2->lv_refcount;
11248
11249 li2 = listitem_alloc();
11250 if (li2 == NULL)
11251 break;
11252 list_append(l2, li2);
11253 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011254 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011255 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11256
11257 li2 = listitem_alloc();
11258 if (li2 == NULL)
11259 break;
11260 list_append(l2, li2);
11261 copy_tv(&di->di_tv, &li2->li_tv);
11262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011263 }
11264 }
11265}
11266
11267/*
11268 * "items(dict)" function
11269 */
11270 static void
11271f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011272 typval_T *argvars;
11273 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011274{
11275 dict_list(argvars, rettv, 2);
11276}
11277
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011279 * "join()" function
11280 */
11281 static void
11282f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 typval_T *argvars;
11284 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011285{
11286 garray_T ga;
11287 char_u *sep;
11288
11289 rettv->vval.v_number = 0;
11290 if (argvars[0].v_type != VAR_LIST)
11291 {
11292 EMSG(_(e_listreq));
11293 return;
11294 }
11295 if (argvars[0].vval.v_list == NULL)
11296 return;
11297 if (argvars[1].v_type == VAR_UNKNOWN)
11298 sep = (char_u *)" ";
11299 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011301
11302 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011303
11304 if (sep != NULL)
11305 {
11306 ga_init2(&ga, (int)sizeof(char), 80);
11307 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11308 ga_append(&ga, NUL);
11309 rettv->vval.v_string = (char_u *)ga.ga_data;
11310 }
11311 else
11312 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011313}
11314
11315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011316 * "keys()" function
11317 */
11318 static void
11319f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011320 typval_T *argvars;
11321 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011322{
11323 dict_list(argvars, rettv, 0);
11324}
11325
11326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 * "last_buffer_nr()" function.
11328 */
11329/*ARGSUSED*/
11330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *argvars;
11333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
11335 int n = 0;
11336 buf_T *buf;
11337
11338 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11339 if (n < buf->b_fnum)
11340 n = buf->b_fnum;
11341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011343}
11344
11345/*
11346 * "len()" function
11347 */
11348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011350 typval_T *argvars;
11351 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011352{
11353 switch (argvars[0].v_type)
11354 {
11355 case VAR_STRING:
11356 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 rettv->vval.v_number = (varnumber_T)STRLEN(
11358 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011359 break;
11360 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011362 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011363 case VAR_DICT:
11364 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11365 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011366 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011367 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011368 break;
11369 }
11370}
11371
Bram Moolenaar33570922005-01-25 22:26:29 +000011372static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011373
11374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 typval_T *argvars;
11377 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011378 int type;
11379{
11380#ifdef FEAT_LIBCALL
11381 char_u *string_in;
11382 char_u **string_result;
11383 int nr_result;
11384#endif
11385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011387 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011391
11392 if (check_restricted() || check_secure())
11393 return;
11394
11395#ifdef FEAT_LIBCALL
11396 /* The first two args must be strings, otherwise its meaningless */
11397 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11398 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011399 string_in = NULL;
11400 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011401 string_in = argvars[2].vval.v_string;
11402 if (type == VAR_NUMBER)
11403 string_result = NULL;
11404 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011405 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011406 if (mch_libcall(argvars[0].vval.v_string,
11407 argvars[1].vval.v_string,
11408 string_in,
11409 argvars[2].vval.v_number,
11410 string_result,
11411 &nr_result) == OK
11412 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011414 }
11415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416}
11417
11418/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011419 * "libcall()" function
11420 */
11421 static void
11422f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011423 typval_T *argvars;
11424 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011425{
11426 libcall_common(argvars, rettv, VAR_STRING);
11427}
11428
11429/*
11430 * "libcallnr()" function
11431 */
11432 static void
11433f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011434 typval_T *argvars;
11435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011436{
11437 libcall_common(argvars, rettv, VAR_NUMBER);
11438}
11439
11440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 * "line(string)" function
11442 */
11443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011445 typval_T *argvars;
11446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447{
11448 linenr_T lnum = 0;
11449 pos_T *fp;
11450
11451 fp = var2fpos(&argvars[0], TRUE);
11452 if (fp != NULL)
11453 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455}
11456
11457/*
11458 * "line2byte(lnum)" function
11459 */
11460/*ARGSUSED*/
11461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011463 typval_T *argvars;
11464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465{
11466#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468#else
11469 linenr_T lnum;
11470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11476 if (rettv->vval.v_number >= 0)
11477 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478#endif
11479}
11480
11481/*
11482 * "lispindent(lnum)" function
11483 */
11484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011486 typval_T *argvars;
11487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488{
11489#ifdef FEAT_LISP
11490 pos_T pos;
11491 linenr_T lnum;
11492
11493 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11496 {
11497 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 curwin->w_cursor = pos;
11500 }
11501 else
11502#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504}
11505
11506/*
11507 * "localtime()" function
11508 */
11509/*ARGSUSED*/
11510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011512 typval_T *argvars;
11513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516}
11517
Bram Moolenaar33570922005-01-25 22:26:29 +000011518static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519
11520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011522 typval_T *argvars;
11523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 int exact;
11525{
11526 char_u *keys;
11527 char_u *which;
11528 char_u buf[NUMBUFLEN];
11529 char_u *keys_buf = NULL;
11530 char_u *rhs;
11531 int mode;
11532 garray_T ga;
11533
11534 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 rettv->v_type = VAR_STRING;
11536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 if (*keys == NUL)
11540 return;
11541
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011542 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 else
11545 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011546 if (which == NULL)
11547 return;
11548
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 mode = get_map_mode(&which, 0);
11550
11551 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011552 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 vim_free(keys_buf);
11554 if (rhs != NULL)
11555 {
11556 ga_init(&ga);
11557 ga.ga_itemsize = 1;
11558 ga.ga_growsize = 40;
11559
11560 while (*rhs != NUL)
11561 ga_concat(&ga, str2special(&rhs, FALSE));
11562
11563 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 }
11566}
11567
11568/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011569 * "map()" function
11570 */
11571 static void
11572f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011573 typval_T *argvars;
11574 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011575{
11576 filter_map(argvars, rettv, TRUE);
11577}
11578
11579/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011580 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 */
11582 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011583f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011584 typval_T *argvars;
11585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011587 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588}
11589
11590/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011591 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 */
11593 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011594f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011595 typval_T *argvars;
11596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011598 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599}
11600
Bram Moolenaar33570922005-01-25 22:26:29 +000011601static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602
11603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011605 typval_T *argvars;
11606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 int type;
11608{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011609 char_u *str = NULL;
11610 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 char_u *pat;
11612 regmatch_T regmatch;
11613 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011614 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 char_u *save_cpo;
11616 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011617 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011618 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011619 list_T *l = NULL;
11620 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011621 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011622 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623
11624 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11625 save_cpo = p_cpo;
11626 p_cpo = (char_u *)"";
11627
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011628 rettv->vval.v_number = -1;
11629 if (type == 3)
11630 {
11631 /* return empty list when there are no matches */
11632 if ((rettv->vval.v_list = list_alloc()) == NULL)
11633 goto theend;
11634 rettv->v_type = VAR_LIST;
11635 ++rettv->vval.v_list->lv_refcount;
11636 }
11637 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639 rettv->v_type = VAR_STRING;
11640 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011643 if (argvars[0].v_type == VAR_LIST)
11644 {
11645 if ((l = argvars[0].vval.v_list) == NULL)
11646 goto theend;
11647 li = l->lv_first;
11648 }
11649 else
11650 expr = str = get_tv_string(&argvars[0]);
11651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011652 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11653 if (pat == NULL)
11654 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011655
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011656 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011658 int error = FALSE;
11659
11660 start = get_tv_number_chk(&argvars[2], &error);
11661 if (error)
11662 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011663 if (l != NULL)
11664 {
11665 li = list_find(l, start);
11666 if (li == NULL)
11667 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011668 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011669 }
11670 else
11671 {
11672 if (start < 0)
11673 start = 0;
11674 if (start > (long)STRLEN(str))
11675 goto theend;
11676 str += start;
11677 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011678
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011679 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 nth = get_tv_number_chk(&argvars[3], &error);
11681 if (error)
11682 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 }
11684
11685 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11686 if (regmatch.regprog != NULL)
11687 {
11688 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011689
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011690 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011691 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011692 if (l != NULL)
11693 {
11694 if (li == NULL)
11695 {
11696 match = FALSE;
11697 break;
11698 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011699 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011700 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011701 if (str == NULL)
11702 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011703 }
11704
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011705 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011706
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011707 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011708 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011709 if (l == NULL && !match)
11710 break;
11711
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011712 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011713 if (l != NULL)
11714 {
11715 li = li->li_next;
11716 ++idx;
11717 }
11718 else
11719 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011720#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011721 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011722#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011723 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011724#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011725 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011726 }
11727
11728 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011730 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011732 int i;
11733
11734 /* return list with matched string and submatches */
11735 for (i = 0; i < NSUBEXP; ++i)
11736 {
11737 if (regmatch.endp[i] == NULL)
11738 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011739 if (list_append_string(rettv->vval.v_list,
11740 regmatch.startp[i],
11741 (int)(regmatch.endp[i] - regmatch.startp[i]))
11742 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011743 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011744 }
11745 }
11746 else if (type == 2)
11747 {
11748 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011749 if (l != NULL)
11750 copy_tv(&li->li_tv, rettv);
11751 else
11752 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011754 }
11755 else if (l != NULL)
11756 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 else
11758 {
11759 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 (varnumber_T)(regmatch.startp[0] - str);
11762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 }
11767 }
11768 vim_free(regmatch.regprog);
11769 }
11770
11771theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011772 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 p_cpo = save_cpo;
11774}
11775
11776/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011777 * "match()" function
11778 */
11779 static void
11780f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011781 typval_T *argvars;
11782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011783{
11784 find_some_match(argvars, rettv, 1);
11785}
11786
11787/*
11788 * "matchend()" function
11789 */
11790 static void
11791f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011792 typval_T *argvars;
11793 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011794{
11795 find_some_match(argvars, rettv, 0);
11796}
11797
11798/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011799 * "matchlist()" function
11800 */
11801 static void
11802f_matchlist(argvars, rettv)
11803 typval_T *argvars;
11804 typval_T *rettv;
11805{
11806 find_some_match(argvars, rettv, 3);
11807}
11808
11809/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011810 * "matchstr()" function
11811 */
11812 static void
11813f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011814 typval_T *argvars;
11815 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011816{
11817 find_some_match(argvars, rettv, 2);
11818}
11819
Bram Moolenaar33570922005-01-25 22:26:29 +000011820static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011821
11822 static void
11823max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011824 typval_T *argvars;
11825 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011826 int domax;
11827{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011828 long n = 0;
11829 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011830 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011831
11832 if (argvars[0].v_type == VAR_LIST)
11833 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011834 list_T *l;
11835 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011836
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011837 l = argvars[0].vval.v_list;
11838 if (l != NULL)
11839 {
11840 li = l->lv_first;
11841 if (li != NULL)
11842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011843 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011844 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011845 {
11846 li = li->li_next;
11847 if (li == NULL)
11848 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011849 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011850 if (domax ? i > n : i < n)
11851 n = i;
11852 }
11853 }
11854 }
11855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011856 else if (argvars[0].v_type == VAR_DICT)
11857 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011859 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011860 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011861 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011862
11863 d = argvars[0].vval.v_dict;
11864 if (d != NULL)
11865 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011866 todo = d->dv_hashtab.ht_used;
11867 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011869 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011871 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011872 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011873 if (first)
11874 {
11875 n = i;
11876 first = FALSE;
11877 }
11878 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011879 n = i;
11880 }
11881 }
11882 }
11883 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011884 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011885 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011886 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011887}
11888
11889/*
11890 * "max()" function
11891 */
11892 static void
11893f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011894 typval_T *argvars;
11895 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011896{
11897 max_min(argvars, rettv, TRUE);
11898}
11899
11900/*
11901 * "min()" function
11902 */
11903 static void
11904f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *argvars;
11906 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011907{
11908 max_min(argvars, rettv, FALSE);
11909}
11910
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011911static int mkdir_recurse __ARGS((char_u *dir, int prot));
11912
11913/*
11914 * Create the directory in which "dir" is located, and higher levels when
11915 * needed.
11916 */
11917 static int
11918mkdir_recurse(dir, prot)
11919 char_u *dir;
11920 int prot;
11921{
11922 char_u *p;
11923 char_u *updir;
11924 int r = FAIL;
11925
11926 /* Get end of directory name in "dir".
11927 * We're done when it's "/" or "c:/". */
11928 p = gettail_sep(dir);
11929 if (p <= get_past_head(dir))
11930 return OK;
11931
11932 /* If the directory exists we're done. Otherwise: create it.*/
11933 updir = vim_strnsave(dir, (int)(p - dir));
11934 if (updir == NULL)
11935 return FAIL;
11936 if (mch_isdir(updir))
11937 r = OK;
11938 else if (mkdir_recurse(updir, prot) == OK)
11939 r = vim_mkdir_emsg(updir, prot);
11940 vim_free(updir);
11941 return r;
11942}
11943
11944#ifdef vim_mkdir
11945/*
11946 * "mkdir()" function
11947 */
11948 static void
11949f_mkdir(argvars, rettv)
11950 typval_T *argvars;
11951 typval_T *rettv;
11952{
11953 char_u *dir;
11954 char_u buf[NUMBUFLEN];
11955 int prot = 0755;
11956
11957 rettv->vval.v_number = FAIL;
11958 if (check_restricted() || check_secure())
11959 return;
11960
11961 dir = get_tv_string_buf(&argvars[0], buf);
11962 if (argvars[1].v_type != VAR_UNKNOWN)
11963 {
11964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011965 prot = get_tv_number_chk(&argvars[2], NULL);
11966 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011967 mkdir_recurse(dir, prot);
11968 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011969 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011970}
11971#endif
11972
Bram Moolenaar0d660222005-01-07 21:51:51 +000011973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 * "mode()" function
11975 */
11976/*ARGSUSED*/
11977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011979 typval_T *argvars;
11980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
11982 char_u buf[2];
11983
11984#ifdef FEAT_VISUAL
11985 if (VIsual_active)
11986 {
11987 if (VIsual_select)
11988 buf[0] = VIsual_mode + 's' - 'v';
11989 else
11990 buf[0] = VIsual_mode;
11991 }
11992 else
11993#endif
11994 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11995 buf[0] = 'r';
11996 else if (State & INSERT)
11997 {
11998 if (State & REPLACE_FLAG)
11999 buf[0] = 'R';
12000 else
12001 buf[0] = 'i';
12002 }
12003 else if (State & CMDLINE)
12004 buf[0] = 'c';
12005 else
12006 buf[0] = 'n';
12007
12008 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 rettv->vval.v_string = vim_strsave(buf);
12010 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011}
12012
12013/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014 * "nextnonblank()" function
12015 */
12016 static void
12017f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012018 typval_T *argvars;
12019 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012020{
12021 linenr_T lnum;
12022
12023 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012025 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012026 {
12027 lnum = 0;
12028 break;
12029 }
12030 if (*skipwhite(ml_get(lnum)) != NUL)
12031 break;
12032 }
12033 rettv->vval.v_number = lnum;
12034}
12035
12036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 * "nr2char()" function
12038 */
12039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012041 typval_T *argvars;
12042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043{
12044 char_u buf[NUMBUFLEN];
12045
12046#ifdef FEAT_MBYTE
12047 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 else
12050#endif
12051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 buf[1] = NUL;
12054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->v_type = VAR_STRING;
12056 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012057}
12058
12059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012060 * "prevnonblank()" function
12061 */
12062 static void
12063f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *argvars;
12065 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012066{
12067 linenr_T lnum;
12068
12069 lnum = get_tv_lnum(argvars);
12070 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12071 lnum = 0;
12072 else
12073 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12074 --lnum;
12075 rettv->vval.v_number = lnum;
12076}
12077
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012078#ifdef HAVE_STDARG_H
12079/* This dummy va_list is here because:
12080 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12081 * - locally in the function results in a "used before set" warning
12082 * - using va_start() to initialize it gives "function with fixed args" error */
12083static va_list ap;
12084#endif
12085
Bram Moolenaar8c711452005-01-14 21:53:12 +000012086/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012087 * "printf()" function
12088 */
12089 static void
12090f_printf(argvars, rettv)
12091 typval_T *argvars;
12092 typval_T *rettv;
12093{
12094 rettv->v_type = VAR_STRING;
12095 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012096#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012097 {
12098 char_u buf[NUMBUFLEN];
12099 int len;
12100 char_u *s;
12101 int saved_did_emsg = did_emsg;
12102 char *fmt;
12103
12104 /* Get the required length, allocate the buffer and do it for real. */
12105 did_emsg = FALSE;
12106 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012107 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012108 if (!did_emsg)
12109 {
12110 s = alloc(len + 1);
12111 if (s != NULL)
12112 {
12113 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012114 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012115 }
12116 }
12117 did_emsg |= saved_did_emsg;
12118 }
12119#endif
12120}
12121
12122/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012123 * "range()" function
12124 */
12125 static void
12126f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012127 typval_T *argvars;
12128 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012129{
12130 long start;
12131 long end;
12132 long stride = 1;
12133 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012134 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012135 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012138 if (argvars[1].v_type == VAR_UNKNOWN)
12139 {
12140 end = start - 1;
12141 start = 0;
12142 }
12143 else
12144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012145 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012146 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012147 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012148 }
12149
12150 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012151 if (error)
12152 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012153 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012154 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012155 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012156 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012157 else
12158 {
12159 l = list_alloc();
12160 if (l != NULL)
12161 {
12162 rettv->v_type = VAR_LIST;
12163 rettv->vval.v_list = l;
12164 ++l->lv_refcount;
12165
12166 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012167 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012168 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012169 }
12170 }
12171}
12172
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012173/*
12174 * "readfile()" function
12175 */
12176 static void
12177f_readfile(argvars, rettv)
12178 typval_T *argvars;
12179 typval_T *rettv;
12180{
12181 int binary = FALSE;
12182 char_u *fname;
12183 FILE *fd;
12184 list_T *l;
12185 listitem_T *li;
12186#define FREAD_SIZE 200 /* optimized for text lines */
12187 char_u buf[FREAD_SIZE];
12188 int readlen; /* size of last fread() */
12189 int buflen; /* nr of valid chars in buf[] */
12190 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12191 int tolist; /* first byte in buf[] still to be put in list */
12192 int chop; /* how many CR to chop off */
12193 char_u *prev = NULL; /* previously read bytes, if any */
12194 int prevlen = 0; /* length of "prev" if not NULL */
12195 char_u *s;
12196 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012197 long maxline = MAXLNUM;
12198 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012199
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012200 if (argvars[1].v_type != VAR_UNKNOWN)
12201 {
12202 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12203 binary = TRUE;
12204 if (argvars[2].v_type != VAR_UNKNOWN)
12205 maxline = get_tv_number(&argvars[2]);
12206 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012207
12208 l = list_alloc();
12209 if (l == NULL)
12210 return;
12211 rettv->v_type = VAR_LIST;
12212 rettv->vval.v_list = l;
12213 l->lv_refcount = 1;
12214
12215 /* Always open the file in binary mode, library functions have a mind of
12216 * their own about CR-LF conversion. */
12217 fname = get_tv_string(&argvars[0]);
12218 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12219 {
12220 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12221 return;
12222 }
12223
12224 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012225 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012226 {
12227 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12228 buflen = filtd + readlen;
12229 tolist = 0;
12230 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12231 {
12232 if (buf[filtd] == '\n' || readlen <= 0)
12233 {
12234 /* Only when in binary mode add an empty list item when the
12235 * last line ends in a '\n'. */
12236 if (!binary && readlen == 0 && filtd == 0)
12237 break;
12238
12239 /* Found end-of-line or end-of-file: add a text line to the
12240 * list. */
12241 chop = 0;
12242 if (!binary)
12243 while (filtd - chop - 1 >= tolist
12244 && buf[filtd - chop - 1] == '\r')
12245 ++chop;
12246 len = filtd - tolist - chop;
12247 if (prev == NULL)
12248 s = vim_strnsave(buf + tolist, len);
12249 else
12250 {
12251 s = alloc((unsigned)(prevlen + len + 1));
12252 if (s != NULL)
12253 {
12254 mch_memmove(s, prev, prevlen);
12255 vim_free(prev);
12256 prev = NULL;
12257 mch_memmove(s + prevlen, buf + tolist, len);
12258 s[prevlen + len] = NUL;
12259 }
12260 }
12261 tolist = filtd + 1;
12262
12263 li = listitem_alloc();
12264 if (li == NULL)
12265 {
12266 vim_free(s);
12267 break;
12268 }
12269 li->li_tv.v_type = VAR_STRING;
12270 li->li_tv.v_lock = 0;
12271 li->li_tv.vval.v_string = s;
12272 list_append(l, li);
12273
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012274 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012275 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012276 if (readlen <= 0)
12277 break;
12278 }
12279 else if (buf[filtd] == NUL)
12280 buf[filtd] = '\n';
12281 }
12282 if (readlen <= 0)
12283 break;
12284
12285 if (tolist == 0)
12286 {
12287 /* "buf" is full, need to move text to an allocated buffer */
12288 if (prev == NULL)
12289 {
12290 prev = vim_strnsave(buf, buflen);
12291 prevlen = buflen;
12292 }
12293 else
12294 {
12295 s = alloc((unsigned)(prevlen + buflen));
12296 if (s != NULL)
12297 {
12298 mch_memmove(s, prev, prevlen);
12299 mch_memmove(s + prevlen, buf, buflen);
12300 vim_free(prev);
12301 prev = s;
12302 prevlen += buflen;
12303 }
12304 }
12305 filtd = 0;
12306 }
12307 else
12308 {
12309 mch_memmove(buf, buf + tolist, buflen - tolist);
12310 filtd -= tolist;
12311 }
12312 }
12313
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012314 /*
12315 * For a negative line count use only the lines at the end of the file,
12316 * free the rest.
12317 */
12318 if (maxline < 0)
12319 while (cnt > -maxline)
12320 {
12321 listitem_remove(l, l->lv_first);
12322 --cnt;
12323 }
12324
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012325 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012326 fclose(fd);
12327}
12328
12329
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12331static void make_connection __ARGS((void));
12332static int check_connection __ARGS((void));
12333
12334 static void
12335make_connection()
12336{
12337 if (X_DISPLAY == NULL
12338# ifdef FEAT_GUI
12339 && !gui.in_use
12340# endif
12341 )
12342 {
12343 x_force_connect = TRUE;
12344 setup_term_clip();
12345 x_force_connect = FALSE;
12346 }
12347}
12348
12349 static int
12350check_connection()
12351{
12352 make_connection();
12353 if (X_DISPLAY == NULL)
12354 {
12355 EMSG(_("E240: No connection to Vim server"));
12356 return FAIL;
12357 }
12358 return OK;
12359}
12360#endif
12361
12362#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012363static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012364
12365 static void
12366remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012367 typval_T *argvars;
12368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012369 int expr;
12370{
12371 char_u *server_name;
12372 char_u *keys;
12373 char_u *r = NULL;
12374 char_u buf[NUMBUFLEN];
12375# ifdef WIN32
12376 HWND w;
12377# else
12378 Window w;
12379# endif
12380
12381 if (check_restricted() || check_secure())
12382 return;
12383
12384# ifdef FEAT_X11
12385 if (check_connection() == FAIL)
12386 return;
12387# endif
12388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012389 server_name = get_tv_string_chk(&argvars[0]);
12390 if (server_name == NULL)
12391 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392 keys = get_tv_string_buf(&argvars[1], buf);
12393# ifdef WIN32
12394 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12395# else
12396 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12397 < 0)
12398# endif
12399 {
12400 if (r != NULL)
12401 EMSG(r); /* sending worked but evaluation failed */
12402 else
12403 EMSG2(_("E241: Unable to send to %s"), server_name);
12404 return;
12405 }
12406
12407 rettv->vval.v_string = r;
12408
12409 if (argvars[2].v_type != VAR_UNKNOWN)
12410 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012411 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012412 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012413 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012414
12415 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012416 v.di_tv.v_type = VAR_STRING;
12417 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012418 idvar = get_tv_string_chk(&argvars[2]);
12419 if (idvar != NULL)
12420 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012422 }
12423}
12424#endif
12425
12426/*
12427 * "remote_expr()" function
12428 */
12429/*ARGSUSED*/
12430 static void
12431f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012432 typval_T *argvars;
12433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434{
12435 rettv->v_type = VAR_STRING;
12436 rettv->vval.v_string = NULL;
12437#ifdef FEAT_CLIENTSERVER
12438 remote_common(argvars, rettv, TRUE);
12439#endif
12440}
12441
12442/*
12443 * "remote_foreground()" function
12444 */
12445/*ARGSUSED*/
12446 static void
12447f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012448 typval_T *argvars;
12449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012450{
12451 rettv->vval.v_number = 0;
12452#ifdef FEAT_CLIENTSERVER
12453# ifdef WIN32
12454 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012455 {
12456 char_u *server_name = get_tv_string_chk(&argvars[0]);
12457
12458 if (server_name != NULL)
12459 serverForeground(server_name);
12460 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012461# else
12462 /* Send a foreground() expression to the server. */
12463 argvars[1].v_type = VAR_STRING;
12464 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12465 argvars[2].v_type = VAR_UNKNOWN;
12466 remote_common(argvars, rettv, TRUE);
12467 vim_free(argvars[1].vval.v_string);
12468# endif
12469#endif
12470}
12471
12472/*ARGSUSED*/
12473 static void
12474f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012475 typval_T *argvars;
12476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012477{
12478#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012479 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012480 char_u *s = NULL;
12481# ifdef WIN32
12482 int n = 0;
12483# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012485
12486 if (check_restricted() || check_secure())
12487 {
12488 rettv->vval.v_number = -1;
12489 return;
12490 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012491 serverid = get_tv_string_chk(&argvars[0]);
12492 if (serverid == NULL)
12493 {
12494 rettv->vval.v_number = -1;
12495 return; /* type error; errmsg already given */
12496 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012498 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499 if (n == 0)
12500 rettv->vval.v_number = -1;
12501 else
12502 {
12503 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12504 rettv->vval.v_number = (s != NULL);
12505 }
12506# else
12507 rettv->vval.v_number = 0;
12508 if (check_connection() == FAIL)
12509 return;
12510
12511 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012512 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513# endif
12514
12515 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 char_u *retvar;
12518
Bram Moolenaar33570922005-01-25 22:26:29 +000012519 v.di_tv.v_type = VAR_STRING;
12520 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012521 retvar = get_tv_string_chk(&argvars[1]);
12522 if (retvar != NULL)
12523 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012524 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012525 }
12526#else
12527 rettv->vval.v_number = -1;
12528#endif
12529}
12530
12531/*ARGSUSED*/
12532 static void
12533f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *argvars;
12535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012536{
12537 char_u *r = NULL;
12538
12539#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012540 char_u *serverid = get_tv_string_chk(&argvars[0]);
12541
12542 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012543 {
12544# ifdef WIN32
12545 /* The server's HWND is encoded in the 'id' parameter */
12546 int n = 0;
12547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012548 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012549 if (n != 0)
12550 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12551 if (r == NULL)
12552# else
12553 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012554 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555# endif
12556 EMSG(_("E277: Unable to read a server reply"));
12557 }
12558#endif
12559 rettv->v_type = VAR_STRING;
12560 rettv->vval.v_string = r;
12561}
12562
12563/*
12564 * "remote_send()" function
12565 */
12566/*ARGSUSED*/
12567 static void
12568f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 typval_T *argvars;
12570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571{
12572 rettv->v_type = VAR_STRING;
12573 rettv->vval.v_string = NULL;
12574#ifdef FEAT_CLIENTSERVER
12575 remote_common(argvars, rettv, FALSE);
12576#endif
12577}
12578
12579/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012580 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012581 */
12582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012584 typval_T *argvars;
12585 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012586{
Bram Moolenaar33570922005-01-25 22:26:29 +000012587 list_T *l;
12588 listitem_T *item, *item2;
12589 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012590 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012591 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012592 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 dict_T *d;
12594 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012595
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012596 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012597 if (argvars[0].v_type == VAR_DICT)
12598 {
12599 if (argvars[2].v_type != VAR_UNKNOWN)
12600 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012601 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012602 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 key = get_tv_string_chk(&argvars[1]);
12605 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012607 di = dict_find(d, key, -1);
12608 if (di == NULL)
12609 EMSG2(_(e_dictkey), key);
12610 else
12611 {
12612 *rettv = di->di_tv;
12613 init_tv(&di->di_tv);
12614 dictitem_remove(d, di);
12615 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012616 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012617 }
12618 }
12619 else if (argvars[0].v_type != VAR_LIST)
12620 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012621 else if ((l = argvars[0].vval.v_list) != NULL
12622 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012624 int error = FALSE;
12625
12626 idx = get_tv_number_chk(&argvars[1], &error);
12627 if (error)
12628 ; /* type error: do nothing, errmsg already given */
12629 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012630 EMSGN(_(e_listidx), idx);
12631 else
12632 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012633 if (argvars[2].v_type == VAR_UNKNOWN)
12634 {
12635 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012636 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012637 *rettv = item->li_tv;
12638 vim_free(item);
12639 }
12640 else
12641 {
12642 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643 end = get_tv_number_chk(&argvars[2], &error);
12644 if (error)
12645 ; /* type error: do nothing */
12646 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012647 EMSGN(_(e_listidx), end);
12648 else
12649 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012650 int cnt = 0;
12651
12652 for (li = item; li != NULL; li = li->li_next)
12653 {
12654 ++cnt;
12655 if (li == item2)
12656 break;
12657 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012658 if (li == NULL) /* didn't find "item2" after "item" */
12659 EMSG(_(e_invrange));
12660 else
12661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012662 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012663 l = list_alloc();
12664 if (l != NULL)
12665 {
12666 rettv->v_type = VAR_LIST;
12667 rettv->vval.v_list = l;
12668 l->lv_first = item;
12669 l->lv_last = item2;
12670 l->lv_refcount = 1;
12671 item->li_prev = NULL;
12672 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012673 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012674 }
12675 }
12676 }
12677 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012678 }
12679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680}
12681
12682/*
12683 * "rename({from}, {to})" function
12684 */
12685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 typval_T *argvars;
12688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689{
12690 char_u buf[NUMBUFLEN];
12691
12692 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12696 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697}
12698
12699/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012700 * "repeat()" function
12701 */
12702/*ARGSUSED*/
12703 static void
12704f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 typval_T *argvars;
12706 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012707{
12708 char_u *p;
12709 int n;
12710 int slen;
12711 int len;
12712 char_u *r;
12713 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012714 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012715
12716 n = get_tv_number(&argvars[1]);
12717 if (argvars[0].v_type == VAR_LIST)
12718 {
12719 l = list_alloc();
12720 if (l != NULL && argvars[0].vval.v_list != NULL)
12721 {
12722 l->lv_refcount = 1;
12723 while (n-- > 0)
12724 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12725 break;
12726 }
12727 rettv->v_type = VAR_LIST;
12728 rettv->vval.v_list = l;
12729 }
12730 else
12731 {
12732 p = get_tv_string(&argvars[0]);
12733 rettv->v_type = VAR_STRING;
12734 rettv->vval.v_string = NULL;
12735
12736 slen = (int)STRLEN(p);
12737 len = slen * n;
12738 if (len <= 0)
12739 return;
12740
12741 r = alloc(len + 1);
12742 if (r != NULL)
12743 {
12744 for (i = 0; i < n; i++)
12745 mch_memmove(r + i * slen, p, (size_t)slen);
12746 r[len] = NUL;
12747 }
12748
12749 rettv->vval.v_string = r;
12750 }
12751}
12752
12753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 * "resolve()" function
12755 */
12756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012758 typval_T *argvars;
12759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760{
12761 char_u *p;
12762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764#ifdef FEAT_SHORTCUT
12765 {
12766 char_u *v = NULL;
12767
12768 v = mch_resolve_shortcut(p);
12769 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 }
12774#else
12775# ifdef HAVE_READLINK
12776 {
12777 char_u buf[MAXPATHL + 1];
12778 char_u *cpy;
12779 int len;
12780 char_u *remain = NULL;
12781 char_u *q;
12782 int is_relative_to_current = FALSE;
12783 int has_trailing_pathsep = FALSE;
12784 int limit = 100;
12785
12786 p = vim_strsave(p);
12787
12788 if (p[0] == '.' && (vim_ispathsep(p[1])
12789 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12790 is_relative_to_current = TRUE;
12791
12792 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012793 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 has_trailing_pathsep = TRUE;
12795
12796 q = getnextcomp(p);
12797 if (*q != NUL)
12798 {
12799 /* Separate the first path component in "p", and keep the
12800 * remainder (beginning with the path separator). */
12801 remain = vim_strsave(q - 1);
12802 q[-1] = NUL;
12803 }
12804
12805 for (;;)
12806 {
12807 for (;;)
12808 {
12809 len = readlink((char *)p, (char *)buf, MAXPATHL);
12810 if (len <= 0)
12811 break;
12812 buf[len] = NUL;
12813
12814 if (limit-- == 0)
12815 {
12816 vim_free(p);
12817 vim_free(remain);
12818 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 goto fail;
12821 }
12822
12823 /* Ensure that the result will have a trailing path separator
12824 * if the argument has one. */
12825 if (remain == NULL && has_trailing_pathsep)
12826 add_pathsep(buf);
12827
12828 /* Separate the first path component in the link value and
12829 * concatenate the remainders. */
12830 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12831 if (*q != NUL)
12832 {
12833 if (remain == NULL)
12834 remain = vim_strsave(q - 1);
12835 else
12836 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012837 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 if (cpy != NULL)
12839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 vim_free(remain);
12841 remain = cpy;
12842 }
12843 }
12844 q[-1] = NUL;
12845 }
12846
12847 q = gettail(p);
12848 if (q > p && *q == NUL)
12849 {
12850 /* Ignore trailing path separator. */
12851 q[-1] = NUL;
12852 q = gettail(p);
12853 }
12854 if (q > p && !mch_isFullName(buf))
12855 {
12856 /* symlink is relative to directory of argument */
12857 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12858 if (cpy != NULL)
12859 {
12860 STRCPY(cpy, p);
12861 STRCPY(gettail(cpy), buf);
12862 vim_free(p);
12863 p = cpy;
12864 }
12865 }
12866 else
12867 {
12868 vim_free(p);
12869 p = vim_strsave(buf);
12870 }
12871 }
12872
12873 if (remain == NULL)
12874 break;
12875
12876 /* Append the first path component of "remain" to "p". */
12877 q = getnextcomp(remain + 1);
12878 len = q - remain - (*q != NUL);
12879 cpy = vim_strnsave(p, STRLEN(p) + len);
12880 if (cpy != NULL)
12881 {
12882 STRNCAT(cpy, remain, len);
12883 vim_free(p);
12884 p = cpy;
12885 }
12886 /* Shorten "remain". */
12887 if (*q != NUL)
12888 STRCPY(remain, q - 1);
12889 else
12890 {
12891 vim_free(remain);
12892 remain = NULL;
12893 }
12894 }
12895
12896 /* If the result is a relative path name, make it explicitly relative to
12897 * the current directory if and only if the argument had this form. */
12898 if (!vim_ispathsep(*p))
12899 {
12900 if (is_relative_to_current
12901 && *p != NUL
12902 && !(p[0] == '.'
12903 && (p[1] == NUL
12904 || vim_ispathsep(p[1])
12905 || (p[1] == '.'
12906 && (p[2] == NUL
12907 || vim_ispathsep(p[2]))))))
12908 {
12909 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012910 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 if (cpy != NULL)
12912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 vim_free(p);
12914 p = cpy;
12915 }
12916 }
12917 else if (!is_relative_to_current)
12918 {
12919 /* Strip leading "./". */
12920 q = p;
12921 while (q[0] == '.' && vim_ispathsep(q[1]))
12922 q += 2;
12923 if (q > p)
12924 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12925 }
12926 }
12927
12928 /* Ensure that the result will have no trailing path separator
12929 * if the argument had none. But keep "/" or "//". */
12930 if (!has_trailing_pathsep)
12931 {
12932 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012933 if (after_pathsep(p, q))
12934 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 }
12936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012937 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 }
12939# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941# endif
12942#endif
12943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
12946#ifdef HAVE_READLINK
12947fail:
12948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950}
12951
12952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012953 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 */
12955 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012957 typval_T *argvars;
12958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959{
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 list_T *l;
12961 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962
Bram Moolenaar0d660222005-01-07 21:51:51 +000012963 rettv->vval.v_number = 0;
12964 if (argvars[0].v_type != VAR_LIST)
12965 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012966 else if ((l = argvars[0].vval.v_list) != NULL
12967 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012968 {
12969 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012970 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012971 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012972 while (li != NULL)
12973 {
12974 ni = li->li_prev;
12975 list_append(l, li);
12976 li = ni;
12977 }
12978 rettv->vval.v_list = l;
12979 rettv->v_type = VAR_LIST;
12980 ++l->lv_refcount;
12981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982}
12983
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012984#define SP_NOMOVE 1 /* don't move cursor */
12985#define SP_REPEAT 2 /* repeat to find outer pair */
12986#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012987#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012988
Bram Moolenaar33570922005-01-25 22:26:29 +000012989static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012990
12991/*
12992 * Get flags for a search function.
12993 * Possibly sets "p_ws".
12994 * Returns BACKWARD, FORWARD or zero (for an error).
12995 */
12996 static int
12997get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012998 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012999 int *flagsp;
13000{
13001 int dir = FORWARD;
13002 char_u *flags;
13003 char_u nbuf[NUMBUFLEN];
13004 int mask;
13005
13006 if (varp->v_type != VAR_UNKNOWN)
13007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 flags = get_tv_string_buf_chk(varp, nbuf);
13009 if (flags == NULL)
13010 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013011 while (*flags != NUL)
13012 {
13013 switch (*flags)
13014 {
13015 case 'b': dir = BACKWARD; break;
13016 case 'w': p_ws = TRUE; break;
13017 case 'W': p_ws = FALSE; break;
13018 default: mask = 0;
13019 if (flagsp != NULL)
13020 switch (*flags)
13021 {
13022 case 'n': mask = SP_NOMOVE; break;
13023 case 'r': mask = SP_REPEAT; break;
13024 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013025 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013026 }
13027 if (mask == 0)
13028 {
13029 EMSG2(_(e_invarg2), flags);
13030 dir = 0;
13031 }
13032 else
13033 *flagsp |= mask;
13034 }
13035 if (dir == 0)
13036 break;
13037 ++flags;
13038 }
13039 }
13040 return dir;
13041}
13042
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043/*
13044 * "search()" function
13045 */
13046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013047f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013048 typval_T *argvars;
13049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050{
13051 char_u *pat;
13052 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013053 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 int save_p_ws = p_ws;
13055 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013056 int flags = 0;
13057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013061 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13062 if (dir == 0)
13063 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013064 /*
13065 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13066 * Check to make sure only those flags are set.
13067 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13068 * flags cannot be set. Check for that condition also.
13069 */
13070 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13071 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013074 goto theend;
13075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013077 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13079 SEARCH_KEEP, RE_SEARCH) != FAIL)
13080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013081 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013082 if (flags & SP_SETPCMARK)
13083 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084 curwin->w_cursor = pos;
13085 /* "/$" will put the cursor after the end of the line, may need to
13086 * correct that here */
13087 check_cursor();
13088 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013089
13090 /* If 'n' flag is used: restore cursor position. */
13091 if (flags & SP_NOMOVE)
13092 curwin->w_cursor = save_cursor;
13093theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 p_ws = save_p_ws;
13095}
13096
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013098 * "searchdecl()" function
13099 */
13100 static void
13101f_searchdecl(argvars, rettv)
13102 typval_T *argvars;
13103 typval_T *rettv;
13104{
13105 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013106 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013107 int error = FALSE;
13108 char_u *name;
13109
13110 rettv->vval.v_number = 1; /* default: FAIL */
13111
13112 name = get_tv_string_chk(&argvars[0]);
13113 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013114 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013115 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013116 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13117 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13118 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013119 if (!error && name != NULL)
13120 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013121 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013122}
13123
13124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 * "searchpair()" function
13126 */
13127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *argvars;
13130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131{
13132 char_u *spat, *mpat, *epat;
13133 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 int dir;
13136 int flags = 0;
13137 char_u nbuf1[NUMBUFLEN];
13138 char_u nbuf2[NUMBUFLEN];
13139 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013144 spat = get_tv_string_chk(&argvars[0]);
13145 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13146 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13147 if (spat == NULL || mpat == NULL || epat == NULL)
13148 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 /* Handle the optional fourth argument: flags */
13151 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013152 if (dir == 0)
13153 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013154 /*
13155 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13156 */
13157 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13158 {
13159 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13160 goto theend;
13161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162
13163 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013164 if (argvars[3].v_type == VAR_UNKNOWN
13165 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 skip = (char_u *)"";
13167 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013168 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13169 if (skip == NULL)
13170 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013172 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13173
13174theend:
13175 p_ws = save_p_ws;
13176}
13177
13178/*
13179 * Search for a start/middle/end thing.
13180 * Used by searchpair(), see its documentation for the details.
13181 * Returns 0 or -1 for no match,
13182 */
13183 long
13184do_searchpair(spat, mpat, epat, dir, skip, flags)
13185 char_u *spat; /* start pattern */
13186 char_u *mpat; /* middle pattern */
13187 char_u *epat; /* end pattern */
13188 int dir; /* BACKWARD or FORWARD */
13189 char_u *skip; /* skip expression */
13190 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13191{
13192 char_u *save_cpo;
13193 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13194 long retval = 0;
13195 pos_T pos;
13196 pos_T firstpos;
13197 pos_T foundpos;
13198 pos_T save_cursor;
13199 pos_T save_pos;
13200 int n;
13201 int r;
13202 int nest = 1;
13203 int err;
13204
13205 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13206 save_cpo = p_cpo;
13207 p_cpo = (char_u *)"";
13208
13209 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13210 * start/middle/end (pat3, for the top pair). */
13211 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13212 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13213 if (pat2 == NULL || pat3 == NULL)
13214 goto theend;
13215 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13216 if (*mpat == NUL)
13217 STRCPY(pat3, pat2);
13218 else
13219 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13220 spat, epat, mpat);
13221
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 save_cursor = curwin->w_cursor;
13223 pos = curwin->w_cursor;
13224 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013225 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226 pat = pat3;
13227 for (;;)
13228 {
13229 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13230 SEARCH_KEEP, RE_SEARCH);
13231 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13232 /* didn't find it or found the first match again: FAIL */
13233 break;
13234
13235 if (firstpos.lnum == 0)
13236 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013237 if (equalpos(pos, foundpos))
13238 {
13239 /* Found the same position again. Can happen with a pattern that
13240 * has "\zs" at the end and searching backwards. Advance one
13241 * character and try again. */
13242 if (dir == BACKWARD)
13243 decl(&pos);
13244 else
13245 incl(&pos);
13246 }
13247 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248
13249 /* If the skip pattern matches, ignore this match. */
13250 if (*skip != NUL)
13251 {
13252 save_pos = curwin->w_cursor;
13253 curwin->w_cursor = pos;
13254 r = eval_to_bool(skip, &err, NULL, FALSE);
13255 curwin->w_cursor = save_pos;
13256 if (err)
13257 {
13258 /* Evaluating {skip} caused an error, break here. */
13259 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013260 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 break;
13262 }
13263 if (r)
13264 continue;
13265 }
13266
13267 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13268 {
13269 /* Found end when searching backwards or start when searching
13270 * forward: nested pair. */
13271 ++nest;
13272 pat = pat2; /* nested, don't search for middle */
13273 }
13274 else
13275 {
13276 /* Found end when searching forward or start when searching
13277 * backward: end of (nested) pair; or found middle in outer pair. */
13278 if (--nest == 1)
13279 pat = pat3; /* outer level, search for middle */
13280 }
13281
13282 if (nest == 0)
13283 {
13284 /* Found the match: return matchcount or line number. */
13285 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013286 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013288 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013289 if (flags & SP_SETPCMARK)
13290 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 curwin->w_cursor = pos;
13292 if (!(flags & SP_REPEAT))
13293 break;
13294 nest = 1; /* search for next unmatched */
13295 }
13296 }
13297
13298 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013299 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 curwin->w_cursor = save_cursor;
13301
13302theend:
13303 vim_free(pat2);
13304 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013306
13307 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308}
13309
Bram Moolenaar0d660222005-01-07 21:51:51 +000013310/*ARGSUSED*/
13311 static void
13312f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013313 typval_T *argvars;
13314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013316#ifdef FEAT_CLIENTSERVER
13317 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013318 char_u *server = get_tv_string_chk(&argvars[0]);
13319 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320
Bram Moolenaar0d660222005-01-07 21:51:51 +000013321 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 if (server == NULL || reply == NULL)
13323 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013324 if (check_restricted() || check_secure())
13325 return;
13326# ifdef FEAT_X11
13327 if (check_connection() == FAIL)
13328 return;
13329# endif
13330
13331 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013333 EMSG(_("E258: Unable to send to client"));
13334 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013336 rettv->vval.v_number = 0;
13337#else
13338 rettv->vval.v_number = -1;
13339#endif
13340}
13341
13342/*ARGSUSED*/
13343 static void
13344f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013347{
13348 char_u *r = NULL;
13349
13350#ifdef FEAT_CLIENTSERVER
13351# ifdef WIN32
13352 r = serverGetVimNames();
13353# else
13354 make_connection();
13355 if (X_DISPLAY != NULL)
13356 r = serverGetVimNames(X_DISPLAY);
13357# endif
13358#endif
13359 rettv->v_type = VAR_STRING;
13360 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361}
13362
13363/*
13364 * "setbufvar()" function
13365 */
13366/*ARGSUSED*/
13367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013369 typval_T *argvars;
13370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371{
13372 buf_T *buf;
13373#ifdef FEAT_AUTOCMD
13374 aco_save_T aco;
13375#else
13376 buf_T *save_curbuf;
13377#endif
13378 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 char_u nbuf[NUMBUFLEN];
13381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 rettv->vval.v_number = 0;
13383
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 if (check_restricted() || check_secure())
13385 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13387 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013388 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 varp = &argvars[2];
13390
13391 if (buf != NULL && varname != NULL && varp != NULL)
13392 {
13393 /* set curbuf to be our buf, temporarily */
13394#ifdef FEAT_AUTOCMD
13395 aucmd_prepbuf(&aco, buf);
13396#else
13397 save_curbuf = curbuf;
13398 curbuf = buf;
13399#endif
13400
13401 if (*varname == '&')
13402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 long numval;
13404 char_u *strval;
13405 int error = FALSE;
13406
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013408 numval = get_tv_number_chk(varp, &error);
13409 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 if (!error && strval != NULL)
13411 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 }
13413 else
13414 {
13415 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13416 if (bufvarname != NULL)
13417 {
13418 STRCPY(bufvarname, "b:");
13419 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013420 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421 vim_free(bufvarname);
13422 }
13423 }
13424
13425 /* reset notion of buffer */
13426#ifdef FEAT_AUTOCMD
13427 aucmd_restbuf(&aco);
13428#else
13429 curbuf = save_curbuf;
13430#endif
13431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432}
13433
13434/*
13435 * "setcmdpos()" function
13436 */
13437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442 int pos = (int)get_tv_number(&argvars[0]) - 1;
13443
13444 if (pos >= 0)
13445 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446}
13447
13448/*
13449 * "setline()" function
13450 */
13451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 typval_T *argvars;
13454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455{
13456 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013457 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013458 list_T *l = NULL;
13459 listitem_T *li = NULL;
13460 long added = 0;
13461 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013463 lnum = get_tv_lnum(&argvars[0]);
13464 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013466 l = argvars[1].vval.v_list;
13467 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013469 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013470 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013471
13472 rettv->vval.v_number = 0; /* OK */
13473 for (;;)
13474 {
13475 if (l != NULL)
13476 {
13477 /* list argument, get next string */
13478 if (li == NULL)
13479 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013480 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013481 li = li->li_next;
13482 }
13483
13484 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013485 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013486 break;
13487 if (lnum <= curbuf->b_ml.ml_line_count)
13488 {
13489 /* existing line, replace it */
13490 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13491 {
13492 changed_bytes(lnum, 0);
13493 check_cursor_col();
13494 rettv->vval.v_number = 0; /* OK */
13495 }
13496 }
13497 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13498 {
13499 /* lnum is one past the last line, append the line */
13500 ++added;
13501 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13502 rettv->vval.v_number = 0; /* OK */
13503 }
13504
13505 if (l == NULL) /* only one string argument */
13506 break;
13507 ++lnum;
13508 }
13509
13510 if (added > 0)
13511 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512}
13513
13514/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013515 * "setqflist()" function
13516 */
13517/*ARGSUSED*/
13518 static void
13519f_setqflist(argvars, rettv)
13520 typval_T *argvars;
13521 typval_T *rettv;
13522{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013523 char_u *act;
13524 int action = ' ';
13525
Bram Moolenaar2641f772005-03-25 21:58:17 +000013526 rettv->vval.v_number = -1;
13527
13528#ifdef FEAT_QUICKFIX
13529 if (argvars[0].v_type != VAR_LIST)
13530 EMSG(_(e_listreq));
13531 else
13532 {
13533 list_T *l = argvars[0].vval.v_list;
13534
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013535 if (argvars[1].v_type == VAR_STRING)
13536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013537 act = get_tv_string_chk(&argvars[1]);
13538 if (act == NULL)
13539 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013540 if (*act == 'a' || *act == 'r')
13541 action = *act;
13542 }
13543
13544 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013545 rettv->vval.v_number = 0;
13546 }
13547#endif
13548}
13549
13550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 * "setreg()" function
13552 */
13553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013554f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 typval_T *argvars;
13556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557{
13558 int regname;
13559 char_u *strregname;
13560 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 int append;
13563 char_u yank_type;
13564 long block_len;
13565
13566 block_len = -1;
13567 yank_type = MAUTO;
13568 append = FALSE;
13569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573 if (strregname == NULL)
13574 return; /* type error; errmsg already given */
13575 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 if (regname == 0 || regname == '@')
13577 regname = '"';
13578 else if (regname == '=')
13579 return;
13580
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 stropt = get_tv_string_chk(&argvars[2]);
13584 if (stropt == NULL)
13585 return; /* type error */
13586 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 switch (*stropt)
13588 {
13589 case 'a': case 'A': /* append */
13590 append = TRUE;
13591 break;
13592 case 'v': case 'c': /* character-wise selection */
13593 yank_type = MCHAR;
13594 break;
13595 case 'V': case 'l': /* line-wise selection */
13596 yank_type = MLINE;
13597 break;
13598#ifdef FEAT_VISUAL
13599 case 'b': case Ctrl_V: /* block-wise selection */
13600 yank_type = MBLOCK;
13601 if (VIM_ISDIGIT(stropt[1]))
13602 {
13603 ++stropt;
13604 block_len = getdigits(&stropt) - 1;
13605 --stropt;
13606 }
13607 break;
13608#endif
13609 }
13610 }
13611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612 strval = get_tv_string_chk(&argvars[1]);
13613 if (strval != NULL)
13614 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617}
13618
13619
13620/*
13621 * "setwinvar(expr)" function
13622 */
13623/*ARGSUSED*/
13624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *argvars;
13627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
13629 win_T *win;
13630#ifdef FEAT_WINDOWS
13631 win_T *save_curwin;
13632#endif
13633 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013634 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 char_u nbuf[NUMBUFLEN];
13636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013637 rettv->vval.v_number = 0;
13638
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 if (check_restricted() || check_secure())
13640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 varp = &argvars[2];
13644
13645 if (win != NULL && varname != NULL && varp != NULL)
13646 {
13647#ifdef FEAT_WINDOWS
13648 /* set curwin to be our win, temporarily */
13649 save_curwin = curwin;
13650 curwin = win;
13651 curbuf = curwin->w_buffer;
13652#endif
13653
13654 if (*varname == '&')
13655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 long numval;
13657 char_u *strval;
13658 int error = FALSE;
13659
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661 numval = get_tv_number_chk(varp, &error);
13662 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013663 if (!error && strval != NULL)
13664 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 }
13666 else
13667 {
13668 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13669 if (winvarname != NULL)
13670 {
13671 STRCPY(winvarname, "w:");
13672 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013673 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 vim_free(winvarname);
13675 }
13676 }
13677
13678#ifdef FEAT_WINDOWS
13679 /* Restore current window, if it's still valid (autocomands can make
13680 * it invalid). */
13681 if (win_valid(save_curwin))
13682 {
13683 curwin = save_curwin;
13684 curbuf = curwin->w_buffer;
13685 }
13686#endif
13687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688}
13689
13690/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013691 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 */
13693 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013694f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 typval_T *argvars;
13696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013698 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699
Bram Moolenaar0d660222005-01-07 21:51:51 +000013700 p = get_tv_string(&argvars[0]);
13701 rettv->vval.v_string = vim_strsave(p);
13702 simplify_filename(rettv->vval.v_string); /* simplify in place */
13703 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704}
13705
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706static int
13707#ifdef __BORLANDC__
13708 _RTLENTRYF
13709#endif
13710 item_compare __ARGS((const void *s1, const void *s2));
13711static int
13712#ifdef __BORLANDC__
13713 _RTLENTRYF
13714#endif
13715 item_compare2 __ARGS((const void *s1, const void *s2));
13716
13717static int item_compare_ic;
13718static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013720#define ITEM_COMPARE_FAIL 999
13721
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013723 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013725 static int
13726#ifdef __BORLANDC__
13727_RTLENTRYF
13728#endif
13729item_compare(s1, s2)
13730 const void *s1;
13731 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733 char_u *p1, *p2;
13734 char_u *tofree1, *tofree2;
13735 int res;
13736 char_u numbuf1[NUMBUFLEN];
13737 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738
Bram Moolenaar33570922005-01-25 22:26:29 +000013739 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13740 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013741 if (item_compare_ic)
13742 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013744 res = STRCMP(p1, p2);
13745 vim_free(tofree1);
13746 vim_free(tofree2);
13747 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748}
13749
13750 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013751#ifdef __BORLANDC__
13752_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013754item_compare2(s1, s2)
13755 const void *s1;
13756 const void *s2;
13757{
13758 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T rettv;
13760 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 /* shortcut after failure in previous call; compare all items equal */
13764 if (item_compare_func_err)
13765 return 0;
13766
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013767 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13768 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13770 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013771
13772 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13773 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013774 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775 clear_tv(&argv[0]);
13776 clear_tv(&argv[1]);
13777
13778 if (res == FAIL)
13779 res = ITEM_COMPARE_FAIL;
13780 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013781 /* return value has wrong type */
13782 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13783 if (item_compare_func_err)
13784 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013785 clear_tv(&rettv);
13786 return res;
13787}
13788
13789/*
13790 * "sort({list})" function
13791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013794 typval_T *argvars;
13795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796{
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 list_T *l;
13798 listitem_T *li;
13799 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013800 long len;
13801 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802
Bram Moolenaar0d660222005-01-07 21:51:51 +000013803 rettv->vval.v_number = 0;
13804 if (argvars[0].v_type != VAR_LIST)
13805 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 else
13807 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013809 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013810 return;
13811 rettv->vval.v_list = l;
13812 rettv->v_type = VAR_LIST;
13813 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814
Bram Moolenaar0d660222005-01-07 21:51:51 +000013815 len = list_len(l);
13816 if (len <= 1)
13817 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818
Bram Moolenaar0d660222005-01-07 21:51:51 +000013819 item_compare_ic = FALSE;
13820 item_compare_func = NULL;
13821 if (argvars[1].v_type != VAR_UNKNOWN)
13822 {
13823 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013824 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013825 else
13826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 int error = FALSE;
13828
13829 i = get_tv_number_chk(&argvars[1], &error);
13830 if (error)
13831 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013832 if (i == 1)
13833 item_compare_ic = TRUE;
13834 else
13835 item_compare_func = get_tv_string(&argvars[1]);
13836 }
13837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838
Bram Moolenaar0d660222005-01-07 21:51:51 +000013839 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013840 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013841 if (ptrs == NULL)
13842 return;
13843 i = 0;
13844 for (li = l->lv_first; li != NULL; li = li->li_next)
13845 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013848 /* test the compare function */
13849 if (item_compare_func != NULL
13850 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13851 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013852 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013854 {
13855 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013856 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013857 item_compare_func == NULL ? item_compare : item_compare2);
13858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 if (!item_compare_func_err)
13860 {
13861 /* Clear the List and append the items in the sorted order. */
13862 l->lv_first = l->lv_last = NULL;
13863 l->lv_len = 0;
13864 for (i = 0; i < len; ++i)
13865 list_append(l, ptrs[i]);
13866 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013867 }
13868
13869 vim_free(ptrs);
13870 }
13871}
13872
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013873/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013874 * "soundfold({word})" function
13875 */
13876 static void
13877f_soundfold(argvars, rettv)
13878 typval_T *argvars;
13879 typval_T *rettv;
13880{
13881 char_u *s;
13882
13883 rettv->v_type = VAR_STRING;
13884 s = get_tv_string(&argvars[0]);
13885#ifdef FEAT_SYN_HL
13886 rettv->vval.v_string = eval_soundfold(s);
13887#else
13888 rettv->vval.v_string = vim_strsave(s);
13889#endif
13890}
13891
13892/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013893 * "spellbadword()" function
13894 */
13895/* ARGSUSED */
13896 static void
13897f_spellbadword(argvars, rettv)
13898 typval_T *argvars;
13899 typval_T *rettv;
13900{
Bram Moolenaar4463f292005-09-25 22:20:24 +000013901 char_u *word = (char_u *)"";
13902#ifdef FEAT_SYN_HL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013903 int len = 0;
13904 hlf_T attr = HLF_COUNT;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013905 list_T *l;
13906#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013907
Bram Moolenaar4463f292005-09-25 22:20:24 +000013908 l = list_alloc();
13909 if (l == NULL)
13910 return;
13911 rettv->v_type = VAR_LIST;
13912 rettv->vval.v_list = l;
13913 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013914
13915#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000013916 if (argvars[0].v_type == VAR_UNKNOWN)
13917 {
13918 /* Find the start and length of the badly spelled word. */
13919 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
13920 if (len != 0)
13921 word = ml_get_cursor();
13922 }
13923 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13924 {
13925 char_u *str = get_tv_string_chk(&argvars[0]);
13926 int capcol = -1;
13927
13928 if (str != NULL)
13929 {
13930 /* Check the argument for spelling. */
13931 while (*str != NUL)
13932 {
13933 len = spell_check(curwin, str, &attr, &capcol);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013934 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013935 {
13936 word = str;
13937 break;
13938 }
13939 str += len;
13940 }
13941 }
13942 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013943#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000013944
13945 list_append_string(l, word, len);
13946 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013947 attr == HLF_SPB ? "bad" :
13948 attr == HLF_SPR ? "rare" :
13949 attr == HLF_SPL ? "local" :
13950 attr == HLF_SPC ? "caps" :
13951 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013952}
13953
13954/*
13955 * "spellsuggest()" function
13956 */
13957 static void
13958f_spellsuggest(argvars, rettv)
13959 typval_T *argvars;
13960 typval_T *rettv;
13961{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013962 list_T *l;
13963#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013964 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013965 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013966 int maxcount;
13967 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013968 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013969 listitem_T *li;
13970 int need_capital = FALSE;
13971#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013972
13973 l = list_alloc();
13974 if (l == NULL)
13975 return;
13976 rettv->v_type = VAR_LIST;
13977 rettv->vval.v_list = l;
13978 ++l->lv_refcount;
13979
13980#ifdef FEAT_SYN_HL
13981 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13982 {
13983 str = get_tv_string(&argvars[0]);
13984 if (argvars[1].v_type != VAR_UNKNOWN)
13985 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013986 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013987 if (maxcount <= 0)
13988 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013989 if (argvars[2].v_type != VAR_UNKNOWN)
13990 {
13991 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
13992 if (typeerr)
13993 return;
13994 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013995 }
13996 else
13997 maxcount = 25;
13998
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013999 spell_suggest_list(&ga, str, maxcount, need_capital);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014000
14001 for (i = 0; i < ga.ga_len; ++i)
14002 {
14003 str = ((char_u **)ga.ga_data)[i];
14004
14005 li = listitem_alloc();
14006 if (li == NULL)
14007 vim_free(str);
14008 else
14009 {
14010 li->li_tv.v_type = VAR_STRING;
14011 li->li_tv.v_lock = 0;
14012 li->li_tv.vval.v_string = str;
14013 list_append(l, li);
14014 }
14015 }
14016 ga_clear(&ga);
14017 }
14018#endif
14019}
14020
Bram Moolenaar0d660222005-01-07 21:51:51 +000014021 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014022f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014023 typval_T *argvars;
14024 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014025{
14026 char_u *str;
14027 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014028 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014029 regmatch_T regmatch;
14030 char_u patbuf[NUMBUFLEN];
14031 char_u *save_cpo;
14032 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014033 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014035 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014036 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037
14038 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14039 save_cpo = p_cpo;
14040 p_cpo = (char_u *)"";
14041
14042 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014043 if (argvars[1].v_type != VAR_UNKNOWN)
14044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14046 if (pat == NULL)
14047 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014048 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014050 }
14051 if (pat == NULL || *pat == NUL)
14052 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014053
14054 l = list_alloc();
14055 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014057 rettv->v_type = VAR_LIST;
14058 rettv->vval.v_list = l;
14059 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 if (typeerr)
14061 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062
Bram Moolenaar0d660222005-01-07 21:51:51 +000014063 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14064 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014067 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014068 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014069 if (*str == NUL)
14070 match = FALSE; /* empty item at the end */
14071 else
14072 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014073 if (match)
14074 end = regmatch.startp[0];
14075 else
14076 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014077 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14078 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014079 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014080 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 }
14083 if (!match)
14084 break;
14085 /* Advance to just after the match. */
14086 if (regmatch.endp[0] > str)
14087 col = 0;
14088 else
14089 {
14090 /* Don't get stuck at the same match. */
14091#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014092 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014093#else
14094 col = 1;
14095#endif
14096 }
14097 str = regmatch.endp[0];
14098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104}
14105
14106#ifdef HAVE_STRFTIME
14107/*
14108 * "strftime({format}[, {time}])" function
14109 */
14110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 typval_T *argvars;
14113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114{
14115 char_u result_buf[256];
14116 struct tm *curtime;
14117 time_t seconds;
14118 char_u *p;
14119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014120 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014123 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 seconds = time(NULL);
14125 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 curtime = localtime(&seconds);
14128 /* MSVC returns NULL for an invalid value of seconds. */
14129 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014130 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 else
14132 {
14133# ifdef FEAT_MBYTE
14134 vimconv_T conv;
14135 char_u *enc;
14136
14137 conv.vc_type = CONV_NONE;
14138 enc = enc_locale();
14139 convert_setup(&conv, p_enc, enc);
14140 if (conv.vc_type != CONV_NONE)
14141 p = string_convert(&conv, p, NULL);
14142# endif
14143 if (p != NULL)
14144 (void)strftime((char *)result_buf, sizeof(result_buf),
14145 (char *)p, curtime);
14146 else
14147 result_buf[0] = NUL;
14148
14149# ifdef FEAT_MBYTE
14150 if (conv.vc_type != CONV_NONE)
14151 vim_free(p);
14152 convert_setup(&conv, enc, p_enc);
14153 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 else
14156# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158
14159# ifdef FEAT_MBYTE
14160 /* Release conversion descriptors */
14161 convert_setup(&conv, NULL, NULL);
14162 vim_free(enc);
14163# endif
14164 }
14165}
14166#endif
14167
14168/*
14169 * "stridx()" function
14170 */
14171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014172f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 typval_T *argvars;
14174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175{
14176 char_u buf[NUMBUFLEN];
14177 char_u *needle;
14178 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014179 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014181 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 needle = get_tv_string_chk(&argvars[1]);
14184 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 if (needle == NULL || haystack == NULL)
14187 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 if (argvars[2].v_type != VAR_UNKNOWN)
14190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 int error = FALSE;
14192
14193 start_idx = get_tv_number_chk(&argvars[2], &error);
14194 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014195 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014196 if (start_idx >= 0)
14197 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014198 }
14199
14200 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14201 if (pos != NULL)
14202 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203}
14204
14205/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014206 * "string()" function
14207 */
14208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014209f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014210 typval_T *argvars;
14211 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014212{
14213 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014214 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014217 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014218 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220}
14221
14222/*
14223 * "strlen()" function
14224 */
14225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 typval_T *argvars;
14228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230 rettv->vval.v_number = (varnumber_T)(STRLEN(
14231 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232}
14233
14234/*
14235 * "strpart()" function
14236 */
14237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 typval_T *argvars;
14240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241{
14242 char_u *p;
14243 int n;
14244 int len;
14245 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014248 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 slen = (int)STRLEN(p);
14250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 n = get_tv_number_chk(&argvars[1], &error);
14252 if (error)
14253 len = 0;
14254 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 else
14257 len = slen - n; /* default len: all bytes that are available. */
14258
14259 /*
14260 * Only return the overlap between the specified part and the actual
14261 * string.
14262 */
14263 if (n < 0)
14264 {
14265 len += n;
14266 n = 0;
14267 }
14268 else if (n > slen)
14269 n = slen;
14270 if (len < 0)
14271 len = 0;
14272 else if (n + len > slen)
14273 len = slen - n;
14274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 rettv->v_type = VAR_STRING;
14276 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277}
14278
14279/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014280 * "strridx()" function
14281 */
14282 static void
14283f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014284 typval_T *argvars;
14285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014286{
14287 char_u buf[NUMBUFLEN];
14288 char_u *needle;
14289 char_u *haystack;
14290 char_u *rest;
14291 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014292 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 needle = get_tv_string_chk(&argvars[1]);
14295 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014296 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297
14298 rettv->vval.v_number = -1;
14299 if (needle == NULL || haystack == NULL)
14300 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014301 if (argvars[2].v_type != VAR_UNKNOWN)
14302 {
14303 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014305 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014307 }
14308 else
14309 end_idx = haystack_len;
14310
Bram Moolenaar0d660222005-01-07 21:51:51 +000014311 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014312 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014313 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014314 lastmatch = haystack + end_idx;
14315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014317 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014318 for (rest = haystack; *rest != '\0'; ++rest)
14319 {
14320 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014321 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014322 break;
14323 lastmatch = rest;
14324 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014325 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014326
14327 if (lastmatch == NULL)
14328 rettv->vval.v_number = -1;
14329 else
14330 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14331}
14332
14333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 * "strtrans()" function
14335 */
14336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014338 typval_T *argvars;
14339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 rettv->v_type = VAR_STRING;
14342 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343}
14344
14345/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346 * "submatch()" function
14347 */
14348 static void
14349f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014350 typval_T *argvars;
14351 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014352{
14353 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 rettv->vval.v_string =
14355 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356}
14357
14358/*
14359 * "substitute()" function
14360 */
14361 static void
14362f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014363 typval_T *argvars;
14364 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014365{
14366 char_u patbuf[NUMBUFLEN];
14367 char_u subbuf[NUMBUFLEN];
14368 char_u flagsbuf[NUMBUFLEN];
14369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 char_u *str = get_tv_string_chk(&argvars[0]);
14371 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14372 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14373 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14374
Bram Moolenaar0d660222005-01-07 21:51:51 +000014375 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14377 rettv->vval.v_string = NULL;
14378 else
14379 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014380}
14381
14382/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014383 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 */
14385/*ARGSUSED*/
14386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 typval_T *argvars;
14389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390{
14391 int id = 0;
14392#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014393 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 long col;
14395 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014396 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014398 lnum = get_tv_lnum(argvars); /* -1 on type error */
14399 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14400 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014402 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014403 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014404 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405#endif
14406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408}
14409
14410/*
14411 * "synIDattr(id, what [, mode])" function
14412 */
14413/*ARGSUSED*/
14414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 typval_T *argvars;
14417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
14419 char_u *p = NULL;
14420#ifdef FEAT_SYN_HL
14421 int id;
14422 char_u *what;
14423 char_u *mode;
14424 char_u modebuf[NUMBUFLEN];
14425 int modec;
14426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427 id = get_tv_number(&argvars[0]);
14428 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 modec = TOLOWER_ASC(mode[0]);
14433 if (modec != 't' && modec != 'c'
14434#ifdef FEAT_GUI
14435 && modec != 'g'
14436#endif
14437 )
14438 modec = 0; /* replace invalid with current */
14439 }
14440 else
14441 {
14442#ifdef FEAT_GUI
14443 if (gui.in_use)
14444 modec = 'g';
14445 else
14446#endif
14447 if (t_colors > 1)
14448 modec = 'c';
14449 else
14450 modec = 't';
14451 }
14452
14453
14454 switch (TOLOWER_ASC(what[0]))
14455 {
14456 case 'b':
14457 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14458 p = highlight_color(id, what, modec);
14459 else /* bold */
14460 p = highlight_has_attr(id, HL_BOLD, modec);
14461 break;
14462
14463 case 'f': /* fg[#] */
14464 p = highlight_color(id, what, modec);
14465 break;
14466
14467 case 'i':
14468 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14469 p = highlight_has_attr(id, HL_INVERSE, modec);
14470 else /* italic */
14471 p = highlight_has_attr(id, HL_ITALIC, modec);
14472 break;
14473
14474 case 'n': /* name */
14475 p = get_highlight_name(NULL, id - 1);
14476 break;
14477
14478 case 'r': /* reverse */
14479 p = highlight_has_attr(id, HL_INVERSE, modec);
14480 break;
14481
14482 case 's': /* standout */
14483 p = highlight_has_attr(id, HL_STANDOUT, modec);
14484 break;
14485
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014486 case 'u':
14487 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14488 /* underline */
14489 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14490 else
14491 /* undercurl */
14492 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 break;
14494 }
14495
14496 if (p != NULL)
14497 p = vim_strsave(p);
14498#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 rettv->v_type = VAR_STRING;
14500 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501}
14502
14503/*
14504 * "synIDtrans(id)" function
14505 */
14506/*ARGSUSED*/
14507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014509 typval_T *argvars;
14510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511{
14512 int id;
14513
14514#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516
14517 if (id > 0)
14518 id = syn_get_final_id(id);
14519 else
14520#endif
14521 id = 0;
14522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524}
14525
14526/*
14527 * "system()" function
14528 */
14529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014531 typval_T *argvars;
14532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014534 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014536 char_u *infile = NULL;
14537 char_u buf[NUMBUFLEN];
14538 int err = FALSE;
14539 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014542 {
14543 /*
14544 * Write the string to a temp file, to be used for input of the shell
14545 * command.
14546 */
14547 if ((infile = vim_tempname('i')) == NULL)
14548 {
14549 EMSG(_(e_notmp));
14550 return;
14551 }
14552
14553 fd = mch_fopen((char *)infile, WRITEBIN);
14554 if (fd == NULL)
14555 {
14556 EMSG2(_(e_notopen), infile);
14557 goto done;
14558 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 p = get_tv_string_buf_chk(&argvars[1], buf);
14560 if (p == NULL)
14561 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014562 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14563 err = TRUE;
14564 if (fclose(fd) != 0)
14565 err = TRUE;
14566 if (err)
14567 {
14568 EMSG(_("E677: Error writing temp file"));
14569 goto done;
14570 }
14571 }
14572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014573 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014574
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575#ifdef USE_CR
14576 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014577 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 {
14579 char_u *s;
14580
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014581 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 {
14583 if (*s == CAR)
14584 *s = NL;
14585 }
14586 }
14587#else
14588# ifdef USE_CRNL
14589 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014590 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 {
14592 char_u *s, *d;
14593
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014594 d = res;
14595 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 {
14597 if (s[0] == CAR && s[1] == NL)
14598 ++s;
14599 *d++ = *s;
14600 }
14601 *d = NUL;
14602 }
14603# endif
14604#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014605
14606done:
14607 if (infile != NULL)
14608 {
14609 mch_remove(infile);
14610 vim_free(infile);
14611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612 rettv->v_type = VAR_STRING;
14613 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614}
14615
14616/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014617 * "tagfiles()" function
14618 */
14619/*ARGSUSED*/
14620 static void
14621f_tagfiles(argvars, rettv)
14622 typval_T *argvars;
14623 typval_T *rettv;
14624{
14625 char_u fname[MAXPATHL + 1];
14626 list_T *l;
14627
14628 l = list_alloc();
14629 if (l == NULL)
14630 {
14631 rettv->vval.v_number = 0;
14632 return;
14633 }
14634 rettv->vval.v_list = l;
14635 rettv->v_type = VAR_LIST;
14636 ++l->lv_refcount;
14637
14638 get_tagfname(TRUE, NULL);
14639 for (;;)
14640 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014641 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014642 break;
14643}
14644
14645/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014646 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014647 */
14648 static void
14649f_taglist(argvars, rettv)
14650 typval_T *argvars;
14651 typval_T *rettv;
14652{
14653 char_u *tag_pattern;
14654 list_T *l;
14655
14656 tag_pattern = get_tv_string(&argvars[0]);
14657
14658 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014659 if (*tag_pattern == NUL)
14660 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014661
14662 l = list_alloc();
14663 if (l != NULL)
14664 {
14665 if (get_tags(l, tag_pattern) != FAIL)
14666 {
14667 rettv->vval.v_list = l;
14668 rettv->v_type = VAR_LIST;
14669 ++l->lv_refcount;
14670 }
14671 else
14672 list_free(l);
14673 }
14674}
14675
14676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 * "tempname()" function
14678 */
14679/*ARGSUSED*/
14680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014682 typval_T *argvars;
14683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684{
14685 static int x = 'A';
14686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->v_type = VAR_STRING;
14688 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689
14690 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14691 * names. Skip 'I' and 'O', they are used for shell redirection. */
14692 do
14693 {
14694 if (x == 'Z')
14695 x = '0';
14696 else if (x == '9')
14697 x = 'A';
14698 else
14699 {
14700#ifdef EBCDIC
14701 if (x == 'I')
14702 x = 'J';
14703 else if (x == 'R')
14704 x = 'S';
14705 else
14706#endif
14707 ++x;
14708 }
14709 } while (x == 'I' || x == 'O');
14710}
14711
14712/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014713 * "test(list)" function: Just checking the walls...
14714 */
14715/*ARGSUSED*/
14716 static void
14717f_test(argvars, rettv)
14718 typval_T *argvars;
14719 typval_T *rettv;
14720{
14721 /* Used for unit testing. Change the code below to your liking. */
14722#if 0
14723 listitem_T *li;
14724 list_T *l;
14725 char_u *bad, *good;
14726
14727 if (argvars[0].v_type != VAR_LIST)
14728 return;
14729 l = argvars[0].vval.v_list;
14730 if (l == NULL)
14731 return;
14732 li = l->lv_first;
14733 if (li == NULL)
14734 return;
14735 bad = get_tv_string(&li->li_tv);
14736 li = li->li_next;
14737 if (li == NULL)
14738 return;
14739 good = get_tv_string(&li->li_tv);
14740 rettv->vval.v_number = test_edit_score(bad, good);
14741#endif
14742}
14743
14744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 * "tolower(string)" function
14746 */
14747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014749 typval_T *argvars;
14750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751{
14752 char_u *p;
14753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 p = vim_strsave(get_tv_string(&argvars[0]));
14755 rettv->v_type = VAR_STRING;
14756 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
14758 if (p != NULL)
14759 while (*p != NUL)
14760 {
14761#ifdef FEAT_MBYTE
14762 int l;
14763
14764 if (enc_utf8)
14765 {
14766 int c, lc;
14767
14768 c = utf_ptr2char(p);
14769 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014770 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 /* TODO: reallocate string when byte count changes. */
14772 if (utf_char2len(lc) == l)
14773 utf_char2bytes(lc, p);
14774 p += l;
14775 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014776 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 p += l; /* skip multi-byte character */
14778 else
14779#endif
14780 {
14781 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14782 ++p;
14783 }
14784 }
14785}
14786
14787/*
14788 * "toupper(string)" function
14789 */
14790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014792 typval_T *argvars;
14793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797}
14798
14799/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014800 * "tr(string, fromstr, tostr)" function
14801 */
14802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014803f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014804 typval_T *argvars;
14805 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014806{
14807 char_u *instr;
14808 char_u *fromstr;
14809 char_u *tostr;
14810 char_u *p;
14811#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014812 int inlen;
14813 int fromlen;
14814 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014815 int idx;
14816 char_u *cpstr;
14817 int cplen;
14818 int first = TRUE;
14819#endif
14820 char_u buf[NUMBUFLEN];
14821 char_u buf2[NUMBUFLEN];
14822 garray_T ga;
14823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14826 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014827
14828 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 rettv->v_type = VAR_STRING;
14830 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 if (fromstr == NULL || tostr == NULL)
14832 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014833 ga_init2(&ga, (int)sizeof(char), 80);
14834
14835#ifdef FEAT_MBYTE
14836 if (!has_mbyte)
14837#endif
14838 /* not multi-byte: fromstr and tostr must be the same length */
14839 if (STRLEN(fromstr) != STRLEN(tostr))
14840 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014841#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014842error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014843#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014844 EMSG2(_(e_invarg2), fromstr);
14845 ga_clear(&ga);
14846 return;
14847 }
14848
14849 /* fromstr and tostr have to contain the same number of chars */
14850 while (*instr != NUL)
14851 {
14852#ifdef FEAT_MBYTE
14853 if (has_mbyte)
14854 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014855 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014856 cpstr = instr;
14857 cplen = inlen;
14858 idx = 0;
14859 for (p = fromstr; *p != NUL; p += fromlen)
14860 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014861 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014862 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14863 {
14864 for (p = tostr; *p != NUL; p += tolen)
14865 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014866 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014867 if (idx-- == 0)
14868 {
14869 cplen = tolen;
14870 cpstr = p;
14871 break;
14872 }
14873 }
14874 if (*p == NUL) /* tostr is shorter than fromstr */
14875 goto error;
14876 break;
14877 }
14878 ++idx;
14879 }
14880
14881 if (first && cpstr == instr)
14882 {
14883 /* Check that fromstr and tostr have the same number of
14884 * (multi-byte) characters. Done only once when a character
14885 * of instr doesn't appear in fromstr. */
14886 first = FALSE;
14887 for (p = tostr; *p != NUL; p += tolen)
14888 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014889 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014890 --idx;
14891 }
14892 if (idx != 0)
14893 goto error;
14894 }
14895
14896 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014897 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014898 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014899
14900 instr += inlen;
14901 }
14902 else
14903#endif
14904 {
14905 /* When not using multi-byte chars we can do it faster. */
14906 p = vim_strchr(fromstr, *instr);
14907 if (p != NULL)
14908 ga_append(&ga, tostr[p - fromstr]);
14909 else
14910 ga_append(&ga, *instr);
14911 ++instr;
14912 }
14913 }
14914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014916}
14917
14918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 * "type(expr)" function
14920 */
14921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014923 typval_T *argvars;
14924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014926 int n;
14927
14928 switch (argvars[0].v_type)
14929 {
14930 case VAR_NUMBER: n = 0; break;
14931 case VAR_STRING: n = 1; break;
14932 case VAR_FUNC: n = 2; break;
14933 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014934 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014935 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14936 }
14937 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938}
14939
14940/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014941 * "values(dict)" function
14942 */
14943 static void
14944f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 typval_T *argvars;
14946 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014947{
14948 dict_list(argvars, rettv, 1);
14949}
14950
14951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952 * "virtcol(string)" function
14953 */
14954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014956 typval_T *argvars;
14957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958{
14959 colnr_T vcol = 0;
14960 pos_T *fp;
14961
14962 fp = var2fpos(&argvars[0], FALSE);
14963 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14964 {
14965 getvvcol(curwin, fp, NULL, NULL, &vcol);
14966 ++vcol;
14967 }
14968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014969 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970}
14971
14972/*
14973 * "visualmode()" function
14974 */
14975/*ARGSUSED*/
14976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014977f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014978 typval_T *argvars;
14979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980{
14981#ifdef FEAT_VISUAL
14982 char_u str[2];
14983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 str[0] = curbuf->b_visual_mode_eval;
14986 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988
14989 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014990 if ((argvars[0].v_type == VAR_NUMBER
14991 && argvars[0].vval.v_number != 0)
14992 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 curbuf->b_visual_mode_eval = NUL;
14995#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997#endif
14998}
14999
15000/*
15001 * "winbufnr(nr)" function
15002 */
15003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 typval_T *argvars;
15006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007{
15008 win_T *wp;
15009
15010 wp = find_win_by_nr(&argvars[0]);
15011 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015}
15016
15017/*
15018 * "wincol()" function
15019 */
15020/*ARGSUSED*/
15021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015022f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 typval_T *argvars;
15024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025{
15026 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028}
15029
15030/*
15031 * "winheight(nr)" function
15032 */
15033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015035 typval_T *argvars;
15036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037{
15038 win_T *wp;
15039
15040 wp = find_win_by_nr(&argvars[0]);
15041 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015044 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045}
15046
15047/*
15048 * "winline()" function
15049 */
15050/*ARGSUSED*/
15051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015053 typval_T *argvars;
15054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055{
15056 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058}
15059
15060/*
15061 * "winnr()" function
15062 */
15063/* ARGSUSED */
15064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015066 typval_T *argvars;
15067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068{
15069 int nr = 1;
15070#ifdef FEAT_WINDOWS
15071 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015072 win_T *twin = curwin;
15073 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015075 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015077 arg = get_tv_string_chk(&argvars[0]);
15078 if (arg == NULL)
15079 nr = 0; /* type error; errmsg already given */
15080 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015081 twin = lastwin;
15082 else if (STRCMP(arg, "#") == 0)
15083 {
15084 twin = prevwin;
15085 if (prevwin == NULL)
15086 nr = 0;
15087 }
15088 else
15089 {
15090 EMSG2(_(e_invexpr2), arg);
15091 nr = 0;
15092 }
15093 }
15094
15095 if (nr > 0)
15096 for (wp = firstwin; wp != twin; wp = wp->w_next)
15097 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015099 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100}
15101
15102/*
15103 * "winrestcmd()" function
15104 */
15105/* ARGSUSED */
15106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015107f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 typval_T *argvars;
15109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110{
15111#ifdef FEAT_WINDOWS
15112 win_T *wp;
15113 int winnr = 1;
15114 garray_T ga;
15115 char_u buf[50];
15116
15117 ga_init2(&ga, (int)sizeof(char), 70);
15118 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15119 {
15120 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15121 ga_concat(&ga, buf);
15122# ifdef FEAT_VERTSPLIT
15123 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15124 ga_concat(&ga, buf);
15125# endif
15126 ++winnr;
15127 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015128 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015134 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135}
15136
15137/*
15138 * "winwidth(nr)" function
15139 */
15140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 typval_T *argvars;
15143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144{
15145 win_T *wp;
15146
15147 wp = find_win_by_nr(&argvars[0]);
15148 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 else
15151#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015154 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155#endif
15156}
15157
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015159 * "writefile()" function
15160 */
15161 static void
15162f_writefile(argvars, rettv)
15163 typval_T *argvars;
15164 typval_T *rettv;
15165{
15166 int binary = FALSE;
15167 char_u *fname;
15168 FILE *fd;
15169 listitem_T *li;
15170 char_u *s;
15171 int ret = 0;
15172 int c;
15173
15174 if (argvars[0].v_type != VAR_LIST)
15175 {
15176 EMSG2(_(e_listarg), "writefile()");
15177 return;
15178 }
15179 if (argvars[0].vval.v_list == NULL)
15180 return;
15181
15182 if (argvars[2].v_type != VAR_UNKNOWN
15183 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15184 binary = TRUE;
15185
15186 /* Always open the file in binary mode, library functions have a mind of
15187 * their own about CR-LF conversion. */
15188 fname = get_tv_string(&argvars[1]);
15189 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15190 {
15191 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15192 ret = -1;
15193 }
15194 else
15195 {
15196 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15197 li = li->li_next)
15198 {
15199 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15200 {
15201 if (*s == '\n')
15202 c = putc(NUL, fd);
15203 else
15204 c = putc(*s, fd);
15205 if (c == EOF)
15206 {
15207 ret = -1;
15208 break;
15209 }
15210 }
15211 if (!binary || li->li_next != NULL)
15212 if (putc('\n', fd) == EOF)
15213 {
15214 ret = -1;
15215 break;
15216 }
15217 if (ret < 0)
15218 {
15219 EMSG(_(e_write));
15220 break;
15221 }
15222 }
15223 fclose(fd);
15224 }
15225
15226 rettv->vval.v_number = ret;
15227}
15228
15229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 * Translate a String variable into a position.
15231 */
15232 static pos_T *
15233var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 int lnum; /* TRUE when $ is last line */
15236{
15237 char_u *name;
15238 static pos_T pos;
15239 pos_T *pp;
15240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015241 name = get_tv_string_chk(varp);
15242 if (name == NULL)
15243 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 if (name[0] == '.') /* cursor */
15245 return &curwin->w_cursor;
15246 if (name[0] == '\'') /* mark */
15247 {
15248 pp = getmark(name[1], FALSE);
15249 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15250 return NULL;
15251 return pp;
15252 }
15253 if (name[0] == '$') /* last column or line */
15254 {
15255 if (lnum)
15256 {
15257 pos.lnum = curbuf->b_ml.ml_line_count;
15258 pos.col = 0;
15259 }
15260 else
15261 {
15262 pos.lnum = curwin->w_cursor.lnum;
15263 pos.col = (colnr_T)STRLEN(ml_get_curline());
15264 }
15265 return &pos;
15266 }
15267 return NULL;
15268}
15269
15270/*
15271 * Get the length of an environment variable name.
15272 * Advance "arg" to the first character after the name.
15273 * Return 0 for error.
15274 */
15275 static int
15276get_env_len(arg)
15277 char_u **arg;
15278{
15279 char_u *p;
15280 int len;
15281
15282 for (p = *arg; vim_isIDc(*p); ++p)
15283 ;
15284 if (p == *arg) /* no name found */
15285 return 0;
15286
15287 len = (int)(p - *arg);
15288 *arg = p;
15289 return len;
15290}
15291
15292/*
15293 * Get the length of the name of a function or internal variable.
15294 * "arg" is advanced to the first non-white character after the name.
15295 * Return 0 if something is wrong.
15296 */
15297 static int
15298get_id_len(arg)
15299 char_u **arg;
15300{
15301 char_u *p;
15302 int len;
15303
15304 /* Find the end of the name. */
15305 for (p = *arg; eval_isnamec(*p); ++p)
15306 ;
15307 if (p == *arg) /* no name found */
15308 return 0;
15309
15310 len = (int)(p - *arg);
15311 *arg = skipwhite(p);
15312
15313 return len;
15314}
15315
15316/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015317 * Get the length of the name of a variable or function.
15318 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015320 * Return -1 if curly braces expansion failed.
15321 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 * If the name contains 'magic' {}'s, expand them and return the
15323 * expanded name in an allocated string via 'alias' - caller must free.
15324 */
15325 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015326get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 char_u **arg;
15328 char_u **alias;
15329 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015330 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331{
15332 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 char_u *p;
15334 char_u *expr_start;
15335 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336
15337 *alias = NULL; /* default to no alias */
15338
15339 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15340 && (*arg)[2] == (int)KE_SNR)
15341 {
15342 /* hard coded <SNR>, already translated */
15343 *arg += 3;
15344 return get_id_len(arg) + 3;
15345 }
15346 len = eval_fname_script(*arg);
15347 if (len > 0)
15348 {
15349 /* literal "<SID>", "s:" or "<SNR>" */
15350 *arg += len;
15351 }
15352
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015356 p = find_name_end(*arg, &expr_start, &expr_end,
15357 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 if (expr_start != NULL)
15359 {
15360 char_u *temp_string;
15361
15362 if (!evaluate)
15363 {
15364 len += (int)(p - *arg);
15365 *arg = skipwhite(p);
15366 return len;
15367 }
15368
15369 /*
15370 * Include any <SID> etc in the expanded string:
15371 * Thus the -len here.
15372 */
15373 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15374 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015375 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 *alias = temp_string;
15377 *arg = skipwhite(p);
15378 return (int)STRLEN(temp_string);
15379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380
15381 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015382 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 EMSG2(_(e_invexpr2), *arg);
15384
15385 return len;
15386}
15387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388/*
15389 * Find the end of a variable or function name, taking care of magic braces.
15390 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15391 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015392 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 * Return a pointer to just after the name. Equal to "arg" if there is no
15394 * valid name.
15395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015397find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 char_u *arg;
15399 char_u **expr_start;
15400 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015401 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 int mb_nest = 0;
15404 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 char_u *p;
15406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 *expr_start = NULL;
15410 *expr_end = NULL;
15411 }
15412
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015413 /* Quick check for valid starting character. */
15414 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15415 return arg;
15416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 for (p = arg; *p != NUL
15418 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015419 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015420 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015421 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015422 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015424 if (*p == '\'')
15425 {
15426 /* skip over 'string' to avoid counting [ and ] inside it. */
15427 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15428 ;
15429 if (*p == NUL)
15430 break;
15431 }
15432 else if (*p == '"')
15433 {
15434 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15435 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15436 if (*p == '\\' && p[1] != NUL)
15437 ++p;
15438 if (*p == NUL)
15439 break;
15440 }
15441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015442 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 if (*p == '[')
15445 ++br_nest;
15446 else if (*p == ']')
15447 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452 if (*p == '{')
15453 {
15454 mb_nest++;
15455 if (expr_start != NULL && *expr_start == NULL)
15456 *expr_start = p;
15457 }
15458 else if (*p == '}')
15459 {
15460 mb_nest--;
15461 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15462 *expr_end = p;
15463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 }
15466
15467 return p;
15468}
15469
15470/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015471 * Expands out the 'magic' {}'s in a variable/function name.
15472 * Note that this can call itself recursively, to deal with
15473 * constructs like foo{bar}{baz}{bam}
15474 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15475 * "in_start" ^
15476 * "expr_start" ^
15477 * "expr_end" ^
15478 * "in_end" ^
15479 *
15480 * Returns a new allocated string, which the caller must free.
15481 * Returns NULL for failure.
15482 */
15483 static char_u *
15484make_expanded_name(in_start, expr_start, expr_end, in_end)
15485 char_u *in_start;
15486 char_u *expr_start;
15487 char_u *expr_end;
15488 char_u *in_end;
15489{
15490 char_u c1;
15491 char_u *retval = NULL;
15492 char_u *temp_result;
15493 char_u *nextcmd = NULL;
15494
15495 if (expr_end == NULL || in_end == NULL)
15496 return NULL;
15497 *expr_start = NUL;
15498 *expr_end = NUL;
15499 c1 = *in_end;
15500 *in_end = NUL;
15501
15502 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15503 if (temp_result != NULL && nextcmd == NULL)
15504 {
15505 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15506 + (in_end - expr_end) + 1));
15507 if (retval != NULL)
15508 {
15509 STRCPY(retval, in_start);
15510 STRCAT(retval, temp_result);
15511 STRCAT(retval, expr_end + 1);
15512 }
15513 }
15514 vim_free(temp_result);
15515
15516 *in_end = c1; /* put char back for error messages */
15517 *expr_start = '{';
15518 *expr_end = '}';
15519
15520 if (retval != NULL)
15521 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015522 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015523 if (expr_start != NULL)
15524 {
15525 /* Further expansion! */
15526 temp_result = make_expanded_name(retval, expr_start,
15527 expr_end, temp_result);
15528 vim_free(retval);
15529 retval = temp_result;
15530 }
15531 }
15532
15533 return retval;
15534}
15535
15536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015538 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 */
15540 static int
15541eval_isnamec(c)
15542 int c;
15543{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015544 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15545}
15546
15547/*
15548 * Return TRUE if character "c" can be used as the first character in a
15549 * variable or function name (excluding '{' and '}').
15550 */
15551 static int
15552eval_isnamec1(c)
15553 int c;
15554{
15555 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556}
15557
15558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 * Set number v: variable to "val".
15560 */
15561 void
15562set_vim_var_nr(idx, val)
15563 int idx;
15564 long val;
15565{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015566 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567}
15568
15569/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015570 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 */
15572 long
15573get_vim_var_nr(idx)
15574 int idx;
15575{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015576 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577}
15578
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015579#if defined(FEAT_AUTOCMD) || defined(PROTO)
15580/*
15581 * Get string v: variable value. Uses a static buffer, can only be used once.
15582 */
15583 char_u *
15584get_vim_var_str(idx)
15585 int idx;
15586{
15587 return get_tv_string(&vimvars[idx].vv_tv);
15588}
15589#endif
15590
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591/*
15592 * Set v:count, v:count1 and v:prevcount.
15593 */
15594 void
15595set_vcount(count, count1)
15596 long count;
15597 long count1;
15598{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015599 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15600 vimvars[VV_COUNT].vv_nr = count;
15601 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602}
15603
15604/*
15605 * Set string v: variable to a copy of "val".
15606 */
15607 void
15608set_vim_var_string(idx, val, len)
15609 int idx;
15610 char_u *val;
15611 int len; /* length of "val" to use or -1 (whole string) */
15612{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015613 /* Need to do this (at least) once, since we can't initialize a union.
15614 * Will always be invoked when "v:progname" is set. */
15615 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15616
Bram Moolenaare9a41262005-01-15 22:18:47 +000015617 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015619 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015621 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015623 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624}
15625
15626/*
15627 * Set v:register if needed.
15628 */
15629 void
15630set_reg_var(c)
15631 int c;
15632{
15633 char_u regname;
15634
15635 if (c == 0 || c == ' ')
15636 regname = '"';
15637 else
15638 regname = c;
15639 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015640 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 set_vim_var_string(VV_REG, &regname, 1);
15642}
15643
15644/*
15645 * Get or set v:exception. If "oldval" == NULL, return the current value.
15646 * Otherwise, restore the value to "oldval" and return NULL.
15647 * Must always be called in pairs to save and restore v:exception! Does not
15648 * take care of memory allocations.
15649 */
15650 char_u *
15651v_exception(oldval)
15652 char_u *oldval;
15653{
15654 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015655 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656
Bram Moolenaare9a41262005-01-15 22:18:47 +000015657 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 return NULL;
15659}
15660
15661/*
15662 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15663 * Otherwise, restore the value to "oldval" and return NULL.
15664 * Must always be called in pairs to save and restore v:throwpoint! Does not
15665 * take care of memory allocations.
15666 */
15667 char_u *
15668v_throwpoint(oldval)
15669 char_u *oldval;
15670{
15671 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015672 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673
Bram Moolenaare9a41262005-01-15 22:18:47 +000015674 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 return NULL;
15676}
15677
15678#if defined(FEAT_AUTOCMD) || defined(PROTO)
15679/*
15680 * Set v:cmdarg.
15681 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15682 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15683 * Must always be called in pairs!
15684 */
15685 char_u *
15686set_cmdarg(eap, oldarg)
15687 exarg_T *eap;
15688 char_u *oldarg;
15689{
15690 char_u *oldval;
15691 char_u *newval;
15692 unsigned len;
15693
Bram Moolenaare9a41262005-01-15 22:18:47 +000015694 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015695 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015697 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015698 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015699 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 }
15701
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015702 if (eap->force_bin == FORCE_BIN)
15703 len = 6;
15704 else if (eap->force_bin == FORCE_NOBIN)
15705 len = 8;
15706 else
15707 len = 0;
15708 if (eap->force_ff != 0)
15709 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15710# ifdef FEAT_MBYTE
15711 if (eap->force_enc != 0)
15712 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015713 if (eap->bad_char != 0)
15714 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015715# endif
15716
15717 newval = alloc(len + 1);
15718 if (newval == NULL)
15719 return NULL;
15720
15721 if (eap->force_bin == FORCE_BIN)
15722 sprintf((char *)newval, " ++bin");
15723 else if (eap->force_bin == FORCE_NOBIN)
15724 sprintf((char *)newval, " ++nobin");
15725 else
15726 *newval = NUL;
15727 if (eap->force_ff != 0)
15728 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15729 eap->cmd + eap->force_ff);
15730# ifdef FEAT_MBYTE
15731 if (eap->force_enc != 0)
15732 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15733 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015734 if (eap->bad_char != 0)
15735 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
15736 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015737# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015738 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015739 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740}
15741#endif
15742
15743/*
15744 * Get the value of internal variable "name".
15745 * Return OK or FAIL.
15746 */
15747 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015748get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 char_u *name;
15750 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015751 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015752 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753{
15754 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015755 typval_T *tv = NULL;
15756 typval_T atv;
15757 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759
15760 /* truncate the name, so that we can use strcmp() */
15761 cc = name[len];
15762 name[len] = NUL;
15763
15764 /*
15765 * Check for "b:changedtick".
15766 */
15767 if (STRCMP(name, "b:changedtick") == 0)
15768 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015769 atv.v_type = VAR_NUMBER;
15770 atv.vval.v_number = curbuf->b_changedtick;
15771 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 }
15773
15774 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 * Check for user-defined variables.
15776 */
15777 else
15778 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015779 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 }
15783
Bram Moolenaare9a41262005-01-15 22:18:47 +000015784 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015786 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 ret = FAIL;
15789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015790 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015791 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792
15793 name[len] = cc;
15794
15795 return ret;
15796}
15797
15798/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015799 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15800 * Also handle function call with Funcref variable: func(expr)
15801 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15802 */
15803 static int
15804handle_subscript(arg, rettv, evaluate, verbose)
15805 char_u **arg;
15806 typval_T *rettv;
15807 int evaluate; /* do more than finding the end */
15808 int verbose; /* give error messages */
15809{
15810 int ret = OK;
15811 dict_T *selfdict = NULL;
15812 char_u *s;
15813 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015814 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015815
15816 while (ret == OK
15817 && (**arg == '['
15818 || (**arg == '.' && rettv->v_type == VAR_DICT)
15819 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15820 && !vim_iswhite(*(*arg - 1)))
15821 {
15822 if (**arg == '(')
15823 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015824 /* need to copy the funcref so that we can clear rettv */
15825 functv = *rettv;
15826 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015827
15828 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015829 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015830 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015831 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15832 &len, evaluate, selfdict);
15833
15834 /* Clear the funcref afterwards, so that deleting it while
15835 * evaluating the arguments is possible (see test55). */
15836 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015837
15838 /* Stop the expression evaluation when immediately aborting on
15839 * error, or when an interrupt occurred or an exception was thrown
15840 * but not caught. */
15841 if (aborting())
15842 {
15843 if (ret == OK)
15844 clear_tv(rettv);
15845 ret = FAIL;
15846 }
15847 dict_unref(selfdict);
15848 selfdict = NULL;
15849 }
15850 else /* **arg == '[' || **arg == '.' */
15851 {
15852 dict_unref(selfdict);
15853 if (rettv->v_type == VAR_DICT)
15854 {
15855 selfdict = rettv->vval.v_dict;
15856 if (selfdict != NULL)
15857 ++selfdict->dv_refcount;
15858 }
15859 else
15860 selfdict = NULL;
15861 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15862 {
15863 clear_tv(rettv);
15864 ret = FAIL;
15865 }
15866 }
15867 }
15868 dict_unref(selfdict);
15869 return ret;
15870}
15871
15872/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015873 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15874 * value).
15875 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015876 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015877alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015878{
Bram Moolenaar33570922005-01-25 22:26:29 +000015879 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015880}
15881
15882/*
15883 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 * The string "s" must have been allocated, it is consumed.
15885 * Return NULL for out of memory, the variable otherwise.
15886 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015887 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015888alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 char_u *s;
15890{
Bram Moolenaar33570922005-01-25 22:26:29 +000015891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015893 rettv = alloc_tv();
15894 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896 rettv->v_type = VAR_STRING;
15897 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898 }
15899 else
15900 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015901 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902}
15903
15904/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015905 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 */
15907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015908free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015909 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910{
15911 if (varp != NULL)
15912 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913 switch (varp->v_type)
15914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015915 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015916 func_unref(varp->vval.v_string);
15917 /*FALLTHROUGH*/
15918 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015919 vim_free(varp->vval.v_string);
15920 break;
15921 case VAR_LIST:
15922 list_unref(varp->vval.v_list);
15923 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015924 case VAR_DICT:
15925 dict_unref(varp->vval.v_dict);
15926 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015927 case VAR_NUMBER:
15928 case VAR_UNKNOWN:
15929 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015931 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015932 break;
15933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 vim_free(varp);
15935 }
15936}
15937
15938/*
15939 * Free the memory for a variable value and set the value to NULL or 0.
15940 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015941 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015942clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015943 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944{
15945 if (varp != NULL)
15946 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015947 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015949 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015950 func_unref(varp->vval.v_string);
15951 /*FALLTHROUGH*/
15952 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015953 vim_free(varp->vval.v_string);
15954 varp->vval.v_string = NULL;
15955 break;
15956 case VAR_LIST:
15957 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015958 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015959 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015960 case VAR_DICT:
15961 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015962 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015963 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015964 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015965 varp->vval.v_number = 0;
15966 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015967 case VAR_UNKNOWN:
15968 break;
15969 default:
15970 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015972 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973 }
15974}
15975
15976/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015977 * Set the value of a variable to NULL without freeing items.
15978 */
15979 static void
15980init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015981 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015982{
15983 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015984 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015985}
15986
15987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 * Get the number value of a variable.
15989 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015990 * For incompatible types, return 0.
15991 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15992 * caller of incompatible types: it sets *denote to TRUE if "denote"
15993 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 */
15995 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015996get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015999 int error = FALSE;
16000
16001 return get_tv_number_chk(varp, &error); /* return 0L on error */
16002}
16003
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016004 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016005get_tv_number_chk(varp, denote)
16006 typval_T *varp;
16007 int *denote;
16008{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016009 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016011 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016013 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016014 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016015 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016016 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016017 break;
16018 case VAR_STRING:
16019 if (varp->vval.v_string != NULL)
16020 vim_str2nr(varp->vval.v_string, NULL, NULL,
16021 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016022 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016023 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016024 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016025 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016026 case VAR_DICT:
16027 EMSG(_("E728: Using a Dictionary as a number"));
16028 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016029 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016030 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016033 if (denote == NULL) /* useful for values that must be unsigned */
16034 n = -1;
16035 else
16036 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016037 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038}
16039
16040/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016041 * Get the lnum from the first argument.
16042 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016043 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044 */
16045 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016046get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016047 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048{
Bram Moolenaar33570922005-01-25 22:26:29 +000016049 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 linenr_T lnum;
16051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016052 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 if (lnum == 0) /* no valid number, try using line() */
16054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016055 rettv.v_type = VAR_NUMBER;
16056 f_line(argvars, &rettv);
16057 lnum = rettv.vval.v_number;
16058 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 }
16060 return lnum;
16061}
16062
16063/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016064 * Get the lnum from the first argument.
16065 * Also accepts "$", then "buf" is used.
16066 * Returns 0 on error.
16067 */
16068 static linenr_T
16069get_tv_lnum_buf(argvars, buf)
16070 typval_T *argvars;
16071 buf_T *buf;
16072{
16073 if (argvars[0].v_type == VAR_STRING
16074 && argvars[0].vval.v_string != NULL
16075 && argvars[0].vval.v_string[0] == '$'
16076 && buf != NULL)
16077 return buf->b_ml.ml_line_count;
16078 return get_tv_number_chk(&argvars[0], NULL);
16079}
16080
16081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 * Get the string value of a variable.
16083 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016084 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16085 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 * If the String variable has never been set, return an empty string.
16087 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016088 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16089 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 */
16091 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016093 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094{
16095 static char_u mybuf[NUMBUFLEN];
16096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098}
16099
16100 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016101get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016102 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016103 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016105 char_u *res = get_tv_string_buf_chk(varp, buf);
16106
16107 return res != NULL ? res : (char_u *)"";
16108}
16109
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016110 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111get_tv_string_chk(varp)
16112 typval_T *varp;
16113{
16114 static char_u mybuf[NUMBUFLEN];
16115
16116 return get_tv_string_buf_chk(varp, mybuf);
16117}
16118
16119 static char_u *
16120get_tv_string_buf_chk(varp, buf)
16121 typval_T *varp;
16122 char_u *buf;
16123{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016126 case VAR_NUMBER:
16127 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16128 return buf;
16129 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016130 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016131 break;
16132 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016133 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016134 break;
16135 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016136 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016137 break;
16138 case VAR_STRING:
16139 if (varp->vval.v_string != NULL)
16140 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016141 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016142 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016143 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016144 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016146 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147}
16148
16149/*
16150 * Find variable "name" in the list of variables.
16151 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016152 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016153 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016154 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016156 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016157find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016162 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163
Bram Moolenaara7043832005-01-21 11:56:39 +000016164 ht = find_var_ht(name, &varname);
16165 if (htp != NULL)
16166 *htp = ht;
16167 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016169 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170}
16171
16172/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016174 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016177find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016179 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016180 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016181{
Bram Moolenaar33570922005-01-25 22:26:29 +000016182 hashitem_T *hi;
16183
16184 if (*varname == NUL)
16185 {
16186 /* Must be something like "s:", otherwise "ht" would be NULL. */
16187 switch (varname[-2])
16188 {
16189 case 's': return &SCRIPT_SV(current_SID).sv_var;
16190 case 'g': return &globvars_var;
16191 case 'v': return &vimvars_var;
16192 case 'b': return &curbuf->b_bufvar;
16193 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016194 case 'l': return current_funccal == NULL
16195 ? NULL : &current_funccal->l_vars_var;
16196 case 'a': return current_funccal == NULL
16197 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016198 }
16199 return NULL;
16200 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016201
16202 hi = hash_find(ht, varname);
16203 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016204 {
16205 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016206 * worked find the variable again. Don't auto-load a script if it was
16207 * loaded already, otherwise it would be loaded every time when
16208 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016209 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016210 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016211 hi = hash_find(ht, varname);
16212 if (HASHITEM_EMPTY(hi))
16213 return NULL;
16214 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016215 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016216}
16217
16218/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016219 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016220 * Set "varname" to the start of name without ':'.
16221 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016223find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 char_u *name;
16225 char_u **varname;
16226{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016227 hashitem_T *hi;
16228
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 if (name[1] != ':')
16230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016231 /* The name must not start with a colon or #. */
16232 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 return NULL;
16234 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016235
16236 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016237 hi = hash_find(&compat_hashtab, name);
16238 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016239 return &compat_hashtab;
16240
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 return &globvarht; /* global variable */
16243 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 }
16245 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016246 if (*name == 'g') /* global variable */
16247 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016248 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16249 */
16250 if (vim_strchr(name + 2, ':') != NULL
16251 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016252 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016256 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016257 if (*name == 'v') /* v: variable */
16258 return &vimvarht;
16259 if (*name == 'a' && current_funccal != NULL) /* function argument */
16260 return &current_funccal->l_avars.dv_hashtab;
16261 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16262 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 if (*name == 's' /* script variable */
16264 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16265 return &SCRIPT_VARS(current_SID);
16266 return NULL;
16267}
16268
16269/*
16270 * Get the string value of a (global/local) variable.
16271 * Returns NULL when it doesn't exist.
16272 */
16273 char_u *
16274get_var_value(name)
16275 char_u *name;
16276{
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278
Bram Moolenaara7043832005-01-21 11:56:39 +000016279 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280 if (v == NULL)
16281 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283}
16284
16285/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 * sourcing this script and when executing functions defined in the script.
16288 */
16289 void
16290new_script_vars(id)
16291 scid_T id;
16292{
Bram Moolenaara7043832005-01-21 11:56:39 +000016293 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 hashtab_T *ht;
16295 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016296
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16298 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016299 /* Re-allocating ga_data means that an ht_array pointing to
16300 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016302 for (i = 1; i <= ga_scripts.ga_len; ++i)
16303 {
16304 ht = &SCRIPT_VARS(i);
16305 if (ht->ht_mask == HT_INIT_SIZE - 1)
16306 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016307 sv = &SCRIPT_SV(i);
16308 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016309 }
16310
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 while (ga_scripts.ga_len < id)
16312 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016313 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16314 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 }
16317 }
16318}
16319
16320/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016321 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16322 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 */
16324 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016325init_var_dict(dict, dict_var)
16326 dict_T *dict;
16327 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328{
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 hash_init(&dict->dv_hashtab);
16330 dict->dv_refcount = 99999;
16331 dict_var->di_tv.vval.v_dict = dict;
16332 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016333 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016334 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16335 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336}
16337
16338/*
16339 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016340 * Frees all allocated variables and the value they contain.
16341 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 */
16343 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016344vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016345 hashtab_T *ht;
16346{
16347 vars_clear_ext(ht, TRUE);
16348}
16349
16350/*
16351 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16352 */
16353 static void
16354vars_clear_ext(ht, free_val)
16355 hashtab_T *ht;
16356 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357{
Bram Moolenaara7043832005-01-21 11:56:39 +000016358 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016359 hashitem_T *hi;
16360 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361
Bram Moolenaar33570922005-01-25 22:26:29 +000016362 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016363 todo = ht->ht_used;
16364 for (hi = ht->ht_array; todo > 0; ++hi)
16365 {
16366 if (!HASHITEM_EMPTY(hi))
16367 {
16368 --todo;
16369
Bram Moolenaar33570922005-01-25 22:26:29 +000016370 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016371 * ht_array might change then. hash_clear() takes care of it
16372 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016373 v = HI2DI(hi);
16374 if (free_val)
16375 clear_tv(&v->di_tv);
16376 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16377 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016378 }
16379 }
16380 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016381 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382}
16383
Bram Moolenaara7043832005-01-21 11:56:39 +000016384/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016385 * Delete a variable from hashtab "ht" at item "hi".
16386 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016389delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 hashtab_T *ht;
16391 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392{
Bram Moolenaar33570922005-01-25 22:26:29 +000016393 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016394
16395 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016396 clear_tv(&di->di_tv);
16397 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398}
16399
16400/*
16401 * List the value of one internal variable.
16402 */
16403 static void
16404list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016405 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 char_u *prefix;
16407{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016408 char_u *tofree;
16409 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016410 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016411
Bram Moolenaar33570922005-01-25 22:26:29 +000016412 s = echo_string(&v->di_tv, &tofree, numbuf);
16413 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016414 s == NULL ? (char_u *)"" : s);
16415 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416}
16417
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 static void
16419list_one_var_a(prefix, name, type, string)
16420 char_u *prefix;
16421 char_u *name;
16422 int type;
16423 char_u *string;
16424{
16425 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16426 if (name != NULL) /* "a:" vars don't have a name stored */
16427 msg_puts(name);
16428 msg_putchar(' ');
16429 msg_advance(22);
16430 if (type == VAR_NUMBER)
16431 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016432 else if (type == VAR_FUNC)
16433 msg_putchar('*');
16434 else if (type == VAR_LIST)
16435 {
16436 msg_putchar('[');
16437 if (*string == '[')
16438 ++string;
16439 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016440 else if (type == VAR_DICT)
16441 {
16442 msg_putchar('{');
16443 if (*string == '{')
16444 ++string;
16445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 else
16447 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016448
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016450
16451 if (type == VAR_FUNC)
16452 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453}
16454
16455/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016456 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 * If the variable already exists, the value is updated.
16458 * Otherwise the variable is created.
16459 */
16460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016461set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016463 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016464 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465{
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016469 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016471 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016472 {
16473 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16474 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16475 ? name[2] : name[0]))
16476 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016477 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016478 return;
16479 }
16480 if (function_exists(name))
16481 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016482 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016483 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016484 return;
16485 }
16486 }
16487
Bram Moolenaara7043832005-01-21 11:56:39 +000016488 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016490 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016491 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016492 return;
16493 }
16494
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016495 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016496 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016498 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016499 if (var_check_ro(v->di_flags, name)
16500 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016501 return;
16502 if (v->di_tv.v_type != tv->v_type
16503 && !((v->di_tv.v_type == VAR_STRING
16504 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016505 && (tv->v_type == VAR_STRING
16506 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016507 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016508 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016509 return;
16510 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016511
16512 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016513 * Handle setting internal v: variables separately: we don't change
16514 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 */
16516 if (ht == &vimvarht)
16517 {
16518 if (v->di_tv.v_type == VAR_STRING)
16519 {
16520 vim_free(v->di_tv.vval.v_string);
16521 if (copy || tv->v_type != VAR_STRING)
16522 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16523 else
16524 {
16525 /* Take over the string to avoid an extra alloc/free. */
16526 v->di_tv.vval.v_string = tv->vval.v_string;
16527 tv->vval.v_string = NULL;
16528 }
16529 }
16530 else if (v->di_tv.v_type != VAR_NUMBER)
16531 EMSG2(_(e_intern2), "set_var()");
16532 else
16533 v->di_tv.vval.v_number = get_tv_number(tv);
16534 return;
16535 }
16536
16537 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 }
16539 else /* add a new variable */
16540 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016541 /* Make sure the variable name is valid. */
16542 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016543 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16544 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016545 {
16546 EMSG2(_(e_illvar), varname);
16547 return;
16548 }
16549
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016550 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16551 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016552 if (v == NULL)
16553 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016554 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016555 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016557 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 return;
16559 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016560 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016563 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016564 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016565 else
16566 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016567 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016568 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016569 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571}
16572
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016573/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016574 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16575 * Also give an error message.
16576 */
16577 static int
16578var_check_ro(flags, name)
16579 int flags;
16580 char_u *name;
16581{
16582 if (flags & DI_FLAGS_RO)
16583 {
16584 EMSG2(_(e_readonlyvar), name);
16585 return TRUE;
16586 }
16587 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16588 {
16589 EMSG2(_(e_readonlysbx), name);
16590 return TRUE;
16591 }
16592 return FALSE;
16593}
16594
16595/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016596 * Return TRUE if typeval "tv" is set to be locked (immutable).
16597 * Also give an error message, using "name".
16598 */
16599 static int
16600tv_check_lock(lock, name)
16601 int lock;
16602 char_u *name;
16603{
16604 if (lock & VAR_LOCKED)
16605 {
16606 EMSG2(_("E741: Value is locked: %s"),
16607 name == NULL ? (char_u *)_("Unknown") : name);
16608 return TRUE;
16609 }
16610 if (lock & VAR_FIXED)
16611 {
16612 EMSG2(_("E742: Cannot change value of %s"),
16613 name == NULL ? (char_u *)_("Unknown") : name);
16614 return TRUE;
16615 }
16616 return FALSE;
16617}
16618
16619/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016620 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016621 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016622 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016625copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016626 typval_T *from;
16627 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016629 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016630 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016631 switch (from->v_type)
16632 {
16633 case VAR_NUMBER:
16634 to->vval.v_number = from->vval.v_number;
16635 break;
16636 case VAR_STRING:
16637 case VAR_FUNC:
16638 if (from->vval.v_string == NULL)
16639 to->vval.v_string = NULL;
16640 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016642 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016643 if (from->v_type == VAR_FUNC)
16644 func_ref(to->vval.v_string);
16645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016646 break;
16647 case VAR_LIST:
16648 if (from->vval.v_list == NULL)
16649 to->vval.v_list = NULL;
16650 else
16651 {
16652 to->vval.v_list = from->vval.v_list;
16653 ++to->vval.v_list->lv_refcount;
16654 }
16655 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016656 case VAR_DICT:
16657 if (from->vval.v_dict == NULL)
16658 to->vval.v_dict = NULL;
16659 else
16660 {
16661 to->vval.v_dict = from->vval.v_dict;
16662 ++to->vval.v_dict->dv_refcount;
16663 }
16664 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016665 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016666 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016667 break;
16668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669}
16670
16671/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016672 * Make a copy of an item.
16673 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016674 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16675 * reference to an already copied list/dict can be used.
16676 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016677 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016678 static int
16679item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016680 typval_T *from;
16681 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016682 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016683 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016684{
16685 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016686 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016687
Bram Moolenaar33570922005-01-25 22:26:29 +000016688 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016689 {
16690 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016691 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016692 }
16693 ++recurse;
16694
16695 switch (from->v_type)
16696 {
16697 case VAR_NUMBER:
16698 case VAR_STRING:
16699 case VAR_FUNC:
16700 copy_tv(from, to);
16701 break;
16702 case VAR_LIST:
16703 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016704 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016705 if (from->vval.v_list == NULL)
16706 to->vval.v_list = NULL;
16707 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16708 {
16709 /* use the copy made earlier */
16710 to->vval.v_list = from->vval.v_list->lv_copylist;
16711 ++to->vval.v_list->lv_refcount;
16712 }
16713 else
16714 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16715 if (to->vval.v_list == NULL)
16716 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016717 break;
16718 case VAR_DICT:
16719 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016720 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016721 if (from->vval.v_dict == NULL)
16722 to->vval.v_dict = NULL;
16723 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16724 {
16725 /* use the copy made earlier */
16726 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16727 ++to->vval.v_dict->dv_refcount;
16728 }
16729 else
16730 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16731 if (to->vval.v_dict == NULL)
16732 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016733 break;
16734 default:
16735 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016736 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016737 }
16738 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016739 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016740}
16741
16742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 * ":echo expr1 ..." print each argument separated with a space, add a
16744 * newline at the end.
16745 * ":echon expr1 ..." print each argument plain.
16746 */
16747 void
16748ex_echo(eap)
16749 exarg_T *eap;
16750{
16751 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016752 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016753 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 char_u *p;
16755 int needclr = TRUE;
16756 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016757 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758
16759 if (eap->skip)
16760 ++emsg_skip;
16761 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16762 {
16763 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016764 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765 {
16766 /*
16767 * Report the invalid expression unless the expression evaluation
16768 * has been cancelled due to an aborting error, an interrupt, or an
16769 * exception.
16770 */
16771 if (!aborting())
16772 EMSG2(_(e_invexpr2), p);
16773 break;
16774 }
16775 if (!eap->skip)
16776 {
16777 if (atstart)
16778 {
16779 atstart = FALSE;
16780 /* Call msg_start() after eval1(), evaluating the expression
16781 * may cause a message to appear. */
16782 if (eap->cmdidx == CMD_echo)
16783 msg_start();
16784 }
16785 else if (eap->cmdidx == CMD_echo)
16786 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016787 p = echo_string(&rettv, &tofree, numbuf);
16788 if (p != NULL)
16789 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016791 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016793 if (*p != TAB && needclr)
16794 {
16795 /* remove any text still there from the command */
16796 msg_clr_eos();
16797 needclr = FALSE;
16798 }
16799 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 }
16801 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016802 {
16803#ifdef FEAT_MBYTE
16804 if (has_mbyte)
16805 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016806 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016807
16808 (void)msg_outtrans_len_attr(p, i, echo_attr);
16809 p += i - 1;
16810 }
16811 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016813 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016816 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016818 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 arg = skipwhite(arg);
16820 }
16821 eap->nextcmd = check_nextcmd(arg);
16822
16823 if (eap->skip)
16824 --emsg_skip;
16825 else
16826 {
16827 /* remove text that may still be there from the command */
16828 if (needclr)
16829 msg_clr_eos();
16830 if (eap->cmdidx == CMD_echo)
16831 msg_end();
16832 }
16833}
16834
16835/*
16836 * ":echohl {name}".
16837 */
16838 void
16839ex_echohl(eap)
16840 exarg_T *eap;
16841{
16842 int id;
16843
16844 id = syn_name2id(eap->arg);
16845 if (id == 0)
16846 echo_attr = 0;
16847 else
16848 echo_attr = syn_id2attr(id);
16849}
16850
16851/*
16852 * ":execute expr1 ..." execute the result of an expression.
16853 * ":echomsg expr1 ..." Print a message
16854 * ":echoerr expr1 ..." Print an error
16855 * Each gets spaces around each argument and a newline at the end for
16856 * echo commands
16857 */
16858 void
16859ex_execute(eap)
16860 exarg_T *eap;
16861{
16862 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 int ret = OK;
16865 char_u *p;
16866 garray_T ga;
16867 int len;
16868 int save_did_emsg;
16869
16870 ga_init2(&ga, 1, 80);
16871
16872 if (eap->skip)
16873 ++emsg_skip;
16874 while (*arg != NUL && *arg != '|' && *arg != '\n')
16875 {
16876 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016877 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 {
16879 /*
16880 * Report the invalid expression unless the expression evaluation
16881 * has been cancelled due to an aborting error, an interrupt, or an
16882 * exception.
16883 */
16884 if (!aborting())
16885 EMSG2(_(e_invexpr2), p);
16886 ret = FAIL;
16887 break;
16888 }
16889
16890 if (!eap->skip)
16891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016892 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 len = (int)STRLEN(p);
16894 if (ga_grow(&ga, len + 2) == FAIL)
16895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897 ret = FAIL;
16898 break;
16899 }
16900 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 ga.ga_len += len;
16904 }
16905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 arg = skipwhite(arg);
16908 }
16909
16910 if (ret != FAIL && ga.ga_data != NULL)
16911 {
16912 if (eap->cmdidx == CMD_echomsg)
16913 MSG_ATTR(ga.ga_data, echo_attr);
16914 else if (eap->cmdidx == CMD_echoerr)
16915 {
16916 /* We don't want to abort following commands, restore did_emsg. */
16917 save_did_emsg = did_emsg;
16918 EMSG((char_u *)ga.ga_data);
16919 if (!force_abort)
16920 did_emsg = save_did_emsg;
16921 }
16922 else if (eap->cmdidx == CMD_execute)
16923 do_cmdline((char_u *)ga.ga_data,
16924 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16925 }
16926
16927 ga_clear(&ga);
16928
16929 if (eap->skip)
16930 --emsg_skip;
16931
16932 eap->nextcmd = check_nextcmd(arg);
16933}
16934
16935/*
16936 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16937 * "arg" points to the "&" or '+' when called, to "option" when returning.
16938 * Returns NULL when no option name found. Otherwise pointer to the char
16939 * after the option name.
16940 */
16941 static char_u *
16942find_option_end(arg, opt_flags)
16943 char_u **arg;
16944 int *opt_flags;
16945{
16946 char_u *p = *arg;
16947
16948 ++p;
16949 if (*p == 'g' && p[1] == ':')
16950 {
16951 *opt_flags = OPT_GLOBAL;
16952 p += 2;
16953 }
16954 else if (*p == 'l' && p[1] == ':')
16955 {
16956 *opt_flags = OPT_LOCAL;
16957 p += 2;
16958 }
16959 else
16960 *opt_flags = 0;
16961
16962 if (!ASCII_ISALPHA(*p))
16963 return NULL;
16964 *arg = p;
16965
16966 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16967 p += 4; /* termcap option */
16968 else
16969 while (ASCII_ISALPHA(*p))
16970 ++p;
16971 return p;
16972}
16973
16974/*
16975 * ":function"
16976 */
16977 void
16978ex_function(eap)
16979 exarg_T *eap;
16980{
16981 char_u *theline;
16982 int j;
16983 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 char_u *name = NULL;
16986 char_u *p;
16987 char_u *arg;
16988 garray_T newargs;
16989 garray_T newlines;
16990 int varargs = FALSE;
16991 int mustend = FALSE;
16992 int flags = 0;
16993 ufunc_T *fp;
16994 int indent;
16995 int nesting;
16996 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016997 dictitem_T *v;
16998 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016999 static int func_nr = 0; /* number for nameless function */
17000 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017001 hashtab_T *ht;
17002 int todo;
17003 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004
17005 /*
17006 * ":function" without argument: list functions.
17007 */
17008 if (ends_excmd(*eap->arg))
17009 {
17010 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017011 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017012 todo = func_hashtab.ht_used;
17013 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017014 {
17015 if (!HASHITEM_EMPTY(hi))
17016 {
17017 --todo;
17018 fp = HI2UF(hi);
17019 if (!isdigit(*fp->uf_name))
17020 list_func_head(fp, FALSE);
17021 }
17022 }
17023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 eap->nextcmd = check_nextcmd(eap->arg);
17025 return;
17026 }
17027
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017028 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017029 * ":function /pat": list functions matching pattern.
17030 */
17031 if (*eap->arg == '/')
17032 {
17033 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17034 if (!eap->skip)
17035 {
17036 regmatch_T regmatch;
17037
17038 c = *p;
17039 *p = NUL;
17040 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17041 *p = c;
17042 if (regmatch.regprog != NULL)
17043 {
17044 regmatch.rm_ic = p_ic;
17045
17046 todo = func_hashtab.ht_used;
17047 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17048 {
17049 if (!HASHITEM_EMPTY(hi))
17050 {
17051 --todo;
17052 fp = HI2UF(hi);
17053 if (!isdigit(*fp->uf_name)
17054 && vim_regexec(&regmatch, fp->uf_name, 0))
17055 list_func_head(fp, FALSE);
17056 }
17057 }
17058 }
17059 }
17060 if (*p == '/')
17061 ++p;
17062 eap->nextcmd = check_nextcmd(p);
17063 return;
17064 }
17065
17066 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017067 * Get the function name. There are these situations:
17068 * func normal function name
17069 * "name" == func, "fudi.fd_dict" == NULL
17070 * dict.func new dictionary entry
17071 * "name" == NULL, "fudi.fd_dict" set,
17072 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17073 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017074 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017075 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17076 * dict.func existing dict entry that's not a Funcref
17077 * "name" == NULL, "fudi.fd_dict" set,
17078 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017081 name = trans_function_name(&p, eap->skip, 0, &fudi);
17082 paren = (vim_strchr(p, '(') != NULL);
17083 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 {
17085 /*
17086 * Return on an invalid expression in braces, unless the expression
17087 * evaluation has been cancelled due to an aborting error, an
17088 * interrupt, or an exception.
17089 */
17090 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017092 if (!eap->skip && fudi.fd_newkey != NULL)
17093 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017094 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 else
17098 eap->skip = TRUE;
17099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 /* An error in a function call during evaluation of an expression in magic
17101 * braces should not cause the function not to be defined. */
17102 saved_did_emsg = did_emsg;
17103 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104
17105 /*
17106 * ":function func" with only function name: list function.
17107 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017108 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 {
17110 if (!ends_excmd(*skipwhite(p)))
17111 {
17112 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017113 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 }
17115 eap->nextcmd = check_nextcmd(p);
17116 if (eap->nextcmd != NULL)
17117 *p = NUL;
17118 if (!eap->skip && !got_int)
17119 {
17120 fp = find_func(name);
17121 if (fp != NULL)
17122 {
17123 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017124 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 {
17126 msg_putchar('\n');
17127 msg_outnum((long)(j + 1));
17128 if (j < 9)
17129 msg_putchar(' ');
17130 if (j < 99)
17131 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017132 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133 out_flush(); /* show a line at a time */
17134 ui_breakcheck();
17135 }
17136 if (!got_int)
17137 {
17138 msg_putchar('\n');
17139 msg_puts((char_u *)" endfunction");
17140 }
17141 }
17142 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017143 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017145 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 }
17147
17148 /*
17149 * ":function name(arg1, arg2)" Define function.
17150 */
17151 p = skipwhite(p);
17152 if (*p != '(')
17153 {
17154 if (!eap->skip)
17155 {
17156 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017157 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 }
17159 /* attempt to continue by skipping some text */
17160 if (vim_strchr(p, '(') != NULL)
17161 p = vim_strchr(p, '(');
17162 }
17163 p = skipwhite(p + 1);
17164
17165 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17166 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17167
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017168 if (!eap->skip)
17169 {
17170 /* Check the name of the function. */
17171 if (name != NULL)
17172 arg = name;
17173 else
17174 arg = fudi.fd_newkey;
17175 if (arg != NULL)
17176 {
17177 if (*arg == K_SPECIAL)
17178 j = 3;
17179 else
17180 j = 0;
17181 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17182 : eval_isnamec(arg[j])))
17183 ++j;
17184 if (arg[j] != NUL)
17185 emsg_funcname(_(e_invarg2), arg);
17186 }
17187 }
17188
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 /*
17190 * Isolate the arguments: "arg1, arg2, ...)"
17191 */
17192 while (*p != ')')
17193 {
17194 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17195 {
17196 varargs = TRUE;
17197 p += 3;
17198 mustend = TRUE;
17199 }
17200 else
17201 {
17202 arg = p;
17203 while (ASCII_ISALNUM(*p) || *p == '_')
17204 ++p;
17205 if (arg == p || isdigit(*arg)
17206 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17207 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17208 {
17209 if (!eap->skip)
17210 EMSG2(_("E125: Illegal argument: %s"), arg);
17211 break;
17212 }
17213 if (ga_grow(&newargs, 1) == FAIL)
17214 goto erret;
17215 c = *p;
17216 *p = NUL;
17217 arg = vim_strsave(arg);
17218 if (arg == NULL)
17219 goto erret;
17220 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17221 *p = c;
17222 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 if (*p == ',')
17224 ++p;
17225 else
17226 mustend = TRUE;
17227 }
17228 p = skipwhite(p);
17229 if (mustend && *p != ')')
17230 {
17231 if (!eap->skip)
17232 EMSG2(_(e_invarg2), eap->arg);
17233 break;
17234 }
17235 }
17236 ++p; /* skip the ')' */
17237
Bram Moolenaare9a41262005-01-15 22:18:47 +000017238 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 for (;;)
17240 {
17241 p = skipwhite(p);
17242 if (STRNCMP(p, "range", 5) == 0)
17243 {
17244 flags |= FC_RANGE;
17245 p += 5;
17246 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017247 else if (STRNCMP(p, "dict", 4) == 0)
17248 {
17249 flags |= FC_DICT;
17250 p += 4;
17251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 else if (STRNCMP(p, "abort", 5) == 0)
17253 {
17254 flags |= FC_ABORT;
17255 p += 5;
17256 }
17257 else
17258 break;
17259 }
17260
17261 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17262 EMSG(_(e_trailing));
17263
17264 /*
17265 * Read the body of the function, until ":endfunction" is found.
17266 */
17267 if (KeyTyped)
17268 {
17269 /* Check if the function already exists, don't let the user type the
17270 * whole function before telling him it doesn't work! For a script we
17271 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017272 if (!eap->skip && !eap->forceit)
17273 {
17274 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17275 EMSG(_(e_funcdict));
17276 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017277 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017280 if (!eap->skip && did_emsg)
17281 goto erret;
17282
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 msg_putchar('\n'); /* don't overwrite the function name */
17284 cmdline_row = msg_row;
17285 }
17286
17287 indent = 2;
17288 nesting = 0;
17289 for (;;)
17290 {
17291 msg_scroll = TRUE;
17292 need_wait_return = FALSE;
17293 if (eap->getline == NULL)
17294 theline = getcmdline(':', 0L, indent);
17295 else
17296 theline = eap->getline(':', eap->cookie, indent);
17297 if (KeyTyped)
17298 lines_left = Rows - 1;
17299 if (theline == NULL)
17300 {
17301 EMSG(_("E126: Missing :endfunction"));
17302 goto erret;
17303 }
17304
17305 if (skip_until != NULL)
17306 {
17307 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17308 * don't check for ":endfunc". */
17309 if (STRCMP(theline, skip_until) == 0)
17310 {
17311 vim_free(skip_until);
17312 skip_until = NULL;
17313 }
17314 }
17315 else
17316 {
17317 /* skip ':' and blanks*/
17318 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17319 ;
17320
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017321 /* Check for "endfunction". */
17322 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 {
17324 vim_free(theline);
17325 break;
17326 }
17327
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017328 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 * at "end". */
17330 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17331 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017332 else if (STRNCMP(p, "if", 2) == 0
17333 || STRNCMP(p, "wh", 2) == 0
17334 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 || STRNCMP(p, "try", 3) == 0)
17336 indent += 2;
17337
17338 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017339 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017341 if (*p == '!')
17342 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 p += eval_fname_script(p);
17344 if (ASCII_ISALPHA(*p))
17345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017346 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 if (*skipwhite(p) == '(')
17348 {
17349 ++nesting;
17350 indent += 2;
17351 }
17352 }
17353 }
17354
17355 /* Check for ":append" or ":insert". */
17356 p = skip_range(p, NULL);
17357 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17358 || (p[0] == 'i'
17359 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17360 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17361 skip_until = vim_strsave((char_u *)".");
17362
17363 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17364 arg = skipwhite(skiptowhite(p));
17365 if (arg[0] == '<' && arg[1] =='<'
17366 && ((p[0] == 'p' && p[1] == 'y'
17367 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17368 || (p[0] == 'p' && p[1] == 'e'
17369 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17370 || (p[0] == 't' && p[1] == 'c'
17371 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17372 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17373 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017374 || (p[0] == 'm' && p[1] == 'z'
17375 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 ))
17377 {
17378 /* ":python <<" continues until a dot, like ":append" */
17379 p = skipwhite(arg + 2);
17380 if (*p == NUL)
17381 skip_until = vim_strsave((char_u *)".");
17382 else
17383 skip_until = vim_strsave(p);
17384 }
17385 }
17386
17387 /* Add the line to the function. */
17388 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017389 {
17390 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017392 }
17393
17394 /* Copy the line to newly allocated memory. get_one_sourceline()
17395 * allocates 250 bytes per line, this saves 80% on average. The cost
17396 * is an extra alloc/free. */
17397 p = vim_strsave(theline);
17398 if (p != NULL)
17399 {
17400 vim_free(theline);
17401 theline = p;
17402 }
17403
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17405 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 }
17407
17408 /* Don't define the function when skipping commands or when an error was
17409 * detected. */
17410 if (eap->skip || did_emsg)
17411 goto erret;
17412
17413 /*
17414 * If there are no errors, add the function
17415 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017416 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017417 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017418 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017419 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017420 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017421 emsg_funcname("E707: Function name conflicts with variable: %s",
17422 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017423 goto erret;
17424 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017425
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017426 fp = find_func(name);
17427 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017429 if (!eap->forceit)
17430 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017431 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017432 goto erret;
17433 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017434 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017435 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017436 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017437 name);
17438 goto erret;
17439 }
17440 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017441 ga_clear_strings(&(fp->uf_args));
17442 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017443 vim_free(name);
17444 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446 }
17447 else
17448 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017449 char numbuf[20];
17450
17451 fp = NULL;
17452 if (fudi.fd_newkey == NULL && !eap->forceit)
17453 {
17454 EMSG(_(e_funcdict));
17455 goto erret;
17456 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017457 if (fudi.fd_di == NULL)
17458 {
17459 /* Can't add a function to a locked dictionary */
17460 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17461 goto erret;
17462 }
17463 /* Can't change an existing function if it is locked */
17464 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17465 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017466
17467 /* Give the function a sequential number. Can only be used with a
17468 * Funcref! */
17469 vim_free(name);
17470 sprintf(numbuf, "%d", ++func_nr);
17471 name = vim_strsave((char_u *)numbuf);
17472 if (name == NULL)
17473 goto erret;
17474 }
17475
17476 if (fp == NULL)
17477 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017478 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017479 {
17480 int slen, plen;
17481 char_u *scriptname;
17482
17483 /* Check that the autoload name matches the script name. */
17484 j = FAIL;
17485 if (sourcing_name != NULL)
17486 {
17487 scriptname = autoload_name(name);
17488 if (scriptname != NULL)
17489 {
17490 p = vim_strchr(scriptname, '/');
17491 plen = STRLEN(p);
17492 slen = STRLEN(sourcing_name);
17493 if (slen > plen && fnamecmp(p,
17494 sourcing_name + slen - plen) == 0)
17495 j = OK;
17496 vim_free(scriptname);
17497 }
17498 }
17499 if (j == FAIL)
17500 {
17501 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17502 goto erret;
17503 }
17504 }
17505
17506 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 if (fp == NULL)
17508 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017509
17510 if (fudi.fd_dict != NULL)
17511 {
17512 if (fudi.fd_di == NULL)
17513 {
17514 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017515 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017516 if (fudi.fd_di == NULL)
17517 {
17518 vim_free(fp);
17519 goto erret;
17520 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017521 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17522 {
17523 vim_free(fudi.fd_di);
17524 goto erret;
17525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017526 }
17527 else
17528 /* overwrite existing dict entry */
17529 clear_tv(&fudi.fd_di->di_tv);
17530 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017531 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017532 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017533 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017534 }
17535
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017537 STRCPY(fp->uf_name, name);
17538 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017540 fp->uf_args = newargs;
17541 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017542#ifdef FEAT_PROFILE
17543 fp->uf_tml_count = NULL;
17544 fp->uf_tml_total = NULL;
17545 fp->uf_tml_self = NULL;
17546 fp->uf_profiling = FALSE;
17547 if (prof_def_func())
17548 func_do_profile(fp);
17549#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017550 fp->uf_varargs = varargs;
17551 fp->uf_flags = flags;
17552 fp->uf_calls = 0;
17553 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017554 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555
17556erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 ga_clear_strings(&newargs);
17558 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017559ret_free:
17560 vim_free(skip_until);
17561 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564}
17565
17566/*
17567 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017568 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017570 * flags:
17571 * TFN_INT: internal function name OK
17572 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 * Advances "pp" to just after the function name (if no error).
17574 */
17575 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017576trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 char_u **pp;
17578 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017579 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017582 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 char_u *start;
17584 char_u *end;
17585 int lead;
17586 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017588 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017589
17590 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017591 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017593
17594 /* Check for hard coded <SNR>: already translated function ID (from a user
17595 * command). */
17596 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17597 && (*pp)[2] == (int)KE_SNR)
17598 {
17599 *pp += 3;
17600 len = get_id_len(pp) + 3;
17601 return vim_strnsave(start, len);
17602 }
17603
17604 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17605 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017607 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017609
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017610 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17611 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017612 if (end == start)
17613 {
17614 if (!skip)
17615 EMSG(_("E129: Function name required"));
17616 goto theend;
17617 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017618 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017619 {
17620 /*
17621 * Report an invalid expression in braces, unless the expression
17622 * evaluation has been cancelled due to an aborting error, an
17623 * interrupt, or an exception.
17624 */
17625 if (!aborting())
17626 {
17627 if (end != NULL)
17628 EMSG2(_(e_invarg2), start);
17629 }
17630 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017631 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017632 goto theend;
17633 }
17634
17635 if (lv.ll_tv != NULL)
17636 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017637 if (fdp != NULL)
17638 {
17639 fdp->fd_dict = lv.ll_dict;
17640 fdp->fd_newkey = lv.ll_newkey;
17641 lv.ll_newkey = NULL;
17642 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017643 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017644 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17645 {
17646 name = vim_strsave(lv.ll_tv->vval.v_string);
17647 *pp = end;
17648 }
17649 else
17650 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017651 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17652 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017653 EMSG(_(e_funcref));
17654 else
17655 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017656 name = NULL;
17657 }
17658 goto theend;
17659 }
17660
17661 if (lv.ll_name == NULL)
17662 {
17663 /* Error found, but continue after the function name. */
17664 *pp = end;
17665 goto theend;
17666 }
17667
17668 if (lv.ll_exp_name != NULL)
17669 len = STRLEN(lv.ll_exp_name);
17670 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017671 {
17672 if (lead == 2) /* skip over "s:" */
17673 lv.ll_name += 2;
17674 len = (int)(end - lv.ll_name);
17675 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017676
17677 /*
17678 * Copy the function name to allocated memory.
17679 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17680 * Accept <SNR>123_name() outside a script.
17681 */
17682 if (skip)
17683 lead = 0; /* do nothing */
17684 else if (lead > 0)
17685 {
17686 lead = 3;
17687 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17688 {
17689 if (current_SID <= 0)
17690 {
17691 EMSG(_(e_usingsid));
17692 goto theend;
17693 }
17694 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17695 lead += (int)STRLEN(sid_buf);
17696 }
17697 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017698 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017699 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017700 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017701 goto theend;
17702 }
17703 name = alloc((unsigned)(len + lead + 1));
17704 if (name != NULL)
17705 {
17706 if (lead > 0)
17707 {
17708 name[0] = K_SPECIAL;
17709 name[1] = KS_EXTRA;
17710 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017711 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017712 STRCPY(name + 3, sid_buf);
17713 }
17714 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17715 name[len + lead] = NUL;
17716 }
17717 *pp = end;
17718
17719theend:
17720 clear_lval(&lv);
17721 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722}
17723
17724/*
17725 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17726 * Return 2 if "p" starts with "s:".
17727 * Return 0 otherwise.
17728 */
17729 static int
17730eval_fname_script(p)
17731 char_u *p;
17732{
17733 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17734 || STRNICMP(p + 1, "SNR>", 4) == 0))
17735 return 5;
17736 if (p[0] == 's' && p[1] == ':')
17737 return 2;
17738 return 0;
17739}
17740
17741/*
17742 * Return TRUE if "p" starts with "<SID>" or "s:".
17743 * Only works if eval_fname_script() returned non-zero for "p"!
17744 */
17745 static int
17746eval_fname_sid(p)
17747 char_u *p;
17748{
17749 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17750}
17751
17752/*
17753 * List the head of the function: "name(arg1, arg2)".
17754 */
17755 static void
17756list_func_head(fp, indent)
17757 ufunc_T *fp;
17758 int indent;
17759{
17760 int j;
17761
17762 msg_start();
17763 if (indent)
17764 MSG_PUTS(" ");
17765 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017766 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 {
17768 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017769 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 }
17771 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017772 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017774 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775 {
17776 if (j)
17777 MSG_PUTS(", ");
17778 msg_puts(FUNCARG(fp, j));
17779 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017780 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 {
17782 if (j)
17783 MSG_PUTS(", ");
17784 MSG_PUTS("...");
17785 }
17786 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017787 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017788 if (p_verbose > 0)
17789 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790}
17791
17792/*
17793 * Find a function by name, return pointer to it in ufuncs.
17794 * Return NULL for unknown function.
17795 */
17796 static ufunc_T *
17797find_func(name)
17798 char_u *name;
17799{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017800 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017802 hi = hash_find(&func_hashtab, name);
17803 if (!HASHITEM_EMPTY(hi))
17804 return HI2UF(hi);
17805 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806}
17807
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017808#if defined(EXITFREE) || defined(PROTO)
17809 void
17810free_all_functions()
17811{
17812 hashitem_T *hi;
17813
17814 /* Need to start all over every time, because func_free() may change the
17815 * hash table. */
17816 while (func_hashtab.ht_used > 0)
17817 for (hi = func_hashtab.ht_array; ; ++hi)
17818 if (!HASHITEM_EMPTY(hi))
17819 {
17820 func_free(HI2UF(hi));
17821 break;
17822 }
17823}
17824#endif
17825
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017826/*
17827 * Return TRUE if a function "name" exists.
17828 */
17829 static int
17830function_exists(name)
17831 char_u *name;
17832{
17833 char_u *p = name;
17834 int n = FALSE;
17835
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017836 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017837 if (p != NULL)
17838 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017839 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017840 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017841 else
17842 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017843 vim_free(p);
17844 }
17845 return n;
17846}
17847
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017848/*
17849 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017850 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017851 */
17852 static int
17853builtin_function(name)
17854 char_u *name;
17855{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017856 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17857 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017858}
17859
Bram Moolenaar05159a02005-02-26 23:04:13 +000017860#if defined(FEAT_PROFILE) || defined(PROTO)
17861/*
17862 * Start profiling function "fp".
17863 */
17864 static void
17865func_do_profile(fp)
17866 ufunc_T *fp;
17867{
17868 fp->uf_tm_count = 0;
17869 profile_zero(&fp->uf_tm_self);
17870 profile_zero(&fp->uf_tm_total);
17871 if (fp->uf_tml_count == NULL)
17872 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17873 (sizeof(int) * fp->uf_lines.ga_len));
17874 if (fp->uf_tml_total == NULL)
17875 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17876 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17877 if (fp->uf_tml_self == NULL)
17878 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17879 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17880 fp->uf_tml_idx = -1;
17881 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17882 || fp->uf_tml_self == NULL)
17883 return; /* out of memory */
17884
17885 fp->uf_profiling = TRUE;
17886}
17887
17888/*
17889 * Dump the profiling results for all functions in file "fd".
17890 */
17891 void
17892func_dump_profile(fd)
17893 FILE *fd;
17894{
17895 hashitem_T *hi;
17896 int todo;
17897 ufunc_T *fp;
17898 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017899 ufunc_T **sorttab;
17900 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017901
17902 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017903 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17904
Bram Moolenaar05159a02005-02-26 23:04:13 +000017905 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17906 {
17907 if (!HASHITEM_EMPTY(hi))
17908 {
17909 --todo;
17910 fp = HI2UF(hi);
17911 if (fp->uf_profiling)
17912 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017913 if (sorttab != NULL)
17914 sorttab[st_len++] = fp;
17915
Bram Moolenaar05159a02005-02-26 23:04:13 +000017916 if (fp->uf_name[0] == K_SPECIAL)
17917 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17918 else
17919 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17920 if (fp->uf_tm_count == 1)
17921 fprintf(fd, "Called 1 time\n");
17922 else
17923 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17924 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17925 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17926 fprintf(fd, "\n");
17927 fprintf(fd, "count total (s) self (s)\n");
17928
17929 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17930 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017931 prof_func_line(fd, fp->uf_tml_count[i],
17932 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017933 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17934 }
17935 fprintf(fd, "\n");
17936 }
17937 }
17938 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017939
17940 if (sorttab != NULL && st_len > 0)
17941 {
17942 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17943 prof_total_cmp);
17944 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17945 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17946 prof_self_cmp);
17947 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17948 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017949}
Bram Moolenaar73830342005-02-28 22:48:19 +000017950
17951 static void
17952prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17953 FILE *fd;
17954 ufunc_T **sorttab;
17955 int st_len;
17956 char *title;
17957 int prefer_self; /* when equal print only self time */
17958{
17959 int i;
17960 ufunc_T *fp;
17961
17962 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17963 fprintf(fd, "count total (s) self (s) function\n");
17964 for (i = 0; i < 20 && i < st_len; ++i)
17965 {
17966 fp = sorttab[i];
17967 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17968 prefer_self);
17969 if (fp->uf_name[0] == K_SPECIAL)
17970 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17971 else
17972 fprintf(fd, " %s()\n", fp->uf_name);
17973 }
17974 fprintf(fd, "\n");
17975}
17976
17977/*
17978 * Print the count and times for one function or function line.
17979 */
17980 static void
17981prof_func_line(fd, count, total, self, prefer_self)
17982 FILE *fd;
17983 int count;
17984 proftime_T *total;
17985 proftime_T *self;
17986 int prefer_self; /* when equal print only self time */
17987{
17988 if (count > 0)
17989 {
17990 fprintf(fd, "%5d ", count);
17991 if (prefer_self && profile_equal(total, self))
17992 fprintf(fd, " ");
17993 else
17994 fprintf(fd, "%s ", profile_msg(total));
17995 if (!prefer_self && profile_equal(total, self))
17996 fprintf(fd, " ");
17997 else
17998 fprintf(fd, "%s ", profile_msg(self));
17999 }
18000 else
18001 fprintf(fd, " ");
18002}
18003
18004/*
18005 * Compare function for total time sorting.
18006 */
18007 static int
18008#ifdef __BORLANDC__
18009_RTLENTRYF
18010#endif
18011prof_total_cmp(s1, s2)
18012 const void *s1;
18013 const void *s2;
18014{
18015 ufunc_T *p1, *p2;
18016
18017 p1 = *(ufunc_T **)s1;
18018 p2 = *(ufunc_T **)s2;
18019 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18020}
18021
18022/*
18023 * Compare function for self time sorting.
18024 */
18025 static int
18026#ifdef __BORLANDC__
18027_RTLENTRYF
18028#endif
18029prof_self_cmp(s1, s2)
18030 const void *s1;
18031 const void *s2;
18032{
18033 ufunc_T *p1, *p2;
18034
18035 p1 = *(ufunc_T **)s1;
18036 p2 = *(ufunc_T **)s2;
18037 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18038}
18039
Bram Moolenaar05159a02005-02-26 23:04:13 +000018040#endif
18041
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018042/* The names of packages that once were loaded is remembered. */
18043static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18044
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018045/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018046 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018047 * Return TRUE if a package was loaded.
18048 */
18049 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018050script_autoload(name, reload)
18051 char_u *name;
18052 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018053{
18054 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018055 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018056 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018057 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018058
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018059 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018060 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018061 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018062 return FALSE;
18063
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018064 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018065
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018066 /* Find the name in the list of previously loaded package names. Skip
18067 * "autoload/", it's always the same. */
18068 for (i = 0; i < ga_loaded.ga_len; ++i)
18069 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18070 break;
18071 if (!reload && i < ga_loaded.ga_len)
18072 ret = FALSE; /* was loaded already */
18073 else
18074 {
18075 /* Remember the name if it wasn't loaded already. */
18076 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18077 {
18078 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18079 tofree = NULL;
18080 }
18081
18082 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018083 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018084 ret = TRUE;
18085 }
18086
18087 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018088 return ret;
18089}
18090
18091/*
18092 * Return the autoload script name for a function or variable name.
18093 * Returns NULL when out of memory.
18094 */
18095 static char_u *
18096autoload_name(name)
18097 char_u *name;
18098{
18099 char_u *p;
18100 char_u *scriptname;
18101
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018102 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018103 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18104 if (scriptname == NULL)
18105 return FALSE;
18106 STRCPY(scriptname, "autoload/");
18107 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018108 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018109 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018110 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018111 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018112 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018113}
18114
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18116
18117/*
18118 * Function given to ExpandGeneric() to obtain the list of user defined
18119 * function names.
18120 */
18121 char_u *
18122get_user_func_name(xp, idx)
18123 expand_T *xp;
18124 int idx;
18125{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018126 static long_u done;
18127 static hashitem_T *hi;
18128 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129
18130 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018132 done = 0;
18133 hi = func_hashtab.ht_array;
18134 }
18135 if (done < func_hashtab.ht_used)
18136 {
18137 if (done++ > 0)
18138 ++hi;
18139 while (HASHITEM_EMPTY(hi))
18140 ++hi;
18141 fp = HI2UF(hi);
18142
18143 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18144 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145
18146 cat_func_name(IObuff, fp);
18147 if (xp->xp_context != EXPAND_USER_FUNC)
18148 {
18149 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018150 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 STRCAT(IObuff, ")");
18152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 return IObuff;
18154 }
18155 return NULL;
18156}
18157
18158#endif /* FEAT_CMDL_COMPL */
18159
18160/*
18161 * Copy the function name of "fp" to buffer "buf".
18162 * "buf" must be able to hold the function name plus three bytes.
18163 * Takes care of script-local function names.
18164 */
18165 static void
18166cat_func_name(buf, fp)
18167 char_u *buf;
18168 ufunc_T *fp;
18169{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018170 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 {
18172 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018173 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 }
18175 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018176 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
18179/*
18180 * ":delfunction {name}"
18181 */
18182 void
18183ex_delfunction(eap)
18184 exarg_T *eap;
18185{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018186 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 char_u *p;
18188 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018189 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190
18191 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018192 name = trans_function_name(&p, eap->skip, 0, &fudi);
18193 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018195 {
18196 if (fudi.fd_dict != NULL && !eap->skip)
18197 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 if (!ends_excmd(*skipwhite(p)))
18201 {
18202 vim_free(name);
18203 EMSG(_(e_trailing));
18204 return;
18205 }
18206 eap->nextcmd = check_nextcmd(p);
18207 if (eap->nextcmd != NULL)
18208 *p = NUL;
18209
18210 if (!eap->skip)
18211 fp = find_func(name);
18212 vim_free(name);
18213
18214 if (!eap->skip)
18215 {
18216 if (fp == NULL)
18217 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018218 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 return;
18220 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018221 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 {
18223 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18224 return;
18225 }
18226
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018227 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018229 /* Delete the dict item that refers to the function, it will
18230 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018231 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018233 else
18234 func_free(fp);
18235 }
18236}
18237
18238/*
18239 * Free a function and remove it from the list of functions.
18240 */
18241 static void
18242func_free(fp)
18243 ufunc_T *fp;
18244{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018245 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018246
18247 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018248 ga_clear_strings(&(fp->uf_args));
18249 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018250#ifdef FEAT_PROFILE
18251 vim_free(fp->uf_tml_count);
18252 vim_free(fp->uf_tml_total);
18253 vim_free(fp->uf_tml_self);
18254#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018255
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018256 /* remove the function from the function hashtable */
18257 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18258 if (HASHITEM_EMPTY(hi))
18259 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018260 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018261 hash_remove(&func_hashtab, hi);
18262
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018263 vim_free(fp);
18264}
18265
18266/*
18267 * Unreference a Function: decrement the reference count and free it when it
18268 * becomes zero. Only for numbered functions.
18269 */
18270 static void
18271func_unref(name)
18272 char_u *name;
18273{
18274 ufunc_T *fp;
18275
18276 if (name != NULL && isdigit(*name))
18277 {
18278 fp = find_func(name);
18279 if (fp == NULL)
18280 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018281 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018282 {
18283 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018284 * when "uf_calls" becomes zero. */
18285 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018286 func_free(fp);
18287 }
18288 }
18289}
18290
18291/*
18292 * Count a reference to a Function.
18293 */
18294 static void
18295func_ref(name)
18296 char_u *name;
18297{
18298 ufunc_T *fp;
18299
18300 if (name != NULL && isdigit(*name))
18301 {
18302 fp = find_func(name);
18303 if (fp == NULL)
18304 EMSG2(_(e_intern2), "func_ref()");
18305 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018306 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 }
18308}
18309
18310/*
18311 * Call a user function.
18312 */
18313 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018314call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 ufunc_T *fp; /* pointer to function */
18316 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018317 typval_T *argvars; /* arguments */
18318 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319 linenr_T firstline; /* first line of range */
18320 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018321 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322{
Bram Moolenaar33570922005-01-25 22:26:29 +000018323 char_u *save_sourcing_name;
18324 linenr_T save_sourcing_lnum;
18325 scid_T save_current_SID;
18326 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018327 int save_did_emsg;
18328 static int depth = 0;
18329 dictitem_T *v;
18330 int fixvar_idx = 0; /* index in fixvar[] */
18331 int i;
18332 int ai;
18333 char_u numbuf[NUMBUFLEN];
18334 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018335#ifdef FEAT_PROFILE
18336 proftime_T wait_start;
18337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338
18339 /* If depth of calling is getting too high, don't execute the function */
18340 if (depth >= p_mfd)
18341 {
18342 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018343 rettv->v_type = VAR_NUMBER;
18344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 return;
18346 }
18347 ++depth;
18348
18349 line_breakcheck(); /* check for CTRL-C hit */
18350
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018351 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018352 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018354 fc.rettv = rettv;
18355 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 fc.linenr = 0;
18357 fc.returned = FALSE;
18358 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018360 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 fc.dbg_tick = debug_tick;
18362
Bram Moolenaar33570922005-01-25 22:26:29 +000018363 /*
18364 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18365 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18366 * each argument variable and saves a lot of time.
18367 */
18368 /*
18369 * Init l: variables.
18370 */
18371 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018372 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018373 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018374 /* Set l:self to "selfdict". */
18375 v = &fc.fixvar[fixvar_idx++].var;
18376 STRCPY(v->di_key, "self");
18377 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18378 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18379 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018380 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018381 v->di_tv.vval.v_dict = selfdict;
18382 ++selfdict->dv_refcount;
18383 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018384
Bram Moolenaar33570922005-01-25 22:26:29 +000018385 /*
18386 * Init a: variables.
18387 * Set a:0 to "argcount".
18388 * Set a:000 to a list with room for the "..." arguments.
18389 */
18390 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18391 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018392 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018393 v = &fc.fixvar[fixvar_idx++].var;
18394 STRCPY(v->di_key, "000");
18395 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18396 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18397 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018398 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018399 v->di_tv.vval.v_list = &fc.l_varlist;
18400 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18401 fc.l_varlist.lv_refcount = 99999;
18402
18403 /*
18404 * Set a:firstline to "firstline" and a:lastline to "lastline".
18405 * Set a:name to named arguments.
18406 * Set a:N to the "..." arguments.
18407 */
18408 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18409 (varnumber_T)firstline);
18410 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18411 (varnumber_T)lastline);
18412 for (i = 0; i < argcount; ++i)
18413 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018414 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018415 if (ai < 0)
18416 /* named argument a:name */
18417 name = FUNCARG(fp, i);
18418 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018419 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018420 /* "..." argument a:1, a:2, etc. */
18421 sprintf((char *)numbuf, "%d", ai + 1);
18422 name = numbuf;
18423 }
18424 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18425 {
18426 v = &fc.fixvar[fixvar_idx++].var;
18427 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18428 }
18429 else
18430 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018431 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18432 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018433 if (v == NULL)
18434 break;
18435 v->di_flags = DI_FLAGS_RO;
18436 }
18437 STRCPY(v->di_key, name);
18438 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18439
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018440 /* Note: the values are copied directly to avoid alloc/free.
18441 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018442 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018443 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018444
18445 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18446 {
18447 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18448 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018449 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018450 }
18451 }
18452
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 /* Don't redraw while executing the function. */
18454 ++RedrawingDisabled;
18455 save_sourcing_name = sourcing_name;
18456 save_sourcing_lnum = sourcing_lnum;
18457 sourcing_lnum = 1;
18458 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018459 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 if (sourcing_name != NULL)
18461 {
18462 if (save_sourcing_name != NULL
18463 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18464 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18465 else
18466 STRCPY(sourcing_name, "function ");
18467 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18468
18469 if (p_verbose >= 12)
18470 {
18471 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018472 verbose_enter_scroll();
18473
Bram Moolenaar555b2802005-05-19 21:08:39 +000018474 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 if (p_verbose >= 14)
18476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018478 char_u numbuf[NUMBUFLEN];
18479 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480
18481 msg_puts((char_u *)"(");
18482 for (i = 0; i < argcount; ++i)
18483 {
18484 if (i > 0)
18485 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018486 if (argvars[i].v_type == VAR_NUMBER)
18487 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 else
18489 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018490 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018491 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018493 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 }
18495 }
18496 msg_puts((char_u *)")");
18497 }
18498 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018499
18500 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 --no_wait_return;
18502 }
18503 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018504#ifdef FEAT_PROFILE
18505 if (do_profiling)
18506 {
18507 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18508 func_do_profile(fp);
18509 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018510 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018511 {
18512 ++fp->uf_tm_count;
18513 profile_start(&fp->uf_tm_start);
18514 profile_zero(&fp->uf_tm_children);
18515 }
18516 script_prof_save(&wait_start);
18517 }
18518#endif
18519
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018521 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 save_did_emsg = did_emsg;
18523 did_emsg = FALSE;
18524
18525 /* call do_cmdline() to execute the lines */
18526 do_cmdline(NULL, get_func_line, (void *)&fc,
18527 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18528
18529 --RedrawingDisabled;
18530
18531 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018532 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018534 clear_tv(rettv);
18535 rettv->v_type = VAR_NUMBER;
18536 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 }
18538
Bram Moolenaar05159a02005-02-26 23:04:13 +000018539#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018540 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018541 {
18542 profile_end(&fp->uf_tm_start);
18543 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18544 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18545 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18546 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018547 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018548 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018549 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18550 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018551 }
18552 }
18553#endif
18554
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 /* when being verbose, mention the return value */
18556 if (p_verbose >= 12)
18557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018559 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018562 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018564 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18565 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018568 char_u buf[MSG_BUF_LEN];
18569 char_u numbuf[NUMBUFLEN];
18570 char_u *tofree;
18571
Bram Moolenaar555b2802005-05-19 21:08:39 +000018572 /* The value may be very long. Skip the middle part, so that we
18573 * have some idea how it starts and ends. smsg() would always
18574 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018575 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018576 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018577 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 }
18580 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018581
18582 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 --no_wait_return;
18584 }
18585
18586 vim_free(sourcing_name);
18587 sourcing_name = save_sourcing_name;
18588 sourcing_lnum = save_sourcing_lnum;
18589 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018590#ifdef FEAT_PROFILE
18591 if (do_profiling)
18592 script_prof_restore(&wait_start);
18593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594
18595 if (p_verbose >= 12 && sourcing_name != NULL)
18596 {
18597 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018598 verbose_enter_scroll();
18599
Bram Moolenaar555b2802005-05-19 21:08:39 +000018600 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018602
18603 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 --no_wait_return;
18605 }
18606
18607 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018608 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609
Bram Moolenaar33570922005-01-25 22:26:29 +000018610 /* The a: variables typevals were not alloced, only free the allocated
18611 * variables. */
18612 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18613
18614 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 --depth;
18616}
18617
18618/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018619 * Add a number variable "name" to dict "dp" with value "nr".
18620 */
18621 static void
18622add_nr_var(dp, v, name, nr)
18623 dict_T *dp;
18624 dictitem_T *v;
18625 char *name;
18626 varnumber_T nr;
18627{
18628 STRCPY(v->di_key, name);
18629 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18630 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18631 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018632 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018633 v->di_tv.vval.v_number = nr;
18634}
18635
18636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 * ":return [expr]"
18638 */
18639 void
18640ex_return(eap)
18641 exarg_T *eap;
18642{
18643 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018644 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 int returning = FALSE;
18646
18647 if (current_funccal == NULL)
18648 {
18649 EMSG(_("E133: :return not inside a function"));
18650 return;
18651 }
18652
18653 if (eap->skip)
18654 ++emsg_skip;
18655
18656 eap->nextcmd = NULL;
18657 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 {
18660 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 }
18665 /* It's safer to return also on error. */
18666 else if (!eap->skip)
18667 {
18668 /*
18669 * Return unless the expression evaluation has been cancelled due to an
18670 * aborting error, an interrupt, or an exception.
18671 */
18672 if (!aborting())
18673 returning = do_return(eap, FALSE, TRUE, NULL);
18674 }
18675
18676 /* When skipping or the return gets pending, advance to the next command
18677 * in this line (!returning). Otherwise, ignore the rest of the line.
18678 * Following lines will be ignored by get_func_line(). */
18679 if (returning)
18680 eap->nextcmd = NULL;
18681 else if (eap->nextcmd == NULL) /* no argument */
18682 eap->nextcmd = check_nextcmd(arg);
18683
18684 if (eap->skip)
18685 --emsg_skip;
18686}
18687
18688/*
18689 * Return from a function. Possibly makes the return pending. Also called
18690 * for a pending return at the ":endtry" or after returning from an extra
18691 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018692 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018693 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 * FALSE when the return gets pending.
18695 */
18696 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 exarg_T *eap;
18699 int reanimate;
18700 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018701 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702{
18703 int idx;
18704 struct condstack *cstack = eap->cstack;
18705
18706 if (reanimate)
18707 /* Undo the return. */
18708 current_funccal->returned = FALSE;
18709
18710 /*
18711 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18712 * not in its finally clause (which then is to be executed next) is found.
18713 * In this case, make the ":return" pending for execution at the ":endtry".
18714 * Otherwise, return normally.
18715 */
18716 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18717 if (idx >= 0)
18718 {
18719 cstack->cs_pending[idx] = CSTP_RETURN;
18720
18721 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722 /* A pending return again gets pending. "rettv" points to an
18723 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018725 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726 else
18727 {
18728 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018729 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018733 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 {
18735 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018736 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018737 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 else
18739 EMSG(_(e_outofmem));
18740 }
18741 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743
18744 if (reanimate)
18745 {
18746 /* The pending return value could be overwritten by a ":return"
18747 * without argument in a finally clause; reset the default
18748 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018749 current_funccal->rettv->v_type = VAR_NUMBER;
18750 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 }
18752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018753 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 }
18755 else
18756 {
18757 current_funccal->returned = TRUE;
18758
18759 /* If the return is carried out now, store the return value. For
18760 * a return immediately after reanimation, the value is already
18761 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018764 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018767 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 }
18769 }
18770
18771 return idx < 0;
18772}
18773
18774/*
18775 * Free the variable with a pending return value.
18776 */
18777 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778discard_pending_return(rettv)
18779 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780{
Bram Moolenaar33570922005-01-25 22:26:29 +000018781 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782}
18783
18784/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018785 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 * is an allocated string. Used by report_pending() for verbose messages.
18787 */
18788 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018789get_return_cmd(rettv)
18790 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018792 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018794 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018796 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018797 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018798 if (s == NULL)
18799 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800
18801 STRCPY(IObuff, ":return ");
18802 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18803 if (STRLEN(s) + 8 >= IOSIZE)
18804 STRCPY(IObuff + IOSIZE - 4, "...");
18805 vim_free(tofree);
18806 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807}
18808
18809/*
18810 * Get next function line.
18811 * Called by do_cmdline() to get the next line.
18812 * Returns allocated string, or NULL for end of function.
18813 */
18814/* ARGSUSED */
18815 char_u *
18816get_func_line(c, cookie, indent)
18817 int c; /* not used */
18818 void *cookie;
18819 int indent; /* not used */
18820{
Bram Moolenaar33570922005-01-25 22:26:29 +000018821 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018822 ufunc_T *fp = fcp->func;
18823 char_u *retval;
18824 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825
18826 /* If breakpoints have been added/deleted need to check for it. */
18827 if (fcp->dbg_tick != debug_tick)
18828 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018829 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830 sourcing_lnum);
18831 fcp->dbg_tick = debug_tick;
18832 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018833#ifdef FEAT_PROFILE
18834 if (do_profiling)
18835 func_line_end(cookie);
18836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837
Bram Moolenaar05159a02005-02-26 23:04:13 +000018838 gap = &fp->uf_lines;
18839 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 retval = NULL;
18841 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18842 retval = NULL;
18843 else
18844 {
18845 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18846 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018847#ifdef FEAT_PROFILE
18848 if (do_profiling)
18849 func_line_start(cookie);
18850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 }
18852
18853 /* Did we encounter a breakpoint? */
18854 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18855 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018856 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018858 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 sourcing_lnum);
18860 fcp->dbg_tick = debug_tick;
18861 }
18862
18863 return retval;
18864}
18865
Bram Moolenaar05159a02005-02-26 23:04:13 +000018866#if defined(FEAT_PROFILE) || defined(PROTO)
18867/*
18868 * Called when starting to read a function line.
18869 * "sourcing_lnum" must be correct!
18870 * When skipping lines it may not actually be executed, but we won't find out
18871 * until later and we need to store the time now.
18872 */
18873 void
18874func_line_start(cookie)
18875 void *cookie;
18876{
18877 funccall_T *fcp = (funccall_T *)cookie;
18878 ufunc_T *fp = fcp->func;
18879
18880 if (fp->uf_profiling && sourcing_lnum >= 1
18881 && sourcing_lnum <= fp->uf_lines.ga_len)
18882 {
18883 fp->uf_tml_idx = sourcing_lnum - 1;
18884 fp->uf_tml_execed = FALSE;
18885 profile_start(&fp->uf_tml_start);
18886 profile_zero(&fp->uf_tml_children);
18887 profile_get_wait(&fp->uf_tml_wait);
18888 }
18889}
18890
18891/*
18892 * Called when actually executing a function line.
18893 */
18894 void
18895func_line_exec(cookie)
18896 void *cookie;
18897{
18898 funccall_T *fcp = (funccall_T *)cookie;
18899 ufunc_T *fp = fcp->func;
18900
18901 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18902 fp->uf_tml_execed = TRUE;
18903}
18904
18905/*
18906 * Called when done with a function line.
18907 */
18908 void
18909func_line_end(cookie)
18910 void *cookie;
18911{
18912 funccall_T *fcp = (funccall_T *)cookie;
18913 ufunc_T *fp = fcp->func;
18914
18915 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18916 {
18917 if (fp->uf_tml_execed)
18918 {
18919 ++fp->uf_tml_count[fp->uf_tml_idx];
18920 profile_end(&fp->uf_tml_start);
18921 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18922 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18923 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18924 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18925 }
18926 fp->uf_tml_idx = -1;
18927 }
18928}
18929#endif
18930
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931/*
18932 * Return TRUE if the currently active function should be ended, because a
18933 * return was encountered or an error occured. Used inside a ":while".
18934 */
18935 int
18936func_has_ended(cookie)
18937 void *cookie;
18938{
Bram Moolenaar33570922005-01-25 22:26:29 +000018939 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940
18941 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18942 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018943 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 || fcp->returned);
18945}
18946
18947/*
18948 * return TRUE if cookie indicates a function which "abort"s on errors.
18949 */
18950 int
18951func_has_abort(cookie)
18952 void *cookie;
18953{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018954 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955}
18956
18957#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18958typedef enum
18959{
18960 VAR_FLAVOUR_DEFAULT,
18961 VAR_FLAVOUR_SESSION,
18962 VAR_FLAVOUR_VIMINFO
18963} var_flavour_T;
18964
18965static var_flavour_T var_flavour __ARGS((char_u *varname));
18966
18967 static var_flavour_T
18968var_flavour(varname)
18969 char_u *varname;
18970{
18971 char_u *p = varname;
18972
18973 if (ASCII_ISUPPER(*p))
18974 {
18975 while (*(++p))
18976 if (ASCII_ISLOWER(*p))
18977 return VAR_FLAVOUR_SESSION;
18978 return VAR_FLAVOUR_VIMINFO;
18979 }
18980 else
18981 return VAR_FLAVOUR_DEFAULT;
18982}
18983#endif
18984
18985#if defined(FEAT_VIMINFO) || defined(PROTO)
18986/*
18987 * Restore global vars that start with a capital from the viminfo file
18988 */
18989 int
18990read_viminfo_varlist(virp, writing)
18991 vir_T *virp;
18992 int writing;
18993{
18994 char_u *tab;
18995 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018996 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997
18998 if (!writing && (find_viminfo_parameter('!') != NULL))
18999 {
19000 tab = vim_strchr(virp->vir_line + 1, '\t');
19001 if (tab != NULL)
19002 {
19003 *tab++ = '\0'; /* isolate the variable name */
19004 if (*tab == 'S') /* string var */
19005 is_string = TRUE;
19006
19007 tab = vim_strchr(tab, '\t');
19008 if (tab != NULL)
19009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 if (is_string)
19011 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019012 tv.v_type = VAR_STRING;
19013 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 }
19016 else
19017 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019018 tv.v_type = VAR_NUMBER;
19019 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019021 set_var(virp->vir_line + 1, &tv, FALSE);
19022 if (is_string)
19023 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 }
19025 }
19026 }
19027
19028 return viminfo_readline(virp);
19029}
19030
19031/*
19032 * Write global vars that start with a capital to the viminfo file
19033 */
19034 void
19035write_viminfo_varlist(fp)
19036 FILE *fp;
19037{
Bram Moolenaar33570922005-01-25 22:26:29 +000019038 hashitem_T *hi;
19039 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019040 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019041 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019042 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019043 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019044 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045
19046 if (find_viminfo_parameter('!') == NULL)
19047 return;
19048
19049 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019050
Bram Moolenaar33570922005-01-25 22:26:29 +000019051 todo = globvarht.ht_used;
19052 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019054 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019056 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019057 this_var = HI2DI(hi);
19058 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019059 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019060 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019061 {
19062 case VAR_STRING: s = "STR"; break;
19063 case VAR_NUMBER: s = "NUM"; break;
19064 default: continue;
19065 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019066 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019067 p = echo_string(&this_var->di_tv, &tofree, numbuf);
19068 if (p != NULL)
19069 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019070 vim_free(tofree);
19071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 }
19073 }
19074}
19075#endif
19076
19077#if defined(FEAT_SESSION) || defined(PROTO)
19078 int
19079store_session_globals(fd)
19080 FILE *fd;
19081{
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 hashitem_T *hi;
19083 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019084 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 char_u *p, *t;
19086
Bram Moolenaar33570922005-01-25 22:26:29 +000019087 todo = globvarht.ht_used;
19088 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019090 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019092 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019093 this_var = HI2DI(hi);
19094 if ((this_var->di_tv.v_type == VAR_NUMBER
19095 || this_var->di_tv.v_type == VAR_STRING)
19096 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019097 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019098 /* Escape special characters with a backslash. Turn a LF and
19099 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019100 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019101 (char_u *)"\\\"\n\r");
19102 if (p == NULL) /* out of memory */
19103 break;
19104 for (t = p; *t != NUL; ++t)
19105 if (*t == '\n')
19106 *t = 'n';
19107 else if (*t == '\r')
19108 *t = 'r';
19109 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 this_var->di_key,
19111 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19112 : ' ',
19113 p,
19114 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19115 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019116 || put_eol(fd) == FAIL)
19117 {
19118 vim_free(p);
19119 return FAIL;
19120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 vim_free(p);
19122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 }
19124 }
19125 return OK;
19126}
19127#endif
19128
Bram Moolenaar661b1822005-07-28 22:36:45 +000019129/*
19130 * Display script name where an item was last set.
19131 * Should only be invoked when 'verbose' is non-zero.
19132 */
19133 void
19134last_set_msg(scriptID)
19135 scid_T scriptID;
19136{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019137 char_u *p;
19138
Bram Moolenaar661b1822005-07-28 22:36:45 +000019139 if (scriptID != 0)
19140 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019141 p = home_replace_save(NULL, get_scriptname(scriptID));
19142 if (p != NULL)
19143 {
19144 verbose_enter();
19145 MSG_PUTS(_("\n\tLast set from "));
19146 MSG_PUTS(p);
19147 vim_free(p);
19148 verbose_leave();
19149 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019150 }
19151}
19152
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153#endif /* FEAT_EVAL */
19154
19155#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19156
19157
19158#ifdef WIN3264
19159/*
19160 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19161 */
19162static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19163static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19164static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19165
19166/*
19167 * Get the short pathname of a file.
19168 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19169 */
19170 static int
19171get_short_pathname(fnamep, bufp, fnamelen)
19172 char_u **fnamep;
19173 char_u **bufp;
19174 int *fnamelen;
19175{
19176 int l,len;
19177 char_u *newbuf;
19178
19179 len = *fnamelen;
19180
19181 l = GetShortPathName(*fnamep, *fnamep, len);
19182 if (l > len - 1)
19183 {
19184 /* If that doesn't work (not enough space), then save the string
19185 * and try again with a new buffer big enough
19186 */
19187 newbuf = vim_strnsave(*fnamep, l);
19188 if (newbuf == NULL)
19189 return 0;
19190
19191 vim_free(*bufp);
19192 *fnamep = *bufp = newbuf;
19193
19194 l = GetShortPathName(*fnamep,*fnamep,l+1);
19195
19196 /* Really should always succeed, as the buffer is big enough */
19197 }
19198
19199 *fnamelen = l;
19200 return 1;
19201}
19202
19203/*
19204 * Create a short path name. Returns the length of the buffer it needs.
19205 * Doesn't copy over the end of the buffer passed in.
19206 */
19207 static int
19208shortpath_for_invalid_fname(fname, bufp, fnamelen)
19209 char_u **fname;
19210 char_u **bufp;
19211 int *fnamelen;
19212{
19213 char_u *s, *p, *pbuf2, *pbuf3;
19214 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019215 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216
19217 /* Make a copy */
19218 len2 = *fnamelen;
19219 pbuf2 = vim_strnsave(*fname, len2);
19220 pbuf3 = NULL;
19221
19222 s = pbuf2 + len2 - 1; /* Find the end */
19223 slen = 1;
19224 plen = len2;
19225
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019226 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 {
19228 --s;
19229 ++slen;
19230 --plen;
19231 }
19232
19233 do
19234 {
19235 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019236 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 {
19238 --s;
19239 ++slen;
19240 --plen;
19241 }
19242 if (s <= pbuf2)
19243 break;
19244
19245 /* Remeber the character that is about to be blatted */
19246 ch = *s;
19247 *s = 0; /* get_short_pathname requires a null-terminated string */
19248
19249 /* Try it in situ */
19250 p = pbuf2;
19251 if (!get_short_pathname(&p, &pbuf3, &plen))
19252 {
19253 vim_free(pbuf2);
19254 return -1;
19255 }
19256 *s = ch; /* Preserve the string */
19257 } while (plen == 0);
19258
19259 if (plen > 0)
19260 {
19261 /* Remeber the length of the new string. */
19262 *fnamelen = len = plen + slen;
19263 vim_free(*bufp);
19264 if (len > len2)
19265 {
19266 /* If there's not enough space in the currently allocated string,
19267 * then copy it to a buffer big enough.
19268 */
19269 *fname= *bufp = vim_strnsave(p, len);
19270 if (*fname == NULL)
19271 return -1;
19272 }
19273 else
19274 {
19275 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19276 *fname = *bufp = pbuf2;
19277 if (p != pbuf2)
19278 strncpy(*fname, p, plen);
19279 pbuf2 = NULL;
19280 }
19281 /* Concat the next bit */
19282 strncpy(*fname + plen, s, slen);
19283 (*fname)[len] = '\0';
19284 }
19285 vim_free(pbuf3);
19286 vim_free(pbuf2);
19287 return 0;
19288}
19289
19290/*
19291 * Get a pathname for a partial path.
19292 */
19293 static int
19294shortpath_for_partial(fnamep, bufp, fnamelen)
19295 char_u **fnamep;
19296 char_u **bufp;
19297 int *fnamelen;
19298{
19299 int sepcount, len, tflen;
19300 char_u *p;
19301 char_u *pbuf, *tfname;
19302 int hasTilde;
19303
19304 /* Count up the path seperators from the RHS.. so we know which part
19305 * of the path to return.
19306 */
19307 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019308 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 if (vim_ispathsep(*p))
19310 ++sepcount;
19311
19312 /* Need full path first (use expand_env() to remove a "~/") */
19313 hasTilde = (**fnamep == '~');
19314 if (hasTilde)
19315 pbuf = tfname = expand_env_save(*fnamep);
19316 else
19317 pbuf = tfname = FullName_save(*fnamep, FALSE);
19318
19319 len = tflen = STRLEN(tfname);
19320
19321 if (!get_short_pathname(&tfname, &pbuf, &len))
19322 return -1;
19323
19324 if (len == 0)
19325 {
19326 /* Don't have a valid filename, so shorten the rest of the
19327 * path if we can. This CAN give us invalid 8.3 filenames, but
19328 * there's not a lot of point in guessing what it might be.
19329 */
19330 len = tflen;
19331 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19332 return -1;
19333 }
19334
19335 /* Count the paths backward to find the beginning of the desired string. */
19336 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019337 {
19338#ifdef FEAT_MBYTE
19339 if (has_mbyte)
19340 p -= mb_head_off(tfname, p);
19341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 if (vim_ispathsep(*p))
19343 {
19344 if (sepcount == 0 || (hasTilde && sepcount == 1))
19345 break;
19346 else
19347 sepcount --;
19348 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 if (hasTilde)
19351 {
19352 --p;
19353 if (p >= tfname)
19354 *p = '~';
19355 else
19356 return -1;
19357 }
19358 else
19359 ++p;
19360
19361 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19362 vim_free(*bufp);
19363 *fnamelen = (int)STRLEN(p);
19364 *bufp = pbuf;
19365 *fnamep = p;
19366
19367 return 0;
19368}
19369#endif /* WIN3264 */
19370
19371/*
19372 * Adjust a filename, according to a string of modifiers.
19373 * *fnamep must be NUL terminated when called. When returning, the length is
19374 * determined by *fnamelen.
19375 * Returns valid flags.
19376 * When there is an error, *fnamep is set to NULL.
19377 */
19378 int
19379modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19380 char_u *src; /* string with modifiers */
19381 int *usedlen; /* characters after src that are used */
19382 char_u **fnamep; /* file name so far */
19383 char_u **bufp; /* buffer for allocated file name or NULL */
19384 int *fnamelen; /* length of fnamep */
19385{
19386 int valid = 0;
19387 char_u *tail;
19388 char_u *s, *p, *pbuf;
19389 char_u dirname[MAXPATHL];
19390 int c;
19391 int has_fullname = 0;
19392#ifdef WIN3264
19393 int has_shortname = 0;
19394#endif
19395
19396repeat:
19397 /* ":p" - full path/file_name */
19398 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19399 {
19400 has_fullname = 1;
19401
19402 valid |= VALID_PATH;
19403 *usedlen += 2;
19404
19405 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19406 if ((*fnamep)[0] == '~'
19407#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19408 && ((*fnamep)[1] == '/'
19409# ifdef BACKSLASH_IN_FILENAME
19410 || (*fnamep)[1] == '\\'
19411# endif
19412 || (*fnamep)[1] == NUL)
19413
19414#endif
19415 )
19416 {
19417 *fnamep = expand_env_save(*fnamep);
19418 vim_free(*bufp); /* free any allocated file name */
19419 *bufp = *fnamep;
19420 if (*fnamep == NULL)
19421 return -1;
19422 }
19423
19424 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019425 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 {
19427 if (vim_ispathsep(*p)
19428 && p[1] == '.'
19429 && (p[2] == NUL
19430 || vim_ispathsep(p[2])
19431 || (p[2] == '.'
19432 && (p[3] == NUL || vim_ispathsep(p[3])))))
19433 break;
19434 }
19435
19436 /* FullName_save() is slow, don't use it when not needed. */
19437 if (*p != NUL || !vim_isAbsName(*fnamep))
19438 {
19439 *fnamep = FullName_save(*fnamep, *p != NUL);
19440 vim_free(*bufp); /* free any allocated file name */
19441 *bufp = *fnamep;
19442 if (*fnamep == NULL)
19443 return -1;
19444 }
19445
19446 /* Append a path separator to a directory. */
19447 if (mch_isdir(*fnamep))
19448 {
19449 /* Make room for one or two extra characters. */
19450 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19451 vim_free(*bufp); /* free any allocated file name */
19452 *bufp = *fnamep;
19453 if (*fnamep == NULL)
19454 return -1;
19455 add_pathsep(*fnamep);
19456 }
19457 }
19458
19459 /* ":." - path relative to the current directory */
19460 /* ":~" - path relative to the home directory */
19461 /* ":8" - shortname path - postponed till after */
19462 while (src[*usedlen] == ':'
19463 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19464 {
19465 *usedlen += 2;
19466 if (c == '8')
19467 {
19468#ifdef WIN3264
19469 has_shortname = 1; /* Postpone this. */
19470#endif
19471 continue;
19472 }
19473 pbuf = NULL;
19474 /* Need full path first (use expand_env() to remove a "~/") */
19475 if (!has_fullname)
19476 {
19477 if (c == '.' && **fnamep == '~')
19478 p = pbuf = expand_env_save(*fnamep);
19479 else
19480 p = pbuf = FullName_save(*fnamep, FALSE);
19481 }
19482 else
19483 p = *fnamep;
19484
19485 has_fullname = 0;
19486
19487 if (p != NULL)
19488 {
19489 if (c == '.')
19490 {
19491 mch_dirname(dirname, MAXPATHL);
19492 s = shorten_fname(p, dirname);
19493 if (s != NULL)
19494 {
19495 *fnamep = s;
19496 if (pbuf != NULL)
19497 {
19498 vim_free(*bufp); /* free any allocated file name */
19499 *bufp = pbuf;
19500 pbuf = NULL;
19501 }
19502 }
19503 }
19504 else
19505 {
19506 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19507 /* Only replace it when it starts with '~' */
19508 if (*dirname == '~')
19509 {
19510 s = vim_strsave(dirname);
19511 if (s != NULL)
19512 {
19513 *fnamep = s;
19514 vim_free(*bufp);
19515 *bufp = s;
19516 }
19517 }
19518 }
19519 vim_free(pbuf);
19520 }
19521 }
19522
19523 tail = gettail(*fnamep);
19524 *fnamelen = (int)STRLEN(*fnamep);
19525
19526 /* ":h" - head, remove "/file_name", can be repeated */
19527 /* Don't remove the first "/" or "c:\" */
19528 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19529 {
19530 valid |= VALID_HEAD;
19531 *usedlen += 2;
19532 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019533 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 --tail;
19535 *fnamelen = (int)(tail - *fnamep);
19536#ifdef VMS
19537 if (*fnamelen > 0)
19538 *fnamelen += 1; /* the path separator is part of the path */
19539#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019540 while (tail > s && !after_pathsep(s, tail))
19541 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 }
19543
19544 /* ":8" - shortname */
19545 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19546 {
19547 *usedlen += 2;
19548#ifdef WIN3264
19549 has_shortname = 1;
19550#endif
19551 }
19552
19553#ifdef WIN3264
19554 /* Check shortname after we have done 'heads' and before we do 'tails'
19555 */
19556 if (has_shortname)
19557 {
19558 pbuf = NULL;
19559 /* Copy the string if it is shortened by :h */
19560 if (*fnamelen < (int)STRLEN(*fnamep))
19561 {
19562 p = vim_strnsave(*fnamep, *fnamelen);
19563 if (p == 0)
19564 return -1;
19565 vim_free(*bufp);
19566 *bufp = *fnamep = p;
19567 }
19568
19569 /* Split into two implementations - makes it easier. First is where
19570 * there isn't a full name already, second is where there is.
19571 */
19572 if (!has_fullname && !vim_isAbsName(*fnamep))
19573 {
19574 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19575 return -1;
19576 }
19577 else
19578 {
19579 int l;
19580
19581 /* Simple case, already have the full-name
19582 * Nearly always shorter, so try first time. */
19583 l = *fnamelen;
19584 if (!get_short_pathname(fnamep, bufp, &l))
19585 return -1;
19586
19587 if (l == 0)
19588 {
19589 /* Couldn't find the filename.. search the paths.
19590 */
19591 l = *fnamelen;
19592 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19593 return -1;
19594 }
19595 *fnamelen = l;
19596 }
19597 }
19598#endif /* WIN3264 */
19599
19600 /* ":t" - tail, just the basename */
19601 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19602 {
19603 *usedlen += 2;
19604 *fnamelen -= (int)(tail - *fnamep);
19605 *fnamep = tail;
19606 }
19607
19608 /* ":e" - extension, can be repeated */
19609 /* ":r" - root, without extension, can be repeated */
19610 while (src[*usedlen] == ':'
19611 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19612 {
19613 /* find a '.' in the tail:
19614 * - for second :e: before the current fname
19615 * - otherwise: The last '.'
19616 */
19617 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19618 s = *fnamep - 2;
19619 else
19620 s = *fnamep + *fnamelen - 1;
19621 for ( ; s > tail; --s)
19622 if (s[0] == '.')
19623 break;
19624 if (src[*usedlen + 1] == 'e') /* :e */
19625 {
19626 if (s > tail)
19627 {
19628 *fnamelen += (int)(*fnamep - (s + 1));
19629 *fnamep = s + 1;
19630#ifdef VMS
19631 /* cut version from the extension */
19632 s = *fnamep + *fnamelen - 1;
19633 for ( ; s > *fnamep; --s)
19634 if (s[0] == ';')
19635 break;
19636 if (s > *fnamep)
19637 *fnamelen = s - *fnamep;
19638#endif
19639 }
19640 else if (*fnamep <= tail)
19641 *fnamelen = 0;
19642 }
19643 else /* :r */
19644 {
19645 if (s > tail) /* remove one extension */
19646 *fnamelen = (int)(s - *fnamep);
19647 }
19648 *usedlen += 2;
19649 }
19650
19651 /* ":s?pat?foo?" - substitute */
19652 /* ":gs?pat?foo?" - global substitute */
19653 if (src[*usedlen] == ':'
19654 && (src[*usedlen + 1] == 's'
19655 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19656 {
19657 char_u *str;
19658 char_u *pat;
19659 char_u *sub;
19660 int sep;
19661 char_u *flags;
19662 int didit = FALSE;
19663
19664 flags = (char_u *)"";
19665 s = src + *usedlen + 2;
19666 if (src[*usedlen + 1] == 'g')
19667 {
19668 flags = (char_u *)"g";
19669 ++s;
19670 }
19671
19672 sep = *s++;
19673 if (sep)
19674 {
19675 /* find end of pattern */
19676 p = vim_strchr(s, sep);
19677 if (p != NULL)
19678 {
19679 pat = vim_strnsave(s, (int)(p - s));
19680 if (pat != NULL)
19681 {
19682 s = p + 1;
19683 /* find end of substitution */
19684 p = vim_strchr(s, sep);
19685 if (p != NULL)
19686 {
19687 sub = vim_strnsave(s, (int)(p - s));
19688 str = vim_strnsave(*fnamep, *fnamelen);
19689 if (sub != NULL && str != NULL)
19690 {
19691 *usedlen = (int)(p + 1 - src);
19692 s = do_string_sub(str, pat, sub, flags);
19693 if (s != NULL)
19694 {
19695 *fnamep = s;
19696 *fnamelen = (int)STRLEN(s);
19697 vim_free(*bufp);
19698 *bufp = s;
19699 didit = TRUE;
19700 }
19701 }
19702 vim_free(sub);
19703 vim_free(str);
19704 }
19705 vim_free(pat);
19706 }
19707 }
19708 /* after using ":s", repeat all the modifiers */
19709 if (didit)
19710 goto repeat;
19711 }
19712 }
19713
19714 return valid;
19715}
19716
19717/*
19718 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19719 * "flags" can be "g" to do a global substitute.
19720 * Returns an allocated string, NULL for error.
19721 */
19722 char_u *
19723do_string_sub(str, pat, sub, flags)
19724 char_u *str;
19725 char_u *pat;
19726 char_u *sub;
19727 char_u *flags;
19728{
19729 int sublen;
19730 regmatch_T regmatch;
19731 int i;
19732 int do_all;
19733 char_u *tail;
19734 garray_T ga;
19735 char_u *ret;
19736 char_u *save_cpo;
19737
19738 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19739 save_cpo = p_cpo;
19740 p_cpo = (char_u *)"";
19741
19742 ga_init2(&ga, 1, 200);
19743
19744 do_all = (flags[0] == 'g');
19745
19746 regmatch.rm_ic = p_ic;
19747 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19748 if (regmatch.regprog != NULL)
19749 {
19750 tail = str;
19751 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19752 {
19753 /*
19754 * Get some space for a temporary buffer to do the substitution
19755 * into. It will contain:
19756 * - The text up to where the match is.
19757 * - The substituted text.
19758 * - The text after the match.
19759 */
19760 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19761 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19762 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19763 {
19764 ga_clear(&ga);
19765 break;
19766 }
19767
19768 /* copy the text up to where the match is */
19769 i = (int)(regmatch.startp[0] - tail);
19770 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19771 /* add the substituted text */
19772 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19773 + ga.ga_len + i, TRUE, TRUE, FALSE);
19774 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 /* avoid getting stuck on a match with an empty string */
19776 if (tail == regmatch.endp[0])
19777 {
19778 if (*tail == NUL)
19779 break;
19780 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19781 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 }
19783 else
19784 {
19785 tail = regmatch.endp[0];
19786 if (*tail == NUL)
19787 break;
19788 }
19789 if (!do_all)
19790 break;
19791 }
19792
19793 if (ga.ga_data != NULL)
19794 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19795
19796 vim_free(regmatch.regprog);
19797 }
19798
19799 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19800 ga_clear(&ga);
19801 p_cpo = save_cpo;
19802
19803 return ret;
19804}
19805
19806#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */