blob: d1e2b19e989489e326751bbb3a95ee89f514a0e2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000403static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static listitem_T *listitem_alloc __ARGS((void));
405static void listitem_free __ARGS((listitem_T *item));
406static void listitem_remove __ARGS((list_T *l, listitem_T *item));
407static long list_len __ARGS((list_T *l));
408static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
409static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
410static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static listitem_T *list_find __ARGS((list_T *l, long n));
412static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static void list_append __ARGS((list_T *l, listitem_T *item));
414static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000415static int list_append_string __ARGS((list_T *l, char_u *str, int len));
416static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
418static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
419static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000420static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000422static char_u *list2string __ARGS((typval_T *tv, int copyID));
423static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000424static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
425static void set_ref_in_list __ARGS((list_T *l, int copyID));
426static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void dict_unref __ARGS((dict_T *d));
428static void dict_free __ARGS((dict_T *d));
429static dictitem_T *dictitem_alloc __ARGS((char_u *key));
430static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
431static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
432static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000433static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static int dict_add __ARGS((dict_T *d, dictitem_T *item));
435static long dict_len __ARGS((dict_T *d));
436static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
440static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static char_u *string_quote __ARGS((char_u *str, int function));
442static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
443static int find_internal_func __ARGS((char_u *name));
444static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
445static 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));
446static 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 +0000447static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448
449static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000468#if defined(FEAT_INS_EXPAND)
469static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
477static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000503static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000505static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000511static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000519static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000542static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000548static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000565static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000569#ifdef vim_mkdir
570static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
571#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000576static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000577static void f_pumvisible __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));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000593static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000600static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000601static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000606static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000607static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
610#ifdef HAVE_STRFTIME
611static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
612#endif
613static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000625static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000626static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000627static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000628static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000629static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000631static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000646static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
649static int get_env_len __ARGS((char_u **arg));
650static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000652static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
653#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
654#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
655 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000656static 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 +0000657static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000658static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000659static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
660static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static typval_T *alloc_tv __ARGS((void));
662static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void init_tv __ARGS((typval_T *varp));
664static long get_tv_number __ARGS((typval_T *varp));
665static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000666static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static char_u *get_tv_string __ARGS((typval_T *varp));
668static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000669static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000671static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
673static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
674static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
675static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
676static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
677static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
678static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000679static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000681static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
683static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
684static int eval_fname_script __ARGS((char_u *p));
685static int eval_fname_sid __ARGS((char_u *p));
686static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static ufunc_T *find_func __ARGS((char_u *name));
688static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000689static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000690#ifdef FEAT_PROFILE
691static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000692static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
693static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
694static int
695# ifdef __BORLANDC__
696 _RTLENTRYF
697# endif
698 prof_total_cmp __ARGS((const void *s1, const void *s2));
699static int
700# ifdef __BORLANDC__
701 _RTLENTRYF
702# endif
703 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000705static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000706static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000707static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void func_free __ARGS((ufunc_T *fp));
709static void func_unref __ARGS((char_u *name));
710static void func_ref __ARGS((char_u *name));
711static 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));
712static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000713static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000714static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
715static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000717/* Character used as separated in autoload function/variable names. */
718#define AUTOLOAD_CHAR '#'
719
Bram Moolenaar33570922005-01-25 22:26:29 +0000720/*
721 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000722 */
723 void
724eval_init()
725{
Bram Moolenaar33570922005-01-25 22:26:29 +0000726 int i;
727 struct vimvar *p;
728
729 init_var_dict(&globvardict, &globvars_var);
730 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000731 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000732 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000733
734 for (i = 0; i < VV_LEN; ++i)
735 {
736 p = &vimvars[i];
737 STRCPY(p->vv_di.di_key, p->vv_name);
738 if (p->vv_flags & VV_RO)
739 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
740 else if (p->vv_flags & VV_RO_SBX)
741 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
742 else
743 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000744
745 /* add to v: scope dict, unless the value is not always available */
746 if (p->vv_type != VAR_UNKNOWN)
747 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000748 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000749 /* add to compat scope dict */
750 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000751 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000752}
753
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000754#if defined(EXITFREE) || defined(PROTO)
755 void
756eval_clear()
757{
758 int i;
759 struct vimvar *p;
760
761 for (i = 0; i < VV_LEN; ++i)
762 {
763 p = &vimvars[i];
764 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000765 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000766 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000767 p->vv_di.di_tv.vval.v_string = NULL;
768 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000769 }
770 hash_clear(&vimvarht);
771 hash_clear(&compat_hashtab);
772
773 /* script-local variables */
774 for (i = 1; i <= ga_scripts.ga_len; ++i)
775 vars_clear(&SCRIPT_VARS(i));
776 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000777 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000778
779 /* global variables */
780 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000781
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000782 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000783 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000784 hash_clear(&func_hashtab);
785
786 /* unreferenced lists and dicts */
787 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000788}
789#endif
790
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 * Return the name of the executed function.
793 */
794 char_u *
795func_name(cookie)
796 void *cookie;
797{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000798 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
801/*
802 * Return the address holding the next breakpoint line for a funccall cookie.
803 */
804 linenr_T *
805func_breakpoint(cookie)
806 void *cookie;
807{
Bram Moolenaar33570922005-01-25 22:26:29 +0000808 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809}
810
811/*
812 * Return the address holding the debug tick for a funccall cookie.
813 */
814 int *
815func_dbg_tick(cookie)
816 void *cookie;
817{
Bram Moolenaar33570922005-01-25 22:26:29 +0000818 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819}
820
821/*
822 * Return the nesting level for a funccall cookie.
823 */
824 int
825func_level(cookie)
826 void *cookie;
827{
Bram Moolenaar33570922005-01-25 22:26:29 +0000828 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
831/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000832funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834/*
835 * Return TRUE when a function was ended by a ":return" command.
836 */
837 int
838current_func_returned()
839{
840 return current_funccal->returned;
841}
842
843
844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 * Set an internal variable to a string value. Creates the variable if it does
846 * not already exist.
847 */
848 void
849set_internal_string_var(name, value)
850 char_u *name;
851 char_u *value;
852{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000853 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000854 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 val = vim_strsave(value);
857 if (val != NULL)
858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000859 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000860 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000862 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 }
866}
867
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000868static lval_T *redir_lval = NULL;
869static char_u *redir_endp = NULL;
870static char_u *redir_varname = NULL;
871
872/*
873 * Start recording command output to a variable
874 * Returns OK if successfully completed the setup. FAIL otherwise.
875 */
876 int
877var_redir_start(name, append)
878 char_u *name;
879 int append; /* append to an existing variable */
880{
881 int save_emsg;
882 int err;
883 typval_T tv;
884
885 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000886 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000887 {
888 EMSG(_(e_invarg));
889 return FAIL;
890 }
891
892 redir_varname = vim_strsave(name);
893 if (redir_varname == NULL)
894 return FAIL;
895
896 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
897 if (redir_lval == NULL)
898 {
899 var_redir_stop();
900 return FAIL;
901 }
902
903 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000904 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
905 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
907 {
908 if (redir_endp != NULL && *redir_endp != NUL)
909 /* Trailing characters are present after the variable name */
910 EMSG(_(e_trailing));
911 else
912 EMSG(_(e_invarg));
913 var_redir_stop();
914 return FAIL;
915 }
916
917 /* check if we can write to the variable: set it to or append an empty
918 * string */
919 save_emsg = did_emsg;
920 did_emsg = FALSE;
921 tv.v_type = VAR_STRING;
922 tv.vval.v_string = (char_u *)"";
923 if (append)
924 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
925 else
926 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
927 err = did_emsg;
928 did_emsg += save_emsg;
929 if (err)
930 {
931 var_redir_stop();
932 return FAIL;
933 }
934 if (redir_lval->ll_newkey != NULL)
935 {
936 /* Dictionary item was created, don't do it again. */
937 vim_free(redir_lval->ll_newkey);
938 redir_lval->ll_newkey = NULL;
939 }
940
941 return OK;
942}
943
944/*
945 * Append "value[len]" to the variable set by var_redir_start().
946 */
947 void
948var_redir_str(value, len)
949 char_u *value;
950 int len;
951{
952 char_u *val;
953 typval_T tv;
954 int save_emsg;
955 int err;
956
957 if (redir_lval == NULL)
958 return;
959
960 if (len == -1)
961 /* Append the entire string */
962 val = vim_strsave(value);
963 else
964 /* Append only the specified number of characters */
965 val = vim_strnsave(value, len);
966 if (val == NULL)
967 return;
968
969 tv.v_type = VAR_STRING;
970 tv.vval.v_string = val;
971
972 save_emsg = did_emsg;
973 did_emsg = FALSE;
974 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
975 err = did_emsg;
976 did_emsg += save_emsg;
977 if (err)
978 var_redir_stop();
979
980 vim_free(tv.vval.v_string);
981}
982
983/*
984 * Stop redirecting command output to a variable.
985 */
986 void
987var_redir_stop()
988{
989 if (redir_lval != NULL)
990 {
991 clear_lval(redir_lval);
992 vim_free(redir_lval);
993 redir_lval = NULL;
994 }
995 vim_free(redir_varname);
996 redir_varname = NULL;
997}
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999# if defined(FEAT_MBYTE) || defined(PROTO)
1000 int
1001eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1002 char_u *enc_from;
1003 char_u *enc_to;
1004 char_u *fname_from;
1005 char_u *fname_to;
1006{
1007 int err = FALSE;
1008
1009 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1010 set_vim_var_string(VV_CC_TO, enc_to, -1);
1011 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1012 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1013 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1014 err = TRUE;
1015 set_vim_var_string(VV_CC_FROM, NULL, -1);
1016 set_vim_var_string(VV_CC_TO, NULL, -1);
1017 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1018 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1019
1020 if (err)
1021 return FAIL;
1022 return OK;
1023}
1024# endif
1025
1026# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1027 int
1028eval_printexpr(fname, args)
1029 char_u *fname;
1030 char_u *args;
1031{
1032 int err = FALSE;
1033
1034 set_vim_var_string(VV_FNAME_IN, fname, -1);
1035 set_vim_var_string(VV_CMDARG, args, -1);
1036 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1037 err = TRUE;
1038 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1039 set_vim_var_string(VV_CMDARG, NULL, -1);
1040
1041 if (err)
1042 {
1043 mch_remove(fname);
1044 return FAIL;
1045 }
1046 return OK;
1047}
1048# endif
1049
1050# if defined(FEAT_DIFF) || defined(PROTO)
1051 void
1052eval_diff(origfile, newfile, outfile)
1053 char_u *origfile;
1054 char_u *newfile;
1055 char_u *outfile;
1056{
1057 int err = FALSE;
1058
1059 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1060 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1061 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1062 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1063 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1064 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1065 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1066}
1067
1068 void
1069eval_patch(origfile, difffile, outfile)
1070 char_u *origfile;
1071 char_u *difffile;
1072 char_u *outfile;
1073{
1074 int err;
1075
1076 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1077 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1078 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1079 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1080 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1081 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1082 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1083}
1084# endif
1085
1086/*
1087 * Top level evaluation function, returning a boolean.
1088 * Sets "error" to TRUE if there was an error.
1089 * Return TRUE or FALSE.
1090 */
1091 int
1092eval_to_bool(arg, error, nextcmd, skip)
1093 char_u *arg;
1094 int *error;
1095 char_u **nextcmd;
1096 int skip; /* only parse, don't execute */
1097{
Bram Moolenaar33570922005-01-25 22:26:29 +00001098 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 int retval = FALSE;
1100
1101 if (skip)
1102 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001103 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 else
1106 {
1107 *error = FALSE;
1108 if (!skip)
1109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001110 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001111 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
1113 }
1114 if (skip)
1115 --emsg_skip;
1116
1117 return retval;
1118}
1119
1120/*
1121 * Top level evaluation function, returning a string. If "skip" is TRUE,
1122 * only parsing to "nextcmd" is done, without reporting errors. Return
1123 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1124 */
1125 char_u *
1126eval_to_string_skip(arg, nextcmd, skip)
1127 char_u *arg;
1128 char_u **nextcmd;
1129 int skip; /* only parse, don't execute */
1130{
Bram Moolenaar33570922005-01-25 22:26:29 +00001131 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 char_u *retval;
1133
1134 if (skip)
1135 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001136 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 retval = NULL;
1138 else
1139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 retval = vim_strsave(get_tv_string(&tv));
1141 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 if (skip)
1144 --emsg_skip;
1145
1146 return retval;
1147}
1148
1149/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001150 * Skip over an expression at "*pp".
1151 * Return FAIL for an error, OK otherwise.
1152 */
1153 int
1154skip_expr(pp)
1155 char_u **pp;
1156{
Bram Moolenaar33570922005-01-25 22:26:29 +00001157 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001158
1159 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001161}
1162
1163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 * Top level evaluation function, returning a string.
1165 * Return pointer to allocated memory, or NULL for failure.
1166 */
1167 char_u *
1168eval_to_string(arg, nextcmd)
1169 char_u *arg;
1170 char_u **nextcmd;
1171{
Bram Moolenaar33570922005-01-25 22:26:29 +00001172 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 char_u *retval;
1174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 retval = NULL;
1177 else
1178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179 retval = vim_strsave(get_tv_string(&tv));
1180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
1182
1183 return retval;
1184}
1185
1186/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001187 * Call eval_to_string() without using current local variables and using
1188 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 */
1190 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001191eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 char_u *arg;
1193 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001194 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
1196 char_u *retval;
1197 void *save_funccalp;
1198
1199 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001200 if (use_sandbox)
1201 ++sandbox;
1202 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001204 if (use_sandbox)
1205 --sandbox;
1206 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 restore_funccal(save_funccalp);
1208 return retval;
1209}
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211/*
1212 * Top level evaluation function, returning a number.
1213 * Evaluates "expr" silently.
1214 * Returns -1 for an error.
1215 */
1216 int
1217eval_to_number(expr)
1218 char_u *expr;
1219{
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001222 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224 ++emsg_off;
1225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001226 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 retval = -1;
1228 else
1229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001230 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233 --emsg_off;
1234
1235 return retval;
1236}
1237
Bram Moolenaara40058a2005-07-11 22:42:07 +00001238/*
1239 * Prepare v: variable "idx" to be used.
1240 * Save the current typeval in "save_tv".
1241 * When not used yet add the variable to the v: hashtable.
1242 */
1243 static void
1244prepare_vimvar(idx, save_tv)
1245 int idx;
1246 typval_T *save_tv;
1247{
1248 *save_tv = vimvars[idx].vv_tv;
1249 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1250 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1251}
1252
1253/*
1254 * Restore v: variable "idx" to typeval "save_tv".
1255 * When no longer defined, remove the variable from the v: hashtable.
1256 */
1257 static void
1258restore_vimvar(idx, save_tv)
1259 int idx;
1260 typval_T *save_tv;
1261{
1262 hashitem_T *hi;
1263
1264 clear_tv(&vimvars[idx].vv_tv);
1265 vimvars[idx].vv_tv = *save_tv;
1266 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1267 {
1268 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1269 if (HASHITEM_EMPTY(hi))
1270 EMSG2(_(e_intern2), "restore_vimvar()");
1271 else
1272 hash_remove(&vimvarht, hi);
1273 }
1274}
1275
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001276#if defined(FEAT_SYN_HL) || defined(PROTO)
1277/*
1278 * Evaluate an expression to a list with suggestions.
1279 * For the "expr:" part of 'spellsuggest'.
1280 */
1281 list_T *
1282eval_spell_expr(badword, expr)
1283 char_u *badword;
1284 char_u *expr;
1285{
1286 typval_T save_val;
1287 typval_T rettv;
1288 list_T *list = NULL;
1289 char_u *p = skipwhite(expr);
1290
1291 /* Set "v:val" to the bad word. */
1292 prepare_vimvar(VV_VAL, &save_val);
1293 vimvars[VV_VAL].vv_type = VAR_STRING;
1294 vimvars[VV_VAL].vv_str = badword;
1295 if (p_verbose == 0)
1296 ++emsg_off;
1297
1298 if (eval1(&p, &rettv, TRUE) == OK)
1299 {
1300 if (rettv.v_type != VAR_LIST)
1301 clear_tv(&rettv);
1302 else
1303 list = rettv.vval.v_list;
1304 }
1305
1306 if (p_verbose == 0)
1307 --emsg_off;
1308 vimvars[VV_VAL].vv_str = NULL;
1309 restore_vimvar(VV_VAL, &save_val);
1310
1311 return list;
1312}
1313
1314/*
1315 * "list" is supposed to contain two items: a word and a number. Return the
1316 * word in "pp" and the number as the return value.
1317 * Return -1 if anything isn't right.
1318 * Used to get the good word and score from the eval_spell_expr() result.
1319 */
1320 int
1321get_spellword(list, pp)
1322 list_T *list;
1323 char_u **pp;
1324{
1325 listitem_T *li;
1326
1327 li = list->lv_first;
1328 if (li == NULL)
1329 return -1;
1330 *pp = get_tv_string(&li->li_tv);
1331
1332 li = li->li_next;
1333 if (li == NULL)
1334 return -1;
1335 return get_tv_number(&li->li_tv);
1336}
1337#endif
1338
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001339/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001340 * Top level evaluation function.
1341 * Returns an allocated typval_T with the result.
1342 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001343 */
1344 typval_T *
1345eval_expr(arg, nextcmd)
1346 char_u *arg;
1347 char_u **nextcmd;
1348{
1349 typval_T *tv;
1350
1351 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001352 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001353 {
1354 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001355 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001356 }
1357
1358 return tv;
1359}
1360
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1363/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001364 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001368 static int
1369call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *func;
1371 int argc;
1372 char_u **argv;
1373 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
Bram Moolenaar33570922005-01-25 22:26:29 +00001376 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 long n;
1378 int len;
1379 int i;
1380 int doesrange;
1381 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001382 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaar33570922005-01-25 22:26:29 +00001384 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001386 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388 for (i = 0; i < argc; i++)
1389 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001390 /* Pass a NULL or empty argument as an empty string */
1391 if (argv[i] == NULL || *argv[i] == NUL)
1392 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001393 argvars[i].v_type = VAR_STRING;
1394 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001395 continue;
1396 }
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 /* Recognize a number argument, the others must be strings. */
1399 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1400 if (len != 0 && len == (int)STRLEN(argv[i]))
1401 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001402 argvars[i].v_type = VAR_NUMBER;
1403 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 else
1406 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001407 argvars[i].v_type = VAR_STRING;
1408 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
1410 }
1411
1412 if (safe)
1413 {
1414 save_funccalp = save_funccal();
1415 ++sandbox;
1416 }
1417
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1419 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001421 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (safe)
1423 {
1424 --sandbox;
1425 restore_funccal(save_funccalp);
1426 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427 vim_free(argvars);
1428
1429 if (ret == FAIL)
1430 clear_tv(rettv);
1431
1432 return ret;
1433}
1434
1435/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001436 * Call vimL function "func" and return the result as a string.
1437 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 * Uses argv[argc] for the function arguments.
1439 */
1440 void *
1441call_func_retstr(func, argc, argv, safe)
1442 char_u *func;
1443 int argc;
1444 char_u **argv;
1445 int safe; /* use the sandbox */
1446{
1447 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001448 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001449
1450 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1451 return NULL;
1452
1453 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001454 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 return retval;
1456}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001457
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001458#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001459/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001460 * Call vimL function "func" and return the result as a number.
1461 * Returns -1 when calling the function fails.
1462 * Uses argv[argc] for the function arguments.
1463 */
1464 long
1465call_func_retnr(func, argc, argv, safe)
1466 char_u *func;
1467 int argc;
1468 char_u **argv;
1469 int safe; /* use the sandbox */
1470{
1471 typval_T rettv;
1472 long retval;
1473
1474 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1475 return -1;
1476
1477 retval = get_tv_number_chk(&rettv, NULL);
1478 clear_tv(&rettv);
1479 return retval;
1480}
1481#endif
1482
1483/*
1484 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 * Uses argv[argc] for the function arguments.
1486 */
1487 void *
1488call_func_retlist(func, argc, argv, safe)
1489 char_u *func;
1490 int argc;
1491 char_u **argv;
1492 int safe; /* use the sandbox */
1493{
1494 typval_T rettv;
1495
1496 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1497 return NULL;
1498
1499 if (rettv.v_type != VAR_LIST)
1500 {
1501 clear_tv(&rettv);
1502 return NULL;
1503 }
1504
1505 return rettv.vval.v_list;
1506}
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#endif
1509
1510/*
1511 * Save the current function call pointer, and set it to NULL.
1512 * Used when executing autocommands and for ":source".
1513 */
1514 void *
1515save_funccal()
1516{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001517 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 current_funccal = NULL;
1520 return (void *)fc;
1521}
1522
1523 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001524restore_funccal(vfc)
1525 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001527 funccall_T *fc = (funccall_T *)vfc;
1528
1529 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530}
1531
Bram Moolenaar05159a02005-02-26 23:04:13 +00001532#if defined(FEAT_PROFILE) || defined(PROTO)
1533/*
1534 * Prepare profiling for entering a child or something else that is not
1535 * counted for the script/function itself.
1536 * Should always be called in pair with prof_child_exit().
1537 */
1538 void
1539prof_child_enter(tm)
1540 proftime_T *tm; /* place to store waittime */
1541{
1542 funccall_T *fc = current_funccal;
1543
1544 if (fc != NULL && fc->func->uf_profiling)
1545 profile_start(&fc->prof_child);
1546 script_prof_save(tm);
1547}
1548
1549/*
1550 * Take care of time spent in a child.
1551 * Should always be called after prof_child_enter().
1552 */
1553 void
1554prof_child_exit(tm)
1555 proftime_T *tm; /* where waittime was stored */
1556{
1557 funccall_T *fc = current_funccal;
1558
1559 if (fc != NULL && fc->func->uf_profiling)
1560 {
1561 profile_end(&fc->prof_child);
1562 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1563 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1564 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1565 }
1566 script_prof_restore(tm);
1567}
1568#endif
1569
1570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571#ifdef FEAT_FOLDING
1572/*
1573 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1574 * it in "*cp". Doesn't give error messages.
1575 */
1576 int
1577eval_foldexpr(arg, cp)
1578 char_u *arg;
1579 int *cp;
1580{
Bram Moolenaar33570922005-01-25 22:26:29 +00001581 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 int retval;
1583 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001584 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1585 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001588 if (use_sandbox)
1589 ++sandbox;
1590 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001592 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 retval = 0;
1594 else
1595 {
1596 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 if (tv.v_type == VAR_NUMBER)
1598 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001599 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 retval = 0;
1601 else
1602 {
1603 /* If the result is a string, check if there is a non-digit before
1604 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (!VIM_ISDIGIT(*s) && *s != '-')
1607 *cp = *s++;
1608 retval = atol((char *)s);
1609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001613 if (use_sandbox)
1614 --sandbox;
1615 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 return retval;
1618}
1619#endif
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001622 * ":let" list all variable values
1623 * ":let var1 var2" list variable values
1624 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 * ":let var += expr" assignment command.
1626 * ":let var -= expr" assignment command.
1627 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 */
1630 void
1631ex_let(eap)
1632 exarg_T *eap;
1633{
1634 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001636 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 int var_count = 0;
1639 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 expr = skip_var_list(arg, &var_count, &semicolon);
1643 if (expr == NULL)
1644 return;
1645 expr = vim_strchr(expr, '=');
1646 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001648 /*
1649 * ":let" without "=": list variables
1650 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 if (*arg == '[')
1652 EMSG(_(e_invarg));
1653 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 /* ":let var1 var2" */
1655 arg = list_arg_vars(eap, arg);
1656 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001659 list_glob_vars();
1660 list_buf_vars();
1661 list_win_vars();
1662 list_vim_vars();
1663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 eap->nextcmd = check_nextcmd(arg);
1665 }
1666 else
1667 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 op[0] = '=';
1669 op[1] = NUL;
1670 if (expr > arg)
1671 {
1672 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1673 op[0] = expr[-1]; /* +=, -= or .= */
1674 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (eap->skip)
1678 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (eap->skip)
1681 {
1682 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 --emsg_skip;
1685 }
1686 else if (i != FAIL)
1687 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001689 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
1692 }
1693}
1694
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695/*
1696 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1697 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 * When "nextchars" is not NULL it points to a string with characters that
1699 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1700 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 * Returns OK or FAIL;
1702 */
1703 static int
1704ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1705 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001706 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 int copy; /* copy values from "tv", don't move */
1708 int semicolon; /* from skip_var_list() */
1709 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001710 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711{
1712 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001713 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001715 listitem_T *item;
1716 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717
1718 if (*arg != '[')
1719 {
1720 /*
1721 * ":let var = expr" or ":for var in list"
1722 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001723 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 return FAIL;
1725 return OK;
1726 }
1727
1728 /*
1729 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1730 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001731 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 {
1733 EMSG(_(e_listreq));
1734 return FAIL;
1735 }
1736
1737 i = list_len(l);
1738 if (semicolon == 0 && var_count < i)
1739 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001740 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 return FAIL;
1742 }
1743 if (var_count - semicolon > i)
1744 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001745 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 return FAIL;
1747 }
1748
1749 item = l->lv_first;
1750 while (*arg != ']')
1751 {
1752 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 item = item->li_next;
1755 if (arg == NULL)
1756 return FAIL;
1757
1758 arg = skipwhite(arg);
1759 if (*arg == ';')
1760 {
1761 /* Put the rest of the list (may be empty) in the var after ';'.
1762 * Create a new list for this. */
1763 l = list_alloc();
1764 if (l == NULL)
1765 return FAIL;
1766 while (item != NULL)
1767 {
1768 list_append_tv(l, &item->li_tv);
1769 item = item->li_next;
1770 }
1771
1772 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001773 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 ltv.vval.v_list = l;
1775 l->lv_refcount = 1;
1776
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1778 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779 clear_tv(&ltv);
1780 if (arg == NULL)
1781 return FAIL;
1782 break;
1783 }
1784 else if (*arg != ',' && *arg != ']')
1785 {
1786 EMSG2(_(e_intern2), "ex_let_vars()");
1787 return FAIL;
1788 }
1789 }
1790
1791 return OK;
1792}
1793
1794/*
1795 * Skip over assignable variable "var" or list of variables "[var, var]".
1796 * Used for ":let varvar = expr" and ":for varvar in expr".
1797 * For "[var, var]" increment "*var_count" for each variable.
1798 * for "[var, var; var]" set "semicolon".
1799 * Return NULL for an error.
1800 */
1801 static char_u *
1802skip_var_list(arg, var_count, semicolon)
1803 char_u *arg;
1804 int *var_count;
1805 int *semicolon;
1806{
1807 char_u *p, *s;
1808
1809 if (*arg == '[')
1810 {
1811 /* "[var, var]": find the matching ']'. */
1812 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001813 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 {
1815 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1816 s = skip_var_one(p);
1817 if (s == p)
1818 {
1819 EMSG2(_(e_invarg2), p);
1820 return NULL;
1821 }
1822 ++*var_count;
1823
1824 p = skipwhite(s);
1825 if (*p == ']')
1826 break;
1827 else if (*p == ';')
1828 {
1829 if (*semicolon == 1)
1830 {
1831 EMSG(_("Double ; in list of variables"));
1832 return NULL;
1833 }
1834 *semicolon = 1;
1835 }
1836 else if (*p != ',')
1837 {
1838 EMSG2(_(e_invarg2), p);
1839 return NULL;
1840 }
1841 }
1842 return p + 1;
1843 }
1844 else
1845 return skip_var_one(arg);
1846}
1847
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001848/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001849 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1850 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001851 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 static char_u *
1853skip_var_one(arg)
1854 char_u *arg;
1855{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001856 if (*arg == '@' && arg[1] != NUL)
1857 return arg + 2;
1858 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1859 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860}
1861
Bram Moolenaara7043832005-01-21 11:56:39 +00001862/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001863 * List variables for hashtab "ht" with prefix "prefix".
1864 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001865 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001867list_hashtable_vars(ht, prefix, empty)
1868 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001871{
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 hashitem_T *hi;
1873 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 int todo;
1875
1876 todo = ht->ht_used;
1877 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1878 {
1879 if (!HASHITEM_EMPTY(hi))
1880 {
1881 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 di = HI2DI(hi);
1883 if (empty || di->di_tv.v_type != VAR_STRING
1884 || di->di_tv.vval.v_string != NULL)
1885 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001886 }
1887 }
1888}
1889
1890/*
1891 * List global variables.
1892 */
1893 static void
1894list_glob_vars()
1895{
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001897}
1898
1899/*
1900 * List buffer variables.
1901 */
1902 static void
1903list_buf_vars()
1904{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001905 char_u numbuf[NUMBUFLEN];
1906
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908
1909 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1910 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001911}
1912
1913/*
1914 * List window variables.
1915 */
1916 static void
1917list_win_vars()
1918{
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001920}
1921
1922/*
1923 * List Vim variables.
1924 */
1925 static void
1926list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927{
Bram Moolenaar33570922005-01-25 22:26:29 +00001928 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929}
1930
1931/*
1932 * List variables in "arg".
1933 */
1934 static char_u *
1935list_arg_vars(eap, arg)
1936 exarg_T *eap;
1937 char_u *arg;
1938{
1939 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 char_u *name_start;
1943 char_u *arg_subsc;
1944 char_u *tofree;
1945 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946
1947 while (!ends_excmd(*arg) && !got_int)
1948 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001949 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001951 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001952 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1953 {
1954 emsg_severe = TRUE;
1955 EMSG(_(e_trailing));
1956 break;
1957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001959 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001961 /* get_name_len() takes care of expanding curly braces */
1962 name_start = name = arg;
1963 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1964 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966 /* This is mainly to keep test 49 working: when expanding
1967 * curly braces fails overrule the exception error message. */
1968 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001970 emsg_severe = TRUE;
1971 EMSG2(_(e_invarg2), arg);
1972 break;
1973 }
1974 error = TRUE;
1975 }
1976 else
1977 {
1978 if (tofree != NULL)
1979 name = tofree;
1980 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982 else
1983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 /* handle d.key, l[idx], f(expr) */
1985 arg_subsc = arg;
1986 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001987 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001990 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001991 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001992 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001994 case 'g': list_glob_vars(); break;
1995 case 'b': list_buf_vars(); break;
1996 case 'w': list_win_vars(); break;
1997 case 'v': list_vim_vars(); break;
1998 default:
1999 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002000 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002001 }
2002 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003 {
2004 char_u numbuf[NUMBUFLEN];
2005 char_u *tf;
2006 int c;
2007 char_u *s;
2008
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002009 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002010 c = *arg;
2011 *arg = NUL;
2012 list_one_var_a((char_u *)"",
2013 arg == arg_subsc ? name : name_start,
2014 tv.v_type, s == NULL ? (char_u *)"" : s);
2015 *arg = c;
2016 vim_free(tf);
2017 }
2018 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 }
2021 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022
2023 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002025
2026 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027 }
2028
2029 return arg;
2030}
2031
2032/*
2033 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2034 * Returns a pointer to the char just after the var name.
2035 * Returns NULL if there is an error.
2036 */
2037 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002040 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044{
2045 int c1;
2046 char_u *name;
2047 char_u *p;
2048 char_u *arg_end = NULL;
2049 int len;
2050 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002051 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052
2053 /*
2054 * ":let $VAR = expr": Set environment variable.
2055 */
2056 if (*arg == '$')
2057 {
2058 /* Find the end of the name. */
2059 ++arg;
2060 name = arg;
2061 len = get_env_len(&arg);
2062 if (len == 0)
2063 EMSG2(_(e_invarg2), name - 1);
2064 else
2065 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002066 if (op != NULL && (*op == '+' || *op == '-'))
2067 EMSG2(_(e_letwrong), op);
2068 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 EMSG(_(e_letunexp));
2071 else
2072 {
2073 c1 = name[len];
2074 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002075 p = get_tv_string_chk(tv);
2076 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002077 {
2078 int mustfree = FALSE;
2079 char_u *s = vim_getenv(name, &mustfree);
2080
2081 if (s != NULL)
2082 {
2083 p = tofree = concat_str(s, p);
2084 if (mustfree)
2085 vim_free(s);
2086 }
2087 }
2088 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002089 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002090 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002091 if (STRICMP(name, "HOME") == 0)
2092 init_homedir();
2093 else if (didset_vim && STRICMP(name, "VIM") == 0)
2094 didset_vim = FALSE;
2095 else if (didset_vimruntime
2096 && STRICMP(name, "VIMRUNTIME") == 0)
2097 didset_vimruntime = FALSE;
2098 arg_end = arg;
2099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 }
2103 }
2104 }
2105
2106 /*
2107 * ":let &option = expr": Set option value.
2108 * ":let &l:option = expr": Set local option value.
2109 * ":let &g:option = expr": Set global option value.
2110 */
2111 else if (*arg == '&')
2112 {
2113 /* Find the end of the name. */
2114 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002115 if (p == NULL || (endchars != NULL
2116 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 EMSG(_(e_letunexp));
2118 else
2119 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002120 long n;
2121 int opt_type;
2122 long numval;
2123 char_u *stringval = NULL;
2124 char_u *s;
2125
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 c1 = *p;
2127 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128
2129 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002130 s = get_tv_string_chk(tv); /* != NULL if number or string */
2131 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002132 {
2133 opt_type = get_option_value(arg, &numval,
2134 &stringval, opt_flags);
2135 if ((opt_type == 1 && *op == '.')
2136 || (opt_type == 0 && *op != '.'))
2137 EMSG2(_(e_letwrong), op);
2138 else
2139 {
2140 if (opt_type == 1) /* number */
2141 {
2142 if (*op == '+')
2143 n = numval + n;
2144 else
2145 n = numval - n;
2146 }
2147 else if (opt_type == 0 && stringval != NULL) /* string */
2148 {
2149 s = concat_str(stringval, s);
2150 vim_free(stringval);
2151 stringval = s;
2152 }
2153 }
2154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002155 if (s != NULL)
2156 {
2157 set_option_value(arg, n, s, opt_flags);
2158 arg_end = p;
2159 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 }
2163 }
2164
2165 /*
2166 * ":let @r = expr": Set register contents.
2167 */
2168 else if (*arg == '@')
2169 {
2170 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 if (op != NULL && (*op == '+' || *op == '-'))
2172 EMSG2(_(e_letwrong), op);
2173 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 EMSG(_(e_letunexp));
2176 else
2177 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 char_u *tofree = NULL;
2179 char_u *s;
2180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002181 p = get_tv_string_chk(tv);
2182 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002183 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002184 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002185 if (s != NULL)
2186 {
2187 p = tofree = concat_str(s, p);
2188 vim_free(s);
2189 }
2190 }
2191 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002192 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002194 arg_end = arg + 1;
2195 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 }
2198 }
2199
2200 /*
2201 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002204 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002206 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2212 EMSG(_(e_letunexp));
2213 else
2214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002215 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 arg_end = p;
2217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 }
2221
2222 else
2223 EMSG2(_(e_invarg2), arg);
2224
2225 return arg_end;
2226}
2227
2228/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002229 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2230 */
2231 static int
2232check_changedtick(arg)
2233 char_u *arg;
2234{
2235 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2236 {
2237 EMSG2(_(e_readonlyvar), arg);
2238 return TRUE;
2239 }
2240 return FALSE;
2241}
2242
2243/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 * Get an lval: variable, Dict item or List item that can be assigned a value
2245 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2246 * "name.key", "name.key[expr]" etc.
2247 * Indexing only works if "name" is an existing List or Dictionary.
2248 * "name" points to the start of the name.
2249 * If "rettv" is not NULL it points to the value to be assigned.
2250 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2251 * wrong; must end in space or cmd separator.
2252 *
2253 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002254 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 */
2257 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002260 typval_T *rettv;
2261 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 int unlet;
2263 int skip;
2264 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002265 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 char_u *expr_start, *expr_end;
2269 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 dictitem_T *v;
2271 typval_T var1;
2272 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002274 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002275 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002276 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002277 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002280 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281
2282 if (skip)
2283 {
2284 /* When skipping just find the end of the name. */
2285 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002286 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 }
2288
2289 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002290 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 if (expr_start != NULL)
2292 {
2293 /* Don't expand the name when we already know there is an error. */
2294 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2295 && *p != '[' && *p != '.')
2296 {
2297 EMSG(_(e_trailing));
2298 return NULL;
2299 }
2300
2301 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2302 if (lp->ll_exp_name == NULL)
2303 {
2304 /* Report an invalid expression in braces, unless the
2305 * expression evaluation has been cancelled due to an
2306 * aborting error, an interrupt, or an exception. */
2307 if (!aborting() && !quiet)
2308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002309 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 EMSG2(_(e_invarg2), name);
2311 return NULL;
2312 }
2313 }
2314 lp->ll_name = lp->ll_exp_name;
2315 }
2316 else
2317 lp->ll_name = name;
2318
2319 /* Without [idx] or .key we are done. */
2320 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2321 return p;
2322
2323 cc = *p;
2324 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002325 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 if (v == NULL && !quiet)
2327 EMSG2(_(e_undefvar), lp->ll_name);
2328 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 if (v == NULL)
2330 return NULL;
2331
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 /*
2333 * Loop until no more [idx] or .key is following.
2334 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002335 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2339 && !(lp->ll_tv->v_type == VAR_DICT
2340 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (!quiet)
2343 EMSG(_("E689: Can only index a List or Dictionary"));
2344 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 if (!quiet)
2349 EMSG(_("E708: [:] must come last"));
2350 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002352
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 len = -1;
2354 if (*p == '.')
2355 {
2356 key = p + 1;
2357 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2358 ;
2359 if (len == 0)
2360 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 if (!quiet)
2362 EMSG(_(e_emptykey));
2363 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002364 }
2365 p = key + len;
2366 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002367 else
2368 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 if (*p == ':')
2372 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002373 else
2374 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 empty1 = FALSE;
2376 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (get_tv_string_chk(&var1) == NULL)
2379 {
2380 /* not a number or string */
2381 clear_tv(&var1);
2382 return NULL;
2383 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002384 }
2385
2386 /* Optionally get the second index [ :expr]. */
2387 if (*p == ':')
2388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002393 if (!empty1)
2394 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002396 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 if (rettv != NULL && (rettv->v_type != VAR_LIST
2398 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 if (!quiet)
2401 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 if (!empty1)
2403 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 }
2406 p = skipwhite(p + 1);
2407 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002409 else
2410 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002412 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2413 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 if (!empty1)
2415 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 if (get_tv_string_chk(&var2) == NULL)
2419 {
2420 /* not a number or string */
2421 if (!empty1)
2422 clear_tv(&var1);
2423 clear_tv(&var2);
2424 return NULL;
2425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002428 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002431
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (!quiet)
2435 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 if (!empty1)
2437 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 }
2442
2443 /* Skip to past ']'. */
2444 ++p;
2445 }
2446
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 {
2449 if (len == -1)
2450 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 if (*key == NUL)
2454 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 if (!quiet)
2456 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 }
2460 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 lp->ll_list = NULL;
2462 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002463 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002466 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 if (len == -1)
2472 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 }
2475 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002479 if (len == -1)
2480 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 p = NULL;
2483 break;
2484 }
2485 if (len == -1)
2486 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 }
2489 else
2490 {
2491 /*
2492 * Get the number and item for the only or first index of the List.
2493 */
2494 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 else
2497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 clear_tv(&var1);
2500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 lp->ll_list = lp->ll_tv->vval.v_list;
2503 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2504 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (!quiet)
2507 EMSGN(_(e_listidx), lp->ll_n1);
2508 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 }
2512
2513 /*
2514 * May need to find the item or absolute index for the second
2515 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 * When no index given: "lp->ll_empty2" is TRUE.
2517 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 if (ni == NULL)
2527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (!quiet)
2529 EMSGN(_(e_listidx), lp->ll_n2);
2530 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 }
2534
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2536 if (lp->ll_n1 < 0)
2537 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2538 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002539 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (!quiet)
2541 EMSGN(_(e_listidx), lp->ll_n2);
2542 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002543 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002544 }
2545
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002547 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 }
2549
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 return p;
2551}
2552
2553/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 */
2556 static void
2557clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559{
2560 vim_free(lp->ll_exp_name);
2561 vim_free(lp->ll_newkey);
2562}
2563
2564/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002565 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 */
2569 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002570set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002575 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576{
2577 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 listitem_T *ri;
2579 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580
2581 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 cc = *endp;
2586 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002587 if (op != NULL && *op != '=')
2588 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002591 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002592 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2593 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594 {
2595 if (tv_op(&tv, rettv, op) == OK)
2596 set_var(lp->ll_name, &tv, FALSE);
2597 clear_tv(&tv);
2598 }
2599 }
2600 else
2601 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002605 else if (tv_check_lock(lp->ll_newkey == NULL
2606 ? lp->ll_tv->v_lock
2607 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2608 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 else if (lp->ll_range)
2610 {
2611 /*
2612 * Assign the List values to the list items.
2613 */
2614 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002616 if (op != NULL && *op != '=')
2617 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2618 else
2619 {
2620 clear_tv(&lp->ll_li->li_tv);
2621 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2622 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 ri = ri->li_next;
2624 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2625 break;
2626 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002629 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 ri = NULL;
2632 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 lp->ll_li = lp->ll_li->li_next;
2636 ++lp->ll_n1;
2637 }
2638 if (ri != NULL)
2639 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 else if (lp->ll_empty2
2641 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 : lp->ll_n1 != lp->ll_n2)
2643 EMSG(_("E711: List value has not enough items"));
2644 }
2645 else
2646 {
2647 /*
2648 * Assign to a List or Dictionary item.
2649 */
2650 if (lp->ll_newkey != NULL)
2651 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002652 if (op != NULL && *op != '=')
2653 {
2654 EMSG2(_(e_letwrong), op);
2655 return;
2656 }
2657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002659 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (di == NULL)
2661 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2663 {
2664 vim_free(di);
2665 return;
2666 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 else if (op != NULL && *op != '=')
2670 {
2671 tv_op(lp->ll_tv, rettv, op);
2672 return;
2673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002674 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 /*
2678 * Assign the value to the variable or list item.
2679 */
2680 if (copy)
2681 copy_tv(rettv, lp->ll_tv);
2682 else
2683 {
2684 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002685 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 }
2688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689}
2690
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2693 * Returns OK or FAIL.
2694 */
2695 static int
2696tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002697 typval_T *tv1;
2698 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 char_u *op;
2700{
2701 long n;
2702 char_u numbuf[NUMBUFLEN];
2703 char_u *s;
2704
2705 /* Can't do anything with a Funcref or a Dict on the right. */
2706 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2707 {
2708 switch (tv1->v_type)
2709 {
2710 case VAR_DICT:
2711 case VAR_FUNC:
2712 break;
2713
2714 case VAR_LIST:
2715 if (*op != '+' || tv2->v_type != VAR_LIST)
2716 break;
2717 /* List += List */
2718 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2719 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2720 return OK;
2721
2722 case VAR_NUMBER:
2723 case VAR_STRING:
2724 if (tv2->v_type == VAR_LIST)
2725 break;
2726 if (*op == '+' || *op == '-')
2727 {
2728 /* nr += nr or nr -= nr*/
2729 n = get_tv_number(tv1);
2730 if (*op == '+')
2731 n += get_tv_number(tv2);
2732 else
2733 n -= get_tv_number(tv2);
2734 clear_tv(tv1);
2735 tv1->v_type = VAR_NUMBER;
2736 tv1->vval.v_number = n;
2737 }
2738 else
2739 {
2740 /* str .= str */
2741 s = get_tv_string(tv1);
2742 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2743 clear_tv(tv1);
2744 tv1->v_type = VAR_STRING;
2745 tv1->vval.v_string = s;
2746 }
2747 return OK;
2748 }
2749 }
2750
2751 EMSG2(_(e_letwrong), op);
2752 return FAIL;
2753}
2754
2755/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002756 * Add a watcher to a list.
2757 */
2758 static void
2759list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002760 list_T *l;
2761 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002762{
2763 lw->lw_next = l->lv_watch;
2764 l->lv_watch = lw;
2765}
2766
2767/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002768 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002769 * No warning when it isn't found...
2770 */
2771 static void
2772list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002773 list_T *l;
2774 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775{
Bram Moolenaar33570922005-01-25 22:26:29 +00002776 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002777
2778 lwp = &l->lv_watch;
2779 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2780 {
2781 if (lw == lwrem)
2782 {
2783 *lwp = lw->lw_next;
2784 break;
2785 }
2786 lwp = &lw->lw_next;
2787 }
2788}
2789
2790/*
2791 * Just before removing an item from a list: advance watchers to the next
2792 * item.
2793 */
2794 static void
2795list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002796 list_T *l;
2797 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002798{
Bram Moolenaar33570922005-01-25 22:26:29 +00002799 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002800
2801 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2802 if (lw->lw_item == item)
2803 lw->lw_item = item->li_next;
2804}
2805
2806/*
2807 * Evaluate the expression used in a ":for var in expr" command.
2808 * "arg" points to "var".
2809 * Set "*errp" to TRUE for an error, FALSE otherwise;
2810 * Return a pointer that holds the info. Null when there is an error.
2811 */
2812 void *
2813eval_for_line(arg, errp, nextcmdp, skip)
2814 char_u *arg;
2815 int *errp;
2816 char_u **nextcmdp;
2817 int skip;
2818{
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002821 typval_T tv;
2822 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002823
2824 *errp = TRUE; /* default: there is an error */
2825
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827 if (fi == NULL)
2828 return NULL;
2829
2830 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2831 if (expr == NULL)
2832 return fi;
2833
2834 expr = skipwhite(expr);
2835 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2836 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002837 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002838 return fi;
2839 }
2840
2841 if (skip)
2842 ++emsg_skip;
2843 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2844 {
2845 *errp = FALSE;
2846 if (!skip)
2847 {
2848 l = tv.vval.v_list;
2849 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002850 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002852 clear_tv(&tv);
2853 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 else
2855 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002856 /* No need to increment the refcount, it's already set for the
2857 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858 fi->fi_list = l;
2859 list_add_watch(l, &fi->fi_lw);
2860 fi->fi_lw.lw_item = l->lv_first;
2861 }
2862 }
2863 }
2864 if (skip)
2865 --emsg_skip;
2866
2867 return fi;
2868}
2869
2870/*
2871 * Use the first item in a ":for" list. Advance to the next.
2872 * Assign the values to the variable (list). "arg" points to the first one.
2873 * Return TRUE when a valid item was found, FALSE when at end of list or
2874 * something wrong.
2875 */
2876 int
2877next_for_item(fi_void, arg)
2878 void *fi_void;
2879 char_u *arg;
2880{
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884
2885 item = fi->fi_lw.lw_item;
2886 if (item == NULL)
2887 result = FALSE;
2888 else
2889 {
2890 fi->fi_lw.lw_item = item->li_next;
2891 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2892 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2893 }
2894 return result;
2895}
2896
2897/*
2898 * Free the structure used to store info used by ":for".
2899 */
2900 void
2901free_for_info(fi_void)
2902 void *fi_void;
2903{
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002906 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002907 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002909 list_unref(fi->fi_list);
2910 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 vim_free(fi);
2912}
2913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2915
2916 void
2917set_context_for_expression(xp, arg, cmdidx)
2918 expand_T *xp;
2919 char_u *arg;
2920 cmdidx_T cmdidx;
2921{
2922 int got_eq = FALSE;
2923 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 if (cmdidx == CMD_let)
2927 {
2928 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002929 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002930 {
2931 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002932 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933 {
2934 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002935 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002936 if (vim_iswhite(*p))
2937 break;
2938 }
2939 return;
2940 }
2941 }
2942 else
2943 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2944 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 while ((xp->xp_pattern = vim_strpbrk(arg,
2946 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2947 {
2948 c = *xp->xp_pattern;
2949 if (c == '&')
2950 {
2951 c = xp->xp_pattern[1];
2952 if (c == '&')
2953 {
2954 ++xp->xp_pattern;
2955 xp->xp_context = cmdidx != CMD_let || got_eq
2956 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2957 }
2958 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002961 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2962 xp->xp_pattern += 2;
2963
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966 else if (c == '$')
2967 {
2968 /* environment variable */
2969 xp->xp_context = EXPAND_ENV_VARS;
2970 }
2971 else if (c == '=')
2972 {
2973 got_eq = TRUE;
2974 xp->xp_context = EXPAND_EXPRESSION;
2975 }
2976 else if (c == '<'
2977 && xp->xp_context == EXPAND_FUNCTIONS
2978 && vim_strchr(xp->xp_pattern, '(') == NULL)
2979 {
2980 /* Function name can start with "<SNR>" */
2981 break;
2982 }
2983 else if (cmdidx != CMD_let || got_eq)
2984 {
2985 if (c == '"') /* string */
2986 {
2987 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2988 if (c == '\\' && xp->xp_pattern[1] != NUL)
2989 ++xp->xp_pattern;
2990 xp->xp_context = EXPAND_NOTHING;
2991 }
2992 else if (c == '\'') /* literal string */
2993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002994 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2996 /* skip */ ;
2997 xp->xp_context = EXPAND_NOTHING;
2998 }
2999 else if (c == '|')
3000 {
3001 if (xp->xp_pattern[1] == '|')
3002 {
3003 ++xp->xp_pattern;
3004 xp->xp_context = EXPAND_EXPRESSION;
3005 }
3006 else
3007 xp->xp_context = EXPAND_COMMANDS;
3008 }
3009 else
3010 xp->xp_context = EXPAND_EXPRESSION;
3011 }
3012 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 /* Doesn't look like something valid, expand as an expression
3014 * anyway. */
3015 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 arg = xp->xp_pattern;
3017 if (*arg != NUL)
3018 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3019 /* skip */ ;
3020 }
3021 xp->xp_pattern = arg;
3022}
3023
3024#endif /* FEAT_CMDL_COMPL */
3025
3026/*
3027 * ":1,25call func(arg1, arg2)" function call.
3028 */
3029 void
3030ex_call(eap)
3031 exarg_T *eap;
3032{
3033 char_u *arg = eap->arg;
3034 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003038 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 linenr_T lnum;
3040 int doesrange;
3041 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003042 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3045 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 if (tofree == NULL)
3047 return;
3048
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003049 /* Increase refcount on dictionary, it could get deleted when evaluating
3050 * the arguments. */
3051 if (fudi.fd_dict != NULL)
3052 ++fudi.fd_dict->dv_refcount;
3053
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3055 len = STRLEN(tofree);
3056 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaar532c7802005-01-27 14:44:31 +00003058 /* Skip white space to allow ":call func ()". Not good, but required for
3059 * backward compatibility. */
3060 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 if (*startarg != '(')
3064 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003065 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 goto end;
3067 }
3068
3069 /*
3070 * When skipping, evaluate the function once, to find the end of the
3071 * arguments.
3072 * When the function takes a range, this is discovered after the first
3073 * call, and the loop is broken.
3074 */
3075 if (eap->skip)
3076 {
3077 ++emsg_skip;
3078 lnum = eap->line2; /* do it once, also with an invalid range */
3079 }
3080 else
3081 lnum = eap->line1;
3082 for ( ; lnum <= eap->line2; ++lnum)
3083 {
3084 if (!eap->skip && eap->addr_count > 0)
3085 {
3086 curwin->w_cursor.lnum = lnum;
3087 curwin->w_cursor.col = 0;
3088 }
3089 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003091 eap->line1, eap->line2, &doesrange,
3092 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
3094 failed = TRUE;
3095 break;
3096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003097 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (doesrange || eap->skip)
3099 break;
3100 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003101 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003102 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003103 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (aborting())
3105 break;
3106 }
3107 if (eap->skip)
3108 --emsg_skip;
3109
3110 if (!failed)
3111 {
3112 /* Check for trailing illegal characters and a following command. */
3113 if (!ends_excmd(*arg))
3114 {
3115 emsg_severe = TRUE;
3116 EMSG(_(e_trailing));
3117 }
3118 else
3119 eap->nextcmd = check_nextcmd(arg);
3120 }
3121
3122end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003123 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125}
3126
3127/*
3128 * ":unlet[!] var1 ... " command.
3129 */
3130 void
3131ex_unlet(eap)
3132 exarg_T *eap;
3133{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003134 ex_unletlock(eap, eap->arg, 0);
3135}
3136
3137/*
3138 * ":lockvar" and ":unlockvar" commands
3139 */
3140 void
3141ex_lockvar(eap)
3142 exarg_T *eap;
3143{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003145 int deep = 2;
3146
3147 if (eap->forceit)
3148 deep = -1;
3149 else if (vim_isdigit(*arg))
3150 {
3151 deep = getdigits(&arg);
3152 arg = skipwhite(arg);
3153 }
3154
3155 ex_unletlock(eap, arg, deep);
3156}
3157
3158/*
3159 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3160 */
3161 static void
3162ex_unletlock(eap, argstart, deep)
3163 exarg_T *eap;
3164 char_u *argstart;
3165 int deep;
3166{
3167 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003170 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172 do
3173 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003174 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003175 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3176 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 if (lv.ll_name == NULL)
3178 error = TRUE; /* error but continue parsing */
3179 if (name_end == NULL || (!vim_iswhite(*name_end)
3180 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003182 if (name_end != NULL)
3183 {
3184 emsg_severe = TRUE;
3185 EMSG(_(e_trailing));
3186 }
3187 if (!(eap->skip || error))
3188 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 break;
3190 }
3191
3192 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003193 {
3194 if (eap->cmdidx == CMD_unlet)
3195 {
3196 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3197 error = TRUE;
3198 }
3199 else
3200 {
3201 if (do_lock_var(&lv, name_end, deep,
3202 eap->cmdidx == CMD_lockvar) == FAIL)
3203 error = TRUE;
3204 }
3205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207 if (!eap->skip)
3208 clear_lval(&lv);
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 arg = skipwhite(name_end);
3211 } while (!ends_excmd(*arg));
3212
3213 eap->nextcmd = check_nextcmd(arg);
3214}
3215
Bram Moolenaar8c711452005-01-14 21:53:12 +00003216 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003220 int forceit;
3221{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003222 int ret = OK;
3223 int cc;
3224
3225 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003227 cc = *name_end;
3228 *name_end = NUL;
3229
3230 /* Normal name or expanded name. */
3231 if (check_changedtick(lp->ll_name))
3232 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003233 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003236 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003237 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3238 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 else if (lp->ll_range)
3240 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003242
3243 /* Delete a range of List items. */
3244 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3245 {
3246 li = lp->ll_li->li_next;
3247 listitem_remove(lp->ll_list, lp->ll_li);
3248 lp->ll_li = li;
3249 ++lp->ll_n1;
3250 }
3251 }
3252 else
3253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 /* unlet a List item. */
3256 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003259 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 }
3261
3262 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263}
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003267 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003272 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 hashtab_T *ht;
3275 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003276 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 ht = find_var_ht(name, &varname);
3279 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 hi = hash_find(ht, varname);
3282 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003283 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003284 if (var_check_ro(HI2DI(hi)->di_flags, name))
3285 return FAIL;
3286 delete_var(ht, hi);
3287 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003290 if (forceit)
3291 return OK;
3292 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 return FAIL;
3294}
3295
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003296/*
3297 * Lock or unlock variable indicated by "lp".
3298 * "deep" is the levels to go (-1 for unlimited);
3299 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3300 */
3301 static int
3302do_lock_var(lp, name_end, deep, lock)
3303 lval_T *lp;
3304 char_u *name_end;
3305 int deep;
3306 int lock;
3307{
3308 int ret = OK;
3309 int cc;
3310 dictitem_T *di;
3311
3312 if (deep == 0) /* nothing to do */
3313 return OK;
3314
3315 if (lp->ll_tv == NULL)
3316 {
3317 cc = *name_end;
3318 *name_end = NUL;
3319
3320 /* Normal name or expanded name. */
3321 if (check_changedtick(lp->ll_name))
3322 ret = FAIL;
3323 else
3324 {
3325 di = find_var(lp->ll_name, NULL);
3326 if (di == NULL)
3327 ret = FAIL;
3328 else
3329 {
3330 if (lock)
3331 di->di_flags |= DI_FLAGS_LOCK;
3332 else
3333 di->di_flags &= ~DI_FLAGS_LOCK;
3334 item_lock(&di->di_tv, deep, lock);
3335 }
3336 }
3337 *name_end = cc;
3338 }
3339 else if (lp->ll_range)
3340 {
3341 listitem_T *li = lp->ll_li;
3342
3343 /* (un)lock a range of List items. */
3344 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3345 {
3346 item_lock(&li->li_tv, deep, lock);
3347 li = li->li_next;
3348 ++lp->ll_n1;
3349 }
3350 }
3351 else if (lp->ll_list != NULL)
3352 /* (un)lock a List item. */
3353 item_lock(&lp->ll_li->li_tv, deep, lock);
3354 else
3355 /* un(lock) a Dictionary item. */
3356 item_lock(&lp->ll_di->di_tv, deep, lock);
3357
3358 return ret;
3359}
3360
3361/*
3362 * Lock or unlock an item. "deep" is nr of levels to go.
3363 */
3364 static void
3365item_lock(tv, deep, lock)
3366 typval_T *tv;
3367 int deep;
3368 int lock;
3369{
3370 static int recurse = 0;
3371 list_T *l;
3372 listitem_T *li;
3373 dict_T *d;
3374 hashitem_T *hi;
3375 int todo;
3376
3377 if (recurse >= DICT_MAXNEST)
3378 {
3379 EMSG(_("E743: variable nested too deep for (un)lock"));
3380 return;
3381 }
3382 if (deep == 0)
3383 return;
3384 ++recurse;
3385
3386 /* lock/unlock the item itself */
3387 if (lock)
3388 tv->v_lock |= VAR_LOCKED;
3389 else
3390 tv->v_lock &= ~VAR_LOCKED;
3391
3392 switch (tv->v_type)
3393 {
3394 case VAR_LIST:
3395 if ((l = tv->vval.v_list) != NULL)
3396 {
3397 if (lock)
3398 l->lv_lock |= VAR_LOCKED;
3399 else
3400 l->lv_lock &= ~VAR_LOCKED;
3401 if (deep < 0 || deep > 1)
3402 /* recursive: lock/unlock the items the List contains */
3403 for (li = l->lv_first; li != NULL; li = li->li_next)
3404 item_lock(&li->li_tv, deep - 1, lock);
3405 }
3406 break;
3407 case VAR_DICT:
3408 if ((d = tv->vval.v_dict) != NULL)
3409 {
3410 if (lock)
3411 d->dv_lock |= VAR_LOCKED;
3412 else
3413 d->dv_lock &= ~VAR_LOCKED;
3414 if (deep < 0 || deep > 1)
3415 {
3416 /* recursive: lock/unlock the items the List contains */
3417 todo = d->dv_hashtab.ht_used;
3418 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3419 {
3420 if (!HASHITEM_EMPTY(hi))
3421 {
3422 --todo;
3423 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3424 }
3425 }
3426 }
3427 }
3428 }
3429 --recurse;
3430}
3431
Bram Moolenaara40058a2005-07-11 22:42:07 +00003432/*
3433 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3434 * it refers to a List or Dictionary that is locked.
3435 */
3436 static int
3437tv_islocked(tv)
3438 typval_T *tv;
3439{
3440 return (tv->v_lock & VAR_LOCKED)
3441 || (tv->v_type == VAR_LIST
3442 && tv->vval.v_list != NULL
3443 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3444 || (tv->v_type == VAR_DICT
3445 && tv->vval.v_dict != NULL
3446 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3447}
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3450/*
3451 * Delete all "menutrans_" variables.
3452 */
3453 void
3454del_menutrans_vars()
3455{
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003457 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 hash_lock(&globvarht);
3460 todo = globvarht.ht_used;
3461 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003462 {
3463 if (!HASHITEM_EMPTY(hi))
3464 {
3465 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003466 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3467 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003468 }
3469 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003470 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471}
3472#endif
3473
3474#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3475
3476/*
3477 * Local string buffer for the next two functions to store a variable name
3478 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3479 * get_user_var_name().
3480 */
3481
3482static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3483
3484static char_u *varnamebuf = NULL;
3485static int varnamebuflen = 0;
3486
3487/*
3488 * Function to concatenate a prefix and a variable name.
3489 */
3490 static char_u *
3491cat_prefix_varname(prefix, name)
3492 int prefix;
3493 char_u *name;
3494{
3495 int len;
3496
3497 len = (int)STRLEN(name) + 3;
3498 if (len > varnamebuflen)
3499 {
3500 vim_free(varnamebuf);
3501 len += 10; /* some additional space */
3502 varnamebuf = alloc(len);
3503 if (varnamebuf == NULL)
3504 {
3505 varnamebuflen = 0;
3506 return NULL;
3507 }
3508 varnamebuflen = len;
3509 }
3510 *varnamebuf = prefix;
3511 varnamebuf[1] = ':';
3512 STRCPY(varnamebuf + 2, name);
3513 return varnamebuf;
3514}
3515
3516/*
3517 * Function given to ExpandGeneric() to obtain the list of user defined
3518 * (global/buffer/window/built-in) variable names.
3519 */
3520/*ARGSUSED*/
3521 char_u *
3522get_user_var_name(xp, idx)
3523 expand_T *xp;
3524 int idx;
3525{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003526 static long_u gdone;
3527 static long_u bdone;
3528 static long_u wdone;
3529 static int vidx;
3530 static hashitem_T *hi;
3531 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
3533 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003534 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003535
3536 /* Global variables */
3537 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003540 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003541 else
3542 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003543 while (HASHITEM_EMPTY(hi))
3544 ++hi;
3545 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3546 return cat_prefix_varname('g', hi->hi_key);
3547 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003549
3550 /* b: variables */
3551 ht = &curbuf->b_vars.dv_hashtab;
3552 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003555 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003556 else
3557 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 while (HASHITEM_EMPTY(hi))
3559 ++hi;
3560 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003564 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 return (char_u *)"b:changedtick";
3566 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003567
3568 /* w: variables */
3569 ht = &curwin->w_vars.dv_hashtab;
3570 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003572 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003573 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003574 else
3575 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003576 while (HASHITEM_EMPTY(hi))
3577 ++hi;
3578 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003580
3581 /* v: variables */
3582 if (vidx < VV_LEN)
3583 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
3585 vim_free(varnamebuf);
3586 varnamebuf = NULL;
3587 varnamebuflen = 0;
3588 return NULL;
3589}
3590
3591#endif /* FEAT_CMDL_COMPL */
3592
3593/*
3594 * types for expressions.
3595 */
3596typedef enum
3597{
3598 TYPE_UNKNOWN = 0
3599 , TYPE_EQUAL /* == */
3600 , TYPE_NEQUAL /* != */
3601 , TYPE_GREATER /* > */
3602 , TYPE_GEQUAL /* >= */
3603 , TYPE_SMALLER /* < */
3604 , TYPE_SEQUAL /* <= */
3605 , TYPE_MATCH /* =~ */
3606 , TYPE_NOMATCH /* !~ */
3607} exptype_T;
3608
3609/*
3610 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003611 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3613 */
3614
3615/*
3616 * Handle zero level expression.
3617 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003618 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003619 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 * Return OK or FAIL.
3621 */
3622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 char_u **nextcmd;
3627 int evaluate;
3628{
3629 int ret;
3630 char_u *p;
3631
3632 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003633 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (ret == FAIL || !ends_excmd(*p))
3635 {
3636 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003637 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 /*
3639 * Report the invalid expression unless the expression evaluation has
3640 * been cancelled due to an aborting error, an interrupt, or an
3641 * exception.
3642 */
3643 if (!aborting())
3644 EMSG2(_(e_invexpr2), arg);
3645 ret = FAIL;
3646 }
3647 if (nextcmd != NULL)
3648 *nextcmd = check_nextcmd(p);
3649
3650 return ret;
3651}
3652
3653/*
3654 * Handle top level expression:
3655 * expr1 ? expr0 : expr0
3656 *
3657 * "arg" must point to the first non-white of the expression.
3658 * "arg" is advanced to the next non-white after the recognized expression.
3659 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003660 * Note: "rettv.v_lock" is not set.
3661 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 * Return OK or FAIL.
3663 */
3664 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003665eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 int evaluate;
3669{
3670 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 /*
3674 * Get the first variable.
3675 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return FAIL;
3678
3679 if ((*arg)[0] == '?')
3680 {
3681 result = FALSE;
3682 if (evaluate)
3683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003684 int error = FALSE;
3685
3686 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003689 if (error)
3690 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692
3693 /*
3694 * Get the second variable.
3695 */
3696 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 return FAIL;
3699
3700 /*
3701 * Check for the ":".
3702 */
3703 if ((*arg)[0] != ':')
3704 {
3705 EMSG(_("E109: Missing ':' after '?'"));
3706 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003707 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 return FAIL;
3709 }
3710
3711 /*
3712 * Get the third variable.
3713 */
3714 *arg = skipwhite(*arg + 1);
3715 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3716 {
3717 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return FAIL;
3720 }
3721 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003722 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724
3725 return OK;
3726}
3727
3728/*
3729 * Handle first level expression:
3730 * expr2 || expr2 || expr2 logical OR
3731 *
3732 * "arg" must point to the first non-white of the expression.
3733 * "arg" is advanced to the next non-white after the recognized expression.
3734 *
3735 * Return OK or FAIL.
3736 */
3737 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003738eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 int evaluate;
3742{
Bram Moolenaar33570922005-01-25 22:26:29 +00003743 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 long result;
3745 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003746 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
3748 /*
3749 * Get the first variable.
3750 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003751 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 return FAIL;
3753
3754 /*
3755 * Repeat until there is no following "||".
3756 */
3757 first = TRUE;
3758 result = FALSE;
3759 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3760 {
3761 if (evaluate && first)
3762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003765 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003766 if (error)
3767 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 first = FALSE;
3769 }
3770
3771 /*
3772 * Get the second variable.
3773 */
3774 *arg = skipwhite(*arg + 2);
3775 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3776 return FAIL;
3777
3778 /*
3779 * Compute the result.
3780 */
3781 if (evaluate && !result)
3782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003783 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 if (error)
3787 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 if (evaluate)
3790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003791 rettv->v_type = VAR_NUMBER;
3792 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 }
3795
3796 return OK;
3797}
3798
3799/*
3800 * Handle second level expression:
3801 * expr3 && expr3 && expr3 logical AND
3802 *
3803 * "arg" must point to the first non-white of the expression.
3804 * "arg" is advanced to the next non-white after the recognized expression.
3805 *
3806 * Return OK or FAIL.
3807 */
3808 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003809eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 int evaluate;
3813{
Bram Moolenaar33570922005-01-25 22:26:29 +00003814 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 long result;
3816 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003817 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819 /*
3820 * Get the first variable.
3821 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 return FAIL;
3824
3825 /*
3826 * Repeat until there is no following "&&".
3827 */
3828 first = TRUE;
3829 result = TRUE;
3830 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3831 {
3832 if (evaluate && first)
3833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 if (error)
3838 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 first = FALSE;
3840 }
3841
3842 /*
3843 * Get the second variable.
3844 */
3845 *arg = skipwhite(*arg + 2);
3846 if (eval4(arg, &var2, evaluate && result) == FAIL)
3847 return FAIL;
3848
3849 /*
3850 * Compute the result.
3851 */
3852 if (evaluate && result)
3853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003857 if (error)
3858 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 }
3860 if (evaluate)
3861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003862 rettv->v_type = VAR_NUMBER;
3863 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865 }
3866
3867 return OK;
3868}
3869
3870/*
3871 * Handle third level expression:
3872 * var1 == var2
3873 * var1 =~ var2
3874 * var1 != var2
3875 * var1 !~ var2
3876 * var1 > var2
3877 * var1 >= var2
3878 * var1 < var2
3879 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003880 * var1 is var2
3881 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 *
3883 * "arg" must point to the first non-white of the expression.
3884 * "arg" is advanced to the next non-white after the recognized expression.
3885 *
3886 * Return OK or FAIL.
3887 */
3888 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 int evaluate;
3893{
Bram Moolenaar33570922005-01-25 22:26:29 +00003894 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 char_u *p;
3896 int i;
3897 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003898 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 int len = 2;
3900 long n1, n2;
3901 char_u *s1, *s2;
3902 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3903 regmatch_T regmatch;
3904 int ic;
3905 char_u *save_cpo;
3906
3907 /*
3908 * Get the first variable.
3909 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return FAIL;
3912
3913 p = *arg;
3914 switch (p[0])
3915 {
3916 case '=': if (p[1] == '=')
3917 type = TYPE_EQUAL;
3918 else if (p[1] == '~')
3919 type = TYPE_MATCH;
3920 break;
3921 case '!': if (p[1] == '=')
3922 type = TYPE_NEQUAL;
3923 else if (p[1] == '~')
3924 type = TYPE_NOMATCH;
3925 break;
3926 case '>': if (p[1] != '=')
3927 {
3928 type = TYPE_GREATER;
3929 len = 1;
3930 }
3931 else
3932 type = TYPE_GEQUAL;
3933 break;
3934 case '<': if (p[1] != '=')
3935 {
3936 type = TYPE_SMALLER;
3937 len = 1;
3938 }
3939 else
3940 type = TYPE_SEQUAL;
3941 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003942 case 'i': if (p[1] == 's')
3943 {
3944 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3945 len = 5;
3946 if (!vim_isIDc(p[len]))
3947 {
3948 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3949 type_is = TRUE;
3950 }
3951 }
3952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954
3955 /*
3956 * If there is a comparitive operator, use it.
3957 */
3958 if (type != TYPE_UNKNOWN)
3959 {
3960 /* extra question mark appended: ignore case */
3961 if (p[len] == '?')
3962 {
3963 ic = TRUE;
3964 ++len;
3965 }
3966 /* extra '#' appended: match case */
3967 else if (p[len] == '#')
3968 {
3969 ic = FALSE;
3970 ++len;
3971 }
3972 /* nothing appened: use 'ignorecase' */
3973 else
3974 ic = p_ic;
3975
3976 /*
3977 * Get the second variable.
3978 */
3979 *arg = skipwhite(p + len);
3980 if (eval5(arg, &var2, evaluate) == FAIL)
3981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003982 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return FAIL;
3984 }
3985
3986 if (evaluate)
3987 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003988 if (type_is && rettv->v_type != var2.v_type)
3989 {
3990 /* For "is" a different type always means FALSE, for "notis"
3991 * it means TRUE. */
3992 n1 = (type == TYPE_NEQUAL);
3993 }
3994 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3995 {
3996 if (type_is)
3997 {
3998 n1 = (rettv->v_type == var2.v_type
3999 && rettv->vval.v_list == var2.vval.v_list);
4000 if (type == TYPE_NEQUAL)
4001 n1 = !n1;
4002 }
4003 else if (rettv->v_type != var2.v_type
4004 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4005 {
4006 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004007 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004008 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004009 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004010 clear_tv(rettv);
4011 clear_tv(&var2);
4012 return FAIL;
4013 }
4014 else
4015 {
4016 /* Compare two Lists for being equal or unequal. */
4017 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4018 if (type == TYPE_NEQUAL)
4019 n1 = !n1;
4020 }
4021 }
4022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004023 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4024 {
4025 if (type_is)
4026 {
4027 n1 = (rettv->v_type == var2.v_type
4028 && rettv->vval.v_dict == var2.vval.v_dict);
4029 if (type == TYPE_NEQUAL)
4030 n1 = !n1;
4031 }
4032 else if (rettv->v_type != var2.v_type
4033 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4034 {
4035 if (rettv->v_type != var2.v_type)
4036 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4037 else
4038 EMSG(_("E736: Invalid operation for Dictionary"));
4039 clear_tv(rettv);
4040 clear_tv(&var2);
4041 return FAIL;
4042 }
4043 else
4044 {
4045 /* Compare two Dictionaries for being equal or unequal. */
4046 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4047 if (type == TYPE_NEQUAL)
4048 n1 = !n1;
4049 }
4050 }
4051
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004052 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4053 {
4054 if (rettv->v_type != var2.v_type
4055 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4056 {
4057 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004058 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004059 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004060 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 clear_tv(rettv);
4062 clear_tv(&var2);
4063 return FAIL;
4064 }
4065 else
4066 {
4067 /* Compare two Funcrefs for being equal or unequal. */
4068 if (rettv->vval.v_string == NULL
4069 || var2.vval.v_string == NULL)
4070 n1 = FALSE;
4071 else
4072 n1 = STRCMP(rettv->vval.v_string,
4073 var2.vval.v_string) == 0;
4074 if (type == TYPE_NEQUAL)
4075 n1 = !n1;
4076 }
4077 }
4078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 /*
4080 * If one of the two variables is a number, compare as a number.
4081 * When using "=~" or "!~", always compare as string.
4082 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004083 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 n1 = get_tv_number(rettv);
4087 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 switch (type)
4089 {
4090 case TYPE_EQUAL: n1 = (n1 == n2); break;
4091 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4092 case TYPE_GREATER: n1 = (n1 > n2); break;
4093 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4094 case TYPE_SMALLER: n1 = (n1 < n2); break;
4095 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4096 case TYPE_UNKNOWN:
4097 case TYPE_MATCH:
4098 case TYPE_NOMATCH: break; /* avoid gcc warning */
4099 }
4100 }
4101 else
4102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 s1 = get_tv_string_buf(rettv, buf1);
4104 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4106 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4107 else
4108 i = 0;
4109 n1 = FALSE;
4110 switch (type)
4111 {
4112 case TYPE_EQUAL: n1 = (i == 0); break;
4113 case TYPE_NEQUAL: n1 = (i != 0); break;
4114 case TYPE_GREATER: n1 = (i > 0); break;
4115 case TYPE_GEQUAL: n1 = (i >= 0); break;
4116 case TYPE_SMALLER: n1 = (i < 0); break;
4117 case TYPE_SEQUAL: n1 = (i <= 0); break;
4118
4119 case TYPE_MATCH:
4120 case TYPE_NOMATCH:
4121 /* avoid 'l' flag in 'cpoptions' */
4122 save_cpo = p_cpo;
4123 p_cpo = (char_u *)"";
4124 regmatch.regprog = vim_regcomp(s2,
4125 RE_MAGIC + RE_STRING);
4126 regmatch.rm_ic = ic;
4127 if (regmatch.regprog != NULL)
4128 {
4129 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4130 vim_free(regmatch.regprog);
4131 if (type == TYPE_NOMATCH)
4132 n1 = !n1;
4133 }
4134 p_cpo = save_cpo;
4135 break;
4136
4137 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4138 }
4139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
4141 clear_tv(&var2);
4142 rettv->v_type = VAR_NUMBER;
4143 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 }
4146
4147 return OK;
4148}
4149
4150/*
4151 * Handle fourth level expression:
4152 * + number addition
4153 * - number subtraction
4154 * . string concatenation
4155 *
4156 * "arg" must point to the first non-white of the expression.
4157 * "arg" is advanced to the next non-white after the recognized expression.
4158 *
4159 * Return OK or FAIL.
4160 */
4161 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 int evaluate;
4166{
Bram Moolenaar33570922005-01-25 22:26:29 +00004167 typval_T var2;
4168 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 int op;
4170 long n1, n2;
4171 char_u *s1, *s2;
4172 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4173 char_u *p;
4174
4175 /*
4176 * Get the first variable.
4177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180
4181 /*
4182 * Repeat computing, until no '+', '-' or '.' is following.
4183 */
4184 for (;;)
4185 {
4186 op = **arg;
4187 if (op != '+' && op != '-' && op != '.')
4188 break;
4189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (op != '+' || rettv->v_type != VAR_LIST)
4191 {
4192 /* For "list + ...", an illegal use of the first operand as
4193 * a number cannot be determined before evaluating the 2nd
4194 * operand: if this is also a list, all is ok.
4195 * For "something . ...", "something - ..." or "non-list + ...",
4196 * we know that the first operand needs to be a string or number
4197 * without evaluating the 2nd operand. So check before to avoid
4198 * side effects after an error. */
4199 if (evaluate && get_tv_string_chk(rettv) == NULL)
4200 {
4201 clear_tv(rettv);
4202 return FAIL;
4203 }
4204 }
4205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 /*
4207 * Get the second variable.
4208 */
4209 *arg = skipwhite(*arg + 1);
4210 if (eval6(arg, &var2, evaluate) == FAIL)
4211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214 }
4215
4216 if (evaluate)
4217 {
4218 /*
4219 * Compute the result.
4220 */
4221 if (op == '.')
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4224 s2 = get_tv_string_buf_chk(&var2, buf2);
4225 if (s2 == NULL) /* type error ? */
4226 {
4227 clear_tv(rettv);
4228 clear_tv(&var2);
4229 return FAIL;
4230 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004231 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 clear_tv(rettv);
4233 rettv->v_type = VAR_STRING;
4234 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004236 else if (op == '+' && rettv->v_type == VAR_LIST
4237 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004238 {
4239 /* concatenate Lists */
4240 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4241 &var3) == FAIL)
4242 {
4243 clear_tv(rettv);
4244 clear_tv(&var2);
4245 return FAIL;
4246 }
4247 clear_tv(rettv);
4248 *rettv = var3;
4249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else
4251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 int error = FALSE;
4253
4254 n1 = get_tv_number_chk(rettv, &error);
4255 if (error)
4256 {
4257 /* This can only happen for "list + non-list".
4258 * For "non-list + ..." or "something - ...", we returned
4259 * before evaluating the 2nd operand. */
4260 clear_tv(rettv);
4261 return FAIL;
4262 }
4263 n2 = get_tv_number_chk(&var2, &error);
4264 if (error)
4265 {
4266 clear_tv(rettv);
4267 clear_tv(&var2);
4268 return FAIL;
4269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (op == '+')
4272 n1 = n1 + n2;
4273 else
4274 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 rettv->v_type = VAR_NUMBER;
4276 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281 return OK;
4282}
4283
4284/*
4285 * Handle fifth level expression:
4286 * * number multiplication
4287 * / number division
4288 * % number modulo
4289 *
4290 * "arg" must point to the first non-white of the expression.
4291 * "arg" is advanced to the next non-white after the recognized expression.
4292 *
4293 * Return OK or FAIL.
4294 */
4295 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 int evaluate;
4300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 int op;
4303 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 /*
4313 * Repeat computing, until no '*', '/' or '%' is following.
4314 */
4315 for (;;)
4316 {
4317 op = **arg;
4318 if (op != '*' && op != '/' && op != '%')
4319 break;
4320
4321 if (evaluate)
4322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (error)
4326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 else
4329 n1 = 0;
4330
4331 /*
4332 * Get the second variable.
4333 */
4334 *arg = skipwhite(*arg + 1);
4335 if (eval7(arg, &var2, evaluate) == FAIL)
4336 return FAIL;
4337
4338 if (evaluate)
4339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (error)
4343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
4345 /*
4346 * Compute the result.
4347 */
4348 if (op == '*')
4349 n1 = n1 * n2;
4350 else if (op == '/')
4351 {
4352 if (n2 == 0) /* give an error message? */
4353 n1 = 0x7fffffffL;
4354 else
4355 n1 = n1 / n2;
4356 }
4357 else
4358 {
4359 if (n2 == 0) /* give an error message? */
4360 n1 = 0;
4361 else
4362 n1 = n1 % n2;
4363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_NUMBER;
4365 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 }
4368
4369 return OK;
4370}
4371
4372/*
4373 * Handle sixth level expression:
4374 * number number constant
4375 * "string" string contstant
4376 * 'string' literal string contstant
4377 * &option-name option value
4378 * @r register contents
4379 * identifier variable value
4380 * function() function call
4381 * $VAR environment variable
4382 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004383 * [expr, expr] List
4384 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 *
4386 * Also handle:
4387 * ! in front logical NOT
4388 * - in front unary minus
4389 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390 * trailing [] subscript in String or List
4391 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 *
4393 * "arg" must point to the first non-white of the expression.
4394 * "arg" is advanced to the next non-white after the recognized expression.
4395 *
4396 * Return OK or FAIL.
4397 */
4398 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004399eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int evaluate;
4403{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 long n;
4405 int len;
4406 char_u *s;
4407 int val;
4408 char_u *start_leader, *end_leader;
4409 int ret = OK;
4410 char_u *alias;
4411
4412 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004416 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 /*
4419 * Skip '!' and '-' characters. They are handled later.
4420 */
4421 start_leader = *arg;
4422 while (**arg == '!' || **arg == '-' || **arg == '+')
4423 *arg = skipwhite(*arg + 1);
4424 end_leader = *arg;
4425
4426 switch (**arg)
4427 {
4428 /*
4429 * Number constant.
4430 */
4431 case '0':
4432 case '1':
4433 case '2':
4434 case '3':
4435 case '4':
4436 case '5':
4437 case '6':
4438 case '7':
4439 case '8':
4440 case '9':
4441 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4442 *arg += len;
4443 if (evaluate)
4444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445 rettv->v_type = VAR_NUMBER;
4446 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 break;
4449
4450 /*
4451 * String constant: "string".
4452 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004453 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 break;
4455
4456 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 break;
4461
4462 /*
4463 * List: [expr, expr]
4464 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 break;
4467
4468 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 * Dictionary: {key: val, key: val}
4470 */
4471 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4472 break;
4473
4474 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004475 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004477 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 break;
4479
4480 /*
4481 * Environment variable: $VAR.
4482 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485
4486 /*
4487 * Register contents: @r.
4488 */
4489 case '@': ++*arg;
4490 if (evaluate)
4491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004492 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004493 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 if (**arg != NUL)
4496 ++*arg;
4497 break;
4498
4499 /*
4500 * nested expression: (expression).
4501 */
4502 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004503 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (**arg == ')')
4505 ++*arg;
4506 else if (ret == OK)
4507 {
4508 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 ret = FAIL;
4511 }
4512 break;
4513
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004517
4518 if (ret == NOTDONE)
4519 {
4520 /*
4521 * Must be a variable or function name.
4522 * Can also be a curly-braces kind of name: {expr}.
4523 */
4524 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004525 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004526 if (alias != NULL)
4527 s = alias;
4528
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004529 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 ret = FAIL;
4531 else
4532 {
4533 if (**arg == '(') /* recursive! */
4534 {
4535 /* If "s" is the name of a variable of type VAR_FUNC
4536 * use its contents. */
4537 s = deref_func_name(s, &len);
4538
4539 /* Invoke the function. */
4540 ret = get_func_tv(s, len, rettv, arg,
4541 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004542 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 /* Stop the expression evaluation when immediately
4544 * aborting on error, or when an interrupt occurred or
4545 * an exception was thrown but not caught. */
4546 if (aborting())
4547 {
4548 if (ret == OK)
4549 clear_tv(rettv);
4550 ret = FAIL;
4551 }
4552 }
4553 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004554 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004555 else
4556 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 }
4558
4559 if (alias != NULL)
4560 vim_free(alias);
4561 }
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 *arg = skipwhite(*arg);
4564
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004565 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4566 * expr(expr). */
4567 if (ret == OK)
4568 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
4570 /*
4571 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4572 */
4573 if (ret == OK && evaluate && end_leader > start_leader)
4574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004575 int error = FALSE;
4576
4577 val = get_tv_number_chk(rettv, &error);
4578 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580 clear_tv(rettv);
4581 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004583 else
4584 {
4585 while (end_leader > start_leader)
4586 {
4587 --end_leader;
4588 if (*end_leader == '!')
4589 val = !val;
4590 else if (*end_leader == '-')
4591 val = -val;
4592 }
4593 clear_tv(rettv);
4594 rettv->v_type = VAR_NUMBER;
4595 rettv->vval.v_number = val;
4596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598
4599 return ret;
4600}
4601
4602/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 * Evaluate an "[expr]" or "[expr:expr]" index.
4604 * "*arg" points to the '['.
4605 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4606 */
4607 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004608eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004610 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004612 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613{
4614 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004615 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 long len = -1;
4618 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004624 if (verbose)
4625 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 return FAIL;
4627 }
4628
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631 /*
4632 * dict.name
4633 */
4634 key = *arg + 1;
4635 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4636 ;
4637 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 }
4641 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 /*
4644 * something[idx]
4645 *
4646 * Get the (first) variable from inside the [].
4647 */
4648 *arg = skipwhite(*arg + 1);
4649 if (**arg == ':')
4650 empty1 = TRUE;
4651 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4652 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004653 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4654 {
4655 /* not a number or string */
4656 clear_tv(&var1);
4657 return FAIL;
4658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659
4660 /*
4661 * Get the second variable from inside the [:].
4662 */
4663 if (**arg == ':')
4664 {
4665 range = TRUE;
4666 *arg = skipwhite(*arg + 1);
4667 if (**arg == ']')
4668 empty2 = TRUE;
4669 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 if (!empty1)
4672 clear_tv(&var1);
4673 return FAIL;
4674 }
4675 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4676 {
4677 /* not a number or string */
4678 if (!empty1)
4679 clear_tv(&var1);
4680 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 return FAIL;
4682 }
4683 }
4684
4685 /* Check for the ']'. */
4686 if (**arg != ']')
4687 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004688 if (verbose)
4689 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 clear_tv(&var1);
4691 if (range)
4692 clear_tv(&var2);
4693 return FAIL;
4694 }
4695 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696 }
4697
4698 if (evaluate)
4699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 n1 = 0;
4701 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004703 n1 = get_tv_number(&var1);
4704 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705 }
4706 if (range)
4707 {
4708 if (empty2)
4709 n2 = -1;
4710 else
4711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 n2 = get_tv_number(&var2);
4713 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 }
4715 }
4716
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004717 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 {
4719 case VAR_NUMBER:
4720 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004721 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 len = (long)STRLEN(s);
4723 if (range)
4724 {
4725 /* The resulting variable is a substring. If the indexes
4726 * are out of range the result is empty. */
4727 if (n1 < 0)
4728 {
4729 n1 = len + n1;
4730 if (n1 < 0)
4731 n1 = 0;
4732 }
4733 if (n2 < 0)
4734 n2 = len + n2;
4735 else if (n2 >= len)
4736 n2 = len;
4737 if (n1 >= len || n2 < 0 || n1 > n2)
4738 s = NULL;
4739 else
4740 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4741 }
4742 else
4743 {
4744 /* The resulting variable is a string of a single
4745 * character. If the index is too big or negative the
4746 * result is empty. */
4747 if (n1 >= len || n1 < 0)
4748 s = NULL;
4749 else
4750 s = vim_strnsave(s + n1, 1);
4751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
4753 rettv->v_type = VAR_STRING;
4754 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 break;
4756
4757 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 if (n1 < 0)
4760 n1 = len + n1;
4761 if (!empty1 && (n1 < 0 || n1 >= len))
4762 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004763 if (verbose)
4764 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 return FAIL;
4766 }
4767 if (range)
4768 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004769 list_T *l;
4770 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771
4772 if (n2 < 0)
4773 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004776 if (verbose)
4777 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 return FAIL;
4779 }
4780 l = list_alloc();
4781 if (l == NULL)
4782 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 n1 <= n2; ++n1)
4785 {
4786 if (list_append_tv(l, &item->li_tv) == FAIL)
4787 {
4788 list_free(l);
4789 return FAIL;
4790 }
4791 item = item->li_next;
4792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793 clear_tv(rettv);
4794 rettv->v_type = VAR_LIST;
4795 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004796 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 }
4798 else
4799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802 clear_tv(rettv);
4803 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 }
4805 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806
4807 case VAR_DICT:
4808 if (range)
4809 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004810 if (verbose)
4811 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812 if (len == -1)
4813 clear_tv(&var1);
4814 return FAIL;
4815 }
4816 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004817 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818
4819 if (len == -1)
4820 {
4821 key = get_tv_string(&var1);
4822 if (*key == NUL)
4823 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004824 if (verbose)
4825 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004826 clear_tv(&var1);
4827 return FAIL;
4828 }
4829 }
4830
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004831 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004833 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004834 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835 if (len == -1)
4836 clear_tv(&var1);
4837 if (item == NULL)
4838 return FAIL;
4839
4840 copy_tv(&item->di_tv, &var1);
4841 clear_tv(rettv);
4842 *rettv = var1;
4843 }
4844 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004845 }
4846 }
4847
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 return OK;
4849}
4850
4851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 * Get an option value.
4853 * "arg" points to the '&' or '+' before the option name.
4854 * "arg" is advanced to character after the option name.
4855 * Return OK or FAIL.
4856 */
4857 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004860 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 int evaluate;
4862{
4863 char_u *option_end;
4864 long numval;
4865 char_u *stringval;
4866 int opt_type;
4867 int c;
4868 int working = (**arg == '+'); /* has("+option") */
4869 int ret = OK;
4870 int opt_flags;
4871
4872 /*
4873 * Isolate the option name and find its value.
4874 */
4875 option_end = find_option_end(arg, &opt_flags);
4876 if (option_end == NULL)
4877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 EMSG2(_("E112: Option name missing: %s"), *arg);
4880 return FAIL;
4881 }
4882
4883 if (!evaluate)
4884 {
4885 *arg = option_end;
4886 return OK;
4887 }
4888
4889 c = *option_end;
4890 *option_end = NUL;
4891 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893
4894 if (opt_type == -3) /* invalid name */
4895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 EMSG2(_("E113: Unknown option: %s"), *arg);
4898 ret = FAIL;
4899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
4902 if (opt_type == -2) /* hidden string option */
4903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 rettv->v_type = VAR_STRING;
4905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else if (opt_type == -1) /* hidden number option */
4908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 rettv->v_type = VAR_NUMBER;
4910 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 else if (opt_type == 1) /* number option */
4913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 rettv->v_type = VAR_NUMBER;
4915 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else /* string option */
4918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919 rettv->v_type = VAR_STRING;
4920 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922 }
4923 else if (working && (opt_type == -2 || opt_type == -1))
4924 ret = FAIL;
4925
4926 *option_end = c; /* put back for error messages */
4927 *arg = option_end;
4928
4929 return ret;
4930}
4931
4932/*
4933 * Allocate a variable for a string constant.
4934 * Return OK or FAIL.
4935 */
4936 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004937get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 int evaluate;
4941{
4942 char_u *p;
4943 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 int extra = 0;
4945
4946 /*
4947 * Find the end of the string, skipping backslashed characters.
4948 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004949 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 if (*p == '\\' && p[1] != NUL)
4952 {
4953 ++p;
4954 /* A "\<x>" form occupies at least 4 characters, and produces up
4955 * to 6 characters: reserve space for 2 extra */
4956 if (*p == '<')
4957 extra += 2;
4958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960
4961 if (*p != '"')
4962 {
4963 EMSG2(_("E114: Missing quote: %s"), *arg);
4964 return FAIL;
4965 }
4966
4967 /* If only parsing, set *arg and return here */
4968 if (!evaluate)
4969 {
4970 *arg = p + 1;
4971 return OK;
4972 }
4973
4974 /*
4975 * Copy the string into allocated memory, handling backslashed
4976 * characters.
4977 */
4978 name = alloc((unsigned)(p - *arg + extra));
4979 if (name == NULL)
4980 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 rettv->v_type = VAR_STRING;
4982 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 if (*p == '\\')
4987 {
4988 switch (*++p)
4989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 case 'b': *name++ = BS; ++p; break;
4991 case 'e': *name++ = ESC; ++p; break;
4992 case 'f': *name++ = FF; ++p; break;
4993 case 'n': *name++ = NL; ++p; break;
4994 case 'r': *name++ = CAR; ++p; break;
4995 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997 case 'X': /* hex: "\x1", "\x12" */
4998 case 'x':
4999 case 'u': /* Unicode: "\u0023" */
5000 case 'U':
5001 if (vim_isxdigit(p[1]))
5002 {
5003 int n, nr;
5004 int c = toupper(*p);
5005
5006 if (c == 'X')
5007 n = 2;
5008 else
5009 n = 4;
5010 nr = 0;
5011 while (--n >= 0 && vim_isxdigit(p[1]))
5012 {
5013 ++p;
5014 nr = (nr << 4) + hex2nr(*p);
5015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017#ifdef FEAT_MBYTE
5018 /* For "\u" store the number according to
5019 * 'encoding'. */
5020 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 else
5023#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 break;
5027
5028 /* octal: "\1", "\12", "\123" */
5029 case '0':
5030 case '1':
5031 case '2':
5032 case '3':
5033 case '4':
5034 case '5':
5035 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 case '7': *name = *p++ - '0';
5037 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 *name = (*name << 3) + *p++ - '0';
5040 if (*p >= '0' && *p <= '7')
5041 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 break;
5045
5046 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (extra != 0)
5049 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 break;
5052 }
5053 /* FALLTHROUGH */
5054
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 break;
5057 }
5058 }
5059 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 *arg = p + 1;
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 return OK;
5067}
5068
5069/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 * Return OK or FAIL.
5072 */
5073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 int evaluate;
5078{
5079 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 int reduce = 0;
5082
5083 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085 */
5086 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 break;
5092 ++reduce;
5093 ++p;
5094 }
5095 }
5096
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 return FAIL;
5101 }
5102
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 if (!evaluate)
5105 {
5106 *arg = p + 1;
5107 return OK;
5108 }
5109
5110 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 */
5113 str = alloc((unsigned)((p - *arg) - reduce));
5114 if (str == NULL)
5115 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 rettv->v_type = VAR_STRING;
5117 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124 break;
5125 ++p;
5126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005130 *arg = p + 1;
5131
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132 return OK;
5133}
5134
5135/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136 * Allocate a variable for a List and fill it from "*arg".
5137 * Return OK or FAIL.
5138 */
5139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005140get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005142 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 int evaluate;
5144{
Bram Moolenaar33570922005-01-25 22:26:29 +00005145 list_T *l = NULL;
5146 typval_T tv;
5147 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005148
5149 if (evaluate)
5150 {
5151 l = list_alloc();
5152 if (l == NULL)
5153 return FAIL;
5154 }
5155
5156 *arg = skipwhite(*arg + 1);
5157 while (**arg != ']' && **arg != NUL)
5158 {
5159 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5160 goto failret;
5161 if (evaluate)
5162 {
5163 item = listitem_alloc();
5164 if (item != NULL)
5165 {
5166 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005167 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 list_append(l, item);
5169 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005170 else
5171 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 }
5173
5174 if (**arg == ']')
5175 break;
5176 if (**arg != ',')
5177 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 goto failret;
5180 }
5181 *arg = skipwhite(*arg + 1);
5182 }
5183
5184 if (**arg != ']')
5185 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187failret:
5188 if (evaluate)
5189 list_free(l);
5190 return FAIL;
5191 }
5192
5193 *arg = skipwhite(*arg + 1);
5194 if (evaluate)
5195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 rettv->v_type = VAR_LIST;
5197 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 ++l->lv_refcount;
5199 }
5200
5201 return OK;
5202}
5203
5204/*
5205 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005206 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005208 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209list_alloc()
5210{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005211 list_T *l;
5212
5213 l = (list_T *)alloc_clear(sizeof(list_T));
5214 if (l != NULL)
5215 {
5216 /* Prepend the list to the list of lists for garbage collection. */
5217 if (first_list != NULL)
5218 first_list->lv_used_prev = l;
5219 l->lv_used_prev = NULL;
5220 l->lv_used_next = first_list;
5221 first_list = l;
5222 }
5223 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224}
5225
5226/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005227 * Allocate an empty list for a return value.
5228 * Returns OK or FAIL.
5229 */
5230 static int
5231rettv_list_alloc(rettv)
5232 typval_T *rettv;
5233{
5234 list_T *l = list_alloc();
5235
5236 if (l == NULL)
5237 return FAIL;
5238
5239 rettv->vval.v_list = l;
5240 rettv->v_type = VAR_LIST;
5241 ++l->lv_refcount;
5242 return OK;
5243}
5244
5245/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 * Unreference a list: decrement the reference count and free it when it
5247 * becomes zero.
5248 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005249 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005251 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005253 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5254 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255}
5256
5257/*
5258 * Free a list, including all items it points to.
5259 * Ignores the reference count.
5260 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005261 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264{
Bram Moolenaar33570922005-01-25 22:26:29 +00005265 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266
Bram Moolenaard9fba312005-06-26 22:34:35 +00005267 /* Avoid that recursive reference to the list frees us again. */
5268 l->lv_refcount = DEL_REFCOUNT;
5269
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270 /* Remove the list from the list of lists for garbage collection. */
5271 if (l->lv_used_prev == NULL)
5272 first_list = l->lv_used_next;
5273 else
5274 l->lv_used_prev->lv_used_next = l->lv_used_next;
5275 if (l->lv_used_next != NULL)
5276 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5277
Bram Moolenaard9fba312005-06-26 22:34:35 +00005278 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005280 /* Remove the item before deleting it. */
5281 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 listitem_free(item);
5283 }
5284 vim_free(l);
5285}
5286
5287/*
5288 * Allocate a list item.
5289 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005290 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291listitem_alloc()
5292{
Bram Moolenaar33570922005-01-25 22:26:29 +00005293 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294}
5295
5296/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005297 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 */
5299 static void
5300listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005301 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 vim_free(item);
5305}
5306
5307/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005308 * Remove a list item from a List and free it. Also clears the value.
5309 */
5310 static void
5311listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005312 list_T *l;
5313 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005314{
5315 list_remove(l, item, item);
5316 listitem_free(item);
5317}
5318
5319/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 * Get the number of items in a list.
5321 */
5322 static long
5323list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 if (l == NULL)
5327 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005328 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329}
5330
5331/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005332 * Return TRUE when two lists have exactly the same values.
5333 */
5334 static int
5335list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 list_T *l1;
5337 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005338 int ic; /* ignore case for strings */
5339{
Bram Moolenaar33570922005-01-25 22:26:29 +00005340 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005341
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005342 if (list_len(l1) != list_len(l2))
5343 return FALSE;
5344
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345 for (item1 = l1->lv_first, item2 = l2->lv_first;
5346 item1 != NULL && item2 != NULL;
5347 item1 = item1->li_next, item2 = item2->li_next)
5348 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5349 return FALSE;
5350 return item1 == NULL && item2 == NULL;
5351}
5352
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005353#if defined(FEAT_PYTHON) || defined(PROTO)
5354/*
5355 * Return the dictitem that an entry in a hashtable points to.
5356 */
5357 dictitem_T *
5358dict_lookup(hi)
5359 hashitem_T *hi;
5360{
5361 return HI2DI(hi);
5362}
5363#endif
5364
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005365/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005366 * Return TRUE when two dictionaries have exactly the same key/values.
5367 */
5368 static int
5369dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005370 dict_T *d1;
5371 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005372 int ic; /* ignore case for strings */
5373{
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 hashitem_T *hi;
5375 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005376 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005377
5378 if (dict_len(d1) != dict_len(d2))
5379 return FALSE;
5380
Bram Moolenaar33570922005-01-25 22:26:29 +00005381 todo = d1->dv_hashtab.ht_used;
5382 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005384 if (!HASHITEM_EMPTY(hi))
5385 {
5386 item2 = dict_find(d2, hi->hi_key, -1);
5387 if (item2 == NULL)
5388 return FALSE;
5389 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5390 return FALSE;
5391 --todo;
5392 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005393 }
5394 return TRUE;
5395}
5396
5397/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005398 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005399 * Compares the items just like "==" would compare them, but strings and
5400 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005401 */
5402 static int
5403tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005404 typval_T *tv1;
5405 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005406 int ic; /* ignore case */
5407{
5408 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005409 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005410
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005411 if (tv1->v_type != tv2->v_type)
5412 return FALSE;
5413
5414 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005415 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005416 case VAR_LIST:
5417 /* recursive! */
5418 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5419
5420 case VAR_DICT:
5421 /* recursive! */
5422 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5423
5424 case VAR_FUNC:
5425 return (tv1->vval.v_string != NULL
5426 && tv2->vval.v_string != NULL
5427 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5428
5429 case VAR_NUMBER:
5430 return tv1->vval.v_number == tv2->vval.v_number;
5431
5432 case VAR_STRING:
5433 s1 = get_tv_string_buf(tv1, buf1);
5434 s2 = get_tv_string_buf(tv2, buf2);
5435 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005436 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005437
5438 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005439 return TRUE;
5440}
5441
5442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 * Locate item with index "n" in list "l" and return it.
5444 * A negative index is counted from the end; -1 is the last item.
5445 * Returns NULL when "n" is out of range.
5446 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 long n;
5451{
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 long idx;
5454
5455 if (l == NULL)
5456 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005457
5458 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005460 n = l->lv_len + n;
5461
5462 /* Check for index out of range. */
5463 if (n < 0 || n >= l->lv_len)
5464 return NULL;
5465
5466 /* When there is a cached index may start search from there. */
5467 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005469 if (n < l->lv_idx / 2)
5470 {
5471 /* closest to the start of the list */
5472 item = l->lv_first;
5473 idx = 0;
5474 }
5475 else if (n > (l->lv_idx + l->lv_len) / 2)
5476 {
5477 /* closest to the end of the list */
5478 item = l->lv_last;
5479 idx = l->lv_len - 1;
5480 }
5481 else
5482 {
5483 /* closest to the cached index */
5484 item = l->lv_idx_item;
5485 idx = l->lv_idx;
5486 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488 else
5489 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005490 if (n < l->lv_len / 2)
5491 {
5492 /* closest to the start of the list */
5493 item = l->lv_first;
5494 idx = 0;
5495 }
5496 else
5497 {
5498 /* closest to the end of the list */
5499 item = l->lv_last;
5500 idx = l->lv_len - 1;
5501 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005503
5504 while (n > idx)
5505 {
5506 /* search forward */
5507 item = item->li_next;
5508 ++idx;
5509 }
5510 while (n < idx)
5511 {
5512 /* search backward */
5513 item = item->li_prev;
5514 --idx;
5515 }
5516
5517 /* cache the used index */
5518 l->lv_idx = idx;
5519 l->lv_idx_item = item;
5520
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 return item;
5522}
5523
5524/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005525 * Locate "item" list "l" and return its index.
5526 * Returns -1 when "item" is not in the list.
5527 */
5528 static long
5529list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005530 list_T *l;
5531 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005532{
5533 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005534 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005535
5536 if (l == NULL)
5537 return -1;
5538 idx = 0;
5539 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5540 ++idx;
5541 if (li == NULL)
5542 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005543 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005544}
5545
5546/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 * Append item "item" to the end of list "l".
5548 */
5549 static void
5550list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 list_T *l;
5552 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553{
5554 if (l->lv_last == NULL)
5555 {
5556 /* empty list */
5557 l->lv_first = item;
5558 l->lv_last = item;
5559 item->li_prev = NULL;
5560 }
5561 else
5562 {
5563 l->lv_last->li_next = item;
5564 item->li_prev = l->lv_last;
5565 l->lv_last = item;
5566 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005567 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 item->li_next = NULL;
5569}
5570
5571/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005573 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 */
5575 static int
5576list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005577 list_T *l;
5578 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005580 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581
Bram Moolenaar05159a02005-02-26 23:04:13 +00005582 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005584 copy_tv(tv, &li->li_tv);
5585 list_append(l, li);
5586 return OK;
5587}
5588
5589/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005590 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005591 * Return FAIL when out of memory.
5592 */
5593 int
5594list_append_dict(list, dict)
5595 list_T *list;
5596 dict_T *dict;
5597{
5598 listitem_T *li = listitem_alloc();
5599
5600 if (li == NULL)
5601 return FAIL;
5602 li->li_tv.v_type = VAR_DICT;
5603 li->li_tv.v_lock = 0;
5604 li->li_tv.vval.v_dict = dict;
5605 list_append(list, li);
5606 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 return OK;
5608}
5609
5610/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005611 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005612 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005613 * Returns FAIL when out of memory.
5614 */
5615 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005616list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005617 list_T *l;
5618 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005619 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005620{
5621 listitem_T *li = listitem_alloc();
5622
5623 if (li == NULL)
5624 return FAIL;
5625 list_append(l, li);
5626 li->li_tv.v_type = VAR_STRING;
5627 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005628 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5629 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005630 return FAIL;
5631 return OK;
5632}
5633
5634/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005635 * Append "n" to list "l".
5636 * Returns FAIL when out of memory.
5637 */
5638 static int
5639list_append_number(l, n)
5640 list_T *l;
5641 varnumber_T n;
5642{
5643 listitem_T *li;
5644
5645 li = listitem_alloc();
5646 if (li == NULL)
5647 return FAIL;
5648 li->li_tv.v_type = VAR_NUMBER;
5649 li->li_tv.v_lock = 0;
5650 li->li_tv.vval.v_number = n;
5651 list_append(l, li);
5652 return OK;
5653}
5654
5655/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005656 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657 * If "item" is NULL append at the end.
5658 * Return FAIL when out of memory.
5659 */
5660 static int
5661list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005662 list_T *l;
5663 typval_T *tv;
5664 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005665{
Bram Moolenaar33570922005-01-25 22:26:29 +00005666 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005667
5668 if (ni == NULL)
5669 return FAIL;
5670 copy_tv(tv, &ni->li_tv);
5671 if (item == NULL)
5672 /* Append new item at end of list. */
5673 list_append(l, ni);
5674 else
5675 {
5676 /* Insert new item before existing item. */
5677 ni->li_prev = item->li_prev;
5678 ni->li_next = item;
5679 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005680 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005681 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005682 ++l->lv_idx;
5683 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005684 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005685 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005686 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005687 l->lv_idx_item = NULL;
5688 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005689 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005690 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005691 }
5692 return OK;
5693}
5694
5695/*
5696 * Extend "l1" with "l2".
5697 * If "bef" is NULL append at the end, otherwise insert before this item.
5698 * Returns FAIL when out of memory.
5699 */
5700 static int
5701list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 list_T *l1;
5703 list_T *l2;
5704 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705{
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005707
5708 for (item = l2->lv_first; item != NULL; item = item->li_next)
5709 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5710 return FAIL;
5711 return OK;
5712}
5713
5714/*
5715 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5716 * Return FAIL when out of memory.
5717 */
5718 static int
5719list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005720 list_T *l1;
5721 list_T *l2;
5722 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005723{
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005725
5726 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005727 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005728 if (l == NULL)
5729 return FAIL;
5730 tv->v_type = VAR_LIST;
5731 tv->vval.v_list = l;
5732
5733 /* append all items from the second list */
5734 return list_extend(l, l2, NULL);
5735}
5736
5737/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005738 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005740 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 * Returns NULL when out of memory.
5742 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005743 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005744list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005745 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005747 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748{
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *copy;
5750 listitem_T *item;
5751 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752
5753 if (orig == NULL)
5754 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755
5756 copy = list_alloc();
5757 if (copy != NULL)
5758 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005759 if (copyID != 0)
5760 {
5761 /* Do this before adding the items, because one of the items may
5762 * refer back to this list. */
5763 orig->lv_copyID = copyID;
5764 orig->lv_copylist = copy;
5765 }
5766 for (item = orig->lv_first; item != NULL && !got_int;
5767 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 {
5769 ni = listitem_alloc();
5770 if (ni == NULL)
5771 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005772 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005773 {
5774 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5775 {
5776 vim_free(ni);
5777 break;
5778 }
5779 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005781 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 list_append(copy, ni);
5783 }
5784 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005785 if (item != NULL)
5786 {
5787 list_unref(copy);
5788 copy = NULL;
5789 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 }
5791
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 return copy;
5793}
5794
5795/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005796 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005797 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005799 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005800list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 list_T *l;
5802 listitem_T *item;
5803 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005804{
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005807 /* notify watchers */
5808 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005810 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005811 list_fix_watch(l, ip);
5812 if (ip == item2)
5813 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005815
5816 if (item2->li_next == NULL)
5817 l->lv_last = item->li_prev;
5818 else
5819 item2->li_next->li_prev = item->li_prev;
5820 if (item->li_prev == NULL)
5821 l->lv_first = item2->li_next;
5822 else
5823 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005824 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825}
5826
5827/*
5828 * Return an allocated string with the string representation of a list.
5829 * May return NULL.
5830 */
5831 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005832list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005834 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835{
5836 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837
5838 if (tv->vval.v_list == NULL)
5839 return NULL;
5840 ga_init2(&ga, (int)sizeof(char), 80);
5841 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005842 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005843 {
5844 vim_free(ga.ga_data);
5845 return NULL;
5846 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 ga_append(&ga, ']');
5848 ga_append(&ga, NUL);
5849 return (char_u *)ga.ga_data;
5850}
5851
5852/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 * Join list "l" into a string in "*gap", using separator "sep".
5854 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005855 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005857 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005858list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 char_u *sep;
5862 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005863 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864{
5865 int first = TRUE;
5866 char_u *tofree;
5867 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 char_u *s;
5870
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005871 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
5873 if (first)
5874 first = FALSE;
5875 else
5876 ga_concat(gap, sep);
5877
5878 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005879 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005881 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 if (s != NULL)
5883 ga_concat(gap, s);
5884 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005885 if (s == NULL)
5886 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005888 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889}
5890
5891/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 * Garbage collection for lists and dictionaries.
5893 *
5894 * We use reference counts to be able to free most items right away when they
5895 * are no longer used. But for composite items it's possible that it becomes
5896 * unused while the reference count is > 0: When there is a recursive
5897 * reference. Example:
5898 * :let l = [1, 2, 3]
5899 * :let d = {9: l}
5900 * :let l[1] = d
5901 *
5902 * Since this is quite unusual we handle this with garbage collection: every
5903 * once in a while find out which lists and dicts are not referenced from any
5904 * variable.
5905 *
5906 * Here is a good reference text about garbage collection (refers to Python
5907 * but it applies to all reference-counting mechanisms):
5908 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005909 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005910
5911/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005912 * Do garbage collection for lists and dicts.
5913 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005914 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005915 int
5916garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005917{
5918 dict_T *dd;
5919 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005920 int copyID = ++current_copyID;
5921 buf_T *buf;
5922 win_T *wp;
5923 int i;
5924 funccall_T *fc;
5925 int did_free = FALSE;
5926
5927 /*
5928 * 1. Go through all accessible variables and mark all lists and dicts
5929 * with copyID.
5930 */
5931 /* script-local variables */
5932 for (i = 1; i <= ga_scripts.ga_len; ++i)
5933 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5934
5935 /* buffer-local variables */
5936 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5937 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5938
5939 /* window-local variables */
5940 FOR_ALL_WINDOWS(wp)
5941 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5942
5943 /* global variables */
5944 set_ref_in_ht(&globvarht, copyID);
5945
5946 /* function-local variables */
5947 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5948 {
5949 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5950 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5951 }
5952
5953 /*
5954 * 2. Go through the list of dicts and free items without the copyID.
5955 */
5956 for (dd = first_dict; dd != NULL; )
5957 if (dd->dv_copyID != copyID)
5958 {
5959 dict_free(dd);
5960 did_free = TRUE;
5961
5962 /* restart, next dict may also have been freed */
5963 dd = first_dict;
5964 }
5965 else
5966 dd = dd->dv_used_next;
5967
5968 /*
5969 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005970 * But don't free a list that has a watcher (used in a for loop), these
5971 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005972 */
5973 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005974 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005975 {
5976 list_free(ll);
5977 did_free = TRUE;
5978
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005979 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005980 ll = first_list;
5981 }
5982 else
5983 ll = ll->lv_used_next;
5984
5985 return did_free;
5986}
5987
5988/*
5989 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5990 */
5991 static void
5992set_ref_in_ht(ht, copyID)
5993 hashtab_T *ht;
5994 int copyID;
5995{
5996 int todo;
5997 hashitem_T *hi;
5998
5999 todo = ht->ht_used;
6000 for (hi = ht->ht_array; todo > 0; ++hi)
6001 if (!HASHITEM_EMPTY(hi))
6002 {
6003 --todo;
6004 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6005 }
6006}
6007
6008/*
6009 * Mark all lists and dicts referenced through list "l" with "copyID".
6010 */
6011 static void
6012set_ref_in_list(l, copyID)
6013 list_T *l;
6014 int copyID;
6015{
6016 listitem_T *li;
6017
6018 for (li = l->lv_first; li != NULL; li = li->li_next)
6019 set_ref_in_item(&li->li_tv, copyID);
6020}
6021
6022/*
6023 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6024 */
6025 static void
6026set_ref_in_item(tv, copyID)
6027 typval_T *tv;
6028 int copyID;
6029{
6030 dict_T *dd;
6031 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006032
6033 switch (tv->v_type)
6034 {
6035 case VAR_DICT:
6036 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 /* Didn't see this dict yet. */
6040 dd->dv_copyID = copyID;
6041 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006043 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044
6045 case VAR_LIST:
6046 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006048 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006049 /* Didn't see this list yet. */
6050 ll->lv_copyID = copyID;
6051 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006052 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006053 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006054 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006056}
6057
6058/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006059 * Allocate an empty header for a dictionary.
6060 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006061 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006062dict_alloc()
6063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006067 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006068 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069 /* Add the list to the hashtable for garbage collection. */
6070 if (first_dict != NULL)
6071 first_dict->dv_used_prev = d;
6072 d->dv_used_next = first_dict;
6073 d->dv_used_prev = NULL;
6074
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006076 d->dv_lock = 0;
6077 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006078 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006079 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006080 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006081}
6082
6083/*
6084 * Unreference a Dictionary: decrement the reference count and free it when it
6085 * becomes zero.
6086 */
6087 static void
6088dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006090{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006091 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6092 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006093}
6094
6095/*
6096 * Free a Dictionary, including all items it contains.
6097 * Ignores the reference count.
6098 */
6099 static void
6100dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006102{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006105 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006106
Bram Moolenaard9fba312005-06-26 22:34:35 +00006107 /* Avoid that recursive reference to the dict frees us again. */
6108 d->dv_refcount = DEL_REFCOUNT;
6109
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 /* Remove the dict from the list of dicts for garbage collection. */
6111 if (d->dv_used_prev == NULL)
6112 first_dict = d->dv_used_next;
6113 else
6114 d->dv_used_prev->dv_used_next = d->dv_used_next;
6115 if (d->dv_used_next != NULL)
6116 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6117
6118 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006119 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 todo = d->dv_hashtab.ht_used;
6121 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 if (!HASHITEM_EMPTY(hi))
6124 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006125 /* Remove the item before deleting it, just in case there is
6126 * something recursive causing trouble. */
6127 di = HI2DI(hi);
6128 hash_remove(&d->dv_hashtab, hi);
6129 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006130 --todo;
6131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006132 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006134 vim_free(d);
6135}
6136
6137/*
6138 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 * The "key" is copied to the new item.
6140 * Note that the value of the item "di_tv" still needs to be initialized!
6141 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006142 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006144dictitem_alloc(key)
6145 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006146{
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006151 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006153 di->di_flags = 0;
6154 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006156}
6157
6158/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 * Make a copy of a Dictionary item.
6160 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006164{
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006166
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006168 if (di != NULL)
6169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006170 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006171 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006172 copy_tv(&org->di_tv, &di->di_tv);
6173 }
6174 return di;
6175}
6176
6177/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006178 * Remove item "item" from Dictionary "dict" and free it.
6179 */
6180 static void
6181dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 dict_T *dict;
6183 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184{
Bram Moolenaar33570922005-01-25 22:26:29 +00006185 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006188 if (HASHITEM_EMPTY(hi))
6189 EMSG2(_(e_intern2), "dictitem_remove()");
6190 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006191 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006192 dictitem_free(item);
6193}
6194
6195/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006196 * Free a dict item. Also clears the value.
6197 */
6198 static void
6199dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006201{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006202 clear_tv(&item->di_tv);
6203 vim_free(item);
6204}
6205
6206/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6208 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006209 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006210 * Returns NULL when out of memory.
6211 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006213dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006215 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006216 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006217{
Bram Moolenaar33570922005-01-25 22:26:29 +00006218 dict_T *copy;
6219 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006220 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006222
6223 if (orig == NULL)
6224 return NULL;
6225
6226 copy = dict_alloc();
6227 if (copy != NULL)
6228 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006229 if (copyID != 0)
6230 {
6231 orig->dv_copyID = copyID;
6232 orig->dv_copydict = copy;
6233 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006235 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006236 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006237 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006239 --todo;
6240
6241 di = dictitem_alloc(hi->hi_key);
6242 if (di == NULL)
6243 break;
6244 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006245 {
6246 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6247 copyID) == FAIL)
6248 {
6249 vim_free(di);
6250 break;
6251 }
6252 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253 else
6254 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6255 if (dict_add(copy, di) == FAIL)
6256 {
6257 dictitem_free(di);
6258 break;
6259 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006261 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006262
Bram Moolenaare9a41262005-01-15 22:18:47 +00006263 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006264 if (todo > 0)
6265 {
6266 dict_unref(copy);
6267 copy = NULL;
6268 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006269 }
6270
6271 return copy;
6272}
6273
6274/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006275 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006276 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006277 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006279dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 dict_T *d;
6281 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006282{
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284}
6285
Bram Moolenaar8c711452005-01-14 21:53:12 +00006286/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006287 * Add a number or string entry to dictionary "d".
6288 * When "str" is NULL use number "nr", otherwise use "str".
6289 * Returns FAIL when out of memory and when key already exists.
6290 */
6291 int
6292dict_add_nr_str(d, key, nr, str)
6293 dict_T *d;
6294 char *key;
6295 long nr;
6296 char_u *str;
6297{
6298 dictitem_T *item;
6299
6300 item = dictitem_alloc((char_u *)key);
6301 if (item == NULL)
6302 return FAIL;
6303 item->di_tv.v_lock = 0;
6304 if (str == NULL)
6305 {
6306 item->di_tv.v_type = VAR_NUMBER;
6307 item->di_tv.vval.v_number = nr;
6308 }
6309 else
6310 {
6311 item->di_tv.v_type = VAR_STRING;
6312 item->di_tv.vval.v_string = vim_strsave(str);
6313 }
6314 if (dict_add(d, item) == FAIL)
6315 {
6316 dictitem_free(item);
6317 return FAIL;
6318 }
6319 return OK;
6320}
6321
6322/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006323 * Get the number of items in a Dictionary.
6324 */
6325 static long
6326dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006328{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329 if (d == NULL)
6330 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006332}
6333
6334/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006335 * Find item "key[len]" in Dictionary "d".
6336 * If "len" is negative use strlen(key).
6337 * Returns NULL when not found.
6338 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006340dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 char_u *key;
6343 int len;
6344{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345#define AKEYLEN 200
6346 char_u buf[AKEYLEN];
6347 char_u *akey;
6348 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 if (len < 0)
6352 akey = key;
6353 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006355 tofree = akey = vim_strnsave(key, len);
6356 if (akey == NULL)
6357 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006358 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006359 else
6360 {
6361 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006362 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363 akey = buf;
6364 }
6365
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367 vim_free(tofree);
6368 if (HASHITEM_EMPTY(hi))
6369 return NULL;
6370 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371}
6372
6373/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006374 * Get a string item from a dictionary.
6375 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006376 * Returns NULL if the entry doesn't exist or out of memory.
6377 */
6378 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006379get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006380 dict_T *d;
6381 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006382 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006383{
6384 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006385 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006386
6387 di = dict_find(d, key, -1);
6388 if (di == NULL)
6389 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006390 s = get_tv_string(&di->di_tv);
6391 if (save && s != NULL)
6392 s = vim_strsave(s);
6393 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006394}
6395
6396/*
6397 * Get a number item from a dictionary.
6398 * Returns 0 if the entry doesn't exist or out of memory.
6399 */
6400 long
6401get_dict_number(d, key)
6402 dict_T *d;
6403 char_u *key;
6404{
6405 dictitem_T *di;
6406
6407 di = dict_find(d, key, -1);
6408 if (di == NULL)
6409 return 0;
6410 return get_tv_number(&di->di_tv);
6411}
6412
6413/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414 * Return an allocated string with the string representation of a Dictionary.
6415 * May return NULL.
6416 */
6417 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006418dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006420 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421{
6422 garray_T ga;
6423 int first = TRUE;
6424 char_u *tofree;
6425 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006429 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006431 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006432 return NULL;
6433 ga_init2(&ga, (int)sizeof(char), 80);
6434 ga_append(&ga, '{');
6435
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006437 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006441 --todo;
6442
6443 if (first)
6444 first = FALSE;
6445 else
6446 ga_concat(&ga, (char_u *)", ");
6447
6448 tofree = string_quote(hi->hi_key, FALSE);
6449 if (tofree != NULL)
6450 {
6451 ga_concat(&ga, tofree);
6452 vim_free(tofree);
6453 }
6454 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006455 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006456 if (s != NULL)
6457 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459 if (s == NULL)
6460 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006462 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006463 if (todo > 0)
6464 {
6465 vim_free(ga.ga_data);
6466 return NULL;
6467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006468
6469 ga_append(&ga, '}');
6470 ga_append(&ga, NUL);
6471 return (char_u *)ga.ga_data;
6472}
6473
6474/*
6475 * Allocate a variable for a Dictionary and fill it from "*arg".
6476 * Return OK or FAIL. Returns NOTDONE for {expr}.
6477 */
6478 static int
6479get_dict_tv(arg, rettv, evaluate)
6480 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006481 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482 int evaluate;
6483{
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 dict_T *d = NULL;
6485 typval_T tvkey;
6486 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006489 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491
6492 /*
6493 * First check if it's not a curly-braces thing: {expr}.
6494 * Must do this without evaluating, otherwise a function may be called
6495 * twice. Unfortunately this means we need to call eval1() twice for the
6496 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006497 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 if (*start != '}')
6500 {
6501 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6502 return FAIL;
6503 if (*start == '}')
6504 return NOTDONE;
6505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506
6507 if (evaluate)
6508 {
6509 d = dict_alloc();
6510 if (d == NULL)
6511 return FAIL;
6512 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 tvkey.v_type = VAR_UNKNOWN;
6514 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006515
6516 *arg = skipwhite(*arg + 1);
6517 while (**arg != '}' && **arg != NUL)
6518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006519 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006520 goto failret;
6521 if (**arg != ':')
6522 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006523 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006524 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 goto failret;
6526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006527 key = get_tv_string_buf_chk(&tvkey, buf);
6528 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006530 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6531 if (key != NULL)
6532 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006533 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006534 goto failret;
6535 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006536
6537 *arg = skipwhite(*arg + 1);
6538 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6539 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006540 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006541 goto failret;
6542 }
6543 if (evaluate)
6544 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006545 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006546 if (item != NULL)
6547 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006548 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006549 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006550 clear_tv(&tv);
6551 goto failret;
6552 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006553 item = dictitem_alloc(key);
6554 clear_tv(&tvkey);
6555 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006558 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006559 if (dict_add(d, item) == FAIL)
6560 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561 }
6562 }
6563
6564 if (**arg == '}')
6565 break;
6566 if (**arg != ',')
6567 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006568 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006569 goto failret;
6570 }
6571 *arg = skipwhite(*arg + 1);
6572 }
6573
6574 if (**arg != '}')
6575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006576 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006577failret:
6578 if (evaluate)
6579 dict_free(d);
6580 return FAIL;
6581 }
6582
6583 *arg = skipwhite(*arg + 1);
6584 if (evaluate)
6585 {
6586 rettv->v_type = VAR_DICT;
6587 rettv->vval.v_dict = d;
6588 ++d->dv_refcount;
6589 }
6590
6591 return OK;
6592}
6593
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 * Return a string with the string representation of a variable.
6596 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006598 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006599 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 * May return NULL;
6601 */
6602 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006603echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006607 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 static int recurse = 0;
6610 char_u *r = NULL;
6611
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006613 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006614 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 *tofree = NULL;
6616 return NULL;
6617 }
6618 ++recurse;
6619
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 switch (tv->v_type)
6621 {
6622 case VAR_FUNC:
6623 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006624 r = tv->vval.v_string;
6625 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006628 if (tv->vval.v_list == NULL)
6629 {
6630 *tofree = NULL;
6631 r = NULL;
6632 }
6633 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6634 {
6635 *tofree = NULL;
6636 r = (char_u *)"[...]";
6637 }
6638 else
6639 {
6640 tv->vval.v_list->lv_copyID = copyID;
6641 *tofree = list2string(tv, copyID);
6642 r = *tofree;
6643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006644 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006645
Bram Moolenaar8c711452005-01-14 21:53:12 +00006646 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006647 if (tv->vval.v_dict == NULL)
6648 {
6649 *tofree = NULL;
6650 r = NULL;
6651 }
6652 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6653 {
6654 *tofree = NULL;
6655 r = (char_u *)"{...}";
6656 }
6657 else
6658 {
6659 tv->vval.v_dict->dv_copyID = copyID;
6660 *tofree = dict2string(tv, copyID);
6661 r = *tofree;
6662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006665 case VAR_STRING:
6666 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006667 *tofree = NULL;
6668 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006671 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006672 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006673 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006675
6676 --recurse;
6677 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006678}
6679
6680/*
6681 * Return a string with the string representation of a variable.
6682 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6683 * "numbuf" is used for a number.
6684 * Puts quotes around strings, so that they can be parsed back by eval().
6685 * May return NULL;
6686 */
6687 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006688tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006690 char_u **tofree;
6691 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006692 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693{
6694 switch (tv->v_type)
6695 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696 case VAR_FUNC:
6697 *tofree = string_quote(tv->vval.v_string, TRUE);
6698 return *tofree;
6699 case VAR_STRING:
6700 *tofree = string_quote(tv->vval.v_string, FALSE);
6701 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006702 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006704 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006705 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006707 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006708 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006709 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006710}
6711
6712/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006713 * Return string "str" in ' quotes, doubling ' characters.
6714 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716 */
6717 static char_u *
6718string_quote(str, function)
6719 char_u *str;
6720 int function;
6721{
Bram Moolenaar33570922005-01-25 22:26:29 +00006722 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 char_u *p, *r, *s;
6724
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 len = (function ? 13 : 3);
6726 if (str != NULL)
6727 {
6728 len += STRLEN(str);
6729 for (p = str; *p != NUL; mb_ptr_adv(p))
6730 if (*p == '\'')
6731 ++len;
6732 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 s = r = alloc(len);
6734 if (r != NULL)
6735 {
6736 if (function)
6737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006738 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739 r += 10;
6740 }
6741 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006742 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 if (str != NULL)
6744 for (p = str; *p != NUL; )
6745 {
6746 if (*p == '\'')
6747 *r++ = '\'';
6748 MB_COPY_CHAR(p, r);
6749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006751 if (function)
6752 *r++ = ')';
6753 *r++ = NUL;
6754 }
6755 return s;
6756}
6757
6758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 * Get the value of an environment variable.
6760 * "arg" is pointing to the '$'. It is advanced to after the name.
6761 * If the environment variable was not set, silently assume it is empty.
6762 * Always return OK.
6763 */
6764 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006765get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 int evaluate;
6769{
6770 char_u *string = NULL;
6771 int len;
6772 int cc;
6773 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006774 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
6776 ++*arg;
6777 name = *arg;
6778 len = get_env_len(arg);
6779 if (evaluate)
6780 {
6781 if (len != 0)
6782 {
6783 cc = name[len];
6784 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006785 /* first try vim_getenv(), fast for normal environment vars */
6786 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006788 {
6789 if (!mustfree)
6790 string = vim_strsave(string);
6791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 else
6793 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006794 if (mustfree)
6795 vim_free(string);
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* next try expanding things like $VIM and ${HOME} */
6798 string = expand_env_save(name - 1);
6799 if (string != NULL && *string == '$')
6800 {
6801 vim_free(string);
6802 string = NULL;
6803 }
6804 }
6805 name[len] = cc;
6806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 rettv->v_type = VAR_STRING;
6808 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810
6811 return OK;
6812}
6813
6814/*
6815 * Array with names and number of arguments of all internal functions
6816 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6817 */
6818static struct fst
6819{
6820 char *f_name; /* function name */
6821 char f_min_argc; /* minimal number of arguments */
6822 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006823 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825} functions[] =
6826{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006827 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {"append", 2, 2, f_append},
6829 {"argc", 0, 0, f_argc},
6830 {"argidx", 0, 0, f_argidx},
6831 {"argv", 1, 1, f_argv},
6832 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006833 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {"bufexists", 1, 1, f_bufexists},
6835 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6836 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6837 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6838 {"buflisted", 1, 1, f_buflisted},
6839 {"bufloaded", 1, 1, f_bufloaded},
6840 {"bufname", 1, 1, f_bufname},
6841 {"bufnr", 1, 1, f_bufnr},
6842 {"bufwinnr", 1, 1, f_bufwinnr},
6843 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006844 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006845 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {"char2nr", 1, 1, f_char2nr},
6847 {"cindent", 1, 1, f_cindent},
6848 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006849#if defined(FEAT_INS_EXPAND)
6850 {"complete_add", 1, 1, f_complete_add},
6851 {"complete_check", 0, 0, f_complete_check},
6852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006854 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006855 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {"cscope_connection",0,3, f_cscope_connection},
6857 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006858 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"delete", 1, 1, f_delete},
6860 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006861 {"diff_filler", 1, 1, f_diff_filler},
6862 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006863 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006865 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {"eventhandler", 0, 0, f_eventhandler},
6867 {"executable", 1, 1, f_executable},
6868 {"exists", 1, 1, f_exists},
6869 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006870 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6872 {"filereadable", 1, 1, f_filereadable},
6873 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006875 {"finddir", 1, 3, f_finddir},
6876 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {"fnamemodify", 2, 2, f_fnamemodify},
6878 {"foldclosed", 1, 1, f_foldclosed},
6879 {"foldclosedend", 1, 1, f_foldclosedend},
6880 {"foldlevel", 1, 1, f_foldlevel},
6881 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006882 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006884 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006886 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006887 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {"getbufvar", 2, 2, f_getbufvar},
6889 {"getchar", 0, 1, f_getchar},
6890 {"getcharmod", 0, 0, f_getcharmod},
6891 {"getcmdline", 0, 0, f_getcmdline},
6892 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006893 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006895 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006896 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {"getfsize", 1, 1, f_getfsize},
6898 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006899 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006900 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006901 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006902 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006903 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"getregtype", 0, 1, f_getregtype},
6905 {"getwinposx", 0, 0, f_getwinposx},
6906 {"getwinposy", 0, 0, f_getwinposy},
6907 {"getwinvar", 2, 2, f_getwinvar},
6908 {"glob", 1, 1, f_glob},
6909 {"globpath", 2, 2, f_globpath},
6910 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006911 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {"hasmapto", 1, 2, f_hasmapto},
6913 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6914 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6915 {"histadd", 2, 2, f_histadd},
6916 {"histdel", 1, 2, f_histdel},
6917 {"histget", 1, 2, f_histget},
6918 {"histnr", 1, 1, f_histnr},
6919 {"hlID", 1, 1, f_hlID},
6920 {"hlexists", 1, 1, f_hlexists},
6921 {"hostname", 0, 0, f_hostname},
6922 {"iconv", 3, 3, f_iconv},
6923 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006924 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006925 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006927 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {"inputrestore", 0, 0, f_inputrestore},
6929 {"inputsave", 0, 0, f_inputsave},
6930 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006931 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006933 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006935 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006938 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {"libcall", 3, 3, f_libcall},
6940 {"libcallnr", 3, 3, f_libcallnr},
6941 {"line", 1, 1, f_line},
6942 {"line2byte", 1, 1, f_line2byte},
6943 {"lispindent", 1, 1, f_lispindent},
6944 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006945 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {"maparg", 1, 2, f_maparg},
6947 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006948 {"match", 2, 4, f_match},
6949 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006950 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006951 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006952 {"max", 1, 1, f_max},
6953 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006954#ifdef vim_mkdir
6955 {"mkdir", 1, 3, f_mkdir},
6956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {"mode", 0, 0, f_mode},
6958 {"nextnonblank", 1, 1, f_nextnonblank},
6959 {"nr2char", 1, 1, f_nr2char},
6960 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006961 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006962 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006963 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006964 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {"remote_expr", 2, 3, f_remote_expr},
6966 {"remote_foreground", 1, 1, f_remote_foreground},
6967 {"remote_peek", 1, 2, f_remote_peek},
6968 {"remote_read", 1, 1, f_remote_read},
6969 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006970 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006972 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006974 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006975 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006976 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006977 {"searchpair", 3, 6, f_searchpair},
6978 {"searchpairpos", 3, 6, f_searchpairpos},
6979 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {"server2client", 2, 2, f_server2client},
6981 {"serverlist", 0, 0, f_serverlist},
6982 {"setbufvar", 3, 3, f_setbufvar},
6983 {"setcmdpos", 1, 1, f_setcmdpos},
6984 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006985 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006986 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 {"setreg", 2, 3, f_setreg},
6988 {"setwinvar", 3, 3, f_setwinvar},
6989 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006990 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006991 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006992 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006993 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006994 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995#ifdef HAVE_STRFTIME
6996 {"strftime", 1, 2, f_strftime},
6997#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006998 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006999 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {"strlen", 1, 1, f_strlen},
7001 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007002 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"strtrans", 1, 1, f_strtrans},
7004 {"submatch", 1, 1, f_submatch},
7005 {"substitute", 4, 4, f_substitute},
7006 {"synID", 3, 3, f_synID},
7007 {"synIDattr", 2, 3, f_synIDattr},
7008 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007009 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007010 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007011 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007012 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007013 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007014 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007016 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 {"tolower", 1, 1, f_tolower},
7018 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007019 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {"virtcol", 1, 1, f_virtcol},
7023 {"visualmode", 0, 1, f_visualmode},
7024 {"winbufnr", 1, 1, f_winbufnr},
7025 {"wincol", 0, 0, f_wincol},
7026 {"winheight", 1, 1, f_winheight},
7027 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007028 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"winrestcmd", 0, 0, f_winrestcmd},
7030 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007031 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032};
7033
7034#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7035
7036/*
7037 * Function given to ExpandGeneric() to obtain the list of internal
7038 * or user defined function names.
7039 */
7040 char_u *
7041get_function_name(xp, idx)
7042 expand_T *xp;
7043 int idx;
7044{
7045 static int intidx = -1;
7046 char_u *name;
7047
7048 if (idx == 0)
7049 intidx = -1;
7050 if (intidx < 0)
7051 {
7052 name = get_user_func_name(xp, idx);
7053 if (name != NULL)
7054 return name;
7055 }
7056 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7057 {
7058 STRCPY(IObuff, functions[intidx].f_name);
7059 STRCAT(IObuff, "(");
7060 if (functions[intidx].f_max_argc == 0)
7061 STRCAT(IObuff, ")");
7062 return IObuff;
7063 }
7064
7065 return NULL;
7066}
7067
7068/*
7069 * Function given to ExpandGeneric() to obtain the list of internal or
7070 * user defined variable or function names.
7071 */
7072/*ARGSUSED*/
7073 char_u *
7074get_expr_name(xp, idx)
7075 expand_T *xp;
7076 int idx;
7077{
7078 static int intidx = -1;
7079 char_u *name;
7080
7081 if (idx == 0)
7082 intidx = -1;
7083 if (intidx < 0)
7084 {
7085 name = get_function_name(xp, idx);
7086 if (name != NULL)
7087 return name;
7088 }
7089 return get_user_var_name(xp, ++intidx);
7090}
7091
7092#endif /* FEAT_CMDL_COMPL */
7093
7094/*
7095 * Find internal function in table above.
7096 * Return index, or -1 if not found
7097 */
7098 static int
7099find_internal_func(name)
7100 char_u *name; /* name of the function */
7101{
7102 int first = 0;
7103 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7104 int cmp;
7105 int x;
7106
7107 /*
7108 * Find the function name in the table. Binary search.
7109 */
7110 while (first <= last)
7111 {
7112 x = first + ((unsigned)(last - first) >> 1);
7113 cmp = STRCMP(name, functions[x].f_name);
7114 if (cmp < 0)
7115 last = x - 1;
7116 else if (cmp > 0)
7117 first = x + 1;
7118 else
7119 return x;
7120 }
7121 return -1;
7122}
7123
7124/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007125 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7126 * name it contains, otherwise return "name".
7127 */
7128 static char_u *
7129deref_func_name(name, lenp)
7130 char_u *name;
7131 int *lenp;
7132{
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007134 int cc;
7135
7136 cc = name[*lenp];
7137 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007138 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007139 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007140 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007141 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007143 {
7144 *lenp = 0;
7145 return (char_u *)""; /* just in case */
7146 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 *lenp = STRLEN(v->di_tv.vval.v_string);
7148 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007149 }
7150
7151 return name;
7152}
7153
7154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 * Allocate a variable for the result of a function.
7156 * Return OK or FAIL.
7157 */
7158 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7160 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 char_u *name; /* name of the function */
7162 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 char_u **arg; /* argument, pointing to the '(' */
7165 linenr_T firstline; /* first line of range */
7166 linenr_T lastline; /* last line of range */
7167 int *doesrange; /* return: function handled range */
7168 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 char_u *argp;
7172 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 int argcount = 0; /* number of arguments found */
7175
7176 /*
7177 * Get the arguments.
7178 */
7179 argp = *arg;
7180 while (argcount < MAX_FUNC_ARGS)
7181 {
7182 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7183 if (*argp == ')' || *argp == ',' || *argp == NUL)
7184 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7186 {
7187 ret = FAIL;
7188 break;
7189 }
7190 ++argcount;
7191 if (*argp != ',')
7192 break;
7193 }
7194 if (*argp == ')')
7195 ++argp;
7196 else
7197 ret = FAIL;
7198
7199 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007200 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 {
7204 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007205 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007207 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
7210 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007211 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
7213 *arg = skipwhite(argp);
7214 return ret;
7215}
7216
7217
7218/*
7219 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007220 * Return OK when the function can't be called, FAIL otherwise.
7221 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 */
7223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007225 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 char_u *name; /* name of the function */
7227 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 linenr_T firstline; /* first line of range */
7232 linenr_T lastline; /* last line of range */
7233 int *doesrange; /* return: function handled range */
7234 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
7237 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238#define ERROR_UNKNOWN 0
7239#define ERROR_TOOMANY 1
7240#define ERROR_TOOFEW 2
7241#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242#define ERROR_DICT 4
7243#define ERROR_NONE 5
7244#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 int error = ERROR_NONE;
7246 int i;
7247 int llen;
7248 ufunc_T *fp;
7249 int cc;
7250#define FLEN_FIXED 40
7251 char_u fname_buf[FLEN_FIXED + 1];
7252 char_u *fname;
7253
7254 /*
7255 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7256 * Change <SNR>123_name() to K_SNR 123_name().
7257 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7258 */
7259 cc = name[len];
7260 name[len] = NUL;
7261 llen = eval_fname_script(name);
7262 if (llen > 0)
7263 {
7264 fname_buf[0] = K_SPECIAL;
7265 fname_buf[1] = KS_EXTRA;
7266 fname_buf[2] = (int)KE_SNR;
7267 i = 3;
7268 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7269 {
7270 if (current_SID <= 0)
7271 error = ERROR_SCRIPT;
7272 else
7273 {
7274 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7275 i = (int)STRLEN(fname_buf);
7276 }
7277 }
7278 if (i + STRLEN(name + llen) < FLEN_FIXED)
7279 {
7280 STRCPY(fname_buf + i, name + llen);
7281 fname = fname_buf;
7282 }
7283 else
7284 {
7285 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7286 if (fname == NULL)
7287 error = ERROR_OTHER;
7288 else
7289 {
7290 mch_memmove(fname, fname_buf, (size_t)i);
7291 STRCPY(fname + i, name + llen);
7292 }
7293 }
7294 }
7295 else
7296 fname = name;
7297
7298 *doesrange = FALSE;
7299
7300
7301 /* execute the function if no errors detected and executing */
7302 if (evaluate && error == ERROR_NONE)
7303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007304 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 error = ERROR_UNKNOWN;
7306
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007307 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
7309 /*
7310 * User defined function.
7311 */
7312 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007313
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007315 /* Trigger FuncUndefined event, may load the function. */
7316 if (fp == NULL
7317 && apply_autocmds(EVENT_FUNCUNDEFINED,
7318 fname, fname, TRUE, NULL)
7319 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007321 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 fp = find_func(fname);
7323 }
7324#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007325 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007326 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007327 {
7328 /* loaded a package, search for the function again */
7329 fp = find_func(fname);
7330 }
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 if (fp != NULL)
7333 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007334 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007336 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007338 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007340 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 else
7343 {
7344 /*
7345 * Call the user function.
7346 * Save and restore search patterns, script variables and
7347 * redo buffer.
7348 */
7349 save_search_patterns();
7350 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007351 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007352 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007354 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7355 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7356 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007357 /* Function was unreferenced while being used, free it
7358 * now. */
7359 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 restoreRedobuff();
7361 restore_search_patterns();
7362 error = ERROR_NONE;
7363 }
7364 }
7365 }
7366 else
7367 {
7368 /*
7369 * Find the function name in the table, call its implementation.
7370 */
7371 i = find_internal_func(fname);
7372 if (i >= 0)
7373 {
7374 if (argcount < functions[i].f_min_argc)
7375 error = ERROR_TOOFEW;
7376 else if (argcount > functions[i].f_max_argc)
7377 error = ERROR_TOOMANY;
7378 else
7379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007380 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007381 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 error = ERROR_NONE;
7383 }
7384 }
7385 }
7386 /*
7387 * The function call (or "FuncUndefined" autocommand sequence) might
7388 * have been aborted by an error, an interrupt, or an explicitly thrown
7389 * exception that has not been caught so far. This situation can be
7390 * tested for by calling aborting(). For an error in an internal
7391 * function or for the "E132" error in call_user_func(), however, the
7392 * throw point at which the "force_abort" flag (temporarily reset by
7393 * emsg()) is normally updated has not been reached yet. We need to
7394 * update that flag first to make aborting() reliable.
7395 */
7396 update_force_abort();
7397 }
7398 if (error == ERROR_NONE)
7399 ret = OK;
7400
7401 /*
7402 * Report an error unless the argument evaluation or function call has been
7403 * cancelled due to an aborting error, an interrupt, or an exception.
7404 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 if (!aborting())
7406 {
7407 switch (error)
7408 {
7409 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007410 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411 break;
7412 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007413 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 break;
7415 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007416 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 name);
7418 break;
7419 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 name);
7422 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007424 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 name);
7426 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 }
7428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
7430 name[len] = cc;
7431 if (fname != name && fname != fname_buf)
7432 vim_free(fname);
7433
7434 return ret;
7435}
7436
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437/*
7438 * Give an error message with a function name. Handle <SNR> things.
7439 */
7440 static void
7441emsg_funcname(msg, name)
7442 char *msg;
7443 char_u *name;
7444{
7445 char_u *p;
7446
7447 if (*name == K_SPECIAL)
7448 p = concat_str((char_u *)"<SNR>", name + 3);
7449 else
7450 p = name;
7451 EMSG2(_(msg), p);
7452 if (p != name)
7453 vim_free(p);
7454}
7455
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456/*********************************************
7457 * Implementation of the built-in functions
7458 */
7459
7460/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007461 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 */
7463 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007464f_add(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{
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007473 if ((l = argvars[0].vval.v_list) != NULL
7474 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7475 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007477 }
7478 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007479 EMSG(_(e_listreq));
7480}
7481
7482/*
7483 * "append(lnum, string/list)" function
7484 */
7485 static void
7486f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007487 typval_T *argvars;
7488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007489{
7490 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007491 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 list_T *l = NULL;
7493 listitem_T *li = NULL;
7494 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007495 long added = 0;
7496
Bram Moolenaar0d660222005-01-07 21:51:51 +00007497 lnum = get_tv_lnum(argvars);
7498 if (lnum >= 0
7499 && lnum <= curbuf->b_ml.ml_line_count
7500 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007501 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007502 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007503 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007504 l = argvars[1].vval.v_list;
7505 if (l == NULL)
7506 return;
7507 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007509 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007510 for (;;)
7511 {
7512 if (l == NULL)
7513 tv = &argvars[1]; /* append a string */
7514 else if (li == NULL)
7515 break; /* end of list */
7516 else
7517 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007518 line = get_tv_string_chk(tv);
7519 if (line == NULL) /* type error */
7520 {
7521 rettv->vval.v_number = 1; /* Failed */
7522 break;
7523 }
7524 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007525 ++added;
7526 if (l == NULL)
7527 break;
7528 li = li->li_next;
7529 }
7530
7531 appended_lines_mark(lnum, added);
7532 if (curwin->w_cursor.lnum > lnum)
7533 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007535 else
7536 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537}
7538
7539/*
7540 * "argc()" function
7541 */
7542/* ARGSUSED */
7543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007544f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 typval_T *argvars;
7546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007548 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549}
7550
7551/*
7552 * "argidx()" function
7553 */
7554/* ARGSUSED */
7555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007556f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007557 typval_T *argvars;
7558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007560 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561}
7562
7563/*
7564 * "argv(nr)" function
7565 */
7566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007567f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *argvars;
7569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570{
7571 int idx;
7572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007573 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007577 rettv->vval.v_string = NULL;
7578 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579}
7580
7581/*
7582 * "browse(save, title, initdir, default)" function
7583 */
7584/* ARGSUSED */
7585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 typval_T *argvars;
7588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590#ifdef FEAT_BROWSE
7591 int save;
7592 char_u *title;
7593 char_u *initdir;
7594 char_u *defname;
7595 char_u buf[NUMBUFLEN];
7596 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007597 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599 save = get_tv_number_chk(&argvars[0], &error);
7600 title = get_tv_string_chk(&argvars[1]);
7601 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7602 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007604 if (error || title == NULL || initdir == NULL || defname == NULL)
7605 rettv->vval.v_string = NULL;
7606 else
7607 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007608 do_browse(save ? BROWSE_SAVE : 0,
7609 title, defname, NULL, initdir, NULL, curbuf);
7610#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007611 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007612#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007614}
7615
7616/*
7617 * "browsedir(title, initdir)" function
7618 */
7619/* ARGSUSED */
7620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 typval_T *argvars;
7623 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007624{
7625#ifdef FEAT_BROWSE
7626 char_u *title;
7627 char_u *initdir;
7628 char_u buf[NUMBUFLEN];
7629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007630 title = get_tv_string_chk(&argvars[0]);
7631 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007633 if (title == NULL || initdir == NULL)
7634 rettv->vval.v_string = NULL;
7635 else
7636 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007637 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642}
7643
Bram Moolenaar33570922005-01-25 22:26:29 +00007644static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007645
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646/*
7647 * Find a buffer by number or exact name.
7648 */
7649 static buf_T *
7650find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007655 if (avar->v_type == VAR_NUMBER)
7656 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007657 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007659 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007660 if (buf == NULL)
7661 {
7662 /* No full path name match, try a match with a URL or a "nofile"
7663 * buffer, these don't use the full path. */
7664 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7665 if (buf->b_fname != NULL
7666 && (path_with_url(buf->b_fname)
7667#ifdef FEAT_QUICKFIX
7668 || bt_nofile(buf)
7669#endif
7670 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007671 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007672 break;
7673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675 return buf;
7676}
7677
7678/*
7679 * "bufexists(expr)" function
7680 */
7681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007683 typval_T *argvars;
7684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687}
7688
7689/*
7690 * "buflisted(expr)" function
7691 */
7692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 typval_T *argvars;
7695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 buf_T *buf;
7698
7699 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007700 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701}
7702
7703/*
7704 * "bufloaded(expr)" function
7705 */
7706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007707f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 typval_T *argvars;
7709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 buf_T *buf;
7712
7713 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715}
7716
Bram Moolenaar33570922005-01-25 22:26:29 +00007717static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007718
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719/*
7720 * Get buffer by number or pattern.
7721 */
7722 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007724 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007726 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 int save_magic;
7728 char_u *save_cpo;
7729 buf_T *buf;
7730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731 if (tv->v_type == VAR_NUMBER)
7732 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007733 if (tv->v_type != VAR_STRING)
7734 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if (name == NULL || *name == NUL)
7736 return curbuf;
7737 if (name[0] == '$' && name[1] == NUL)
7738 return lastbuf;
7739
7740 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7741 save_magic = p_magic;
7742 p_magic = TRUE;
7743 save_cpo = p_cpo;
7744 p_cpo = (char_u *)"";
7745
7746 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7747 TRUE, FALSE));
7748
7749 p_magic = save_magic;
7750 p_cpo = save_cpo;
7751
7752 /* If not found, try expanding the name, like done for bufexists(). */
7753 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 return buf;
7757}
7758
7759/*
7760 * "bufname(expr)" function
7761 */
7762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *argvars;
7765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 buf_T *buf;
7768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771 buf = get_buf_tv(&argvars[0]);
7772 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 --emsg_off;
7778}
7779
7780/*
7781 * "bufnr(expr)" function
7782 */
7783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007784f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 typval_T *argvars;
7786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 buf_T *buf;
7789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 --emsg_off;
7798}
7799
7800/*
7801 * "bufwinnr(nr)" function
7802 */
7803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 typval_T *argvars;
7806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808#ifdef FEAT_WINDOWS
7809 win_T *wp;
7810 int winnr = 0;
7811#endif
7812 buf_T *buf;
7813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007814 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817#ifdef FEAT_WINDOWS
7818 for (wp = firstwin; wp; wp = wp->w_next)
7819 {
7820 ++winnr;
7821 if (wp->w_buffer == buf)
7822 break;
7823 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007824 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827#endif
7828 --emsg_off;
7829}
7830
7831/*
7832 * "byte2line(byte)" function
7833 */
7834/*ARGSUSED*/
7835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007837 typval_T *argvars;
7838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842#else
7843 long boff = 0;
7844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007845 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 (linenr_T)0, &boff);
7851#endif
7852}
7853
7854/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007855 * "byteidx()" function
7856 */
7857/*ARGSUSED*/
7858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007859f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 typval_T *argvars;
7861 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862{
7863#ifdef FEAT_MBYTE
7864 char_u *t;
7865#endif
7866 char_u *str;
7867 long idx;
7868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869 str = get_tv_string_chk(&argvars[0]);
7870 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007873 return;
7874
7875#ifdef FEAT_MBYTE
7876 t = str;
7877 for ( ; idx > 0; idx--)
7878 {
7879 if (*t == NUL) /* EOL reached */
7880 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007881 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007884#else
7885 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007886 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007887#endif
7888}
7889
7890/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007891 * "call(func, arglist)" function
7892 */
7893 static void
7894f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 typval_T *argvars;
7896 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007897{
7898 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007900 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007901 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007902 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007904
7905 rettv->vval.v_number = 0;
7906 if (argvars[1].v_type != VAR_LIST)
7907 {
7908 EMSG(_(e_listreq));
7909 return;
7910 }
7911 if (argvars[1].vval.v_list == NULL)
7912 return;
7913
7914 if (argvars[0].v_type == VAR_FUNC)
7915 func = argvars[0].vval.v_string;
7916 else
7917 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007918 if (*func == NUL)
7919 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007920
Bram Moolenaare9a41262005-01-15 22:18:47 +00007921 if (argvars[2].v_type != VAR_UNKNOWN)
7922 {
7923 if (argvars[2].v_type != VAR_DICT)
7924 {
7925 EMSG(_(e_dictreq));
7926 return;
7927 }
7928 selfdict = argvars[2].vval.v_dict;
7929 }
7930
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007931 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7932 item = item->li_next)
7933 {
7934 if (argc == MAX_FUNC_ARGS)
7935 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007936 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007937 break;
7938 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007939 /* Make a copy of each argument. This is needed to be able to set
7940 * v_lock to VAR_FIXED in the copy without changing the original list.
7941 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007942 copy_tv(&item->li_tv, &argv[argc++]);
7943 }
7944
7945 if (item == NULL)
7946 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7948 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007949
7950 /* Free the arguments. */
7951 while (argc > 0)
7952 clear_tv(&argv[--argc]);
7953}
7954
7955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 * "char2nr(string)" function
7957 */
7958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 typval_T *argvars;
7961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963#ifdef FEAT_MBYTE
7964 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007965 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 else
7967#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969}
7970
7971/*
7972 * "cindent(lnum)" function
7973 */
7974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 typval_T *argvars;
7977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979#ifdef FEAT_CINDENT
7980 pos_T pos;
7981 linenr_T lnum;
7982
7983 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7986 {
7987 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 curwin->w_cursor = pos;
7990 }
7991 else
7992#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994}
7995
7996/*
7997 * "col(string)" function
7998 */
7999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008000f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 typval_T *argvars;
8002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
8004 colnr_T col = 0;
8005 pos_T *fp;
8006
8007 fp = var2fpos(&argvars[0], FALSE);
8008 if (fp != NULL)
8009 {
8010 if (fp->col == MAXCOL)
8011 {
8012 /* '> can be MAXCOL, get the length of the line then */
8013 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8014 col = STRLEN(ml_get(fp->lnum)) + 1;
8015 else
8016 col = MAXCOL;
8017 }
8018 else
8019 {
8020 col = fp->col + 1;
8021#ifdef FEAT_VIRTUALEDIT
8022 /* col(".") when the cursor is on the NUL at the end of the line
8023 * because of "coladd" can be seen as an extra column. */
8024 if (virtual_active() && fp == &curwin->w_cursor)
8025 {
8026 char_u *p = ml_get_cursor();
8027
8028 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8029 curwin->w_virtcol - curwin->w_cursor.coladd))
8030 {
8031# ifdef FEAT_MBYTE
8032 int l;
8033
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008034 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 col += l;
8036# else
8037 if (*p != NUL && p[1] == NUL)
8038 ++col;
8039# endif
8040 }
8041 }
8042#endif
8043 }
8044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046}
8047
Bram Moolenaar572cb562005-08-05 21:35:02 +00008048#if defined(FEAT_INS_EXPAND)
8049/*
8050 * "complete_add()" function
8051 */
8052/*ARGSUSED*/
8053 static void
8054f_complete_add(argvars, rettv)
8055 typval_T *argvars;
8056 typval_T *rettv;
8057{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008058 char_u *word;
8059 char_u *extra = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008060 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008061
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008062 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8063 {
8064 word = get_dict_string(argvars[0].vval.v_dict,
8065 (char_u *)"word", FALSE);
8066 extra = get_dict_string(argvars[0].vval.v_dict,
8067 (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008068 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008069 }
8070 else
8071 word = get_tv_string_chk(&argvars[0]);
8072 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008073 rettv->vval.v_number = ins_compl_add(word, -1, icase,
8074 NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008075}
8076
8077/*
8078 * "complete_check()" function
8079 */
8080/*ARGSUSED*/
8081 static void
8082f_complete_check(argvars, rettv)
8083 typval_T *argvars;
8084 typval_T *rettv;
8085{
8086 int saved = RedrawingDisabled;
8087
8088 RedrawingDisabled = 0;
8089 ins_compl_check_keys(0);
8090 rettv->vval.v_number = compl_interrupted;
8091 RedrawingDisabled = saved;
8092}
8093#endif
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095/*
8096 * "confirm(message, buttons[, default [, type]])" function
8097 */
8098/*ARGSUSED*/
8099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008100f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008101 typval_T *argvars;
8102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8105 char_u *message;
8106 char_u *buttons = NULL;
8107 char_u buf[NUMBUFLEN];
8108 char_u buf2[NUMBUFLEN];
8109 int def = 1;
8110 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008111 char_u *typestr;
8112 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008114 message = get_tv_string_chk(&argvars[0]);
8115 if (message == NULL)
8116 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008117 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008119 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8120 if (buttons == NULL)
8121 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008122 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008124 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008127 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8128 if (typestr == NULL)
8129 error = TRUE;
8130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008132 switch (TOUPPER_ASC(*typestr))
8133 {
8134 case 'E': type = VIM_ERROR; break;
8135 case 'Q': type = VIM_QUESTION; break;
8136 case 'I': type = VIM_INFO; break;
8137 case 'W': type = VIM_WARNING; break;
8138 case 'G': type = VIM_GENERIC; break;
8139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 }
8142 }
8143 }
8144
8145 if (buttons == NULL || *buttons == NUL)
8146 buttons = (char_u *)_("&Ok");
8147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 if (error)
8149 rettv->vval.v_number = 0;
8150 else
8151 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 def, NULL);
8153#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008154 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155#endif
8156}
8157
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158/*
8159 * "copy()" function
8160 */
8161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 typval_T *argvars;
8164 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008165{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008166 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
8169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170 * "count()" function
8171 */
8172 static void
8173f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 typval_T *argvars;
8175 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008176{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008177 long n = 0;
8178 int ic = FALSE;
8179
Bram Moolenaare9a41262005-01-15 22:18:47 +00008180 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008181 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 listitem_T *li;
8183 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008185
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 if ((l = argvars[0].vval.v_list) != NULL)
8187 {
8188 li = l->lv_first;
8189 if (argvars[2].v_type != VAR_UNKNOWN)
8190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 int error = FALSE;
8192
8193 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 if (argvars[3].v_type != VAR_UNKNOWN)
8195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008196 idx = get_tv_number_chk(&argvars[3], &error);
8197 if (!error)
8198 {
8199 li = list_find(l, idx);
8200 if (li == NULL)
8201 EMSGN(_(e_listidx), idx);
8202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008204 if (error)
8205 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008206 }
8207
8208 for ( ; li != NULL; li = li->li_next)
8209 if (tv_equal(&li->li_tv, &argvars[1], ic))
8210 ++n;
8211 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008212 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 else if (argvars[0].v_type == VAR_DICT)
8214 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008215 int todo;
8216 dict_T *d;
8217 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008219 if ((d = argvars[0].vval.v_dict) != NULL)
8220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008221 int error = FALSE;
8222
Bram Moolenaare9a41262005-01-15 22:18:47 +00008223 if (argvars[2].v_type != VAR_UNKNOWN)
8224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008225 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008226 if (argvars[3].v_type != VAR_UNKNOWN)
8227 EMSG(_(e_invarg));
8228 }
8229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008230 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008232 {
8233 if (!HASHITEM_EMPTY(hi))
8234 {
8235 --todo;
8236 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8237 ++n;
8238 }
8239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008240 }
8241 }
8242 else
8243 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008244 rettv->vval.v_number = n;
8245}
8246
8247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8249 *
8250 * Checks the existence of a cscope connection.
8251 */
8252/*ARGSUSED*/
8253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008254f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 typval_T *argvars;
8256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258#ifdef FEAT_CSCOPE
8259 int num = 0;
8260 char_u *dbpath = NULL;
8261 char_u *prepend = NULL;
8262 char_u buf[NUMBUFLEN];
8263
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008264 if (argvars[0].v_type != VAR_UNKNOWN
8265 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267 num = (int)get_tv_number(&argvars[0]);
8268 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008270 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 }
8272
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008275 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276#endif
8277}
8278
8279/*
8280 * "cursor(lnum, col)" function
8281 *
8282 * Moves the cursor to the specified line and column
8283 */
8284/*ARGSUSED*/
8285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008286f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008287 typval_T *argvars;
8288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
8290 long line, col;
8291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008293 col = get_tv_number_chk(&argvars[1], NULL);
8294 if (line < 0 || col < 0)
8295 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (line > 0)
8297 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 if (col > 0)
8299 curwin->w_cursor.col = col - 1;
8300#ifdef FEAT_VIRTUALEDIT
8301 curwin->w_cursor.coladd = 0;
8302#endif
8303
8304 /* Make sure the cursor is in a valid position. */
8305 check_cursor();
8306#ifdef FEAT_MBYTE
8307 /* Correct cursor for multi-byte character. */
8308 if (has_mbyte)
8309 mb_adjust_cursor();
8310#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008311
8312 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313}
8314
8315/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 */
8318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008320 typval_T *argvars;
8321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008323 int noref = 0;
8324
8325 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008326 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008327 if (noref < 0 || noref > 1)
8328 EMSG(_(e_invarg));
8329 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008330 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331}
8332
8333/*
8334 * "delete()" function
8335 */
8336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 typval_T *argvars;
8339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008342 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345}
8346
8347/*
8348 * "did_filetype()" function
8349 */
8350/*ARGSUSED*/
8351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008352f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 typval_T *argvars;
8354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
8356#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360#endif
8361}
8362
8363/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008364 * "diff_filler()" function
8365 */
8366/*ARGSUSED*/
8367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008371{
8372#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008374#endif
8375}
8376
8377/*
8378 * "diff_hlID()" function
8379 */
8380/*ARGSUSED*/
8381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008383 typval_T *argvars;
8384 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008385{
8386#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008388 static linenr_T prev_lnum = 0;
8389 static int changedtick = 0;
8390 static int fnum = 0;
8391 static int change_start = 0;
8392 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008393 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008394 int filler_lines;
8395 int col;
8396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008397 if (lnum < 0) /* ignore type error in {lnum} arg */
8398 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008399 if (lnum != prev_lnum
8400 || changedtick != curbuf->b_changedtick
8401 || fnum != curbuf->b_fnum)
8402 {
8403 /* New line, buffer, change: need to get the values. */
8404 filler_lines = diff_check(curwin, lnum);
8405 if (filler_lines < 0)
8406 {
8407 if (filler_lines == -1)
8408 {
8409 change_start = MAXCOL;
8410 change_end = -1;
8411 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8412 hlID = HLF_ADD; /* added line */
8413 else
8414 hlID = HLF_CHD; /* changed line */
8415 }
8416 else
8417 hlID = HLF_ADD; /* added line */
8418 }
8419 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008420 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008421 prev_lnum = lnum;
8422 changedtick = curbuf->b_changedtick;
8423 fnum = curbuf->b_fnum;
8424 }
8425
8426 if (hlID == HLF_CHD || hlID == HLF_TXD)
8427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008429 if (col >= change_start && col <= change_end)
8430 hlID = HLF_TXD; /* changed text */
8431 else
8432 hlID = HLF_CHD; /* changed line */
8433 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008434 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008435#endif
8436}
8437
8438/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008439 * "empty({expr})" function
8440 */
8441 static void
8442f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008443 typval_T *argvars;
8444 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008445{
8446 int n;
8447
8448 switch (argvars[0].v_type)
8449 {
8450 case VAR_STRING:
8451 case VAR_FUNC:
8452 n = argvars[0].vval.v_string == NULL
8453 || *argvars[0].vval.v_string == NUL;
8454 break;
8455 case VAR_NUMBER:
8456 n = argvars[0].vval.v_number == 0;
8457 break;
8458 case VAR_LIST:
8459 n = argvars[0].vval.v_list == NULL
8460 || argvars[0].vval.v_list->lv_first == NULL;
8461 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008462 case VAR_DICT:
8463 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008465 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008466 default:
8467 EMSG2(_(e_intern2), "f_empty()");
8468 n = 0;
8469 }
8470
8471 rettv->vval.v_number = n;
8472}
8473
8474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 * "escape({string}, {chars})" function
8476 */
8477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008479 typval_T *argvars;
8480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
8482 char_u buf[NUMBUFLEN];
8483
Bram Moolenaar758711c2005-02-02 23:11:38 +00008484 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8485 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008486 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487}
8488
8489/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008490 * "eval()" function
8491 */
8492/*ARGSUSED*/
8493 static void
8494f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 typval_T *argvars;
8496 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008497{
8498 char_u *s;
8499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008500 s = get_tv_string_chk(&argvars[0]);
8501 if (s != NULL)
8502 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008504 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8505 {
8506 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008507 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008509 else if (*s != NUL)
8510 EMSG(_(e_trailing));
8511}
8512
8513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 * "eventhandler()" function
8515 */
8516/*ARGSUSED*/
8517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008518f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 typval_T *argvars;
8520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
8524
8525/*
8526 * "executable()" function
8527 */
8528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008529f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 typval_T *argvars;
8531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534}
8535
8536/*
8537 * "exists()" function
8538 */
8539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 typval_T *argvars;
8542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 char_u *p;
8545 char_u *name;
8546 int n = FALSE;
8547 int len = 0;
8548
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008549 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (*p == '$') /* environment variable */
8551 {
8552 /* first try "normal" environment variables (fast) */
8553 if (mch_getenv(p + 1) != NULL)
8554 n = TRUE;
8555 else
8556 {
8557 /* try expanding things like $VIM and ${HOME} */
8558 p = expand_env_save(p);
8559 if (p != NULL && *p != '$')
8560 n = TRUE;
8561 vim_free(p);
8562 }
8563 }
8564 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 else if (*p == '*') /* internal or user defined function */
8567 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008568 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
8570 else if (*p == ':')
8571 {
8572 n = cmd_exists(p + 1);
8573 }
8574 else if (*p == '#')
8575 {
8576#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008577 if (p[1] == '#')
8578 n = autocmd_supported(p + 2);
8579 else
8580 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#endif
8582 }
8583 else /* internal variable */
8584 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008585 char_u *tofree;
8586 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008588 /* get_name_len() takes care of expanding curly braces */
8589 name = p;
8590 len = get_name_len(&p, &tofree, TRUE, FALSE);
8591 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008593 if (tofree != NULL)
8594 name = tofree;
8595 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8596 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008598 /* handle d.key, l[idx], f(expr) */
8599 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8600 if (n)
8601 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
8603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008605 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 }
8607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
8611/*
8612 * "expand()" function
8613 */
8614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 typval_T *argvars;
8617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
8619 char_u *s;
8620 int len;
8621 char_u *errormsg;
8622 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8623 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008624 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626 rettv->v_type = VAR_STRING;
8627 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 if (*s == '%' || *s == '#' || *s == '<')
8629 {
8630 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 --emsg_off;
8633 }
8634 else
8635 {
8636 /* When the optional second argument is non-zero, don't remove matches
8637 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 if (argvars[1].v_type != VAR_UNKNOWN
8639 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641 if (!error)
8642 {
8643 ExpandInit(&xpc);
8644 xpc.xp_context = EXPAND_FILES;
8645 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8646 ExpandCleanup(&xpc);
8647 }
8648 else
8649 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 }
8651}
8652
8653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008654 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008655 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008656 */
8657 static void
8658f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 typval_T *argvars;
8660 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008661{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008662 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008663 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 list_T *l1, *l2;
8666 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008667 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008668 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008669
Bram Moolenaare9a41262005-01-15 22:18:47 +00008670 l1 = argvars[0].vval.v_list;
8671 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008672 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8673 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008674 {
8675 if (argvars[2].v_type != VAR_UNKNOWN)
8676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008677 before = get_tv_number_chk(&argvars[2], &error);
8678 if (error)
8679 return; /* type error; errmsg already given */
8680
Bram Moolenaar758711c2005-02-02 23:11:38 +00008681 if (before == l1->lv_len)
8682 item = NULL;
8683 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008684 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008685 item = list_find(l1, before);
8686 if (item == NULL)
8687 {
8688 EMSGN(_(e_listidx), before);
8689 return;
8690 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008691 }
8692 }
8693 else
8694 item = NULL;
8695 list_extend(l1, l2, item);
8696
Bram Moolenaare9a41262005-01-15 22:18:47 +00008697 copy_tv(&argvars[0], rettv);
8698 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008700 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8701 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 dict_T *d1, *d2;
8703 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008704 char_u *action;
8705 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008707 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708
8709 d1 = argvars[0].vval.v_dict;
8710 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008711 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8712 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 {
8714 /* Check the third argument. */
8715 if (argvars[2].v_type != VAR_UNKNOWN)
8716 {
8717 static char *(av[]) = {"keep", "force", "error"};
8718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 action = get_tv_string_chk(&argvars[2]);
8720 if (action == NULL)
8721 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008722 for (i = 0; i < 3; ++i)
8723 if (STRCMP(action, av[i]) == 0)
8724 break;
8725 if (i == 3)
8726 {
8727 EMSGN(_(e_invarg2), action);
8728 return;
8729 }
8730 }
8731 else
8732 action = (char_u *)"force";
8733
8734 /* Go over all entries in the second dict and add them to the
8735 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 todo = d2->dv_hashtab.ht_used;
8737 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008739 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008741 --todo;
8742 di1 = dict_find(d1, hi2->hi_key, -1);
8743 if (di1 == NULL)
8744 {
8745 di1 = dictitem_copy(HI2DI(hi2));
8746 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8747 dictitem_free(di1);
8748 }
8749 else if (*action == 'e')
8750 {
8751 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8752 break;
8753 }
8754 else if (*action == 'f')
8755 {
8756 clear_tv(&di1->di_tv);
8757 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8758 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008759 }
8760 }
8761
Bram Moolenaare9a41262005-01-15 22:18:47 +00008762 copy_tv(&argvars[0], rettv);
8763 }
8764 }
8765 else
8766 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008767}
8768
8769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 * "filereadable()" function
8771 */
8772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008774 typval_T *argvars;
8775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
8777 FILE *fd;
8778 char_u *p;
8779 int n;
8780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8783 {
8784 n = TRUE;
8785 fclose(fd);
8786 }
8787 else
8788 n = FALSE;
8789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791}
8792
8793/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008794 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 * rights to write into.
8796 */
8797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008799 typval_T *argvars;
8800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008802 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803}
8804
Bram Moolenaar33570922005-01-25 22:26:29 +00008805static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008806
8807 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008808findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 typval_T *argvars;
8810 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008811 int dir;
8812{
8813#ifdef FEAT_SEARCHPATH
8814 char_u *fname;
8815 char_u *fresult = NULL;
8816 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8817 char_u *p;
8818 char_u pathbuf[NUMBUFLEN];
8819 int count = 1;
8820 int first = TRUE;
8821
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008823
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008824 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8827 if (p == NULL)
8828 count = -1; /* error */
8829 else
8830 {
8831 if (*p != NUL)
8832 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008834 if (argvars[2].v_type != VAR_UNKNOWN)
8835 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8836 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008837 }
8838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 do
8842 {
8843 vim_free(fresult);
8844 fresult = find_file_in_path_option(first ? fname : NULL,
8845 first ? (int)STRLEN(fname) : 0,
8846 0, first, path, dir, NULL);
8847 first = FALSE;
8848 } while (--count > 0 && fresult != NULL);
8849 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008850
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008852#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008854#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008856}
8857
Bram Moolenaar33570922005-01-25 22:26:29 +00008858static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8859static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008860
8861/*
8862 * Implementation of map() and filter().
8863 */
8864 static void
8865filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008868 int map;
8869{
8870 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008871 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 listitem_T *li, *nli;
8873 list_T *l = NULL;
8874 dictitem_T *di;
8875 hashtab_T *ht;
8876 hashitem_T *hi;
8877 dict_T *d = NULL;
8878 typval_T save_val;
8879 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008880 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008881 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008882 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008883 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008884
8885 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008886 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008887 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008888 if ((l = argvars[0].vval.v_list) == NULL
8889 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008890 return;
8891 }
8892 else if (argvars[0].v_type == VAR_DICT)
8893 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008894 if ((d = argvars[0].vval.v_dict) == NULL
8895 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008896 return;
8897 }
8898 else
8899 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008900 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008901 return;
8902 }
8903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 expr = get_tv_string_buf_chk(&argvars[1], buf);
8905 /* On type errors, the preceding call has already displayed an error
8906 * message. Avoid a misleading error message for an empty string that
8907 * was not passed as argument. */
8908 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 prepare_vimvar(VV_VAL, &save_val);
8911 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008912
Bram Moolenaar280f1262006-01-30 00:14:18 +00008913 /* We reset "called_emsg" to be able to detect whether an error
8914 * occurred during evaluation of the expression. "did_emsg" can't be
8915 * used, because it is reset when calling a function. */
8916 save_called_emsg = called_emsg;
8917 called_emsg = FALSE;
8918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008919 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008921 prepare_vimvar(VV_KEY, &save_key);
8922 vimvars[VV_KEY].vv_type = VAR_STRING;
8923
8924 ht = &d->dv_hashtab;
8925 hash_lock(ht);
8926 todo = ht->ht_used;
8927 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008929 if (!HASHITEM_EMPTY(hi))
8930 {
8931 --todo;
8932 di = HI2DI(hi);
8933 if (tv_check_lock(di->di_tv.v_lock, msg))
8934 break;
8935 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008936 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8937 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 break;
8939 if (!map && rem)
8940 dictitem_remove(d, di);
8941 clear_tv(&vimvars[VV_KEY].vv_tv);
8942 }
8943 }
8944 hash_unlock(ht);
8945
8946 restore_vimvar(VV_KEY, &save_key);
8947 }
8948 else
8949 {
8950 for (li = l->lv_first; li != NULL; li = nli)
8951 {
8952 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008953 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008955 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8956 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008957 break;
8958 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008959 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008960 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008964
8965 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967
8968 copy_tv(&argvars[0], rettv);
8969}
8970
8971 static int
8972filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008973 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008974 char_u *expr;
8975 int map;
8976 int *remp;
8977{
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008979 char_u *s;
8980
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008982 s = expr;
8983 if (eval1(&s, &rettv, TRUE) == FAIL)
8984 return FAIL;
8985 if (*s != NUL) /* check for trailing chars after expr */
8986 {
8987 EMSG2(_(e_invexpr2), s);
8988 return FAIL;
8989 }
8990 if (map)
8991 {
8992 /* map(): replace the list item value */
8993 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008994 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008995 *tv = rettv;
8996 }
8997 else
8998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 int error = FALSE;
9000
Bram Moolenaare9a41262005-01-15 22:18:47 +00009001 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009003 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004 /* On type error, nothing has been removed; return FAIL to stop the
9005 * loop. The error message was given by get_tv_number_chk(). */
9006 if (error)
9007 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009011}
9012
9013/*
9014 * "filter()" function
9015 */
9016 static void
9017f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 typval_T *argvars;
9019 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009020{
9021 filter_map(argvars, rettv, FALSE);
9022}
9023
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009024/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009025 * "finddir({fname}[, {path}[, {count}]])" function
9026 */
9027 static void
9028f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *argvars;
9030 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009031{
9032 findfilendir(argvars, rettv, TRUE);
9033}
9034
9035/*
9036 * "findfile({fname}[, {path}[, {count}]])" function
9037 */
9038 static void
9039f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 typval_T *argvars;
9041 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042{
9043 findfilendir(argvars, rettv, FALSE);
9044}
9045
9046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 * "fnamemodify({fname}, {mods})" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 char_u *fname;
9055 char_u *mods;
9056 int usedlen = 0;
9057 int len;
9058 char_u *fbuf = NULL;
9059 char_u buf[NUMBUFLEN];
9060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009061 fname = get_tv_string_chk(&argvars[0]);
9062 mods = get_tv_string_buf_chk(&argvars[1], buf);
9063 if (fname == NULL || mods == NULL)
9064 fname = NULL;
9065 else
9066 {
9067 len = (int)STRLEN(fname);
9068 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 vim_free(fbuf);
9077}
9078
Bram Moolenaar33570922005-01-25 22:26:29 +00009079static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080
9081/*
9082 * "foldclosed()" function
9083 */
9084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 typval_T *argvars;
9087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 int end;
9089{
9090#ifdef FEAT_FOLDING
9091 linenr_T lnum;
9092 linenr_T first, last;
9093
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9096 {
9097 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9098 {
9099 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 return;
9104 }
9105 }
9106#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108}
9109
9110/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009111 * "foldclosed()" function
9112 */
9113 static void
9114f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009115 typval_T *argvars;
9116 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009117{
9118 foldclosed_both(argvars, rettv, FALSE);
9119}
9120
9121/*
9122 * "foldclosedend()" function
9123 */
9124 static void
9125f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009126 typval_T *argvars;
9127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009128{
9129 foldclosed_both(argvars, rettv, TRUE);
9130}
9131
9132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 * "foldlevel()" function
9134 */
9135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *argvars;
9138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140#ifdef FEAT_FOLDING
9141 linenr_T lnum;
9142
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 else
9147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149}
9150
9151/*
9152 * "foldtext()" function
9153 */
9154/*ARGSUSED*/
9155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009157 typval_T *argvars;
9158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160#ifdef FEAT_FOLDING
9161 linenr_T lnum;
9162 char_u *s;
9163 char_u *r;
9164 int len;
9165 char *txt;
9166#endif
9167
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 rettv->v_type = VAR_STRING;
9169 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009171 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9172 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9173 <= curbuf->b_ml.ml_line_count
9174 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 {
9176 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009177 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9178 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 {
9180 if (!linewhite(lnum))
9181 break;
9182 ++lnum;
9183 }
9184
9185 /* Find interesting text in this line. */
9186 s = skipwhite(ml_get(lnum));
9187 /* skip C comment-start */
9188 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009191 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009192 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009193 {
9194 s = skipwhite(ml_get(lnum + 1));
9195 if (*s == '*')
9196 s = skipwhite(s + 1);
9197 }
9198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 txt = _("+-%s%3ld lines: ");
9200 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009201 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 + 20 /* for %3ld */
9203 + STRLEN(s))); /* concatenated */
9204 if (r != NULL)
9205 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009206 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9207 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9208 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 len = (int)STRLEN(r);
9210 STRCAT(r, s);
9211 /* remove 'foldmarker' and 'commentstring' */
9212 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009213 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
9215 }
9216#endif
9217}
9218
9219/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009220 * "foldtextresult(lnum)" function
9221 */
9222/*ARGSUSED*/
9223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009225 typval_T *argvars;
9226 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009227{
9228#ifdef FEAT_FOLDING
9229 linenr_T lnum;
9230 char_u *text;
9231 char_u buf[51];
9232 foldinfo_T foldinfo;
9233 int fold_count;
9234#endif
9235
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 rettv->v_type = VAR_STRING;
9237 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009238#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 /* treat illegal types and illegal string values for {lnum} the same */
9241 if (lnum < 0)
9242 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009243 fold_count = foldedCount(curwin, lnum, &foldinfo);
9244 if (fold_count > 0)
9245 {
9246 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9247 &foldinfo, buf);
9248 if (text == buf)
9249 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009250 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009251 }
9252#endif
9253}
9254
9255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 * "foreground()" function
9257 */
9258/*ARGSUSED*/
9259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009261 typval_T *argvars;
9262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265#ifdef FEAT_GUI
9266 if (gui.in_use)
9267 gui_mch_set_foreground();
9268#else
9269# ifdef WIN32
9270 win32_set_foreground();
9271# endif
9272#endif
9273}
9274
9275/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009276 * "function()" function
9277 */
9278/*ARGSUSED*/
9279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 typval_T *argvars;
9282 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009283{
9284 char_u *s;
9285
Bram Moolenaara7043832005-01-21 11:56:39 +00009286 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009288 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009289 EMSG2(_(e_invarg2), s);
9290 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009291 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009292 else
9293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 rettv->vval.v_string = vim_strsave(s);
9295 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009296 }
9297}
9298
9299/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009300 * "garbagecollect()" function
9301 */
9302/*ARGSUSED*/
9303 static void
9304f_garbagecollect(argvars, rettv)
9305 typval_T *argvars;
9306 typval_T *rettv;
9307{
9308 garbage_collect();
9309}
9310
9311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009312 * "get()" function
9313 */
9314 static void
9315f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 typval_T *argvars;
9317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009318{
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 listitem_T *li;
9320 list_T *l;
9321 dictitem_T *di;
9322 dict_T *d;
9323 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009324
Bram Moolenaare9a41262005-01-15 22:18:47 +00009325 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009326 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009327 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 int error = FALSE;
9330
9331 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9332 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009333 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009334 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009336 else if (argvars[0].v_type == VAR_DICT)
9337 {
9338 if ((d = argvars[0].vval.v_dict) != NULL)
9339 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009340 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009341 if (di != NULL)
9342 tv = &di->di_tv;
9343 }
9344 }
9345 else
9346 EMSG2(_(e_listdictarg), "get()");
9347
9348 if (tv == NULL)
9349 {
9350 if (argvars[2].v_type == VAR_UNKNOWN)
9351 rettv->vval.v_number = 0;
9352 else
9353 copy_tv(&argvars[2], rettv);
9354 }
9355 else
9356 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009357}
9358
Bram Moolenaar342337a2005-07-21 21:11:17 +00009359static 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 +00009360
9361/*
9362 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009363 * Return a range (from start to end) of lines in rettv from the specified
9364 * buffer.
9365 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009366 */
9367 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009368get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009369 buf_T *buf;
9370 linenr_T start;
9371 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009372 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009373 typval_T *rettv;
9374{
9375 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009376
Bram Moolenaar342337a2005-07-21 21:11:17 +00009377 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009378 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009379 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009380 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009381 }
9382 else
9383 rettv->vval.v_number = 0;
9384
9385 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9386 return;
9387
9388 if (!retlist)
9389 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009390 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9391 p = ml_get_buf(buf, start, FALSE);
9392 else
9393 p = (char_u *)"";
9394
9395 rettv->v_type = VAR_STRING;
9396 rettv->vval.v_string = vim_strsave(p);
9397 }
9398 else
9399 {
9400 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009401 return;
9402
9403 if (start < 1)
9404 start = 1;
9405 if (end > buf->b_ml.ml_line_count)
9406 end = buf->b_ml.ml_line_count;
9407 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009408 if (list_append_string(rettv->vval.v_list,
9409 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009410 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009411 }
9412}
9413
9414/*
9415 * "getbufline()" function
9416 */
9417 static void
9418f_getbufline(argvars, rettv)
9419 typval_T *argvars;
9420 typval_T *rettv;
9421{
9422 linenr_T lnum;
9423 linenr_T end;
9424 buf_T *buf;
9425
9426 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9427 ++emsg_off;
9428 buf = get_buf_tv(&argvars[0]);
9429 --emsg_off;
9430
Bram Moolenaar661b1822005-07-28 22:36:45 +00009431 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009432 if (argvars[2].v_type == VAR_UNKNOWN)
9433 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009434 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009435 end = get_tv_lnum_buf(&argvars[2], buf);
9436
Bram Moolenaar342337a2005-07-21 21:11:17 +00009437 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009438}
9439
Bram Moolenaar0d660222005-01-07 21:51:51 +00009440/*
9441 * "getbufvar()" function
9442 */
9443 static void
9444f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 typval_T *argvars;
9446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009447{
9448 buf_T *buf;
9449 buf_T *save_curbuf;
9450 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9454 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009455 ++emsg_off;
9456 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009457
9458 rettv->v_type = VAR_STRING;
9459 rettv->vval.v_string = NULL;
9460
9461 if (buf != NULL && varname != NULL)
9462 {
9463 if (*varname == '&') /* buffer-local-option */
9464 {
9465 /* set curbuf to be our buf, temporarily */
9466 save_curbuf = curbuf;
9467 curbuf = buf;
9468
9469 get_option_tv(&varname, rettv, TRUE);
9470
9471 /* restore previous notion of curbuf */
9472 curbuf = save_curbuf;
9473 }
9474 else
9475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 if (*varname == NUL)
9477 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9478 * scope prefix before the NUL byte is required by
9479 * find_var_in_ht(). */
9480 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009481 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009482 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009483 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009485 }
9486 }
9487
9488 --emsg_off;
9489}
9490
9491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 * "getchar()" function
9493 */
9494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009496 typval_T *argvars;
9497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009500 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502 ++no_mapping;
9503 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009504 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 /* getchar(): blocking wait. */
9506 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 /* getchar(1): only check if char avail */
9509 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 else if (error || vpeekc() == NUL)
9511 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 n = 0;
9513 else
9514 /* getchar(0) and char avail: return char */
9515 n = safe_vgetc();
9516 --no_mapping;
9517 --allow_keys;
9518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 if (IS_SPECIAL(n) || mod_mask != 0)
9521 {
9522 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9523 int i = 0;
9524
9525 /* Turn a special key into three bytes, plus modifier. */
9526 if (mod_mask != 0)
9527 {
9528 temp[i++] = K_SPECIAL;
9529 temp[i++] = KS_MODIFIER;
9530 temp[i++] = mod_mask;
9531 }
9532 if (IS_SPECIAL(n))
9533 {
9534 temp[i++] = K_SPECIAL;
9535 temp[i++] = K_SECOND(n);
9536 temp[i++] = K_THIRD(n);
9537 }
9538#ifdef FEAT_MBYTE
9539 else if (has_mbyte)
9540 i += (*mb_char2bytes)(n, temp + i);
9541#endif
9542 else
9543 temp[i++] = n;
9544 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->v_type = VAR_STRING;
9546 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 }
9548}
9549
9550/*
9551 * "getcharmod()" function
9552 */
9553/*ARGSUSED*/
9554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009556 typval_T *argvars;
9557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560}
9561
9562/*
9563 * "getcmdline()" function
9564 */
9565/*ARGSUSED*/
9566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 typval_T *argvars;
9569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->v_type = VAR_STRING;
9572 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573}
9574
9575/*
9576 * "getcmdpos()" function
9577 */
9578/*ARGSUSED*/
9579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009581 typval_T *argvars;
9582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585}
9586
9587/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009588 * "getcmdtype()" function
9589 */
9590/*ARGSUSED*/
9591 static void
9592f_getcmdtype(argvars, rettv)
9593 typval_T *argvars;
9594 typval_T *rettv;
9595{
9596 rettv->v_type = VAR_STRING;
9597 rettv->vval.v_string = alloc(2);
9598 if (rettv->vval.v_string != NULL)
9599 {
9600 rettv->vval.v_string[0] = get_cmdline_type();
9601 rettv->vval.v_string[1] = NUL;
9602 }
9603}
9604
9605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 * "getcwd()" function
9607 */
9608/*ARGSUSED*/
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 char_u cwd[MAXPATHL];
9615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 else
9620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009623 if (rettv->vval.v_string != NULL)
9624 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#endif
9626 }
9627}
9628
9629/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009630 * "getfontname()" function
9631 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009632/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *argvars;
9636 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->v_type = VAR_STRING;
9639 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009640#ifdef FEAT_GUI
9641 if (gui.in_use)
9642 {
9643 GuiFont font;
9644 char_u *name = NULL;
9645
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009646 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009647 {
9648 /* Get the "Normal" font. Either the name saved by
9649 * hl_set_font_name() or from the font ID. */
9650 font = gui.norm_font;
9651 name = hl_get_font_name();
9652 }
9653 else
9654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009656 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9657 return;
9658 font = gui_mch_get_font(name, FALSE);
9659 if (font == NOFONT)
9660 return; /* Invalid font name, return empty string. */
9661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009663 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009664 gui_mch_free_font(font);
9665 }
9666#endif
9667}
9668
9669/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009670 * "getfperm({fname})" function
9671 */
9672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009676{
9677 char_u *fname;
9678 struct stat st;
9679 char_u *perm = NULL;
9680 char_u flags[] = "rwx";
9681 int i;
9682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009686 if (mch_stat((char *)fname, &st) >= 0)
9687 {
9688 perm = vim_strsave((char_u *)"---------");
9689 if (perm != NULL)
9690 {
9691 for (i = 0; i < 9; i++)
9692 {
9693 if (st.st_mode & (1 << (8 - i)))
9694 perm[i] = flags[i % 3];
9695 }
9696 }
9697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009699}
9700
9701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 * "getfsize({fname})" function
9703 */
9704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009706 typval_T *argvars;
9707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
9709 char_u *fname;
9710 struct stat st;
9711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715
9716 if (mch_stat((char *)fname, &st) >= 0)
9717 {
9718 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 }
9723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
9727/*
9728 * "getftime({fname})" function
9729 */
9730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009732 typval_T *argvars;
9733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 char_u *fname;
9736 struct stat st;
9737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739
9740 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744}
9745
9746/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009747 * "getftype({fname})" function
9748 */
9749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009751 typval_T *argvars;
9752 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009753{
9754 char_u *fname;
9755 struct stat st;
9756 char_u *type = NULL;
9757 char *t;
9758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009762 if (mch_lstat((char *)fname, &st) >= 0)
9763 {
9764#ifdef S_ISREG
9765 if (S_ISREG(st.st_mode))
9766 t = "file";
9767 else if (S_ISDIR(st.st_mode))
9768 t = "dir";
9769# ifdef S_ISLNK
9770 else if (S_ISLNK(st.st_mode))
9771 t = "link";
9772# endif
9773# ifdef S_ISBLK
9774 else if (S_ISBLK(st.st_mode))
9775 t = "bdev";
9776# endif
9777# ifdef S_ISCHR
9778 else if (S_ISCHR(st.st_mode))
9779 t = "cdev";
9780# endif
9781# ifdef S_ISFIFO
9782 else if (S_ISFIFO(st.st_mode))
9783 t = "fifo";
9784# endif
9785# ifdef S_ISSOCK
9786 else if (S_ISSOCK(st.st_mode))
9787 t = "fifo";
9788# endif
9789 else
9790 t = "other";
9791#else
9792# ifdef S_IFMT
9793 switch (st.st_mode & S_IFMT)
9794 {
9795 case S_IFREG: t = "file"; break;
9796 case S_IFDIR: t = "dir"; break;
9797# ifdef S_IFLNK
9798 case S_IFLNK: t = "link"; break;
9799# endif
9800# ifdef S_IFBLK
9801 case S_IFBLK: t = "bdev"; break;
9802# endif
9803# ifdef S_IFCHR
9804 case S_IFCHR: t = "cdev"; break;
9805# endif
9806# ifdef S_IFIFO
9807 case S_IFIFO: t = "fifo"; break;
9808# endif
9809# ifdef S_IFSOCK
9810 case S_IFSOCK: t = "socket"; break;
9811# endif
9812 default: t = "other";
9813 }
9814# else
9815 if (mch_isdir(fname))
9816 t = "dir";
9817 else
9818 t = "file";
9819# endif
9820#endif
9821 type = vim_strsave((char_u *)t);
9822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009824}
9825
9826/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009827 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009828 */
9829 static void
9830f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 typval_T *argvars;
9832 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009833{
9834 linenr_T lnum;
9835 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009836 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009837
9838 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009839 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009840 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009841 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009842 retlist = FALSE;
9843 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009844 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009845 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009846 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009847 retlist = TRUE;
9848 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009849
Bram Moolenaar342337a2005-07-21 21:11:17 +00009850 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009851}
9852
9853/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009854 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009855 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009856/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009857 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009858f_getqflist(argvars, rettv)
9859 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009860 typval_T *rettv;
9861{
9862#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +00009863 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009864#endif
9865
9866 rettv->vval.v_number = FALSE;
9867#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009868 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +00009869 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00009870 wp = NULL;
9871 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9872 {
9873 wp = find_win_by_nr(&argvars[0]);
9874 if (wp == NULL)
9875 return;
9876 }
9877
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009878 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009879 }
9880#endif
9881}
9882
9883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 * "getreg()" function
9885 */
9886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 typval_T *argvars;
9889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890{
9891 char_u *strregname;
9892 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009893 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009896 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 strregname = get_tv_string_chk(&argvars[0]);
9899 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009900 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009904 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 regname = (strregname == NULL ? '"' : *strregname);
9906 if (regname == 0)
9907 regname = '"';
9908
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009910 rettv->vval.v_string = error ? NULL :
9911 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912}
9913
9914/*
9915 * "getregtype()" function
9916 */
9917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 typval_T *argvars;
9920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
9922 char_u *strregname;
9923 int regname;
9924 char_u buf[NUMBUFLEN + 2];
9925 long reglen = 0;
9926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009927 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 {
9929 strregname = get_tv_string_chk(&argvars[0]);
9930 if (strregname == NULL) /* type error; errmsg already given */
9931 {
9932 rettv->v_type = VAR_STRING;
9933 rettv->vval.v_string = NULL;
9934 return;
9935 }
9936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
9941 regname = (strregname == NULL ? '"' : *strregname);
9942 if (regname == 0)
9943 regname = '"';
9944
9945 buf[0] = NUL;
9946 buf[1] = NUL;
9947 switch (get_reg_type(regname, &reglen))
9948 {
9949 case MLINE: buf[0] = 'V'; break;
9950 case MCHAR: buf[0] = 'v'; break;
9951#ifdef FEAT_VISUAL
9952 case MBLOCK:
9953 buf[0] = Ctrl_V;
9954 sprintf((char *)buf + 1, "%ld", reglen + 1);
9955 break;
9956#endif
9957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958 rettv->v_type = VAR_STRING;
9959 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960}
9961
9962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 * "getwinposx()" function
9964 */
9965/*ARGSUSED*/
9966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967f_getwinposx(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972#ifdef FEAT_GUI
9973 if (gui.in_use)
9974 {
9975 int x, y;
9976
9977 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 }
9980#endif
9981}
9982
9983/*
9984 * "getwinposy()" function
9985 */
9986/*ARGSUSED*/
9987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 typval_T *argvars;
9990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993#ifdef FEAT_GUI
9994 if (gui.in_use)
9995 {
9996 int x, y;
9997
9998 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 }
10001#endif
10002}
10003
Bram Moolenaara40058a2005-07-11 22:42:07 +000010004 static win_T *
10005find_win_by_nr(vp)
10006 typval_T *vp;
10007{
10008#ifdef FEAT_WINDOWS
10009 win_T *wp;
10010#endif
10011 int nr;
10012
10013 nr = get_tv_number_chk(vp, NULL);
10014
10015#ifdef FEAT_WINDOWS
10016 if (nr < 0)
10017 return NULL;
10018 if (nr == 0)
10019 return curwin;
10020
10021 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10022 if (--nr <= 0)
10023 break;
10024 return wp;
10025#else
10026 if (nr == 0 || nr == 1)
10027 return curwin;
10028 return NULL;
10029#endif
10030}
10031
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032/*
10033 * "getwinvar()" function
10034 */
10035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010037 typval_T *argvars;
10038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039{
10040 win_T *win, *oldcurwin;
10041 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010042 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010045 varname = get_tv_string_chk(&argvars[1]);
10046 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_STRING;
10049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
10051 if (win != NULL && varname != NULL)
10052 {
10053 if (*varname == '&') /* window-local-option */
10054 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010055 /* Set curwin to be our win, temporarily. Also set curbuf, so
10056 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 oldcurwin = curwin;
10058 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010059 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062
10063 /* restore previous notion of curwin */
10064 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010065 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 }
10067 else
10068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 if (*varname == NUL)
10070 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10071 * scope prefix before the NUL byte is required by
10072 * find_var_in_ht(). */
10073 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010075 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010077 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079 }
10080
10081 --emsg_off;
10082}
10083
10084/*
10085 * "glob()" function
10086 */
10087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010089 typval_T *argvars;
10090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091{
10092 expand_T xpc;
10093
10094 ExpandInit(&xpc);
10095 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 rettv->v_type = VAR_STRING;
10097 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10099 ExpandCleanup(&xpc);
10100}
10101
10102/*
10103 * "globpath()" function
10104 */
10105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010107 typval_T *argvars;
10108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109{
10110 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 if (file == NULL)
10115 rettv->vval.v_string = NULL;
10116 else
10117 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118}
10119
10120/*
10121 * "has()" function
10122 */
10123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010125 typval_T *argvars;
10126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 int i;
10129 char_u *name;
10130 int n = FALSE;
10131 static char *(has_list[]) =
10132 {
10133#ifdef AMIGA
10134 "amiga",
10135# ifdef FEAT_ARP
10136 "arp",
10137# endif
10138#endif
10139#ifdef __BEOS__
10140 "beos",
10141#endif
10142#ifdef MSDOS
10143# ifdef DJGPP
10144 "dos32",
10145# else
10146 "dos16",
10147# endif
10148#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010149#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 "mac",
10151#endif
10152#if defined(MACOS_X_UNIX)
10153 "macunix",
10154#endif
10155#ifdef OS2
10156 "os2",
10157#endif
10158#ifdef __QNX__
10159 "qnx",
10160#endif
10161#ifdef RISCOS
10162 "riscos",
10163#endif
10164#ifdef UNIX
10165 "unix",
10166#endif
10167#ifdef VMS
10168 "vms",
10169#endif
10170#ifdef WIN16
10171 "win16",
10172#endif
10173#ifdef WIN32
10174 "win32",
10175#endif
10176#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10177 "win32unix",
10178#endif
10179#ifdef WIN64
10180 "win64",
10181#endif
10182#ifdef EBCDIC
10183 "ebcdic",
10184#endif
10185#ifndef CASE_INSENSITIVE_FILENAME
10186 "fname_case",
10187#endif
10188#ifdef FEAT_ARABIC
10189 "arabic",
10190#endif
10191#ifdef FEAT_AUTOCMD
10192 "autocmd",
10193#endif
10194#ifdef FEAT_BEVAL
10195 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010196# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10197 "balloon_multiline",
10198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199#endif
10200#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10201 "builtin_terms",
10202# ifdef ALL_BUILTIN_TCAPS
10203 "all_builtin_terms",
10204# endif
10205#endif
10206#ifdef FEAT_BYTEOFF
10207 "byte_offset",
10208#endif
10209#ifdef FEAT_CINDENT
10210 "cindent",
10211#endif
10212#ifdef FEAT_CLIENTSERVER
10213 "clientserver",
10214#endif
10215#ifdef FEAT_CLIPBOARD
10216 "clipboard",
10217#endif
10218#ifdef FEAT_CMDL_COMPL
10219 "cmdline_compl",
10220#endif
10221#ifdef FEAT_CMDHIST
10222 "cmdline_hist",
10223#endif
10224#ifdef FEAT_COMMENTS
10225 "comments",
10226#endif
10227#ifdef FEAT_CRYPT
10228 "cryptv",
10229#endif
10230#ifdef FEAT_CSCOPE
10231 "cscope",
10232#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010233#ifdef CURSOR_SHAPE
10234 "cursorshape",
10235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236#ifdef DEBUG
10237 "debug",
10238#endif
10239#ifdef FEAT_CON_DIALOG
10240 "dialog_con",
10241#endif
10242#ifdef FEAT_GUI_DIALOG
10243 "dialog_gui",
10244#endif
10245#ifdef FEAT_DIFF
10246 "diff",
10247#endif
10248#ifdef FEAT_DIGRAPHS
10249 "digraphs",
10250#endif
10251#ifdef FEAT_DND
10252 "dnd",
10253#endif
10254#ifdef FEAT_EMACS_TAGS
10255 "emacs_tags",
10256#endif
10257 "eval", /* always present, of course! */
10258#ifdef FEAT_EX_EXTRA
10259 "ex_extra",
10260#endif
10261#ifdef FEAT_SEARCH_EXTRA
10262 "extra_search",
10263#endif
10264#ifdef FEAT_FKMAP
10265 "farsi",
10266#endif
10267#ifdef FEAT_SEARCHPATH
10268 "file_in_path",
10269#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010270#if defined(UNIX) && !defined(USE_SYSTEM)
10271 "filterpipe",
10272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273#ifdef FEAT_FIND_ID
10274 "find_in_path",
10275#endif
10276#ifdef FEAT_FOLDING
10277 "folding",
10278#endif
10279#ifdef FEAT_FOOTER
10280 "footer",
10281#endif
10282#if !defined(USE_SYSTEM) && defined(UNIX)
10283 "fork",
10284#endif
10285#ifdef FEAT_GETTEXT
10286 "gettext",
10287#endif
10288#ifdef FEAT_GUI
10289 "gui",
10290#endif
10291#ifdef FEAT_GUI_ATHENA
10292# ifdef FEAT_GUI_NEXTAW
10293 "gui_neXtaw",
10294# else
10295 "gui_athena",
10296# endif
10297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298#ifdef FEAT_GUI_GTK
10299 "gui_gtk",
10300# ifdef HAVE_GTK2
10301 "gui_gtk2",
10302# endif
10303#endif
10304#ifdef FEAT_GUI_MAC
10305 "gui_mac",
10306#endif
10307#ifdef FEAT_GUI_MOTIF
10308 "gui_motif",
10309#endif
10310#ifdef FEAT_GUI_PHOTON
10311 "gui_photon",
10312#endif
10313#ifdef FEAT_GUI_W16
10314 "gui_win16",
10315#endif
10316#ifdef FEAT_GUI_W32
10317 "gui_win32",
10318#endif
10319#ifdef FEAT_HANGULIN
10320 "hangul_input",
10321#endif
10322#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10323 "iconv",
10324#endif
10325#ifdef FEAT_INS_EXPAND
10326 "insert_expand",
10327#endif
10328#ifdef FEAT_JUMPLIST
10329 "jumplist",
10330#endif
10331#ifdef FEAT_KEYMAP
10332 "keymap",
10333#endif
10334#ifdef FEAT_LANGMAP
10335 "langmap",
10336#endif
10337#ifdef FEAT_LIBCALL
10338 "libcall",
10339#endif
10340#ifdef FEAT_LINEBREAK
10341 "linebreak",
10342#endif
10343#ifdef FEAT_LISP
10344 "lispindent",
10345#endif
10346#ifdef FEAT_LISTCMDS
10347 "listcmds",
10348#endif
10349#ifdef FEAT_LOCALMAP
10350 "localmap",
10351#endif
10352#ifdef FEAT_MENU
10353 "menu",
10354#endif
10355#ifdef FEAT_SESSION
10356 "mksession",
10357#endif
10358#ifdef FEAT_MODIFY_FNAME
10359 "modify_fname",
10360#endif
10361#ifdef FEAT_MOUSE
10362 "mouse",
10363#endif
10364#ifdef FEAT_MOUSESHAPE
10365 "mouseshape",
10366#endif
10367#if defined(UNIX) || defined(VMS)
10368# ifdef FEAT_MOUSE_DEC
10369 "mouse_dec",
10370# endif
10371# ifdef FEAT_MOUSE_GPM
10372 "mouse_gpm",
10373# endif
10374# ifdef FEAT_MOUSE_JSB
10375 "mouse_jsbterm",
10376# endif
10377# ifdef FEAT_MOUSE_NET
10378 "mouse_netterm",
10379# endif
10380# ifdef FEAT_MOUSE_PTERM
10381 "mouse_pterm",
10382# endif
10383# ifdef FEAT_MOUSE_XTERM
10384 "mouse_xterm",
10385# endif
10386#endif
10387#ifdef FEAT_MBYTE
10388 "multi_byte",
10389#endif
10390#ifdef FEAT_MBYTE_IME
10391 "multi_byte_ime",
10392#endif
10393#ifdef FEAT_MULTI_LANG
10394 "multi_lang",
10395#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010396#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010397#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010398 "mzscheme",
10399#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401#ifdef FEAT_OLE
10402 "ole",
10403#endif
10404#ifdef FEAT_OSFILETYPE
10405 "osfiletype",
10406#endif
10407#ifdef FEAT_PATH_EXTRA
10408 "path_extra",
10409#endif
10410#ifdef FEAT_PERL
10411#ifndef DYNAMIC_PERL
10412 "perl",
10413#endif
10414#endif
10415#ifdef FEAT_PYTHON
10416#ifndef DYNAMIC_PYTHON
10417 "python",
10418#endif
10419#endif
10420#ifdef FEAT_POSTSCRIPT
10421 "postscript",
10422#endif
10423#ifdef FEAT_PRINTER
10424 "printer",
10425#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010426#ifdef FEAT_PROFILE
10427 "profile",
10428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#ifdef FEAT_QUICKFIX
10430 "quickfix",
10431#endif
10432#ifdef FEAT_RIGHTLEFT
10433 "rightleft",
10434#endif
10435#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10436 "ruby",
10437#endif
10438#ifdef FEAT_SCROLLBIND
10439 "scrollbind",
10440#endif
10441#ifdef FEAT_CMDL_INFO
10442 "showcmd",
10443 "cmdline_info",
10444#endif
10445#ifdef FEAT_SIGNS
10446 "signs",
10447#endif
10448#ifdef FEAT_SMARTINDENT
10449 "smartindent",
10450#endif
10451#ifdef FEAT_SNIFF
10452 "sniff",
10453#endif
10454#ifdef FEAT_STL_OPT
10455 "statusline",
10456#endif
10457#ifdef FEAT_SUN_WORKSHOP
10458 "sun_workshop",
10459#endif
10460#ifdef FEAT_NETBEANS_INTG
10461 "netbeans_intg",
10462#endif
10463#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010464 "spell",
10465#endif
10466#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 "syntax",
10468#endif
10469#if defined(USE_SYSTEM) || !defined(UNIX)
10470 "system",
10471#endif
10472#ifdef FEAT_TAG_BINS
10473 "tag_binary",
10474#endif
10475#ifdef FEAT_TAG_OLDSTATIC
10476 "tag_old_static",
10477#endif
10478#ifdef FEAT_TAG_ANYWHITE
10479 "tag_any_white",
10480#endif
10481#ifdef FEAT_TCL
10482# ifndef DYNAMIC_TCL
10483 "tcl",
10484# endif
10485#endif
10486#ifdef TERMINFO
10487 "terminfo",
10488#endif
10489#ifdef FEAT_TERMRESPONSE
10490 "termresponse",
10491#endif
10492#ifdef FEAT_TEXTOBJ
10493 "textobjects",
10494#endif
10495#ifdef HAVE_TGETENT
10496 "tgetent",
10497#endif
10498#ifdef FEAT_TITLE
10499 "title",
10500#endif
10501#ifdef FEAT_TOOLBAR
10502 "toolbar",
10503#endif
10504#ifdef FEAT_USR_CMDS
10505 "user-commands", /* was accidentally included in 5.4 */
10506 "user_commands",
10507#endif
10508#ifdef FEAT_VIMINFO
10509 "viminfo",
10510#endif
10511#ifdef FEAT_VERTSPLIT
10512 "vertsplit",
10513#endif
10514#ifdef FEAT_VIRTUALEDIT
10515 "virtualedit",
10516#endif
10517#ifdef FEAT_VISUAL
10518 "visual",
10519#endif
10520#ifdef FEAT_VISUALEXTRA
10521 "visualextra",
10522#endif
10523#ifdef FEAT_VREPLACE
10524 "vreplace",
10525#endif
10526#ifdef FEAT_WILDIGN
10527 "wildignore",
10528#endif
10529#ifdef FEAT_WILDMENU
10530 "wildmenu",
10531#endif
10532#ifdef FEAT_WINDOWS
10533 "windows",
10534#endif
10535#ifdef FEAT_WAK
10536 "winaltkeys",
10537#endif
10538#ifdef FEAT_WRITEBACKUP
10539 "writebackup",
10540#endif
10541#ifdef FEAT_XIM
10542 "xim",
10543#endif
10544#ifdef FEAT_XFONTSET
10545 "xfontset",
10546#endif
10547#ifdef USE_XSMP
10548 "xsmp",
10549#endif
10550#ifdef USE_XSMP_INTERACT
10551 "xsmp_interact",
10552#endif
10553#ifdef FEAT_XCLIPBOARD
10554 "xterm_clipboard",
10555#endif
10556#ifdef FEAT_XTERM_SAVE
10557 "xterm_save",
10558#endif
10559#if defined(UNIX) && defined(FEAT_X11)
10560 "X11",
10561#endif
10562 NULL
10563 };
10564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 for (i = 0; has_list[i] != NULL; ++i)
10567 if (STRICMP(name, has_list[i]) == 0)
10568 {
10569 n = TRUE;
10570 break;
10571 }
10572
10573 if (n == FALSE)
10574 {
10575 if (STRNICMP(name, "patch", 5) == 0)
10576 n = has_patch(atoi((char *)name + 5));
10577 else if (STRICMP(name, "vim_starting") == 0)
10578 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010579#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10580 else if (STRICMP(name, "balloon_multiline") == 0)
10581 n = multiline_balloon_available();
10582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583#ifdef DYNAMIC_TCL
10584 else if (STRICMP(name, "tcl") == 0)
10585 n = tcl_enabled(FALSE);
10586#endif
10587#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10588 else if (STRICMP(name, "iconv") == 0)
10589 n = iconv_enabled(FALSE);
10590#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010591#ifdef DYNAMIC_MZSCHEME
10592 else if (STRICMP(name, "mzscheme") == 0)
10593 n = mzscheme_enabled(FALSE);
10594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#ifdef DYNAMIC_RUBY
10596 else if (STRICMP(name, "ruby") == 0)
10597 n = ruby_enabled(FALSE);
10598#endif
10599#ifdef DYNAMIC_PYTHON
10600 else if (STRICMP(name, "python") == 0)
10601 n = python_enabled(FALSE);
10602#endif
10603#ifdef DYNAMIC_PERL
10604 else if (STRICMP(name, "perl") == 0)
10605 n = perl_enabled(FALSE);
10606#endif
10607#ifdef FEAT_GUI
10608 else if (STRICMP(name, "gui_running") == 0)
10609 n = (gui.in_use || gui.starting);
10610# ifdef FEAT_GUI_W32
10611 else if (STRICMP(name, "gui_win32s") == 0)
10612 n = gui_is_win32s();
10613# endif
10614# ifdef FEAT_BROWSE
10615 else if (STRICMP(name, "browse") == 0)
10616 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10617# endif
10618#endif
10619#ifdef FEAT_SYN_HL
10620 else if (STRICMP(name, "syntax_items") == 0)
10621 n = syntax_present(curbuf);
10622#endif
10623#if defined(WIN3264)
10624 else if (STRICMP(name, "win95") == 0)
10625 n = mch_windows95();
10626#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010627#ifdef FEAT_NETBEANS_INTG
10628 else if (STRICMP(name, "netbeans_enabled") == 0)
10629 n = usingNetbeans;
10630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 }
10632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634}
10635
10636/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637 * "has_key()" function
10638 */
10639 static void
10640f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 typval_T *argvars;
10642 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643{
10644 rettv->vval.v_number = 0;
10645 if (argvars[0].v_type != VAR_DICT)
10646 {
10647 EMSG(_(e_dictreq));
10648 return;
10649 }
10650 if (argvars[0].vval.v_dict == NULL)
10651 return;
10652
10653 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010654 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010655}
10656
10657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 * "hasmapto()" function
10659 */
10660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010662 typval_T *argvars;
10663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664{
10665 char_u *name;
10666 char_u *mode;
10667 char_u buf[NUMBUFLEN];
10668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010670 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 mode = (char_u *)"nvo";
10672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674
10675 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
10681/*
10682 * "histadd()" function
10683 */
10684/*ARGSUSED*/
10685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010687 typval_T *argvars;
10688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
10690#ifdef FEAT_CMDHIST
10691 int histype;
10692 char_u *str;
10693 char_u buf[NUMBUFLEN];
10694#endif
10695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 if (check_restricted() || check_secure())
10698 return;
10699#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10701 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 if (histype >= 0)
10703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 if (*str != NUL)
10706 {
10707 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 return;
10710 }
10711 }
10712#endif
10713}
10714
10715/*
10716 * "histdel()" function
10717 */
10718/*ARGSUSED*/
10719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010720f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010721 typval_T *argvars;
10722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723{
10724#ifdef FEAT_CMDHIST
10725 int n;
10726 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010729 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10730 if (str == NULL)
10731 n = 0;
10732 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010735 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010737 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010738 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 else
10740 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742 get_tv_string_buf(&argvars[1], buf));
10743 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746#endif
10747}
10748
10749/*
10750 * "histget()" function
10751 */
10752/*ARGSUSED*/
10753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010755 typval_T *argvars;
10756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757{
10758#ifdef FEAT_CMDHIST
10759 int type;
10760 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10764 if (str == NULL)
10765 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010767 {
10768 type = get_histtype(str);
10769 if (argvars[1].v_type == VAR_UNKNOWN)
10770 idx = get_history_idx(type);
10771 else
10772 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10773 /* -1 on type error */
10774 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780}
10781
10782/*
10783 * "histnr()" function
10784 */
10785/*ARGSUSED*/
10786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010788 typval_T *argvars;
10789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790{
10791 int i;
10792
10793#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010794 char_u *history = get_tv_string_chk(&argvars[0]);
10795
10796 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 if (i >= HIST_CMD && i < HIST_COUNT)
10798 i = get_history_idx(i);
10799 else
10800#endif
10801 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803}
10804
10805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 * "highlightID(name)" function
10807 */
10808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010810 typval_T *argvars;
10811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814}
10815
10816/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010817 * "highlight_exists()" function
10818 */
10819 static void
10820f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010821 typval_T *argvars;
10822 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010823{
10824 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10825}
10826
10827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 * "hostname()" function
10829 */
10830/*ARGSUSED*/
10831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010832f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 typval_T *argvars;
10834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
10836 char_u hostname[256];
10837
10838 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->v_type = VAR_STRING;
10840 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841}
10842
10843/*
10844 * iconv() function
10845 */
10846/*ARGSUSED*/
10847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 typval_T *argvars;
10850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
10852#ifdef FEAT_MBYTE
10853 char_u buf1[NUMBUFLEN];
10854 char_u buf2[NUMBUFLEN];
10855 char_u *from, *to, *str;
10856 vimconv_T vimconv;
10857#endif
10858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->v_type = VAR_STRING;
10860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861
10862#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863 str = get_tv_string(&argvars[0]);
10864 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10865 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 vimconv.vc_type = CONV_NONE;
10867 convert_setup(&vimconv, from, to);
10868
10869 /* If the encodings are equal, no conversion needed. */
10870 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874
10875 convert_setup(&vimconv, NULL, NULL);
10876 vim_free(from);
10877 vim_free(to);
10878#endif
10879}
10880
10881/*
10882 * "indent()" function
10883 */
10884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 typval_T *argvars;
10887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888{
10889 linenr_T lnum;
10890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896}
10897
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010898/*
10899 * "index()" function
10900 */
10901 static void
10902f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *argvars;
10904 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010905{
Bram Moolenaar33570922005-01-25 22:26:29 +000010906 list_T *l;
10907 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010908 long idx = 0;
10909 int ic = FALSE;
10910
10911 rettv->vval.v_number = -1;
10912 if (argvars[0].v_type != VAR_LIST)
10913 {
10914 EMSG(_(e_listreq));
10915 return;
10916 }
10917 l = argvars[0].vval.v_list;
10918 if (l != NULL)
10919 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010920 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 int error = FALSE;
10924
Bram Moolenaar758711c2005-02-02 23:11:38 +000010925 /* Start at specified item. Use the cached index that list_find()
10926 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010928 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010929 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 ic = get_tv_number_chk(&argvars[3], &error);
10931 if (error)
10932 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010933 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010934
Bram Moolenaar758711c2005-02-02 23:11:38 +000010935 for ( ; item != NULL; item = item->li_next, ++idx)
10936 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010937 {
10938 rettv->vval.v_number = idx;
10939 break;
10940 }
10941 }
10942}
10943
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944static int inputsecret_flag = 0;
10945
10946/*
10947 * "input()" function
10948 * Also handles inputsecret() when inputsecret is set.
10949 */
10950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010952 typval_T *argvars;
10953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 char_u *p = NULL;
10957 int c;
10958 char_u buf[NUMBUFLEN];
10959 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010961 int xp_type = EXPAND_NOTHING;
10962 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965
10966#ifdef NO_CONSOLE_INPUT
10967 /* While starting up, there is no place to enter text. */
10968 if (no_console_input())
10969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010970 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 return;
10972 }
10973#endif
10974
10975 cmd_silent = FALSE; /* Want to see the prompt. */
10976 if (prompt != NULL)
10977 {
10978 /* Only the part of the message after the last NL is considered as
10979 * prompt for the command line */
10980 p = vim_strrchr(prompt, '\n');
10981 if (p == NULL)
10982 p = prompt;
10983 else
10984 {
10985 ++p;
10986 c = *p;
10987 *p = NUL;
10988 msg_start();
10989 msg_clr_eos();
10990 msg_puts_attr(prompt, echo_attr);
10991 msg_didout = FALSE;
10992 msg_starthere();
10993 *p = c;
10994 }
10995 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010997 if (argvars[1].v_type != VAR_UNKNOWN)
10998 {
10999 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11000 if (defstr != NULL)
11001 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002
Bram Moolenaar4463f292005-09-25 22:20:24 +000011003 if (argvars[2].v_type != VAR_UNKNOWN)
11004 {
11005 char_u *xp_name;
11006 int xp_namelen;
11007 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011008
Bram Moolenaar4463f292005-09-25 22:20:24 +000011009 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011010
Bram Moolenaar4463f292005-09-25 22:20:24 +000011011 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11012 if (xp_name == NULL)
11013 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011014
Bram Moolenaar4463f292005-09-25 22:20:24 +000011015 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011016
Bram Moolenaar4463f292005-09-25 22:20:24 +000011017 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11018 &xp_arg) == FAIL)
11019 return;
11020 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011021 }
11022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 if (defstr != NULL)
11024 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011025 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11026 xp_type, xp_arg);
11027
11028 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011030 /* since the user typed this, no need to wait for return */
11031 need_wait_return = FALSE;
11032 msg_didout = FALSE;
11033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 cmd_silent = cmd_silent_save;
11035}
11036
11037/*
11038 * "inputdialog()" function
11039 */
11040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011042 typval_T *argvars;
11043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044{
11045#if defined(FEAT_GUI_TEXTDIALOG)
11046 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11047 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11048 {
11049 char_u *message;
11050 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011053 message = get_tv_string_chk(&argvars[0]);
11054 if (argvars[1].v_type != VAR_UNKNOWN
11055 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011056 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 else
11058 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 if (message != NULL && defstr != NULL
11060 && do_dialog(VIM_QUESTION, NULL, message,
11061 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 else
11064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 if (message != NULL && defstr != NULL
11066 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011067 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068 rettv->vval.v_string = vim_strsave(
11069 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 }
11075 else
11076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078}
11079
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011080/*
11081 * "inputlist()" function
11082 */
11083 static void
11084f_inputlist(argvars, rettv)
11085 typval_T *argvars;
11086 typval_T *rettv;
11087{
11088 listitem_T *li;
11089 int selected;
11090 int mouse_used;
11091
11092 rettv->vval.v_number = 0;
11093#ifdef NO_CONSOLE_INPUT
11094 /* While starting up, there is no place to enter text. */
11095 if (no_console_input())
11096 return;
11097#endif
11098 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11099 {
11100 EMSG2(_(e_listarg), "inputlist()");
11101 return;
11102 }
11103
11104 msg_start();
11105 lines_left = Rows; /* avoid more prompt */
11106 msg_scroll = TRUE;
11107 msg_clr_eos();
11108
11109 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11110 {
11111 msg_puts(get_tv_string(&li->li_tv));
11112 msg_putchar('\n');
11113 }
11114
11115 /* Ask for choice. */
11116 selected = prompt_for_number(&mouse_used);
11117 if (mouse_used)
11118 selected -= lines_left;
11119
11120 rettv->vval.v_number = selected;
11121}
11122
11123
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11125
11126/*
11127 * "inputrestore()" function
11128 */
11129/*ARGSUSED*/
11130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011132 typval_T *argvars;
11133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134{
11135 if (ga_userinput.ga_len > 0)
11136 {
11137 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11139 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 }
11142 else if (p_verbose > 1)
11143 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011144 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 }
11147}
11148
11149/*
11150 * "inputsave()" function
11151 */
11152/*ARGSUSED*/
11153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011155 typval_T *argvars;
11156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157{
11158 /* Add an entry to the stack of typehead storage. */
11159 if (ga_grow(&ga_userinput, 1) == OK)
11160 {
11161 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11162 + ga_userinput.ga_len);
11163 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 }
11166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168}
11169
11170/*
11171 * "inputsecret()" function
11172 */
11173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011175 typval_T *argvars;
11176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177{
11178 ++cmdline_star;
11179 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 --cmdline_star;
11182 --inputsecret_flag;
11183}
11184
11185/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011186 * "insert()" function
11187 */
11188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *argvars;
11191 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011192{
11193 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011194 listitem_T *item;
11195 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011197
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011198 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011199 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011200 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011201 else if ((l = argvars[0].vval.v_list) != NULL
11202 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011203 {
11204 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 before = get_tv_number_chk(&argvars[2], &error);
11206 if (error)
11207 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011208
Bram Moolenaar758711c2005-02-02 23:11:38 +000011209 if (before == l->lv_len)
11210 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011211 else
11212 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011213 item = list_find(l, before);
11214 if (item == NULL)
11215 {
11216 EMSGN(_(e_listidx), before);
11217 l = NULL;
11218 }
11219 }
11220 if (l != NULL)
11221 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011222 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011223 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011224 }
11225 }
11226}
11227
11228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 * "isdirectory()" function
11230 */
11231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011233 typval_T *argvars;
11234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237}
11238
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011239/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011240 * "islocked()" function
11241 */
11242 static void
11243f_islocked(argvars, rettv)
11244 typval_T *argvars;
11245 typval_T *rettv;
11246{
11247 lval_T lv;
11248 char_u *end;
11249 dictitem_T *di;
11250
11251 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011252 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11253 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011254 if (end != NULL && lv.ll_name != NULL)
11255 {
11256 if (*end != NUL)
11257 EMSG(_(e_trailing));
11258 else
11259 {
11260 if (lv.ll_tv == NULL)
11261 {
11262 if (check_changedtick(lv.ll_name))
11263 rettv->vval.v_number = 1; /* always locked */
11264 else
11265 {
11266 di = find_var(lv.ll_name, NULL);
11267 if (di != NULL)
11268 {
11269 /* Consider a variable locked when:
11270 * 1. the variable itself is locked
11271 * 2. the value of the variable is locked.
11272 * 3. the List or Dict value is locked.
11273 */
11274 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11275 || tv_islocked(&di->di_tv));
11276 }
11277 }
11278 }
11279 else if (lv.ll_range)
11280 EMSG(_("E745: Range not allowed"));
11281 else if (lv.ll_newkey != NULL)
11282 EMSG2(_(e_dictkey), lv.ll_newkey);
11283 else if (lv.ll_list != NULL)
11284 /* List item. */
11285 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11286 else
11287 /* Dictionary item. */
11288 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11289 }
11290 }
11291
11292 clear_lval(&lv);
11293}
11294
Bram Moolenaar33570922005-01-25 22:26:29 +000011295static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011296
11297/*
11298 * Turn a dict into a list:
11299 * "what" == 0: list of keys
11300 * "what" == 1: list of values
11301 * "what" == 2: list of items
11302 */
11303 static void
11304dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011305 typval_T *argvars;
11306 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011307 int what;
11308{
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 list_T *l2;
11310 dictitem_T *di;
11311 hashitem_T *hi;
11312 listitem_T *li;
11313 listitem_T *li2;
11314 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011315 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011316
11317 rettv->vval.v_number = 0;
11318 if (argvars[0].v_type != VAR_DICT)
11319 {
11320 EMSG(_(e_dictreq));
11321 return;
11322 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011323 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011324 return;
11325
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011326 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011327 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011328
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 todo = d->dv_hashtab.ht_used;
11330 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011332 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011334 --todo;
11335 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011336
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011337 li = listitem_alloc();
11338 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011339 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011340 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011341
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011342 if (what == 0)
11343 {
11344 /* keys() */
11345 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011346 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011347 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11348 }
11349 else if (what == 1)
11350 {
11351 /* values() */
11352 copy_tv(&di->di_tv, &li->li_tv);
11353 }
11354 else
11355 {
11356 /* items() */
11357 l2 = list_alloc();
11358 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011359 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011360 li->li_tv.vval.v_list = l2;
11361 if (l2 == NULL)
11362 break;
11363 ++l2->lv_refcount;
11364
11365 li2 = listitem_alloc();
11366 if (li2 == NULL)
11367 break;
11368 list_append(l2, li2);
11369 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011370 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011371 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11372
11373 li2 = listitem_alloc();
11374 if (li2 == NULL)
11375 break;
11376 list_append(l2, li2);
11377 copy_tv(&di->di_tv, &li2->li_tv);
11378 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011379 }
11380 }
11381}
11382
11383/*
11384 * "items(dict)" function
11385 */
11386 static void
11387f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011388 typval_T *argvars;
11389 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011390{
11391 dict_list(argvars, rettv, 2);
11392}
11393
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011395 * "join()" function
11396 */
11397 static void
11398f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *argvars;
11400 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011401{
11402 garray_T ga;
11403 char_u *sep;
11404
11405 rettv->vval.v_number = 0;
11406 if (argvars[0].v_type != VAR_LIST)
11407 {
11408 EMSG(_(e_listreq));
11409 return;
11410 }
11411 if (argvars[0].vval.v_list == NULL)
11412 return;
11413 if (argvars[1].v_type == VAR_UNKNOWN)
11414 sep = (char_u *)" ";
11415 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011417
11418 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011419
11420 if (sep != NULL)
11421 {
11422 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011423 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 ga_append(&ga, NUL);
11425 rettv->vval.v_string = (char_u *)ga.ga_data;
11426 }
11427 else
11428 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011429}
11430
11431/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011432 * "keys()" function
11433 */
11434 static void
11435f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 typval_T *argvars;
11437 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011438{
11439 dict_list(argvars, rettv, 0);
11440}
11441
11442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 * "last_buffer_nr()" function.
11444 */
11445/*ARGSUSED*/
11446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450{
11451 int n = 0;
11452 buf_T *buf;
11453
11454 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11455 if (n < buf->b_fnum)
11456 n = buf->b_fnum;
11457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011459}
11460
11461/*
11462 * "len()" function
11463 */
11464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 typval_T *argvars;
11467 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468{
11469 switch (argvars[0].v_type)
11470 {
11471 case VAR_STRING:
11472 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_number = (varnumber_T)STRLEN(
11474 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475 break;
11476 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011478 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011479 case VAR_DICT:
11480 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11481 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011482 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011483 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011484 break;
11485 }
11486}
11487
Bram Moolenaar33570922005-01-25 22:26:29 +000011488static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011489
11490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *argvars;
11493 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011494 int type;
11495{
11496#ifdef FEAT_LIBCALL
11497 char_u *string_in;
11498 char_u **string_result;
11499 int nr_result;
11500#endif
11501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011503 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011505 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011507
11508 if (check_restricted() || check_secure())
11509 return;
11510
11511#ifdef FEAT_LIBCALL
11512 /* The first two args must be strings, otherwise its meaningless */
11513 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11514 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011515 string_in = NULL;
11516 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011517 string_in = argvars[2].vval.v_string;
11518 if (type == VAR_NUMBER)
11519 string_result = NULL;
11520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011522 if (mch_libcall(argvars[0].vval.v_string,
11523 argvars[1].vval.v_string,
11524 string_in,
11525 argvars[2].vval.v_number,
11526 string_result,
11527 &nr_result) == OK
11528 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011530 }
11531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532}
11533
11534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 * "libcall()" function
11536 */
11537 static void
11538f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011539 typval_T *argvars;
11540 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011541{
11542 libcall_common(argvars, rettv, VAR_STRING);
11543}
11544
11545/*
11546 * "libcallnr()" function
11547 */
11548 static void
11549f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 typval_T *argvars;
11551 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011552{
11553 libcall_common(argvars, rettv, VAR_NUMBER);
11554}
11555
11556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 * "line(string)" function
11558 */
11559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011561 typval_T *argvars;
11562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563{
11564 linenr_T lnum = 0;
11565 pos_T *fp;
11566
11567 fp = var2fpos(&argvars[0], TRUE);
11568 if (fp != NULL)
11569 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571}
11572
11573/*
11574 * "line2byte(lnum)" function
11575 */
11576/*ARGSUSED*/
11577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011579 typval_T *argvars;
11580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581{
11582#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584#else
11585 linenr_T lnum;
11586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11592 if (rettv->vval.v_number >= 0)
11593 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594#endif
11595}
11596
11597/*
11598 * "lispindent(lnum)" function
11599 */
11600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011602 typval_T *argvars;
11603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
11605#ifdef FEAT_LISP
11606 pos_T pos;
11607 linenr_T lnum;
11608
11609 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11612 {
11613 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 curwin->w_cursor = pos;
11616 }
11617 else
11618#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620}
11621
11622/*
11623 * "localtime()" function
11624 */
11625/*ARGSUSED*/
11626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011628 typval_T *argvars;
11629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632}
11633
Bram Moolenaar33570922005-01-25 22:26:29 +000011634static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635
11636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 typval_T *argvars;
11639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 int exact;
11641{
11642 char_u *keys;
11643 char_u *which;
11644 char_u buf[NUMBUFLEN];
11645 char_u *keys_buf = NULL;
11646 char_u *rhs;
11647 int mode;
11648 garray_T ga;
11649
11650 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->v_type = VAR_STRING;
11652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 if (*keys == NUL)
11656 return;
11657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011658 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011659 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 else
11661 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011662 if (which == NULL)
11663 return;
11664
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 mode = get_map_mode(&which, 0);
11666
11667 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011668 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 vim_free(keys_buf);
11670 if (rhs != NULL)
11671 {
11672 ga_init(&ga);
11673 ga.ga_itemsize = 1;
11674 ga.ga_growsize = 40;
11675
11676 while (*rhs != NUL)
11677 ga_concat(&ga, str2special(&rhs, FALSE));
11678
11679 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 }
11682}
11683
11684/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011685 * "map()" function
11686 */
11687 static void
11688f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011689 typval_T *argvars;
11690 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011691{
11692 filter_map(argvars, rettv, TRUE);
11693}
11694
11695/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 */
11698 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011700 typval_T *argvars;
11701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011703 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704}
11705
11706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011707 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 */
11709 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011711 typval_T *argvars;
11712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011714 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715}
11716
Bram Moolenaar33570922005-01-25 22:26:29 +000011717static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718
11719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011721 typval_T *argvars;
11722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 int type;
11724{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011725 char_u *str = NULL;
11726 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 char_u *pat;
11728 regmatch_T regmatch;
11729 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011730 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 char_u *save_cpo;
11732 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011733 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011734 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011735 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011736 list_T *l = NULL;
11737 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011738 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011739 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740
11741 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11742 save_cpo = p_cpo;
11743 p_cpo = (char_u *)"";
11744
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011745 rettv->vval.v_number = -1;
11746 if (type == 3)
11747 {
11748 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011749 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011750 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011751 }
11752 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754 rettv->v_type = VAR_STRING;
11755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011758 if (argvars[0].v_type == VAR_LIST)
11759 {
11760 if ((l = argvars[0].vval.v_list) == NULL)
11761 goto theend;
11762 li = l->lv_first;
11763 }
11764 else
11765 expr = str = get_tv_string(&argvars[0]);
11766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011767 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11768 if (pat == NULL)
11769 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011770
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011771 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 int error = FALSE;
11774
11775 start = get_tv_number_chk(&argvars[2], &error);
11776 if (error)
11777 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011778 if (l != NULL)
11779 {
11780 li = list_find(l, start);
11781 if (li == NULL)
11782 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011783 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011784 }
11785 else
11786 {
11787 if (start < 0)
11788 start = 0;
11789 if (start > (long)STRLEN(str))
11790 goto theend;
11791 str += start;
11792 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011793
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011794 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 nth = get_tv_number_chk(&argvars[3], &error);
11796 if (error)
11797 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 }
11799
11800 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11801 if (regmatch.regprog != NULL)
11802 {
11803 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011804
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011805 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011806 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011807 if (l != NULL)
11808 {
11809 if (li == NULL)
11810 {
11811 match = FALSE;
11812 break;
11813 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011814 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011815 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011816 if (str == NULL)
11817 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011818 }
11819
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011820 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011821
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011822 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011823 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011824 if (l == NULL && !match)
11825 break;
11826
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011827 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011828 if (l != NULL)
11829 {
11830 li = li->li_next;
11831 ++idx;
11832 }
11833 else
11834 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011835#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011836 startcol = regmatch.startp[0]
11837 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011838#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011839 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011840#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011841 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011842 }
11843
11844 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011846 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011847 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011848 int i;
11849
11850 /* return list with matched string and submatches */
11851 for (i = 0; i < NSUBEXP; ++i)
11852 {
11853 if (regmatch.endp[i] == NULL)
11854 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011855 if (list_append_string(rettv->vval.v_list,
11856 regmatch.startp[i],
11857 (int)(regmatch.endp[i] - regmatch.startp[i]))
11858 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011859 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011860 }
11861 }
11862 else if (type == 2)
11863 {
11864 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011865 if (l != NULL)
11866 copy_tv(&li->li_tv, rettv);
11867 else
11868 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011870 }
11871 else if (l != NULL)
11872 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 else
11874 {
11875 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 (varnumber_T)(regmatch.startp[0] - str);
11878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011881 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 }
11883 }
11884 vim_free(regmatch.regprog);
11885 }
11886
11887theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011888 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 p_cpo = save_cpo;
11890}
11891
11892/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011893 * "match()" function
11894 */
11895 static void
11896f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011897 typval_T *argvars;
11898 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011899{
11900 find_some_match(argvars, rettv, 1);
11901}
11902
11903/*
11904 * "matchend()" function
11905 */
11906 static void
11907f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 typval_T *argvars;
11909 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011910{
11911 find_some_match(argvars, rettv, 0);
11912}
11913
11914/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011915 * "matchlist()" function
11916 */
11917 static void
11918f_matchlist(argvars, rettv)
11919 typval_T *argvars;
11920 typval_T *rettv;
11921{
11922 find_some_match(argvars, rettv, 3);
11923}
11924
11925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011926 * "matchstr()" function
11927 */
11928 static void
11929f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011930 typval_T *argvars;
11931 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011932{
11933 find_some_match(argvars, rettv, 2);
11934}
11935
Bram Moolenaar33570922005-01-25 22:26:29 +000011936static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011937
11938 static void
11939max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011940 typval_T *argvars;
11941 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011942 int domax;
11943{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011944 long n = 0;
11945 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011946 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011947
11948 if (argvars[0].v_type == VAR_LIST)
11949 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 list_T *l;
11951 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011952
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011953 l = argvars[0].vval.v_list;
11954 if (l != NULL)
11955 {
11956 li = l->lv_first;
11957 if (li != NULL)
11958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011960 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011961 {
11962 li = li->li_next;
11963 if (li == NULL)
11964 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011965 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011966 if (domax ? i > n : i < n)
11967 n = i;
11968 }
11969 }
11970 }
11971 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011972 else if (argvars[0].v_type == VAR_DICT)
11973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011974 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011975 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011977 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011978
11979 d = argvars[0].vval.v_dict;
11980 if (d != NULL)
11981 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011982 todo = d->dv_hashtab.ht_used;
11983 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011985 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011986 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011987 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011989 if (first)
11990 {
11991 n = i;
11992 first = FALSE;
11993 }
11994 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011995 n = i;
11996 }
11997 }
11998 }
11999 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012000 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012001 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012002 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012003}
12004
12005/*
12006 * "max()" function
12007 */
12008 static void
12009f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012010 typval_T *argvars;
12011 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012012{
12013 max_min(argvars, rettv, TRUE);
12014}
12015
12016/*
12017 * "min()" function
12018 */
12019 static void
12020f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012021 typval_T *argvars;
12022 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012023{
12024 max_min(argvars, rettv, FALSE);
12025}
12026
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012027static int mkdir_recurse __ARGS((char_u *dir, int prot));
12028
12029/*
12030 * Create the directory in which "dir" is located, and higher levels when
12031 * needed.
12032 */
12033 static int
12034mkdir_recurse(dir, prot)
12035 char_u *dir;
12036 int prot;
12037{
12038 char_u *p;
12039 char_u *updir;
12040 int r = FAIL;
12041
12042 /* Get end of directory name in "dir".
12043 * We're done when it's "/" or "c:/". */
12044 p = gettail_sep(dir);
12045 if (p <= get_past_head(dir))
12046 return OK;
12047
12048 /* If the directory exists we're done. Otherwise: create it.*/
12049 updir = vim_strnsave(dir, (int)(p - dir));
12050 if (updir == NULL)
12051 return FAIL;
12052 if (mch_isdir(updir))
12053 r = OK;
12054 else if (mkdir_recurse(updir, prot) == OK)
12055 r = vim_mkdir_emsg(updir, prot);
12056 vim_free(updir);
12057 return r;
12058}
12059
12060#ifdef vim_mkdir
12061/*
12062 * "mkdir()" function
12063 */
12064 static void
12065f_mkdir(argvars, rettv)
12066 typval_T *argvars;
12067 typval_T *rettv;
12068{
12069 char_u *dir;
12070 char_u buf[NUMBUFLEN];
12071 int prot = 0755;
12072
12073 rettv->vval.v_number = FAIL;
12074 if (check_restricted() || check_secure())
12075 return;
12076
12077 dir = get_tv_string_buf(&argvars[0], buf);
12078 if (argvars[1].v_type != VAR_UNKNOWN)
12079 {
12080 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 prot = get_tv_number_chk(&argvars[2], NULL);
12082 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012083 mkdir_recurse(dir, prot);
12084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012085 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012086}
12087#endif
12088
Bram Moolenaar0d660222005-01-07 21:51:51 +000012089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090 * "mode()" function
12091 */
12092/*ARGSUSED*/
12093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012095 typval_T *argvars;
12096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097{
12098 char_u buf[2];
12099
12100#ifdef FEAT_VISUAL
12101 if (VIsual_active)
12102 {
12103 if (VIsual_select)
12104 buf[0] = VIsual_mode + 's' - 'v';
12105 else
12106 buf[0] = VIsual_mode;
12107 }
12108 else
12109#endif
12110 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12111 buf[0] = 'r';
12112 else if (State & INSERT)
12113 {
12114 if (State & REPLACE_FLAG)
12115 buf[0] = 'R';
12116 else
12117 buf[0] = 'i';
12118 }
12119 else if (State & CMDLINE)
12120 buf[0] = 'c';
12121 else
12122 buf[0] = 'n';
12123
12124 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->vval.v_string = vim_strsave(buf);
12126 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127}
12128
12129/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130 * "nextnonblank()" function
12131 */
12132 static void
12133f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012134 typval_T *argvars;
12135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012136{
12137 linenr_T lnum;
12138
12139 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012141 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012142 {
12143 lnum = 0;
12144 break;
12145 }
12146 if (*skipwhite(ml_get(lnum)) != NUL)
12147 break;
12148 }
12149 rettv->vval.v_number = lnum;
12150}
12151
12152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 * "nr2char()" function
12154 */
12155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012157 typval_T *argvars;
12158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159{
12160 char_u buf[NUMBUFLEN];
12161
12162#ifdef FEAT_MBYTE
12163 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 else
12166#endif
12167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 buf[1] = NUL;
12170 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012171 rettv->v_type = VAR_STRING;
12172 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012173}
12174
12175/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012176 * "prevnonblank()" function
12177 */
12178 static void
12179f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012180 typval_T *argvars;
12181 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012182{
12183 linenr_T lnum;
12184
12185 lnum = get_tv_lnum(argvars);
12186 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12187 lnum = 0;
12188 else
12189 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12190 --lnum;
12191 rettv->vval.v_number = lnum;
12192}
12193
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012194#ifdef HAVE_STDARG_H
12195/* This dummy va_list is here because:
12196 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12197 * - locally in the function results in a "used before set" warning
12198 * - using va_start() to initialize it gives "function with fixed args" error */
12199static va_list ap;
12200#endif
12201
Bram Moolenaar8c711452005-01-14 21:53:12 +000012202/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012203 * "printf()" function
12204 */
12205 static void
12206f_printf(argvars, rettv)
12207 typval_T *argvars;
12208 typval_T *rettv;
12209{
12210 rettv->v_type = VAR_STRING;
12211 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012212#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012213 {
12214 char_u buf[NUMBUFLEN];
12215 int len;
12216 char_u *s;
12217 int saved_did_emsg = did_emsg;
12218 char *fmt;
12219
12220 /* Get the required length, allocate the buffer and do it for real. */
12221 did_emsg = FALSE;
12222 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012223 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012224 if (!did_emsg)
12225 {
12226 s = alloc(len + 1);
12227 if (s != NULL)
12228 {
12229 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012230 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012231 }
12232 }
12233 did_emsg |= saved_did_emsg;
12234 }
12235#endif
12236}
12237
12238/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012239 * "pumvisible()" function
12240 */
12241/*ARGSUSED*/
12242 static void
12243f_pumvisible(argvars, rettv)
12244 typval_T *argvars;
12245 typval_T *rettv;
12246{
12247 rettv->vval.v_number = 0;
12248#ifdef FEAT_INS_EXPAND
12249 if (pum_visible())
12250 rettv->vval.v_number = 1;
12251#endif
12252}
12253
12254/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012255 * "range()" function
12256 */
12257 static void
12258f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012259 typval_T *argvars;
12260 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012261{
12262 long start;
12263 long end;
12264 long stride = 1;
12265 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012266 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012268 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012269 if (argvars[1].v_type == VAR_UNKNOWN)
12270 {
12271 end = start - 1;
12272 start = 0;
12273 }
12274 else
12275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012276 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012277 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012278 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012279 }
12280
12281 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012282 if (error)
12283 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012284 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012285 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012286 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012287 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012288 else
12289 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012290 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012291 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012292 if (list_append_number(rettv->vval.v_list,
12293 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012294 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012295 }
12296}
12297
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012298/*
12299 * "readfile()" function
12300 */
12301 static void
12302f_readfile(argvars, rettv)
12303 typval_T *argvars;
12304 typval_T *rettv;
12305{
12306 int binary = FALSE;
12307 char_u *fname;
12308 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012309 listitem_T *li;
12310#define FREAD_SIZE 200 /* optimized for text lines */
12311 char_u buf[FREAD_SIZE];
12312 int readlen; /* size of last fread() */
12313 int buflen; /* nr of valid chars in buf[] */
12314 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12315 int tolist; /* first byte in buf[] still to be put in list */
12316 int chop; /* how many CR to chop off */
12317 char_u *prev = NULL; /* previously read bytes, if any */
12318 int prevlen = 0; /* length of "prev" if not NULL */
12319 char_u *s;
12320 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012321 long maxline = MAXLNUM;
12322 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012323
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012324 if (argvars[1].v_type != VAR_UNKNOWN)
12325 {
12326 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12327 binary = TRUE;
12328 if (argvars[2].v_type != VAR_UNKNOWN)
12329 maxline = get_tv_number(&argvars[2]);
12330 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012331
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012332 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012333 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012334
12335 /* Always open the file in binary mode, library functions have a mind of
12336 * their own about CR-LF conversion. */
12337 fname = get_tv_string(&argvars[0]);
12338 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12339 {
12340 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12341 return;
12342 }
12343
12344 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012345 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012346 {
12347 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12348 buflen = filtd + readlen;
12349 tolist = 0;
12350 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12351 {
12352 if (buf[filtd] == '\n' || readlen <= 0)
12353 {
12354 /* Only when in binary mode add an empty list item when the
12355 * last line ends in a '\n'. */
12356 if (!binary && readlen == 0 && filtd == 0)
12357 break;
12358
12359 /* Found end-of-line or end-of-file: add a text line to the
12360 * list. */
12361 chop = 0;
12362 if (!binary)
12363 while (filtd - chop - 1 >= tolist
12364 && buf[filtd - chop - 1] == '\r')
12365 ++chop;
12366 len = filtd - tolist - chop;
12367 if (prev == NULL)
12368 s = vim_strnsave(buf + tolist, len);
12369 else
12370 {
12371 s = alloc((unsigned)(prevlen + len + 1));
12372 if (s != NULL)
12373 {
12374 mch_memmove(s, prev, prevlen);
12375 vim_free(prev);
12376 prev = NULL;
12377 mch_memmove(s + prevlen, buf + tolist, len);
12378 s[prevlen + len] = NUL;
12379 }
12380 }
12381 tolist = filtd + 1;
12382
12383 li = listitem_alloc();
12384 if (li == NULL)
12385 {
12386 vim_free(s);
12387 break;
12388 }
12389 li->li_tv.v_type = VAR_STRING;
12390 li->li_tv.v_lock = 0;
12391 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012392 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012393
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012394 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012395 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012396 if (readlen <= 0)
12397 break;
12398 }
12399 else if (buf[filtd] == NUL)
12400 buf[filtd] = '\n';
12401 }
12402 if (readlen <= 0)
12403 break;
12404
12405 if (tolist == 0)
12406 {
12407 /* "buf" is full, need to move text to an allocated buffer */
12408 if (prev == NULL)
12409 {
12410 prev = vim_strnsave(buf, buflen);
12411 prevlen = buflen;
12412 }
12413 else
12414 {
12415 s = alloc((unsigned)(prevlen + buflen));
12416 if (s != NULL)
12417 {
12418 mch_memmove(s, prev, prevlen);
12419 mch_memmove(s + prevlen, buf, buflen);
12420 vim_free(prev);
12421 prev = s;
12422 prevlen += buflen;
12423 }
12424 }
12425 filtd = 0;
12426 }
12427 else
12428 {
12429 mch_memmove(buf, buf + tolist, buflen - tolist);
12430 filtd -= tolist;
12431 }
12432 }
12433
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012434 /*
12435 * For a negative line count use only the lines at the end of the file,
12436 * free the rest.
12437 */
12438 if (maxline < 0)
12439 while (cnt > -maxline)
12440 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012441 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012442 --cnt;
12443 }
12444
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012445 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012446 fclose(fd);
12447}
12448
12449
Bram Moolenaar0d660222005-01-07 21:51:51 +000012450#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12451static void make_connection __ARGS((void));
12452static int check_connection __ARGS((void));
12453
12454 static void
12455make_connection()
12456{
12457 if (X_DISPLAY == NULL
12458# ifdef FEAT_GUI
12459 && !gui.in_use
12460# endif
12461 )
12462 {
12463 x_force_connect = TRUE;
12464 setup_term_clip();
12465 x_force_connect = FALSE;
12466 }
12467}
12468
12469 static int
12470check_connection()
12471{
12472 make_connection();
12473 if (X_DISPLAY == NULL)
12474 {
12475 EMSG(_("E240: No connection to Vim server"));
12476 return FAIL;
12477 }
12478 return OK;
12479}
12480#endif
12481
12482#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012483static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012484
12485 static void
12486remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012487 typval_T *argvars;
12488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012489 int expr;
12490{
12491 char_u *server_name;
12492 char_u *keys;
12493 char_u *r = NULL;
12494 char_u buf[NUMBUFLEN];
12495# ifdef WIN32
12496 HWND w;
12497# else
12498 Window w;
12499# endif
12500
12501 if (check_restricted() || check_secure())
12502 return;
12503
12504# ifdef FEAT_X11
12505 if (check_connection() == FAIL)
12506 return;
12507# endif
12508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 server_name = get_tv_string_chk(&argvars[0]);
12510 if (server_name == NULL)
12511 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012512 keys = get_tv_string_buf(&argvars[1], buf);
12513# ifdef WIN32
12514 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12515# else
12516 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12517 < 0)
12518# endif
12519 {
12520 if (r != NULL)
12521 EMSG(r); /* sending worked but evaluation failed */
12522 else
12523 EMSG2(_("E241: Unable to send to %s"), server_name);
12524 return;
12525 }
12526
12527 rettv->vval.v_string = r;
12528
12529 if (argvars[2].v_type != VAR_UNKNOWN)
12530 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012532 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012533 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534
12535 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012536 v.di_tv.v_type = VAR_STRING;
12537 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012538 idvar = get_tv_string_chk(&argvars[2]);
12539 if (idvar != NULL)
12540 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012541 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542 }
12543}
12544#endif
12545
12546/*
12547 * "remote_expr()" function
12548 */
12549/*ARGSUSED*/
12550 static void
12551f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012552 typval_T *argvars;
12553 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012554{
12555 rettv->v_type = VAR_STRING;
12556 rettv->vval.v_string = NULL;
12557#ifdef FEAT_CLIENTSERVER
12558 remote_common(argvars, rettv, TRUE);
12559#endif
12560}
12561
12562/*
12563 * "remote_foreground()" function
12564 */
12565/*ARGSUSED*/
12566 static void
12567f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012568 typval_T *argvars;
12569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570{
12571 rettv->vval.v_number = 0;
12572#ifdef FEAT_CLIENTSERVER
12573# ifdef WIN32
12574 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012575 {
12576 char_u *server_name = get_tv_string_chk(&argvars[0]);
12577
12578 if (server_name != NULL)
12579 serverForeground(server_name);
12580 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581# else
12582 /* Send a foreground() expression to the server. */
12583 argvars[1].v_type = VAR_STRING;
12584 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12585 argvars[2].v_type = VAR_UNKNOWN;
12586 remote_common(argvars, rettv, TRUE);
12587 vim_free(argvars[1].vval.v_string);
12588# endif
12589#endif
12590}
12591
12592/*ARGSUSED*/
12593 static void
12594f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012595 typval_T *argvars;
12596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012597{
12598#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012599 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600 char_u *s = NULL;
12601# ifdef WIN32
12602 int n = 0;
12603# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012605
12606 if (check_restricted() || check_secure())
12607 {
12608 rettv->vval.v_number = -1;
12609 return;
12610 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611 serverid = get_tv_string_chk(&argvars[0]);
12612 if (serverid == NULL)
12613 {
12614 rettv->vval.v_number = -1;
12615 return; /* type error; errmsg already given */
12616 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012618 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 if (n == 0)
12620 rettv->vval.v_number = -1;
12621 else
12622 {
12623 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12624 rettv->vval.v_number = (s != NULL);
12625 }
12626# else
12627 rettv->vval.v_number = 0;
12628 if (check_connection() == FAIL)
12629 return;
12630
12631 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012632 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012633# endif
12634
12635 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012637 char_u *retvar;
12638
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 v.di_tv.v_type = VAR_STRING;
12640 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 retvar = get_tv_string_chk(&argvars[1]);
12642 if (retvar != NULL)
12643 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645 }
12646#else
12647 rettv->vval.v_number = -1;
12648#endif
12649}
12650
12651/*ARGSUSED*/
12652 static void
12653f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012654 typval_T *argvars;
12655 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012656{
12657 char_u *r = NULL;
12658
12659#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012660 char_u *serverid = get_tv_string_chk(&argvars[0]);
12661
12662 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012663 {
12664# ifdef WIN32
12665 /* The server's HWND is encoded in the 'id' parameter */
12666 int n = 0;
12667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012668 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012669 if (n != 0)
12670 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12671 if (r == NULL)
12672# else
12673 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675# endif
12676 EMSG(_("E277: Unable to read a server reply"));
12677 }
12678#endif
12679 rettv->v_type = VAR_STRING;
12680 rettv->vval.v_string = r;
12681}
12682
12683/*
12684 * "remote_send()" function
12685 */
12686/*ARGSUSED*/
12687 static void
12688f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012689 typval_T *argvars;
12690 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012691{
12692 rettv->v_type = VAR_STRING;
12693 rettv->vval.v_string = NULL;
12694#ifdef FEAT_CLIENTSERVER
12695 remote_common(argvars, rettv, FALSE);
12696#endif
12697}
12698
12699/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012700 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 */
12702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012704 typval_T *argvars;
12705 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012706{
Bram Moolenaar33570922005-01-25 22:26:29 +000012707 list_T *l;
12708 listitem_T *item, *item2;
12709 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012711 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012712 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012713 dict_T *d;
12714 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012715
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012716 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012717 if (argvars[0].v_type == VAR_DICT)
12718 {
12719 if (argvars[2].v_type != VAR_UNKNOWN)
12720 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012721 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012722 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 key = get_tv_string_chk(&argvars[1]);
12725 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 di = dict_find(d, key, -1);
12728 if (di == NULL)
12729 EMSG2(_(e_dictkey), key);
12730 else
12731 {
12732 *rettv = di->di_tv;
12733 init_tv(&di->di_tv);
12734 dictitem_remove(d, di);
12735 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012737 }
12738 }
12739 else if (argvars[0].v_type != VAR_LIST)
12740 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012741 else if ((l = argvars[0].vval.v_list) != NULL
12742 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 int error = FALSE;
12745
12746 idx = get_tv_number_chk(&argvars[1], &error);
12747 if (error)
12748 ; /* type error: do nothing, errmsg already given */
12749 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012750 EMSGN(_(e_listidx), idx);
12751 else
12752 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012753 if (argvars[2].v_type == VAR_UNKNOWN)
12754 {
12755 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012756 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012757 *rettv = item->li_tv;
12758 vim_free(item);
12759 }
12760 else
12761 {
12762 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012763 end = get_tv_number_chk(&argvars[2], &error);
12764 if (error)
12765 ; /* type error: do nothing */
12766 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012767 EMSGN(_(e_listidx), end);
12768 else
12769 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012770 int cnt = 0;
12771
12772 for (li = item; li != NULL; li = li->li_next)
12773 {
12774 ++cnt;
12775 if (li == item2)
12776 break;
12777 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012778 if (li == NULL) /* didn't find "item2" after "item" */
12779 EMSG(_(e_invrange));
12780 else
12781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012782 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012783 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012784 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012785 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012786 l->lv_first = item;
12787 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012788 item->li_prev = NULL;
12789 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012790 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012791 }
12792 }
12793 }
12794 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012795 }
12796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797}
12798
12799/*
12800 * "rename({from}, {to})" function
12801 */
12802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012804 typval_T *argvars;
12805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806{
12807 char_u buf[NUMBUFLEN];
12808
12809 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12813 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814}
12815
12816/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012817 * "repeat()" function
12818 */
12819/*ARGSUSED*/
12820 static void
12821f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012822 typval_T *argvars;
12823 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012824{
12825 char_u *p;
12826 int n;
12827 int slen;
12828 int len;
12829 char_u *r;
12830 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012831
12832 n = get_tv_number(&argvars[1]);
12833 if (argvars[0].v_type == VAR_LIST)
12834 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012835 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012836 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012837 if (list_extend(rettv->vval.v_list,
12838 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012839 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012840 }
12841 else
12842 {
12843 p = get_tv_string(&argvars[0]);
12844 rettv->v_type = VAR_STRING;
12845 rettv->vval.v_string = NULL;
12846
12847 slen = (int)STRLEN(p);
12848 len = slen * n;
12849 if (len <= 0)
12850 return;
12851
12852 r = alloc(len + 1);
12853 if (r != NULL)
12854 {
12855 for (i = 0; i < n; i++)
12856 mch_memmove(r + i * slen, p, (size_t)slen);
12857 r[len] = NUL;
12858 }
12859
12860 rettv->vval.v_string = r;
12861 }
12862}
12863
12864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 * "resolve()" function
12866 */
12867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012869 typval_T *argvars;
12870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871{
12872 char_u *p;
12873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875#ifdef FEAT_SHORTCUT
12876 {
12877 char_u *v = NULL;
12878
12879 v = mch_resolve_shortcut(p);
12880 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 }
12885#else
12886# ifdef HAVE_READLINK
12887 {
12888 char_u buf[MAXPATHL + 1];
12889 char_u *cpy;
12890 int len;
12891 char_u *remain = NULL;
12892 char_u *q;
12893 int is_relative_to_current = FALSE;
12894 int has_trailing_pathsep = FALSE;
12895 int limit = 100;
12896
12897 p = vim_strsave(p);
12898
12899 if (p[0] == '.' && (vim_ispathsep(p[1])
12900 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12901 is_relative_to_current = TRUE;
12902
12903 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012904 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 has_trailing_pathsep = TRUE;
12906
12907 q = getnextcomp(p);
12908 if (*q != NUL)
12909 {
12910 /* Separate the first path component in "p", and keep the
12911 * remainder (beginning with the path separator). */
12912 remain = vim_strsave(q - 1);
12913 q[-1] = NUL;
12914 }
12915
12916 for (;;)
12917 {
12918 for (;;)
12919 {
12920 len = readlink((char *)p, (char *)buf, MAXPATHL);
12921 if (len <= 0)
12922 break;
12923 buf[len] = NUL;
12924
12925 if (limit-- == 0)
12926 {
12927 vim_free(p);
12928 vim_free(remain);
12929 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 goto fail;
12932 }
12933
12934 /* Ensure that the result will have a trailing path separator
12935 * if the argument has one. */
12936 if (remain == NULL && has_trailing_pathsep)
12937 add_pathsep(buf);
12938
12939 /* Separate the first path component in the link value and
12940 * concatenate the remainders. */
12941 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12942 if (*q != NUL)
12943 {
12944 if (remain == NULL)
12945 remain = vim_strsave(q - 1);
12946 else
12947 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012948 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 if (cpy != NULL)
12950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 vim_free(remain);
12952 remain = cpy;
12953 }
12954 }
12955 q[-1] = NUL;
12956 }
12957
12958 q = gettail(p);
12959 if (q > p && *q == NUL)
12960 {
12961 /* Ignore trailing path separator. */
12962 q[-1] = NUL;
12963 q = gettail(p);
12964 }
12965 if (q > p && !mch_isFullName(buf))
12966 {
12967 /* symlink is relative to directory of argument */
12968 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12969 if (cpy != NULL)
12970 {
12971 STRCPY(cpy, p);
12972 STRCPY(gettail(cpy), buf);
12973 vim_free(p);
12974 p = cpy;
12975 }
12976 }
12977 else
12978 {
12979 vim_free(p);
12980 p = vim_strsave(buf);
12981 }
12982 }
12983
12984 if (remain == NULL)
12985 break;
12986
12987 /* Append the first path component of "remain" to "p". */
12988 q = getnextcomp(remain + 1);
12989 len = q - remain - (*q != NUL);
12990 cpy = vim_strnsave(p, STRLEN(p) + len);
12991 if (cpy != NULL)
12992 {
12993 STRNCAT(cpy, remain, len);
12994 vim_free(p);
12995 p = cpy;
12996 }
12997 /* Shorten "remain". */
12998 if (*q != NUL)
12999 STRCPY(remain, q - 1);
13000 else
13001 {
13002 vim_free(remain);
13003 remain = NULL;
13004 }
13005 }
13006
13007 /* If the result is a relative path name, make it explicitly relative to
13008 * the current directory if and only if the argument had this form. */
13009 if (!vim_ispathsep(*p))
13010 {
13011 if (is_relative_to_current
13012 && *p != NUL
13013 && !(p[0] == '.'
13014 && (p[1] == NUL
13015 || vim_ispathsep(p[1])
13016 || (p[1] == '.'
13017 && (p[2] == NUL
13018 || vim_ispathsep(p[2]))))))
13019 {
13020 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013021 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 if (cpy != NULL)
13023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 vim_free(p);
13025 p = cpy;
13026 }
13027 }
13028 else if (!is_relative_to_current)
13029 {
13030 /* Strip leading "./". */
13031 q = p;
13032 while (q[0] == '.' && vim_ispathsep(q[1]))
13033 q += 2;
13034 if (q > p)
13035 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13036 }
13037 }
13038
13039 /* Ensure that the result will have no trailing path separator
13040 * if the argument had none. But keep "/" or "//". */
13041 if (!has_trailing_pathsep)
13042 {
13043 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013044 if (after_pathsep(p, q))
13045 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046 }
13047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 }
13050# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013051 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052# endif
13053#endif
13054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056
13057#ifdef HAVE_READLINK
13058fail:
13059#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061}
13062
13063/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013064 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065 */
13066 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013067f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013068 typval_T *argvars;
13069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070{
Bram Moolenaar33570922005-01-25 22:26:29 +000013071 list_T *l;
13072 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073
Bram Moolenaar0d660222005-01-07 21:51:51 +000013074 rettv->vval.v_number = 0;
13075 if (argvars[0].v_type != VAR_LIST)
13076 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013077 else if ((l = argvars[0].vval.v_list) != NULL
13078 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013079 {
13080 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013081 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013082 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013083 while (li != NULL)
13084 {
13085 ni = li->li_prev;
13086 list_append(l, li);
13087 li = ni;
13088 }
13089 rettv->vval.v_list = l;
13090 rettv->v_type = VAR_LIST;
13091 ++l->lv_refcount;
13092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093}
13094
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013095#define SP_NOMOVE 1 /* don't move cursor */
13096#define SP_REPEAT 2 /* repeat to find outer pair */
13097#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013098#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013099
Bram Moolenaar33570922005-01-25 22:26:29 +000013100static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013101
13102/*
13103 * Get flags for a search function.
13104 * Possibly sets "p_ws".
13105 * Returns BACKWARD, FORWARD or zero (for an error).
13106 */
13107 static int
13108get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013109 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013110 int *flagsp;
13111{
13112 int dir = FORWARD;
13113 char_u *flags;
13114 char_u nbuf[NUMBUFLEN];
13115 int mask;
13116
13117 if (varp->v_type != VAR_UNKNOWN)
13118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013119 flags = get_tv_string_buf_chk(varp, nbuf);
13120 if (flags == NULL)
13121 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013122 while (*flags != NUL)
13123 {
13124 switch (*flags)
13125 {
13126 case 'b': dir = BACKWARD; break;
13127 case 'w': p_ws = TRUE; break;
13128 case 'W': p_ws = FALSE; break;
13129 default: mask = 0;
13130 if (flagsp != NULL)
13131 switch (*flags)
13132 {
13133 case 'n': mask = SP_NOMOVE; break;
13134 case 'r': mask = SP_REPEAT; break;
13135 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013136 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013137 }
13138 if (mask == 0)
13139 {
13140 EMSG2(_(e_invarg2), flags);
13141 dir = 0;
13142 }
13143 else
13144 *flagsp |= mask;
13145 }
13146 if (dir == 0)
13147 break;
13148 ++flags;
13149 }
13150 }
13151 return dir;
13152}
13153
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013155 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013157 static int
13158search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013159 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013160 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161{
13162 char_u *pat;
13163 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013164 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 int save_p_ws = p_ws;
13166 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013167 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013168 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013169 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013172 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13173 if (dir == 0)
13174 goto theend;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013175
13176 /* Optional extra argument: line number to stop searching. */
13177 if (argvars[1].v_type != VAR_UNKNOWN
13178 && argvars[2].v_type != VAR_UNKNOWN)
13179 {
13180 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13181 if (lnum_stop < 0)
13182 goto theend;
13183 }
13184
Bram Moolenaar231334e2005-07-25 20:46:57 +000013185 /*
13186 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13187 * Check to make sure only those flags are set.
13188 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13189 * flags cannot be set. Check for that condition also.
13190 */
13191 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13192 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013195 goto theend;
13196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013198 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013200 SEARCH_KEEP, RE_SEARCH, (linenr_T)lnum_stop) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013202 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013203 if (flags & SP_SETPCMARK)
13204 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013206 if (match_pos != NULL)
13207 {
13208 /* Store the match cursor position */
13209 match_pos->lnum = pos.lnum;
13210 match_pos->col = pos.col + 1;
13211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 /* "/$" will put the cursor after the end of the line, may need to
13213 * correct that here */
13214 check_cursor();
13215 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013216
13217 /* If 'n' flag is used: restore cursor position. */
13218 if (flags & SP_NOMOVE)
13219 curwin->w_cursor = save_cursor;
13220theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013222
13223 return retval;
13224}
13225
13226/*
13227 * "search()" function
13228 */
13229 static void
13230f_search(argvars, rettv)
13231 typval_T *argvars;
13232 typval_T *rettv;
13233{
13234 rettv->vval.v_number = search_cmn(argvars, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235}
13236
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013238 * "searchdecl()" function
13239 */
13240 static void
13241f_searchdecl(argvars, rettv)
13242 typval_T *argvars;
13243 typval_T *rettv;
13244{
13245 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013246 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013247 int error = FALSE;
13248 char_u *name;
13249
13250 rettv->vval.v_number = 1; /* default: FAIL */
13251
13252 name = get_tv_string_chk(&argvars[0]);
13253 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013254 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013255 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013256 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13257 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13258 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013259 if (!error && name != NULL)
13260 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013261 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013262}
13263
13264/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013265 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013267 static int
13268searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013269 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013270 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271{
13272 char_u *spat, *mpat, *epat;
13273 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 int dir;
13276 int flags = 0;
13277 char_u nbuf1[NUMBUFLEN];
13278 char_u nbuf2[NUMBUFLEN];
13279 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013280 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013281 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013284 spat = get_tv_string_chk(&argvars[0]);
13285 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13286 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13287 if (spat == NULL || mpat == NULL || epat == NULL)
13288 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 /* Handle the optional fourth argument: flags */
13291 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013292 if (dir == 0)
13293 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013294 /*
13295 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13296 */
13297 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13298 {
13299 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13300 goto theend;
13301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013303 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013304 if (argvars[3].v_type == VAR_UNKNOWN
13305 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 skip = (char_u *)"";
13307 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013309 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013310 if (argvars[5].v_type != VAR_UNKNOWN)
13311 {
13312 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13313 if (lnum_stop < 0)
13314 goto theend;
13315 }
13316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 if (skip == NULL)
13318 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013320 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13321 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013322
13323theend:
13324 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013325
13326 return retval;
13327}
13328
13329/*
13330 * "searchpair()" function
13331 */
13332 static void
13333f_searchpair(argvars, rettv)
13334 typval_T *argvars;
13335 typval_T *rettv;
13336{
13337 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13338}
13339
13340/*
13341 * "searchpairpos()" function
13342 */
13343 static void
13344f_searchpairpos(argvars, rettv)
13345 typval_T *argvars;
13346 typval_T *rettv;
13347{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013348 pos_T match_pos;
13349 int lnum = 0;
13350 int col = 0;
13351
13352 rettv->vval.v_number = 0;
13353
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013354 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013355 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013356
13357 if (searchpair_cmn(argvars, &match_pos) > 0)
13358 {
13359 lnum = match_pos.lnum;
13360 col = match_pos.col;
13361 }
13362
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013363 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13364 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013365}
13366
13367/*
13368 * Search for a start/middle/end thing.
13369 * Used by searchpair(), see its documentation for the details.
13370 * Returns 0 or -1 for no match,
13371 */
13372 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013373do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013374 char_u *spat; /* start pattern */
13375 char_u *mpat; /* middle pattern */
13376 char_u *epat; /* end pattern */
13377 int dir; /* BACKWARD or FORWARD */
13378 char_u *skip; /* skip expression */
13379 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013380 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013381 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013382{
13383 char_u *save_cpo;
13384 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13385 long retval = 0;
13386 pos_T pos;
13387 pos_T firstpos;
13388 pos_T foundpos;
13389 pos_T save_cursor;
13390 pos_T save_pos;
13391 int n;
13392 int r;
13393 int nest = 1;
13394 int err;
13395
13396 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13397 save_cpo = p_cpo;
13398 p_cpo = (char_u *)"";
13399
13400 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13401 * start/middle/end (pat3, for the top pair). */
13402 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13403 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13404 if (pat2 == NULL || pat3 == NULL)
13405 goto theend;
13406 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13407 if (*mpat == NUL)
13408 STRCPY(pat3, pat2);
13409 else
13410 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13411 spat, epat, mpat);
13412
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 save_cursor = curwin->w_cursor;
13414 pos = curwin->w_cursor;
13415 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013416 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 pat = pat3;
13418 for (;;)
13419 {
13420 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013421 SEARCH_KEEP, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13423 /* didn't find it or found the first match again: FAIL */
13424 break;
13425
13426 if (firstpos.lnum == 0)
13427 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013428 if (equalpos(pos, foundpos))
13429 {
13430 /* Found the same position again. Can happen with a pattern that
13431 * has "\zs" at the end and searching backwards. Advance one
13432 * character and try again. */
13433 if (dir == BACKWARD)
13434 decl(&pos);
13435 else
13436 incl(&pos);
13437 }
13438 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439
13440 /* If the skip pattern matches, ignore this match. */
13441 if (*skip != NUL)
13442 {
13443 save_pos = curwin->w_cursor;
13444 curwin->w_cursor = pos;
13445 r = eval_to_bool(skip, &err, NULL, FALSE);
13446 curwin->w_cursor = save_pos;
13447 if (err)
13448 {
13449 /* Evaluating {skip} caused an error, break here. */
13450 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013451 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 break;
13453 }
13454 if (r)
13455 continue;
13456 }
13457
13458 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13459 {
13460 /* Found end when searching backwards or start when searching
13461 * forward: nested pair. */
13462 ++nest;
13463 pat = pat2; /* nested, don't search for middle */
13464 }
13465 else
13466 {
13467 /* Found end when searching forward or start when searching
13468 * backward: end of (nested) pair; or found middle in outer pair. */
13469 if (--nest == 1)
13470 pat = pat3; /* outer level, search for middle */
13471 }
13472
13473 if (nest == 0)
13474 {
13475 /* Found the match: return matchcount or line number. */
13476 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013477 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013479 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013480 if (flags & SP_SETPCMARK)
13481 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 curwin->w_cursor = pos;
13483 if (!(flags & SP_REPEAT))
13484 break;
13485 nest = 1; /* search for next unmatched */
13486 }
13487 }
13488
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013489 if (match_pos != NULL)
13490 {
13491 /* Store the match cursor position */
13492 match_pos->lnum = curwin->w_cursor.lnum;
13493 match_pos->col = curwin->w_cursor.col + 1;
13494 }
13495
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013497 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 curwin->w_cursor = save_cursor;
13499
13500theend:
13501 vim_free(pat2);
13502 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013504
13505 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506}
13507
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013508/*
13509 * "searchpos()" function
13510 */
13511 static void
13512f_searchpos(argvars, rettv)
13513 typval_T *argvars;
13514 typval_T *rettv;
13515{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013516 pos_T match_pos;
13517 int lnum = 0;
13518 int col = 0;
13519
13520 rettv->vval.v_number = 0;
13521
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013522 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013523 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013524
13525 if (search_cmn(argvars, &match_pos) > 0)
13526 {
13527 lnum = match_pos.lnum;
13528 col = match_pos.col;
13529 }
13530
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013531 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13532 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013533
13534}
13535
13536
Bram Moolenaar0d660222005-01-07 21:51:51 +000013537/*ARGSUSED*/
13538 static void
13539f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013540 typval_T *argvars;
13541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013543#ifdef FEAT_CLIENTSERVER
13544 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 char_u *server = get_tv_string_chk(&argvars[0]);
13546 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547
Bram Moolenaar0d660222005-01-07 21:51:51 +000013548 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 if (server == NULL || reply == NULL)
13550 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013551 if (check_restricted() || check_secure())
13552 return;
13553# ifdef FEAT_X11
13554 if (check_connection() == FAIL)
13555 return;
13556# endif
13557
13558 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013560 EMSG(_("E258: Unable to send to client"));
13561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013563 rettv->vval.v_number = 0;
13564#else
13565 rettv->vval.v_number = -1;
13566#endif
13567}
13568
13569/*ARGSUSED*/
13570 static void
13571f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013572 typval_T *argvars;
13573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013574{
13575 char_u *r = NULL;
13576
13577#ifdef FEAT_CLIENTSERVER
13578# ifdef WIN32
13579 r = serverGetVimNames();
13580# else
13581 make_connection();
13582 if (X_DISPLAY != NULL)
13583 r = serverGetVimNames(X_DISPLAY);
13584# endif
13585#endif
13586 rettv->v_type = VAR_STRING;
13587 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588}
13589
13590/*
13591 * "setbufvar()" function
13592 */
13593/*ARGSUSED*/
13594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 typval_T *argvars;
13597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598{
13599 buf_T *buf;
13600#ifdef FEAT_AUTOCMD
13601 aco_save_T aco;
13602#else
13603 buf_T *save_curbuf;
13604#endif
13605 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013606 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 char_u nbuf[NUMBUFLEN];
13608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 rettv->vval.v_number = 0;
13610
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 if (check_restricted() || check_secure())
13612 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13614 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 varp = &argvars[2];
13617
13618 if (buf != NULL && varname != NULL && varp != NULL)
13619 {
13620 /* set curbuf to be our buf, temporarily */
13621#ifdef FEAT_AUTOCMD
13622 aucmd_prepbuf(&aco, buf);
13623#else
13624 save_curbuf = curbuf;
13625 curbuf = buf;
13626#endif
13627
13628 if (*varname == '&')
13629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 long numval;
13631 char_u *strval;
13632 int error = FALSE;
13633
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 numval = get_tv_number_chk(varp, &error);
13636 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013637 if (!error && strval != NULL)
13638 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 }
13640 else
13641 {
13642 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13643 if (bufvarname != NULL)
13644 {
13645 STRCPY(bufvarname, "b:");
13646 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013647 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 vim_free(bufvarname);
13649 }
13650 }
13651
13652 /* reset notion of buffer */
13653#ifdef FEAT_AUTOCMD
13654 aucmd_restbuf(&aco);
13655#else
13656 curbuf = save_curbuf;
13657#endif
13658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659}
13660
13661/*
13662 * "setcmdpos()" function
13663 */
13664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *argvars;
13667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 int pos = (int)get_tv_number(&argvars[0]) - 1;
13670
13671 if (pos >= 0)
13672 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673}
13674
13675/*
13676 * "setline()" function
13677 */
13678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *argvars;
13681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682{
13683 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013684 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013685 list_T *l = NULL;
13686 listitem_T *li = NULL;
13687 long added = 0;
13688 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013690 lnum = get_tv_lnum(&argvars[0]);
13691 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013693 l = argvars[1].vval.v_list;
13694 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013696 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013698
13699 rettv->vval.v_number = 0; /* OK */
13700 for (;;)
13701 {
13702 if (l != NULL)
13703 {
13704 /* list argument, get next string */
13705 if (li == NULL)
13706 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013708 li = li->li_next;
13709 }
13710
13711 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013712 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013713 break;
13714 if (lnum <= curbuf->b_ml.ml_line_count)
13715 {
13716 /* existing line, replace it */
13717 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13718 {
13719 changed_bytes(lnum, 0);
13720 check_cursor_col();
13721 rettv->vval.v_number = 0; /* OK */
13722 }
13723 }
13724 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13725 {
13726 /* lnum is one past the last line, append the line */
13727 ++added;
13728 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13729 rettv->vval.v_number = 0; /* OK */
13730 }
13731
13732 if (l == NULL) /* only one string argument */
13733 break;
13734 ++lnum;
13735 }
13736
13737 if (added > 0)
13738 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739}
13740
13741/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013742 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013743 */
13744/*ARGSUSED*/
13745 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013746set_qf_ll_list(wp, list_arg, action_arg, rettv)
13747 win_T *wp;
13748 typval_T *list_arg;
13749 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013750 typval_T *rettv;
13751{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013752#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013753 char_u *act;
13754 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013755#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013756
Bram Moolenaar2641f772005-03-25 21:58:17 +000013757 rettv->vval.v_number = -1;
13758
13759#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013760 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013761 EMSG(_(e_listreq));
13762 else
13763 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013764 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013765
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013766 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013767 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013768 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 if (act == NULL)
13770 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013771 if (*act == 'a' || *act == 'r')
13772 action = *act;
13773 }
13774
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013775 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013776 rettv->vval.v_number = 0;
13777 }
13778#endif
13779}
13780
13781/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013782 * "setloclist()" function
13783 */
13784/*ARGSUSED*/
13785 static void
13786f_setloclist(argvars, rettv)
13787 typval_T *argvars;
13788 typval_T *rettv;
13789{
13790 win_T *win;
13791
13792 rettv->vval.v_number = -1;
13793
13794 win = find_win_by_nr(&argvars[0]);
13795 if (win != NULL)
13796 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13797}
13798
13799/*
13800 * "setqflist()" function
13801 */
13802/*ARGSUSED*/
13803 static void
13804f_setqflist(argvars, rettv)
13805 typval_T *argvars;
13806 typval_T *rettv;
13807{
13808 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13809}
13810
13811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 * "setreg()" function
13813 */
13814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013815f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013816 typval_T *argvars;
13817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818{
13819 int regname;
13820 char_u *strregname;
13821 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 int append;
13824 char_u yank_type;
13825 long block_len;
13826
13827 block_len = -1;
13828 yank_type = MAUTO;
13829 append = FALSE;
13830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013832 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013834 if (strregname == NULL)
13835 return; /* type error; errmsg already given */
13836 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 if (regname == 0 || regname == '@')
13838 regname = '"';
13839 else if (regname == '=')
13840 return;
13841
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 stropt = get_tv_string_chk(&argvars[2]);
13845 if (stropt == NULL)
13846 return; /* type error */
13847 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 switch (*stropt)
13849 {
13850 case 'a': case 'A': /* append */
13851 append = TRUE;
13852 break;
13853 case 'v': case 'c': /* character-wise selection */
13854 yank_type = MCHAR;
13855 break;
13856 case 'V': case 'l': /* line-wise selection */
13857 yank_type = MLINE;
13858 break;
13859#ifdef FEAT_VISUAL
13860 case 'b': case Ctrl_V: /* block-wise selection */
13861 yank_type = MBLOCK;
13862 if (VIM_ISDIGIT(stropt[1]))
13863 {
13864 ++stropt;
13865 block_len = getdigits(&stropt) - 1;
13866 --stropt;
13867 }
13868 break;
13869#endif
13870 }
13871 }
13872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 strval = get_tv_string_chk(&argvars[1]);
13874 if (strval != NULL)
13875 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013877 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878}
13879
13880
13881/*
13882 * "setwinvar(expr)" function
13883 */
13884/*ARGSUSED*/
13885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013886f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013887 typval_T *argvars;
13888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889{
13890 win_T *win;
13891#ifdef FEAT_WINDOWS
13892 win_T *save_curwin;
13893#endif
13894 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013895 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 char_u nbuf[NUMBUFLEN];
13897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 rettv->vval.v_number = 0;
13899
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 if (check_restricted() || check_secure())
13901 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013903 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 varp = &argvars[2];
13905
13906 if (win != NULL && varname != NULL && varp != NULL)
13907 {
13908#ifdef FEAT_WINDOWS
13909 /* set curwin to be our win, temporarily */
13910 save_curwin = curwin;
13911 curwin = win;
13912 curbuf = curwin->w_buffer;
13913#endif
13914
13915 if (*varname == '&')
13916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013917 long numval;
13918 char_u *strval;
13919 int error = FALSE;
13920
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922 numval = get_tv_number_chk(varp, &error);
13923 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013924 if (!error && strval != NULL)
13925 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926 }
13927 else
13928 {
13929 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13930 if (winvarname != NULL)
13931 {
13932 STRCPY(winvarname, "w:");
13933 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013934 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 vim_free(winvarname);
13936 }
13937 }
13938
13939#ifdef FEAT_WINDOWS
13940 /* Restore current window, if it's still valid (autocomands can make
13941 * it invalid). */
13942 if (win_valid(save_curwin))
13943 {
13944 curwin = save_curwin;
13945 curbuf = curwin->w_buffer;
13946 }
13947#endif
13948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949}
13950
13951/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013952 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 */
13954 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013955f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013956 typval_T *argvars;
13957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960
Bram Moolenaar0d660222005-01-07 21:51:51 +000013961 p = get_tv_string(&argvars[0]);
13962 rettv->vval.v_string = vim_strsave(p);
13963 simplify_filename(rettv->vval.v_string); /* simplify in place */
13964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965}
13966
Bram Moolenaar0d660222005-01-07 21:51:51 +000013967static int
13968#ifdef __BORLANDC__
13969 _RTLENTRYF
13970#endif
13971 item_compare __ARGS((const void *s1, const void *s2));
13972static int
13973#ifdef __BORLANDC__
13974 _RTLENTRYF
13975#endif
13976 item_compare2 __ARGS((const void *s1, const void *s2));
13977
13978static int item_compare_ic;
13979static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013981#define ITEM_COMPARE_FAIL 999
13982
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013984 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013986 static int
13987#ifdef __BORLANDC__
13988_RTLENTRYF
13989#endif
13990item_compare(s1, s2)
13991 const void *s1;
13992 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013994 char_u *p1, *p2;
13995 char_u *tofree1, *tofree2;
13996 int res;
13997 char_u numbuf1[NUMBUFLEN];
13998 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014000 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14001 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014002 if (item_compare_ic)
14003 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014005 res = STRCMP(p1, p2);
14006 vim_free(tofree1);
14007 vim_free(tofree2);
14008 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009}
14010
14011 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014012#ifdef __BORLANDC__
14013_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014015item_compare2(s1, s2)
14016 const void *s1;
14017 const void *s2;
14018{
14019 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T rettv;
14021 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014022 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024 /* shortcut after failure in previous call; compare all items equal */
14025 if (item_compare_func_err)
14026 return 0;
14027
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014028 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14029 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014030 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14031 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014032
14033 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14034 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014035 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014036 clear_tv(&argv[0]);
14037 clear_tv(&argv[1]);
14038
14039 if (res == FAIL)
14040 res = ITEM_COMPARE_FAIL;
14041 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 /* return value has wrong type */
14043 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14044 if (item_compare_func_err)
14045 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014046 clear_tv(&rettv);
14047 return res;
14048}
14049
14050/*
14051 * "sort({list})" function
14052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014054f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014055 typval_T *argvars;
14056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057{
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 list_T *l;
14059 listitem_T *li;
14060 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014061 long len;
14062 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063
Bram Moolenaar0d660222005-01-07 21:51:51 +000014064 rettv->vval.v_number = 0;
14065 if (argvars[0].v_type != VAR_LIST)
14066 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 else
14068 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014069 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014070 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 return;
14072 rettv->vval.v_list = l;
14073 rettv->v_type = VAR_LIST;
14074 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 len = list_len(l);
14077 if (len <= 1)
14078 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079
Bram Moolenaar0d660222005-01-07 21:51:51 +000014080 item_compare_ic = FALSE;
14081 item_compare_func = NULL;
14082 if (argvars[1].v_type != VAR_UNKNOWN)
14083 {
14084 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014085 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014086 else
14087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014088 int error = FALSE;
14089
14090 i = get_tv_number_chk(&argvars[1], &error);
14091 if (error)
14092 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014093 if (i == 1)
14094 item_compare_ic = TRUE;
14095 else
14096 item_compare_func = get_tv_string(&argvars[1]);
14097 }
14098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014102 if (ptrs == NULL)
14103 return;
14104 i = 0;
14105 for (li = l->lv_first; li != NULL; li = li->li_next)
14106 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014109 /* test the compare function */
14110 if (item_compare_func != NULL
14111 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14112 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014113 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014115 {
14116 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014117 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014118 item_compare_func == NULL ? item_compare : item_compare2);
14119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 if (!item_compare_func_err)
14121 {
14122 /* Clear the List and append the items in the sorted order. */
14123 l->lv_first = l->lv_last = NULL;
14124 l->lv_len = 0;
14125 for (i = 0; i < len; ++i)
14126 list_append(l, ptrs[i]);
14127 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014128 }
14129
14130 vim_free(ptrs);
14131 }
14132}
14133
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014134/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014135 * "soundfold({word})" function
14136 */
14137 static void
14138f_soundfold(argvars, rettv)
14139 typval_T *argvars;
14140 typval_T *rettv;
14141{
14142 char_u *s;
14143
14144 rettv->v_type = VAR_STRING;
14145 s = get_tv_string(&argvars[0]);
14146#ifdef FEAT_SYN_HL
14147 rettv->vval.v_string = eval_soundfold(s);
14148#else
14149 rettv->vval.v_string = vim_strsave(s);
14150#endif
14151}
14152
14153/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014154 * "spellbadword()" function
14155 */
14156/* ARGSUSED */
14157 static void
14158f_spellbadword(argvars, rettv)
14159 typval_T *argvars;
14160 typval_T *rettv;
14161{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014162 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014163 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014164 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014165
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014166 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014167 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014168
14169#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014170 if (argvars[0].v_type == VAR_UNKNOWN)
14171 {
14172 /* Find the start and length of the badly spelled word. */
14173 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14174 if (len != 0)
14175 word = ml_get_cursor();
14176 }
14177 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14178 {
14179 char_u *str = get_tv_string_chk(&argvars[0]);
14180 int capcol = -1;
14181
14182 if (str != NULL)
14183 {
14184 /* Check the argument for spelling. */
14185 while (*str != NUL)
14186 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014187 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014188 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014189 {
14190 word = str;
14191 break;
14192 }
14193 str += len;
14194 }
14195 }
14196 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014197#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014198
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014199 list_append_string(rettv->vval.v_list, word, len);
14200 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014201 attr == HLF_SPB ? "bad" :
14202 attr == HLF_SPR ? "rare" :
14203 attr == HLF_SPL ? "local" :
14204 attr == HLF_SPC ? "caps" :
14205 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014206}
14207
14208/*
14209 * "spellsuggest()" function
14210 */
14211 static void
14212f_spellsuggest(argvars, rettv)
14213 typval_T *argvars;
14214 typval_T *rettv;
14215{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014216#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014217 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014218 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014219 int maxcount;
14220 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014221 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014222 listitem_T *li;
14223 int need_capital = FALSE;
14224#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014225
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014226 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014227 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014228
14229#ifdef FEAT_SYN_HL
14230 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14231 {
14232 str = get_tv_string(&argvars[0]);
14233 if (argvars[1].v_type != VAR_UNKNOWN)
14234 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014235 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014236 if (maxcount <= 0)
14237 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014238 if (argvars[2].v_type != VAR_UNKNOWN)
14239 {
14240 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14241 if (typeerr)
14242 return;
14243 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014244 }
14245 else
14246 maxcount = 25;
14247
Bram Moolenaar4770d092006-01-12 23:22:24 +000014248 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014249
14250 for (i = 0; i < ga.ga_len; ++i)
14251 {
14252 str = ((char_u **)ga.ga_data)[i];
14253
14254 li = listitem_alloc();
14255 if (li == NULL)
14256 vim_free(str);
14257 else
14258 {
14259 li->li_tv.v_type = VAR_STRING;
14260 li->li_tv.v_lock = 0;
14261 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014262 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014263 }
14264 }
14265 ga_clear(&ga);
14266 }
14267#endif
14268}
14269
Bram Moolenaar0d660222005-01-07 21:51:51 +000014270 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014271f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014272 typval_T *argvars;
14273 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014274{
14275 char_u *str;
14276 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014277 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014278 regmatch_T regmatch;
14279 char_u patbuf[NUMBUFLEN];
14280 char_u *save_cpo;
14281 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014282 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014283 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014284 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285
14286 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14287 save_cpo = p_cpo;
14288 p_cpo = (char_u *)"";
14289
14290 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014291 if (argvars[1].v_type != VAR_UNKNOWN)
14292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14294 if (pat == NULL)
14295 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014296 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014298 }
14299 if (pat == NULL || *pat == NUL)
14300 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014301
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014302 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 if (typeerr)
14305 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306
Bram Moolenaar0d660222005-01-07 21:51:51 +000014307 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14308 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014310 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014311 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014312 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014313 if (*str == NUL)
14314 match = FALSE; /* empty item at the end */
14315 else
14316 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317 if (match)
14318 end = regmatch.startp[0];
14319 else
14320 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014321 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14322 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014324 if (list_append_string(rettv->vval.v_list, str,
14325 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014326 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014327 }
14328 if (!match)
14329 break;
14330 /* Advance to just after the match. */
14331 if (regmatch.endp[0] > str)
14332 col = 0;
14333 else
14334 {
14335 /* Don't get stuck at the same match. */
14336#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014337 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014338#else
14339 col = 1;
14340#endif
14341 }
14342 str = regmatch.endp[0];
14343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344
Bram Moolenaar0d660222005-01-07 21:51:51 +000014345 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347
Bram Moolenaar0d660222005-01-07 21:51:51 +000014348 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349}
14350
14351#ifdef HAVE_STRFTIME
14352/*
14353 * "strftime({format}[, {time}])" function
14354 */
14355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014357 typval_T *argvars;
14358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359{
14360 char_u result_buf[256];
14361 struct tm *curtime;
14362 time_t seconds;
14363 char_u *p;
14364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014368 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 seconds = time(NULL);
14370 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 curtime = localtime(&seconds);
14373 /* MSVC returns NULL for an invalid value of seconds. */
14374 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 else
14377 {
14378# ifdef FEAT_MBYTE
14379 vimconv_T conv;
14380 char_u *enc;
14381
14382 conv.vc_type = CONV_NONE;
14383 enc = enc_locale();
14384 convert_setup(&conv, p_enc, enc);
14385 if (conv.vc_type != CONV_NONE)
14386 p = string_convert(&conv, p, NULL);
14387# endif
14388 if (p != NULL)
14389 (void)strftime((char *)result_buf, sizeof(result_buf),
14390 (char *)p, curtime);
14391 else
14392 result_buf[0] = NUL;
14393
14394# ifdef FEAT_MBYTE
14395 if (conv.vc_type != CONV_NONE)
14396 vim_free(p);
14397 convert_setup(&conv, enc, p_enc);
14398 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 else
14401# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403
14404# ifdef FEAT_MBYTE
14405 /* Release conversion descriptors */
14406 convert_setup(&conv, NULL, NULL);
14407 vim_free(enc);
14408# endif
14409 }
14410}
14411#endif
14412
14413/*
14414 * "stridx()" function
14415 */
14416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014418 typval_T *argvars;
14419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420{
14421 char_u buf[NUMBUFLEN];
14422 char_u *needle;
14423 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014424 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014426 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014428 needle = get_tv_string_chk(&argvars[1]);
14429 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014430 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 if (needle == NULL || haystack == NULL)
14432 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433
Bram Moolenaar33570922005-01-25 22:26:29 +000014434 if (argvars[2].v_type != VAR_UNKNOWN)
14435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014436 int error = FALSE;
14437
14438 start_idx = get_tv_number_chk(&argvars[2], &error);
14439 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014440 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014441 if (start_idx >= 0)
14442 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014443 }
14444
14445 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14446 if (pos != NULL)
14447 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448}
14449
14450/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014451 * "string()" function
14452 */
14453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014455 typval_T *argvars;
14456 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014457{
14458 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014459 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014462 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014463 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465}
14466
14467/*
14468 * "strlen()" function
14469 */
14470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014472 typval_T *argvars;
14473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_number = (varnumber_T)(STRLEN(
14476 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477}
14478
14479/*
14480 * "strpart()" function
14481 */
14482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 typval_T *argvars;
14485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486{
14487 char_u *p;
14488 int n;
14489 int len;
14490 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014493 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 slen = (int)STRLEN(p);
14495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 n = get_tv_number_chk(&argvars[1], &error);
14497 if (error)
14498 len = 0;
14499 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 else
14502 len = slen - n; /* default len: all bytes that are available. */
14503
14504 /*
14505 * Only return the overlap between the specified part and the actual
14506 * string.
14507 */
14508 if (n < 0)
14509 {
14510 len += n;
14511 n = 0;
14512 }
14513 else if (n > slen)
14514 n = slen;
14515 if (len < 0)
14516 len = 0;
14517 else if (n + len > slen)
14518 len = slen - n;
14519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 rettv->v_type = VAR_STRING;
14521 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522}
14523
14524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014525 * "strridx()" function
14526 */
14527 static void
14528f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 typval_T *argvars;
14530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014531{
14532 char_u buf[NUMBUFLEN];
14533 char_u *needle;
14534 char_u *haystack;
14535 char_u *rest;
14536 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014537 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014539 needle = get_tv_string_chk(&argvars[1]);
14540 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014541 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542
14543 rettv->vval.v_number = -1;
14544 if (needle == NULL || haystack == NULL)
14545 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014546 if (argvars[2].v_type != VAR_UNKNOWN)
14547 {
14548 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014550 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014552 }
14553 else
14554 end_idx = haystack_len;
14555
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014557 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014558 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014559 lastmatch = haystack + end_idx;
14560 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014562 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 for (rest = haystack; *rest != '\0'; ++rest)
14564 {
14565 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014566 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 break;
14568 lastmatch = rest;
14569 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014570 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571
14572 if (lastmatch == NULL)
14573 rettv->vval.v_number = -1;
14574 else
14575 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14576}
14577
14578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 * "strtrans()" function
14580 */
14581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014582f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 typval_T *argvars;
14584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014586 rettv->v_type = VAR_STRING;
14587 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588}
14589
14590/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014591 * "submatch()" function
14592 */
14593 static void
14594f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 typval_T *argvars;
14596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597{
14598 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 rettv->vval.v_string =
14600 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601}
14602
14603/*
14604 * "substitute()" function
14605 */
14606 static void
14607f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014608 typval_T *argvars;
14609 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014610{
14611 char_u patbuf[NUMBUFLEN];
14612 char_u subbuf[NUMBUFLEN];
14613 char_u flagsbuf[NUMBUFLEN];
14614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014615 char_u *str = get_tv_string_chk(&argvars[0]);
14616 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14617 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14618 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14619
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14622 rettv->vval.v_string = NULL;
14623 else
14624 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625}
14626
14627/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014628 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 */
14630/*ARGSUSED*/
14631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014632f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014633 typval_T *argvars;
14634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635{
14636 int id = 0;
14637#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014638 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 long col;
14640 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014641 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014643 lnum = get_tv_lnum(argvars); /* -1 on type error */
14644 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14645 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014647 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014648 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014649 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650#endif
14651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653}
14654
14655/*
14656 * "synIDattr(id, what [, mode])" function
14657 */
14658/*ARGSUSED*/
14659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014660f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014661 typval_T *argvars;
14662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663{
14664 char_u *p = NULL;
14665#ifdef FEAT_SYN_HL
14666 int id;
14667 char_u *what;
14668 char_u *mode;
14669 char_u modebuf[NUMBUFLEN];
14670 int modec;
14671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014672 id = get_tv_number(&argvars[0]);
14673 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014674 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 modec = TOLOWER_ASC(mode[0]);
14678 if (modec != 't' && modec != 'c'
14679#ifdef FEAT_GUI
14680 && modec != 'g'
14681#endif
14682 )
14683 modec = 0; /* replace invalid with current */
14684 }
14685 else
14686 {
14687#ifdef FEAT_GUI
14688 if (gui.in_use)
14689 modec = 'g';
14690 else
14691#endif
14692 if (t_colors > 1)
14693 modec = 'c';
14694 else
14695 modec = 't';
14696 }
14697
14698
14699 switch (TOLOWER_ASC(what[0]))
14700 {
14701 case 'b':
14702 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14703 p = highlight_color(id, what, modec);
14704 else /* bold */
14705 p = highlight_has_attr(id, HL_BOLD, modec);
14706 break;
14707
14708 case 'f': /* fg[#] */
14709 p = highlight_color(id, what, modec);
14710 break;
14711
14712 case 'i':
14713 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14714 p = highlight_has_attr(id, HL_INVERSE, modec);
14715 else /* italic */
14716 p = highlight_has_attr(id, HL_ITALIC, modec);
14717 break;
14718
14719 case 'n': /* name */
14720 p = get_highlight_name(NULL, id - 1);
14721 break;
14722
14723 case 'r': /* reverse */
14724 p = highlight_has_attr(id, HL_INVERSE, modec);
14725 break;
14726
14727 case 's': /* standout */
14728 p = highlight_has_attr(id, HL_STANDOUT, modec);
14729 break;
14730
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014731 case 'u':
14732 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14733 /* underline */
14734 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14735 else
14736 /* undercurl */
14737 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738 break;
14739 }
14740
14741 if (p != NULL)
14742 p = vim_strsave(p);
14743#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014744 rettv->v_type = VAR_STRING;
14745 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746}
14747
14748/*
14749 * "synIDtrans(id)" function
14750 */
14751/*ARGSUSED*/
14752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014753f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 typval_T *argvars;
14755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756{
14757 int id;
14758
14759#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761
14762 if (id > 0)
14763 id = syn_get_final_id(id);
14764 else
14765#endif
14766 id = 0;
14767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769}
14770
14771/*
14772 * "system()" function
14773 */
14774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 typval_T *argvars;
14777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014779 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014781 char_u *infile = NULL;
14782 char_u buf[NUMBUFLEN];
14783 int err = FALSE;
14784 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014786 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014787 {
14788 /*
14789 * Write the string to a temp file, to be used for input of the shell
14790 * command.
14791 */
14792 if ((infile = vim_tempname('i')) == NULL)
14793 {
14794 EMSG(_(e_notmp));
14795 return;
14796 }
14797
14798 fd = mch_fopen((char *)infile, WRITEBIN);
14799 if (fd == NULL)
14800 {
14801 EMSG2(_(e_notopen), infile);
14802 goto done;
14803 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014804 p = get_tv_string_buf_chk(&argvars[1], buf);
14805 if (p == NULL)
14806 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014807 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14808 err = TRUE;
14809 if (fclose(fd) != 0)
14810 err = TRUE;
14811 if (err)
14812 {
14813 EMSG(_("E677: Error writing temp file"));
14814 goto done;
14815 }
14816 }
14817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014818 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014819
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820#ifdef USE_CR
14821 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014822 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 {
14824 char_u *s;
14825
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014826 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 {
14828 if (*s == CAR)
14829 *s = NL;
14830 }
14831 }
14832#else
14833# ifdef USE_CRNL
14834 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014835 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 {
14837 char_u *s, *d;
14838
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014839 d = res;
14840 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 {
14842 if (s[0] == CAR && s[1] == NL)
14843 ++s;
14844 *d++ = *s;
14845 }
14846 *d = NUL;
14847 }
14848# endif
14849#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014850
14851done:
14852 if (infile != NULL)
14853 {
14854 mch_remove(infile);
14855 vim_free(infile);
14856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014857 rettv->v_type = VAR_STRING;
14858 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859}
14860
14861/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014862 * "tabpagebuflist()" function
14863 */
14864/* ARGSUSED */
14865 static void
14866f_tabpagebuflist(argvars, rettv)
14867 typval_T *argvars;
14868 typval_T *rettv;
14869{
14870#ifndef FEAT_WINDOWS
14871 rettv->vval.v_number = 0;
14872#else
14873 tabpage_T *tp;
14874 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014875
14876 if (argvars[0].v_type == VAR_UNKNOWN)
14877 wp = firstwin;
14878 else
14879 {
14880 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14881 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000014882 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014883 }
14884 if (wp == NULL)
14885 rettv->vval.v_number = 0;
14886 else
14887 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014888 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014889 rettv->vval.v_number = 0;
14890 else
14891 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014892 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014893 if (list_append_number(rettv->vval.v_list,
14894 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014895 break;
14896 }
14897 }
14898#endif
14899}
14900
14901
14902/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014903 * "tabpagenr()" function
14904 */
14905/* ARGSUSED */
14906 static void
14907f_tabpagenr(argvars, rettv)
14908 typval_T *argvars;
14909 typval_T *rettv;
14910{
14911 int nr = 1;
14912#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014913 char_u *arg;
14914
14915 if (argvars[0].v_type != VAR_UNKNOWN)
14916 {
14917 arg = get_tv_string_chk(&argvars[0]);
14918 nr = 0;
14919 if (arg != NULL)
14920 {
14921 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000014922 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014923 else
14924 EMSG2(_(e_invexpr2), arg);
14925 }
14926 }
14927 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000014928 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014929#endif
14930 rettv->vval.v_number = nr;
14931}
14932
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014933
14934#ifdef FEAT_WINDOWS
14935static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
14936
14937/*
14938 * Common code for tabpagewinnr() and winnr().
14939 */
14940 static int
14941get_winnr(tp, argvar)
14942 tabpage_T *tp;
14943 typval_T *argvar;
14944{
14945 win_T *twin;
14946 int nr = 1;
14947 win_T *wp;
14948 char_u *arg;
14949
14950 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14951 if (argvar->v_type != VAR_UNKNOWN)
14952 {
14953 arg = get_tv_string_chk(argvar);
14954 if (arg == NULL)
14955 nr = 0; /* type error; errmsg already given */
14956 else if (STRCMP(arg, "$") == 0)
14957 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14958 else if (STRCMP(arg, "#") == 0)
14959 {
14960 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14961 if (twin == NULL)
14962 nr = 0;
14963 }
14964 else
14965 {
14966 EMSG2(_(e_invexpr2), arg);
14967 nr = 0;
14968 }
14969 }
14970
14971 if (nr > 0)
14972 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14973 wp != twin; wp = wp->w_next)
14974 ++nr;
14975 return nr;
14976}
14977#endif
14978
14979/*
14980 * "tabpagewinnr()" function
14981 */
14982/* ARGSUSED */
14983 static void
14984f_tabpagewinnr(argvars, rettv)
14985 typval_T *argvars;
14986 typval_T *rettv;
14987{
14988 int nr = 1;
14989#ifdef FEAT_WINDOWS
14990 tabpage_T *tp;
14991
14992 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14993 if (tp == NULL)
14994 nr = 0;
14995 else
14996 nr = get_winnr(tp, &argvars[1]);
14997#endif
14998 rettv->vval.v_number = nr;
14999}
15000
15001
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015002/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015003 * "tagfiles()" function
15004 */
15005/*ARGSUSED*/
15006 static void
15007f_tagfiles(argvars, rettv)
15008 typval_T *argvars;
15009 typval_T *rettv;
15010{
15011 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015012 tagname_T tn;
15013 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015014
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015015 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015016 {
15017 rettv->vval.v_number = 0;
15018 return;
15019 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015020
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015021 for (first = TRUE; ; first = FALSE)
15022 if (get_tagfname(&tn, first, fname) == FAIL
15023 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015024 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015025 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015026}
15027
15028/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015029 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015030 */
15031 static void
15032f_taglist(argvars, rettv)
15033 typval_T *argvars;
15034 typval_T *rettv;
15035{
15036 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015037
15038 tag_pattern = get_tv_string(&argvars[0]);
15039
15040 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015041 if (*tag_pattern == NUL)
15042 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015043
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015044 if (rettv_list_alloc(rettv) == OK)
15045 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015046}
15047
15048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 * "tempname()" function
15050 */
15051/*ARGSUSED*/
15052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015054 typval_T *argvars;
15055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056{
15057 static int x = 'A';
15058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015059 rettv->v_type = VAR_STRING;
15060 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061
15062 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15063 * names. Skip 'I' and 'O', they are used for shell redirection. */
15064 do
15065 {
15066 if (x == 'Z')
15067 x = '0';
15068 else if (x == '9')
15069 x = 'A';
15070 else
15071 {
15072#ifdef EBCDIC
15073 if (x == 'I')
15074 x = 'J';
15075 else if (x == 'R')
15076 x = 'S';
15077 else
15078#endif
15079 ++x;
15080 }
15081 } while (x == 'I' || x == 'O');
15082}
15083
15084/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015085 * "test(list)" function: Just checking the walls...
15086 */
15087/*ARGSUSED*/
15088 static void
15089f_test(argvars, rettv)
15090 typval_T *argvars;
15091 typval_T *rettv;
15092{
15093 /* Used for unit testing. Change the code below to your liking. */
15094#if 0
15095 listitem_T *li;
15096 list_T *l;
15097 char_u *bad, *good;
15098
15099 if (argvars[0].v_type != VAR_LIST)
15100 return;
15101 l = argvars[0].vval.v_list;
15102 if (l == NULL)
15103 return;
15104 li = l->lv_first;
15105 if (li == NULL)
15106 return;
15107 bad = get_tv_string(&li->li_tv);
15108 li = li->li_next;
15109 if (li == NULL)
15110 return;
15111 good = get_tv_string(&li->li_tv);
15112 rettv->vval.v_number = test_edit_score(bad, good);
15113#endif
15114}
15115
15116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 * "tolower(string)" function
15118 */
15119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015120f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015121 typval_T *argvars;
15122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123{
15124 char_u *p;
15125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015126 p = vim_strsave(get_tv_string(&argvars[0]));
15127 rettv->v_type = VAR_STRING;
15128 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129
15130 if (p != NULL)
15131 while (*p != NUL)
15132 {
15133#ifdef FEAT_MBYTE
15134 int l;
15135
15136 if (enc_utf8)
15137 {
15138 int c, lc;
15139
15140 c = utf_ptr2char(p);
15141 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015142 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 /* TODO: reallocate string when byte count changes. */
15144 if (utf_char2len(lc) == l)
15145 utf_char2bytes(lc, p);
15146 p += l;
15147 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015148 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 p += l; /* skip multi-byte character */
15150 else
15151#endif
15152 {
15153 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15154 ++p;
15155 }
15156 }
15157}
15158
15159/*
15160 * "toupper(string)" function
15161 */
15162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015164 typval_T *argvars;
15165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015167 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015168 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169}
15170
15171/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015172 * "tr(string, fromstr, tostr)" function
15173 */
15174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015175f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015176 typval_T *argvars;
15177 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015178{
15179 char_u *instr;
15180 char_u *fromstr;
15181 char_u *tostr;
15182 char_u *p;
15183#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015184 int inlen;
15185 int fromlen;
15186 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015187 int idx;
15188 char_u *cpstr;
15189 int cplen;
15190 int first = TRUE;
15191#endif
15192 char_u buf[NUMBUFLEN];
15193 char_u buf2[NUMBUFLEN];
15194 garray_T ga;
15195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015196 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015197 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15198 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015199
15200 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015201 rettv->v_type = VAR_STRING;
15202 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 if (fromstr == NULL || tostr == NULL)
15204 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015205 ga_init2(&ga, (int)sizeof(char), 80);
15206
15207#ifdef FEAT_MBYTE
15208 if (!has_mbyte)
15209#endif
15210 /* not multi-byte: fromstr and tostr must be the same length */
15211 if (STRLEN(fromstr) != STRLEN(tostr))
15212 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015213#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015214error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015215#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015216 EMSG2(_(e_invarg2), fromstr);
15217 ga_clear(&ga);
15218 return;
15219 }
15220
15221 /* fromstr and tostr have to contain the same number of chars */
15222 while (*instr != NUL)
15223 {
15224#ifdef FEAT_MBYTE
15225 if (has_mbyte)
15226 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015227 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015228 cpstr = instr;
15229 cplen = inlen;
15230 idx = 0;
15231 for (p = fromstr; *p != NUL; p += fromlen)
15232 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015233 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015234 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15235 {
15236 for (p = tostr; *p != NUL; p += tolen)
15237 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015238 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015239 if (idx-- == 0)
15240 {
15241 cplen = tolen;
15242 cpstr = p;
15243 break;
15244 }
15245 }
15246 if (*p == NUL) /* tostr is shorter than fromstr */
15247 goto error;
15248 break;
15249 }
15250 ++idx;
15251 }
15252
15253 if (first && cpstr == instr)
15254 {
15255 /* Check that fromstr and tostr have the same number of
15256 * (multi-byte) characters. Done only once when a character
15257 * of instr doesn't appear in fromstr. */
15258 first = FALSE;
15259 for (p = tostr; *p != NUL; p += tolen)
15260 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015261 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015262 --idx;
15263 }
15264 if (idx != 0)
15265 goto error;
15266 }
15267
15268 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015269 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015270 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015271
15272 instr += inlen;
15273 }
15274 else
15275#endif
15276 {
15277 /* When not using multi-byte chars we can do it faster. */
15278 p = vim_strchr(fromstr, *instr);
15279 if (p != NULL)
15280 ga_append(&ga, tostr[p - fromstr]);
15281 else
15282 ga_append(&ga, *instr);
15283 ++instr;
15284 }
15285 }
15286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015287 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015288}
15289
15290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291 * "type(expr)" function
15292 */
15293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015295 typval_T *argvars;
15296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015298 int n;
15299
15300 switch (argvars[0].v_type)
15301 {
15302 case VAR_NUMBER: n = 0; break;
15303 case VAR_STRING: n = 1; break;
15304 case VAR_FUNC: n = 2; break;
15305 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015306 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015307 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15308 }
15309 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310}
15311
15312/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015313 * "values(dict)" function
15314 */
15315 static void
15316f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015317 typval_T *argvars;
15318 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015319{
15320 dict_list(argvars, rettv, 1);
15321}
15322
15323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 * "virtcol(string)" function
15325 */
15326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015327f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015328 typval_T *argvars;
15329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330{
15331 colnr_T vcol = 0;
15332 pos_T *fp;
15333
15334 fp = var2fpos(&argvars[0], FALSE);
15335 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15336 {
15337 getvvcol(curwin, fp, NULL, NULL, &vcol);
15338 ++vcol;
15339 }
15340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015341 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342}
15343
15344/*
15345 * "visualmode()" function
15346 */
15347/*ARGSUSED*/
15348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015350 typval_T *argvars;
15351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352{
15353#ifdef FEAT_VISUAL
15354 char_u str[2];
15355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 str[0] = curbuf->b_visual_mode_eval;
15358 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360
15361 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015362 if ((argvars[0].v_type == VAR_NUMBER
15363 && argvars[0].vval.v_number != 0)
15364 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015365 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 curbuf->b_visual_mode_eval = NUL;
15367#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015368 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369#endif
15370}
15371
15372/*
15373 * "winbufnr(nr)" function
15374 */
15375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015376f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015377 typval_T *argvars;
15378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379{
15380 win_T *wp;
15381
15382 wp = find_win_by_nr(&argvars[0]);
15383 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387}
15388
15389/*
15390 * "wincol()" function
15391 */
15392/*ARGSUSED*/
15393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015395 typval_T *argvars;
15396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397{
15398 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400}
15401
15402/*
15403 * "winheight(nr)" function
15404 */
15405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015407 typval_T *argvars;
15408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409{
15410 win_T *wp;
15411
15412 wp = find_win_by_nr(&argvars[0]);
15413 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417}
15418
15419/*
15420 * "winline()" function
15421 */
15422/*ARGSUSED*/
15423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015425 typval_T *argvars;
15426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427{
15428 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430}
15431
15432/*
15433 * "winnr()" function
15434 */
15435/* ARGSUSED */
15436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015438 typval_T *argvars;
15439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440{
15441 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015442
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015444 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015446 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447}
15448
15449/*
15450 * "winrestcmd()" function
15451 */
15452/* ARGSUSED */
15453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015454f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015455 typval_T *argvars;
15456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457{
15458#ifdef FEAT_WINDOWS
15459 win_T *wp;
15460 int winnr = 1;
15461 garray_T ga;
15462 char_u buf[50];
15463
15464 ga_init2(&ga, (int)sizeof(char), 70);
15465 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15466 {
15467 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15468 ga_concat(&ga, buf);
15469# ifdef FEAT_VERTSPLIT
15470 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15471 ga_concat(&ga, buf);
15472# endif
15473 ++winnr;
15474 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015475 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015481 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482}
15483
15484/*
15485 * "winwidth(nr)" function
15486 */
15487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015488f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015489 typval_T *argvars;
15490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491{
15492 win_T *wp;
15493
15494 wp = find_win_by_nr(&argvars[0]);
15495 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 else
15498#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502#endif
15503}
15504
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015506 * "writefile()" function
15507 */
15508 static void
15509f_writefile(argvars, rettv)
15510 typval_T *argvars;
15511 typval_T *rettv;
15512{
15513 int binary = FALSE;
15514 char_u *fname;
15515 FILE *fd;
15516 listitem_T *li;
15517 char_u *s;
15518 int ret = 0;
15519 int c;
15520
15521 if (argvars[0].v_type != VAR_LIST)
15522 {
15523 EMSG2(_(e_listarg), "writefile()");
15524 return;
15525 }
15526 if (argvars[0].vval.v_list == NULL)
15527 return;
15528
15529 if (argvars[2].v_type != VAR_UNKNOWN
15530 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15531 binary = TRUE;
15532
15533 /* Always open the file in binary mode, library functions have a mind of
15534 * their own about CR-LF conversion. */
15535 fname = get_tv_string(&argvars[1]);
15536 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15537 {
15538 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15539 ret = -1;
15540 }
15541 else
15542 {
15543 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15544 li = li->li_next)
15545 {
15546 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15547 {
15548 if (*s == '\n')
15549 c = putc(NUL, fd);
15550 else
15551 c = putc(*s, fd);
15552 if (c == EOF)
15553 {
15554 ret = -1;
15555 break;
15556 }
15557 }
15558 if (!binary || li->li_next != NULL)
15559 if (putc('\n', fd) == EOF)
15560 {
15561 ret = -1;
15562 break;
15563 }
15564 if (ret < 0)
15565 {
15566 EMSG(_(e_write));
15567 break;
15568 }
15569 }
15570 fclose(fd);
15571 }
15572
15573 rettv->vval.v_number = ret;
15574}
15575
15576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015578 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 */
15580 static pos_T *
15581var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015582 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 int lnum; /* TRUE when $ is last line */
15584{
15585 char_u *name;
15586 static pos_T pos;
15587 pos_T *pp;
15588
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015589 /* Argument can be [lnum, col]. */
15590 if (varp->v_type == VAR_LIST)
15591 {
15592 list_T *l;
15593 listitem_T *li;
15594 int len;
15595
15596 l = varp->vval.v_list;
15597 if (l == NULL)
15598 return NULL;
15599
15600 /* Get the line number */
15601 li = list_find(l, 0L);
15602 if (li == NULL)
15603 return NULL;
15604 pos.lnum = get_tv_number(&li->li_tv);
15605 if (pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
15606 return NULL; /* invalid line number */
15607
15608 /* Get the column number */
15609 li = list_find(l, 1L);
15610 if (li == NULL)
15611 return NULL;
15612 pos.col = get_tv_number(&li->li_tv);
15613 len = (long)STRLEN(ml_get(pos.lnum));
15614 if (pos.col <= 0 || ((len == 0 && pos.col > 1)
Bram Moolenaare224ffa2006-03-01 00:01:28 +000015615 || (len > 0 && (int)pos.col > len)))
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015616 return NULL; /* invalid column number */
15617
15618 pos.col--;
15619 return &pos;
15620 }
15621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015622 name = get_tv_string_chk(varp);
15623 if (name == NULL)
15624 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 if (name[0] == '.') /* cursor */
15626 return &curwin->w_cursor;
15627 if (name[0] == '\'') /* mark */
15628 {
15629 pp = getmark(name[1], FALSE);
15630 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15631 return NULL;
15632 return pp;
15633 }
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015634 if (name[0] == 'w' && lnum)
15635 {
15636 pos.col = 0;
15637 if (name[1] == '0') /* "w0": first visible line */
15638 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015639 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015640 pos.lnum = curwin->w_topline;
15641 return &pos;
15642 }
15643 else if (name[1] == '$') /* "w$": last visible line */
15644 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015645 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015646 pos.lnum = curwin->w_botline - 1;
15647 return &pos;
15648 }
15649 }
15650 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 {
15652 if (lnum)
15653 {
15654 pos.lnum = curbuf->b_ml.ml_line_count;
15655 pos.col = 0;
15656 }
15657 else
15658 {
15659 pos.lnum = curwin->w_cursor.lnum;
15660 pos.col = (colnr_T)STRLEN(ml_get_curline());
15661 }
15662 return &pos;
15663 }
15664 return NULL;
15665}
15666
15667/*
15668 * Get the length of an environment variable name.
15669 * Advance "arg" to the first character after the name.
15670 * Return 0 for error.
15671 */
15672 static int
15673get_env_len(arg)
15674 char_u **arg;
15675{
15676 char_u *p;
15677 int len;
15678
15679 for (p = *arg; vim_isIDc(*p); ++p)
15680 ;
15681 if (p == *arg) /* no name found */
15682 return 0;
15683
15684 len = (int)(p - *arg);
15685 *arg = p;
15686 return len;
15687}
15688
15689/*
15690 * Get the length of the name of a function or internal variable.
15691 * "arg" is advanced to the first non-white character after the name.
15692 * Return 0 if something is wrong.
15693 */
15694 static int
15695get_id_len(arg)
15696 char_u **arg;
15697{
15698 char_u *p;
15699 int len;
15700
15701 /* Find the end of the name. */
15702 for (p = *arg; eval_isnamec(*p); ++p)
15703 ;
15704 if (p == *arg) /* no name found */
15705 return 0;
15706
15707 len = (int)(p - *arg);
15708 *arg = skipwhite(p);
15709
15710 return len;
15711}
15712
15713/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015714 * Get the length of the name of a variable or function.
15715 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015717 * Return -1 if curly braces expansion failed.
15718 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 * If the name contains 'magic' {}'s, expand them and return the
15720 * expanded name in an allocated string via 'alias' - caller must free.
15721 */
15722 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015723get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 char_u **arg;
15725 char_u **alias;
15726 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015727 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728{
15729 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 char_u *p;
15731 char_u *expr_start;
15732 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733
15734 *alias = NULL; /* default to no alias */
15735
15736 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15737 && (*arg)[2] == (int)KE_SNR)
15738 {
15739 /* hard coded <SNR>, already translated */
15740 *arg += 3;
15741 return get_id_len(arg) + 3;
15742 }
15743 len = eval_fname_script(*arg);
15744 if (len > 0)
15745 {
15746 /* literal "<SID>", "s:" or "<SNR>" */
15747 *arg += len;
15748 }
15749
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015751 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015753 p = find_name_end(*arg, &expr_start, &expr_end,
15754 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 if (expr_start != NULL)
15756 {
15757 char_u *temp_string;
15758
15759 if (!evaluate)
15760 {
15761 len += (int)(p - *arg);
15762 *arg = skipwhite(p);
15763 return len;
15764 }
15765
15766 /*
15767 * Include any <SID> etc in the expanded string:
15768 * Thus the -len here.
15769 */
15770 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15771 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015772 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 *alias = temp_string;
15774 *arg = skipwhite(p);
15775 return (int)STRLEN(temp_string);
15776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777
15778 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015779 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 EMSG2(_(e_invexpr2), *arg);
15781
15782 return len;
15783}
15784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785/*
15786 * Find the end of a variable or function name, taking care of magic braces.
15787 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15788 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015789 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015790 * Return a pointer to just after the name. Equal to "arg" if there is no
15791 * valid name.
15792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015794find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795 char_u *arg;
15796 char_u **expr_start;
15797 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015798 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800 int mb_nest = 0;
15801 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 char_u *p;
15803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015804 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015806 *expr_start = NULL;
15807 *expr_end = NULL;
15808 }
15809
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015810 /* Quick check for valid starting character. */
15811 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15812 return arg;
15813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015814 for (p = arg; *p != NUL
15815 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015816 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015817 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015818 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015819 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015820 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015821 if (*p == '\'')
15822 {
15823 /* skip over 'string' to avoid counting [ and ] inside it. */
15824 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15825 ;
15826 if (*p == NUL)
15827 break;
15828 }
15829 else if (*p == '"')
15830 {
15831 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15832 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15833 if (*p == '\\' && p[1] != NUL)
15834 ++p;
15835 if (*p == NUL)
15836 break;
15837 }
15838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015839 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841 if (*p == '[')
15842 ++br_nest;
15843 else if (*p == ']')
15844 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015847 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015849 if (*p == '{')
15850 {
15851 mb_nest++;
15852 if (expr_start != NULL && *expr_start == NULL)
15853 *expr_start = p;
15854 }
15855 else if (*p == '}')
15856 {
15857 mb_nest--;
15858 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15859 *expr_end = p;
15860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 }
15863
15864 return p;
15865}
15866
15867/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015868 * Expands out the 'magic' {}'s in a variable/function name.
15869 * Note that this can call itself recursively, to deal with
15870 * constructs like foo{bar}{baz}{bam}
15871 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15872 * "in_start" ^
15873 * "expr_start" ^
15874 * "expr_end" ^
15875 * "in_end" ^
15876 *
15877 * Returns a new allocated string, which the caller must free.
15878 * Returns NULL for failure.
15879 */
15880 static char_u *
15881make_expanded_name(in_start, expr_start, expr_end, in_end)
15882 char_u *in_start;
15883 char_u *expr_start;
15884 char_u *expr_end;
15885 char_u *in_end;
15886{
15887 char_u c1;
15888 char_u *retval = NULL;
15889 char_u *temp_result;
15890 char_u *nextcmd = NULL;
15891
15892 if (expr_end == NULL || in_end == NULL)
15893 return NULL;
15894 *expr_start = NUL;
15895 *expr_end = NUL;
15896 c1 = *in_end;
15897 *in_end = NUL;
15898
15899 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15900 if (temp_result != NULL && nextcmd == NULL)
15901 {
15902 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15903 + (in_end - expr_end) + 1));
15904 if (retval != NULL)
15905 {
15906 STRCPY(retval, in_start);
15907 STRCAT(retval, temp_result);
15908 STRCAT(retval, expr_end + 1);
15909 }
15910 }
15911 vim_free(temp_result);
15912
15913 *in_end = c1; /* put char back for error messages */
15914 *expr_start = '{';
15915 *expr_end = '}';
15916
15917 if (retval != NULL)
15918 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015919 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015920 if (expr_start != NULL)
15921 {
15922 /* Further expansion! */
15923 temp_result = make_expanded_name(retval, expr_start,
15924 expr_end, temp_result);
15925 vim_free(retval);
15926 retval = temp_result;
15927 }
15928 }
15929
15930 return retval;
15931}
15932
15933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015935 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 */
15937 static int
15938eval_isnamec(c)
15939 int c;
15940{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015941 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15942}
15943
15944/*
15945 * Return TRUE if character "c" can be used as the first character in a
15946 * variable or function name (excluding '{' and '}').
15947 */
15948 static int
15949eval_isnamec1(c)
15950 int c;
15951{
15952 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953}
15954
15955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 * Set number v: variable to "val".
15957 */
15958 void
15959set_vim_var_nr(idx, val)
15960 int idx;
15961 long val;
15962{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015963 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964}
15965
15966/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015967 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 */
15969 long
15970get_vim_var_nr(idx)
15971 int idx;
15972{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015973 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974}
15975
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015976#if defined(FEAT_AUTOCMD) || defined(PROTO)
15977/*
15978 * Get string v: variable value. Uses a static buffer, can only be used once.
15979 */
15980 char_u *
15981get_vim_var_str(idx)
15982 int idx;
15983{
15984 return get_tv_string(&vimvars[idx].vv_tv);
15985}
15986#endif
15987
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988/*
15989 * Set v:count, v:count1 and v:prevcount.
15990 */
15991 void
15992set_vcount(count, count1)
15993 long count;
15994 long count1;
15995{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015996 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15997 vimvars[VV_COUNT].vv_nr = count;
15998 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999}
16000
16001/*
16002 * Set string v: variable to a copy of "val".
16003 */
16004 void
16005set_vim_var_string(idx, val, len)
16006 int idx;
16007 char_u *val;
16008 int len; /* length of "val" to use or -1 (whole string) */
16009{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016010 /* Need to do this (at least) once, since we can't initialize a union.
16011 * Will always be invoked when "v:progname" is set. */
16012 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16013
Bram Moolenaare9a41262005-01-15 22:18:47 +000016014 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016016 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016018 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016020 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021}
16022
16023/*
16024 * Set v:register if needed.
16025 */
16026 void
16027set_reg_var(c)
16028 int c;
16029{
16030 char_u regname;
16031
16032 if (c == 0 || c == ' ')
16033 regname = '"';
16034 else
16035 regname = c;
16036 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016037 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 set_vim_var_string(VV_REG, &regname, 1);
16039}
16040
16041/*
16042 * Get or set v:exception. If "oldval" == NULL, return the current value.
16043 * Otherwise, restore the value to "oldval" and return NULL.
16044 * Must always be called in pairs to save and restore v:exception! Does not
16045 * take care of memory allocations.
16046 */
16047 char_u *
16048v_exception(oldval)
16049 char_u *oldval;
16050{
16051 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016052 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053
Bram Moolenaare9a41262005-01-15 22:18:47 +000016054 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 return NULL;
16056}
16057
16058/*
16059 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16060 * Otherwise, restore the value to "oldval" and return NULL.
16061 * Must always be called in pairs to save and restore v:throwpoint! Does not
16062 * take care of memory allocations.
16063 */
16064 char_u *
16065v_throwpoint(oldval)
16066 char_u *oldval;
16067{
16068 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016069 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070
Bram Moolenaare9a41262005-01-15 22:18:47 +000016071 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 return NULL;
16073}
16074
16075#if defined(FEAT_AUTOCMD) || defined(PROTO)
16076/*
16077 * Set v:cmdarg.
16078 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16079 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16080 * Must always be called in pairs!
16081 */
16082 char_u *
16083set_cmdarg(eap, oldarg)
16084 exarg_T *eap;
16085 char_u *oldarg;
16086{
16087 char_u *oldval;
16088 char_u *newval;
16089 unsigned len;
16090
Bram Moolenaare9a41262005-01-15 22:18:47 +000016091 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016092 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016094 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016095 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016096 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 }
16098
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016099 if (eap->force_bin == FORCE_BIN)
16100 len = 6;
16101 else if (eap->force_bin == FORCE_NOBIN)
16102 len = 8;
16103 else
16104 len = 0;
16105 if (eap->force_ff != 0)
16106 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16107# ifdef FEAT_MBYTE
16108 if (eap->force_enc != 0)
16109 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016110 if (eap->bad_char != 0)
16111 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016112# endif
16113
16114 newval = alloc(len + 1);
16115 if (newval == NULL)
16116 return NULL;
16117
16118 if (eap->force_bin == FORCE_BIN)
16119 sprintf((char *)newval, " ++bin");
16120 else if (eap->force_bin == FORCE_NOBIN)
16121 sprintf((char *)newval, " ++nobin");
16122 else
16123 *newval = NUL;
16124 if (eap->force_ff != 0)
16125 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16126 eap->cmd + eap->force_ff);
16127# ifdef FEAT_MBYTE
16128 if (eap->force_enc != 0)
16129 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16130 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016131 if (eap->bad_char != 0)
16132 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16133 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016134# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016135 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016136 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137}
16138#endif
16139
16140/*
16141 * Get the value of internal variable "name".
16142 * Return OK or FAIL.
16143 */
16144 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016145get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 char_u *name;
16147 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016148 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016149 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150{
16151 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016152 typval_T *tv = NULL;
16153 typval_T atv;
16154 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156
16157 /* truncate the name, so that we can use strcmp() */
16158 cc = name[len];
16159 name[len] = NUL;
16160
16161 /*
16162 * Check for "b:changedtick".
16163 */
16164 if (STRCMP(name, "b:changedtick") == 0)
16165 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016166 atv.v_type = VAR_NUMBER;
16167 atv.vval.v_number = curbuf->b_changedtick;
16168 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169 }
16170
16171 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 * Check for user-defined variables.
16173 */
16174 else
16175 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016176 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 }
16180
Bram Moolenaare9a41262005-01-15 22:18:47 +000016181 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016183 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016184 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 ret = FAIL;
16186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016187 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016188 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189
16190 name[len] = cc;
16191
16192 return ret;
16193}
16194
16195/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016196 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16197 * Also handle function call with Funcref variable: func(expr)
16198 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16199 */
16200 static int
16201handle_subscript(arg, rettv, evaluate, verbose)
16202 char_u **arg;
16203 typval_T *rettv;
16204 int evaluate; /* do more than finding the end */
16205 int verbose; /* give error messages */
16206{
16207 int ret = OK;
16208 dict_T *selfdict = NULL;
16209 char_u *s;
16210 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016211 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016212
16213 while (ret == OK
16214 && (**arg == '['
16215 || (**arg == '.' && rettv->v_type == VAR_DICT)
16216 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16217 && !vim_iswhite(*(*arg - 1)))
16218 {
16219 if (**arg == '(')
16220 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016221 /* need to copy the funcref so that we can clear rettv */
16222 functv = *rettv;
16223 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016224
16225 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016226 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016227 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016228 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16229 &len, evaluate, selfdict);
16230
16231 /* Clear the funcref afterwards, so that deleting it while
16232 * evaluating the arguments is possible (see test55). */
16233 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016234
16235 /* Stop the expression evaluation when immediately aborting on
16236 * error, or when an interrupt occurred or an exception was thrown
16237 * but not caught. */
16238 if (aborting())
16239 {
16240 if (ret == OK)
16241 clear_tv(rettv);
16242 ret = FAIL;
16243 }
16244 dict_unref(selfdict);
16245 selfdict = NULL;
16246 }
16247 else /* **arg == '[' || **arg == '.' */
16248 {
16249 dict_unref(selfdict);
16250 if (rettv->v_type == VAR_DICT)
16251 {
16252 selfdict = rettv->vval.v_dict;
16253 if (selfdict != NULL)
16254 ++selfdict->dv_refcount;
16255 }
16256 else
16257 selfdict = NULL;
16258 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16259 {
16260 clear_tv(rettv);
16261 ret = FAIL;
16262 }
16263 }
16264 }
16265 dict_unref(selfdict);
16266 return ret;
16267}
16268
16269/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016270 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16271 * value).
16272 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016273 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016274alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275{
Bram Moolenaar33570922005-01-25 22:26:29 +000016276 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016277}
16278
16279/*
16280 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 * The string "s" must have been allocated, it is consumed.
16282 * Return NULL for out of memory, the variable otherwise.
16283 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016284 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016285alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 char_u *s;
16287{
Bram Moolenaar33570922005-01-25 22:26:29 +000016288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016290 rettv = alloc_tv();
16291 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016293 rettv->v_type = VAR_STRING;
16294 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 }
16296 else
16297 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016298 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299}
16300
16301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016302 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016304 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016305free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016306 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307{
16308 if (varp != NULL)
16309 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016310 switch (varp->v_type)
16311 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016312 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016313 func_unref(varp->vval.v_string);
16314 /*FALLTHROUGH*/
16315 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016316 vim_free(varp->vval.v_string);
16317 break;
16318 case VAR_LIST:
16319 list_unref(varp->vval.v_list);
16320 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016321 case VAR_DICT:
16322 dict_unref(varp->vval.v_dict);
16323 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016324 case VAR_NUMBER:
16325 case VAR_UNKNOWN:
16326 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016327 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016328 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016329 break;
16330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 vim_free(varp);
16332 }
16333}
16334
16335/*
16336 * Free the memory for a variable value and set the value to NULL or 0.
16337 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016338 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016339clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016340 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341{
16342 if (varp != NULL)
16343 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016344 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016346 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016347 func_unref(varp->vval.v_string);
16348 /*FALLTHROUGH*/
16349 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016350 vim_free(varp->vval.v_string);
16351 varp->vval.v_string = NULL;
16352 break;
16353 case VAR_LIST:
16354 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016355 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016356 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016357 case VAR_DICT:
16358 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016359 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016360 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016362 varp->vval.v_number = 0;
16363 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016364 case VAR_UNKNOWN:
16365 break;
16366 default:
16367 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016369 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 }
16371}
16372
16373/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016374 * Set the value of a variable to NULL without freeing items.
16375 */
16376 static void
16377init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016378 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016379{
16380 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016381 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016382}
16383
16384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 * Get the number value of a variable.
16386 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016387 * For incompatible types, return 0.
16388 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16389 * caller of incompatible types: it sets *denote to TRUE if "denote"
16390 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 */
16392 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016393get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016396 int error = FALSE;
16397
16398 return get_tv_number_chk(varp, &error); /* return 0L on error */
16399}
16400
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016401 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402get_tv_number_chk(varp, denote)
16403 typval_T *varp;
16404 int *denote;
16405{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016406 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016408 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016410 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016411 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016412 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016413 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016414 break;
16415 case VAR_STRING:
16416 if (varp->vval.v_string != NULL)
16417 vim_str2nr(varp->vval.v_string, NULL, NULL,
16418 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016419 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016420 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016421 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016422 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016423 case VAR_DICT:
16424 EMSG(_("E728: Using a Dictionary as a number"));
16425 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016426 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016427 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016430 if (denote == NULL) /* useful for values that must be unsigned */
16431 n = -1;
16432 else
16433 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016434 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435}
16436
16437/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016438 * Get the lnum from the first argument.
16439 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016440 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441 */
16442 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016443get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016444 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445{
Bram Moolenaar33570922005-01-25 22:26:29 +000016446 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 linenr_T lnum;
16448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016449 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 if (lnum == 0) /* no valid number, try using line() */
16451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016452 rettv.v_type = VAR_NUMBER;
16453 f_line(argvars, &rettv);
16454 lnum = rettv.vval.v_number;
16455 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 }
16457 return lnum;
16458}
16459
16460/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016461 * Get the lnum from the first argument.
16462 * Also accepts "$", then "buf" is used.
16463 * Returns 0 on error.
16464 */
16465 static linenr_T
16466get_tv_lnum_buf(argvars, buf)
16467 typval_T *argvars;
16468 buf_T *buf;
16469{
16470 if (argvars[0].v_type == VAR_STRING
16471 && argvars[0].vval.v_string != NULL
16472 && argvars[0].vval.v_string[0] == '$'
16473 && buf != NULL)
16474 return buf->b_ml.ml_line_count;
16475 return get_tv_number_chk(&argvars[0], NULL);
16476}
16477
16478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 * Get the string value of a variable.
16480 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016481 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16482 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 * If the String variable has never been set, return an empty string.
16484 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16486 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 */
16488 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016489get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491{
16492 static char_u mybuf[NUMBUFLEN];
16493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016494 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495}
16496
16497 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016498get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016499 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016500 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016502 char_u *res = get_tv_string_buf_chk(varp, buf);
16503
16504 return res != NULL ? res : (char_u *)"";
16505}
16506
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016507 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016508get_tv_string_chk(varp)
16509 typval_T *varp;
16510{
16511 static char_u mybuf[NUMBUFLEN];
16512
16513 return get_tv_string_buf_chk(varp, mybuf);
16514}
16515
16516 static char_u *
16517get_tv_string_buf_chk(varp, buf)
16518 typval_T *varp;
16519 char_u *buf;
16520{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016521 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016523 case VAR_NUMBER:
16524 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16525 return buf;
16526 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016527 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016528 break;
16529 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016530 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016531 break;
16532 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016533 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016534 break;
16535 case VAR_STRING:
16536 if (varp->vval.v_string != NULL)
16537 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016538 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016539 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016540 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016541 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544}
16545
16546/*
16547 * Find variable "name" in the list of variables.
16548 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016549 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016550 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016551 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016553 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016554find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016556 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016559 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560
Bram Moolenaara7043832005-01-21 11:56:39 +000016561 ht = find_var_ht(name, &varname);
16562 if (htp != NULL)
16563 *htp = ht;
16564 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016566 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567}
16568
16569/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016570 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016571 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016573 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016574find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016575 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016576 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016577 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016578{
Bram Moolenaar33570922005-01-25 22:26:29 +000016579 hashitem_T *hi;
16580
16581 if (*varname == NUL)
16582 {
16583 /* Must be something like "s:", otherwise "ht" would be NULL. */
16584 switch (varname[-2])
16585 {
16586 case 's': return &SCRIPT_SV(current_SID).sv_var;
16587 case 'g': return &globvars_var;
16588 case 'v': return &vimvars_var;
16589 case 'b': return &curbuf->b_bufvar;
16590 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016591 case 'l': return current_funccal == NULL
16592 ? NULL : &current_funccal->l_vars_var;
16593 case 'a': return current_funccal == NULL
16594 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016595 }
16596 return NULL;
16597 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016598
16599 hi = hash_find(ht, varname);
16600 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016601 {
16602 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016603 * worked find the variable again. Don't auto-load a script if it was
16604 * loaded already, otherwise it would be loaded every time when
16605 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016606 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016607 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016608 hi = hash_find(ht, varname);
16609 if (HASHITEM_EMPTY(hi))
16610 return NULL;
16611 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016612 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016613}
16614
16615/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016616 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016617 * Set "varname" to the start of name without ':'.
16618 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016619 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016620find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 char_u *name;
16622 char_u **varname;
16623{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016624 hashitem_T *hi;
16625
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 if (name[1] != ':')
16627 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016628 /* The name must not start with a colon or #. */
16629 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 return NULL;
16631 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016632
16633 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016634 hi = hash_find(&compat_hashtab, name);
16635 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016636 return &compat_hashtab;
16637
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016639 return &globvarht; /* global variable */
16640 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 }
16642 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016643 if (*name == 'g') /* global variable */
16644 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016645 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16646 */
16647 if (vim_strchr(name + 2, ':') != NULL
16648 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016649 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016651 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016653 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016654 if (*name == 'v') /* v: variable */
16655 return &vimvarht;
16656 if (*name == 'a' && current_funccal != NULL) /* function argument */
16657 return &current_funccal->l_avars.dv_hashtab;
16658 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16659 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 if (*name == 's' /* script variable */
16661 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16662 return &SCRIPT_VARS(current_SID);
16663 return NULL;
16664}
16665
16666/*
16667 * Get the string value of a (global/local) variable.
16668 * Returns NULL when it doesn't exist.
16669 */
16670 char_u *
16671get_var_value(name)
16672 char_u *name;
16673{
Bram Moolenaar33570922005-01-25 22:26:29 +000016674 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675
Bram Moolenaara7043832005-01-21 11:56:39 +000016676 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 if (v == NULL)
16678 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016679 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680}
16681
16682/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 * sourcing this script and when executing functions defined in the script.
16685 */
16686 void
16687new_script_vars(id)
16688 scid_T id;
16689{
Bram Moolenaara7043832005-01-21 11:56:39 +000016690 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016691 hashtab_T *ht;
16692 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016693
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16695 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016696 /* Re-allocating ga_data means that an ht_array pointing to
16697 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016698 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016699 for (i = 1; i <= ga_scripts.ga_len; ++i)
16700 {
16701 ht = &SCRIPT_VARS(i);
16702 if (ht->ht_mask == HT_INIT_SIZE - 1)
16703 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016704 sv = &SCRIPT_SV(i);
16705 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016706 }
16707
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708 while (ga_scripts.ga_len < id)
16709 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016710 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16711 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 }
16714 }
16715}
16716
16717/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016718 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16719 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 */
16721 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016722init_var_dict(dict, dict_var)
16723 dict_T *dict;
16724 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725{
Bram Moolenaar33570922005-01-25 22:26:29 +000016726 hash_init(&dict->dv_hashtab);
16727 dict->dv_refcount = 99999;
16728 dict_var->di_tv.vval.v_dict = dict;
16729 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016730 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016731 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16732 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733}
16734
16735/*
16736 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016737 * Frees all allocated variables and the value they contain.
16738 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 */
16740 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016741vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016742 hashtab_T *ht;
16743{
16744 vars_clear_ext(ht, TRUE);
16745}
16746
16747/*
16748 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16749 */
16750 static void
16751vars_clear_ext(ht, free_val)
16752 hashtab_T *ht;
16753 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754{
Bram Moolenaara7043832005-01-21 11:56:39 +000016755 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016756 hashitem_T *hi;
16757 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758
Bram Moolenaar33570922005-01-25 22:26:29 +000016759 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016760 todo = ht->ht_used;
16761 for (hi = ht->ht_array; todo > 0; ++hi)
16762 {
16763 if (!HASHITEM_EMPTY(hi))
16764 {
16765 --todo;
16766
Bram Moolenaar33570922005-01-25 22:26:29 +000016767 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016768 * ht_array might change then. hash_clear() takes care of it
16769 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016770 v = HI2DI(hi);
16771 if (free_val)
16772 clear_tv(&v->di_tv);
16773 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16774 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016775 }
16776 }
16777 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016778 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779}
16780
Bram Moolenaara7043832005-01-21 11:56:39 +000016781/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016782 * Delete a variable from hashtab "ht" at item "hi".
16783 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016786delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016787 hashtab_T *ht;
16788 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789{
Bram Moolenaar33570922005-01-25 22:26:29 +000016790 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016791
16792 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016793 clear_tv(&di->di_tv);
16794 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795}
16796
16797/*
16798 * List the value of one internal variable.
16799 */
16800 static void
16801list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016802 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 char_u *prefix;
16804{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016805 char_u *tofree;
16806 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016807 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016808
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016809 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016810 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016811 s == NULL ? (char_u *)"" : s);
16812 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813}
16814
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 static void
16816list_one_var_a(prefix, name, type, string)
16817 char_u *prefix;
16818 char_u *name;
16819 int type;
16820 char_u *string;
16821{
16822 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16823 if (name != NULL) /* "a:" vars don't have a name stored */
16824 msg_puts(name);
16825 msg_putchar(' ');
16826 msg_advance(22);
16827 if (type == VAR_NUMBER)
16828 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016829 else if (type == VAR_FUNC)
16830 msg_putchar('*');
16831 else if (type == VAR_LIST)
16832 {
16833 msg_putchar('[');
16834 if (*string == '[')
16835 ++string;
16836 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016837 else if (type == VAR_DICT)
16838 {
16839 msg_putchar('{');
16840 if (*string == '{')
16841 ++string;
16842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 else
16844 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016845
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016847
16848 if (type == VAR_FUNC)
16849 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850}
16851
16852/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016853 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 * If the variable already exists, the value is updated.
16855 * Otherwise the variable is created.
16856 */
16857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016858set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016861 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862{
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016865 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016866 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016868 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016869 {
16870 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16871 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16872 ? name[2] : name[0]))
16873 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016874 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016875 return;
16876 }
16877 if (function_exists(name))
16878 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016879 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016880 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016881 return;
16882 }
16883 }
16884
Bram Moolenaara7043832005-01-21 11:56:39 +000016885 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016886 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016887 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016888 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016889 return;
16890 }
16891
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016892 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016893 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016895 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016896 if (var_check_ro(v->di_flags, name)
16897 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016898 return;
16899 if (v->di_tv.v_type != tv->v_type
16900 && !((v->di_tv.v_type == VAR_STRING
16901 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016902 && (tv->v_type == VAR_STRING
16903 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016904 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016905 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016906 return;
16907 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016908
16909 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016910 * Handle setting internal v: variables separately: we don't change
16911 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016912 */
16913 if (ht == &vimvarht)
16914 {
16915 if (v->di_tv.v_type == VAR_STRING)
16916 {
16917 vim_free(v->di_tv.vval.v_string);
16918 if (copy || tv->v_type != VAR_STRING)
16919 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16920 else
16921 {
16922 /* Take over the string to avoid an extra alloc/free. */
16923 v->di_tv.vval.v_string = tv->vval.v_string;
16924 tv->vval.v_string = NULL;
16925 }
16926 }
16927 else if (v->di_tv.v_type != VAR_NUMBER)
16928 EMSG2(_(e_intern2), "set_var()");
16929 else
16930 v->di_tv.vval.v_number = get_tv_number(tv);
16931 return;
16932 }
16933
16934 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 }
16936 else /* add a new variable */
16937 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016938 /* Make sure the variable name is valid. */
16939 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016940 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16941 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016942 {
16943 EMSG2(_(e_illvar), varname);
16944 return;
16945 }
16946
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016947 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16948 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016949 if (v == NULL)
16950 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016951 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016952 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016954 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 return;
16956 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016957 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016960 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016961 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016962 else
16963 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016964 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016965 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016966 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968}
16969
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016970/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016971 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16972 * Also give an error message.
16973 */
16974 static int
16975var_check_ro(flags, name)
16976 int flags;
16977 char_u *name;
16978{
16979 if (flags & DI_FLAGS_RO)
16980 {
16981 EMSG2(_(e_readonlyvar), name);
16982 return TRUE;
16983 }
16984 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16985 {
16986 EMSG2(_(e_readonlysbx), name);
16987 return TRUE;
16988 }
16989 return FALSE;
16990}
16991
16992/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016993 * Return TRUE if typeval "tv" is set to be locked (immutable).
16994 * Also give an error message, using "name".
16995 */
16996 static int
16997tv_check_lock(lock, name)
16998 int lock;
16999 char_u *name;
17000{
17001 if (lock & VAR_LOCKED)
17002 {
17003 EMSG2(_("E741: Value is locked: %s"),
17004 name == NULL ? (char_u *)_("Unknown") : name);
17005 return TRUE;
17006 }
17007 if (lock & VAR_FIXED)
17008 {
17009 EMSG2(_("E742: Cannot change value of %s"),
17010 name == NULL ? (char_u *)_("Unknown") : name);
17011 return TRUE;
17012 }
17013 return FALSE;
17014}
17015
17016/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017017 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017018 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017019 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017023 typval_T *from;
17024 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017026 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017027 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017028 switch (from->v_type)
17029 {
17030 case VAR_NUMBER:
17031 to->vval.v_number = from->vval.v_number;
17032 break;
17033 case VAR_STRING:
17034 case VAR_FUNC:
17035 if (from->vval.v_string == NULL)
17036 to->vval.v_string = NULL;
17037 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017038 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017039 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017040 if (from->v_type == VAR_FUNC)
17041 func_ref(to->vval.v_string);
17042 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017043 break;
17044 case VAR_LIST:
17045 if (from->vval.v_list == NULL)
17046 to->vval.v_list = NULL;
17047 else
17048 {
17049 to->vval.v_list = from->vval.v_list;
17050 ++to->vval.v_list->lv_refcount;
17051 }
17052 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017053 case VAR_DICT:
17054 if (from->vval.v_dict == NULL)
17055 to->vval.v_dict = NULL;
17056 else
17057 {
17058 to->vval.v_dict = from->vval.v_dict;
17059 ++to->vval.v_dict->dv_refcount;
17060 }
17061 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017062 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017063 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017064 break;
17065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066}
17067
17068/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017069 * Make a copy of an item.
17070 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017071 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17072 * reference to an already copied list/dict can be used.
17073 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017074 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017075 static int
17076item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017077 typval_T *from;
17078 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017079 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017080 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017081{
17082 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017083 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017084
Bram Moolenaar33570922005-01-25 22:26:29 +000017085 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017086 {
17087 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017088 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017089 }
17090 ++recurse;
17091
17092 switch (from->v_type)
17093 {
17094 case VAR_NUMBER:
17095 case VAR_STRING:
17096 case VAR_FUNC:
17097 copy_tv(from, to);
17098 break;
17099 case VAR_LIST:
17100 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017101 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017102 if (from->vval.v_list == NULL)
17103 to->vval.v_list = NULL;
17104 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17105 {
17106 /* use the copy made earlier */
17107 to->vval.v_list = from->vval.v_list->lv_copylist;
17108 ++to->vval.v_list->lv_refcount;
17109 }
17110 else
17111 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17112 if (to->vval.v_list == NULL)
17113 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017114 break;
17115 case VAR_DICT:
17116 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017117 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017118 if (from->vval.v_dict == NULL)
17119 to->vval.v_dict = NULL;
17120 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17121 {
17122 /* use the copy made earlier */
17123 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17124 ++to->vval.v_dict->dv_refcount;
17125 }
17126 else
17127 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17128 if (to->vval.v_dict == NULL)
17129 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017130 break;
17131 default:
17132 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017133 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017134 }
17135 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017136 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017137}
17138
17139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 * ":echo expr1 ..." print each argument separated with a space, add a
17141 * newline at the end.
17142 * ":echon expr1 ..." print each argument plain.
17143 */
17144 void
17145ex_echo(eap)
17146 exarg_T *eap;
17147{
17148 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017149 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017150 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 char_u *p;
17152 int needclr = TRUE;
17153 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017154 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155
17156 if (eap->skip)
17157 ++emsg_skip;
17158 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17159 {
17160 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017161 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 {
17163 /*
17164 * Report the invalid expression unless the expression evaluation
17165 * has been cancelled due to an aborting error, an interrupt, or an
17166 * exception.
17167 */
17168 if (!aborting())
17169 EMSG2(_(e_invexpr2), p);
17170 break;
17171 }
17172 if (!eap->skip)
17173 {
17174 if (atstart)
17175 {
17176 atstart = FALSE;
17177 /* Call msg_start() after eval1(), evaluating the expression
17178 * may cause a message to appear. */
17179 if (eap->cmdidx == CMD_echo)
17180 msg_start();
17181 }
17182 else if (eap->cmdidx == CMD_echo)
17183 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017184 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017185 if (p != NULL)
17186 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017188 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017190 if (*p != TAB && needclr)
17191 {
17192 /* remove any text still there from the command */
17193 msg_clr_eos();
17194 needclr = FALSE;
17195 }
17196 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197 }
17198 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017199 {
17200#ifdef FEAT_MBYTE
17201 if (has_mbyte)
17202 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017203 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017204
17205 (void)msg_outtrans_len_attr(p, i, echo_attr);
17206 p += i - 1;
17207 }
17208 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017210 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017213 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017215 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 arg = skipwhite(arg);
17217 }
17218 eap->nextcmd = check_nextcmd(arg);
17219
17220 if (eap->skip)
17221 --emsg_skip;
17222 else
17223 {
17224 /* remove text that may still be there from the command */
17225 if (needclr)
17226 msg_clr_eos();
17227 if (eap->cmdidx == CMD_echo)
17228 msg_end();
17229 }
17230}
17231
17232/*
17233 * ":echohl {name}".
17234 */
17235 void
17236ex_echohl(eap)
17237 exarg_T *eap;
17238{
17239 int id;
17240
17241 id = syn_name2id(eap->arg);
17242 if (id == 0)
17243 echo_attr = 0;
17244 else
17245 echo_attr = syn_id2attr(id);
17246}
17247
17248/*
17249 * ":execute expr1 ..." execute the result of an expression.
17250 * ":echomsg expr1 ..." Print a message
17251 * ":echoerr expr1 ..." Print an error
17252 * Each gets spaces around each argument and a newline at the end for
17253 * echo commands
17254 */
17255 void
17256ex_execute(eap)
17257 exarg_T *eap;
17258{
17259 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 int ret = OK;
17262 char_u *p;
17263 garray_T ga;
17264 int len;
17265 int save_did_emsg;
17266
17267 ga_init2(&ga, 1, 80);
17268
17269 if (eap->skip)
17270 ++emsg_skip;
17271 while (*arg != NUL && *arg != '|' && *arg != '\n')
17272 {
17273 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017274 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 {
17276 /*
17277 * Report the invalid expression unless the expression evaluation
17278 * has been cancelled due to an aborting error, an interrupt, or an
17279 * exception.
17280 */
17281 if (!aborting())
17282 EMSG2(_(e_invexpr2), p);
17283 ret = FAIL;
17284 break;
17285 }
17286
17287 if (!eap->skip)
17288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017289 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 len = (int)STRLEN(p);
17291 if (ga_grow(&ga, len + 2) == FAIL)
17292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 ret = FAIL;
17295 break;
17296 }
17297 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 ga.ga_len += len;
17301 }
17302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017303 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 arg = skipwhite(arg);
17305 }
17306
17307 if (ret != FAIL && ga.ga_data != NULL)
17308 {
17309 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017312 out_flush();
17313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 else if (eap->cmdidx == CMD_echoerr)
17315 {
17316 /* We don't want to abort following commands, restore did_emsg. */
17317 save_did_emsg = did_emsg;
17318 EMSG((char_u *)ga.ga_data);
17319 if (!force_abort)
17320 did_emsg = save_did_emsg;
17321 }
17322 else if (eap->cmdidx == CMD_execute)
17323 do_cmdline((char_u *)ga.ga_data,
17324 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17325 }
17326
17327 ga_clear(&ga);
17328
17329 if (eap->skip)
17330 --emsg_skip;
17331
17332 eap->nextcmd = check_nextcmd(arg);
17333}
17334
17335/*
17336 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17337 * "arg" points to the "&" or '+' when called, to "option" when returning.
17338 * Returns NULL when no option name found. Otherwise pointer to the char
17339 * after the option name.
17340 */
17341 static char_u *
17342find_option_end(arg, opt_flags)
17343 char_u **arg;
17344 int *opt_flags;
17345{
17346 char_u *p = *arg;
17347
17348 ++p;
17349 if (*p == 'g' && p[1] == ':')
17350 {
17351 *opt_flags = OPT_GLOBAL;
17352 p += 2;
17353 }
17354 else if (*p == 'l' && p[1] == ':')
17355 {
17356 *opt_flags = OPT_LOCAL;
17357 p += 2;
17358 }
17359 else
17360 *opt_flags = 0;
17361
17362 if (!ASCII_ISALPHA(*p))
17363 return NULL;
17364 *arg = p;
17365
17366 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17367 p += 4; /* termcap option */
17368 else
17369 while (ASCII_ISALPHA(*p))
17370 ++p;
17371 return p;
17372}
17373
17374/*
17375 * ":function"
17376 */
17377 void
17378ex_function(eap)
17379 exarg_T *eap;
17380{
17381 char_u *theline;
17382 int j;
17383 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 char_u *name = NULL;
17386 char_u *p;
17387 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017388 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 garray_T newargs;
17390 garray_T newlines;
17391 int varargs = FALSE;
17392 int mustend = FALSE;
17393 int flags = 0;
17394 ufunc_T *fp;
17395 int indent;
17396 int nesting;
17397 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 dictitem_T *v;
17399 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017400 static int func_nr = 0; /* number for nameless function */
17401 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017402 hashtab_T *ht;
17403 int todo;
17404 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017405 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406
17407 /*
17408 * ":function" without argument: list functions.
17409 */
17410 if (ends_excmd(*eap->arg))
17411 {
17412 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017413 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017414 todo = func_hashtab.ht_used;
17415 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017416 {
17417 if (!HASHITEM_EMPTY(hi))
17418 {
17419 --todo;
17420 fp = HI2UF(hi);
17421 if (!isdigit(*fp->uf_name))
17422 list_func_head(fp, FALSE);
17423 }
17424 }
17425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 eap->nextcmd = check_nextcmd(eap->arg);
17427 return;
17428 }
17429
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017430 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017431 * ":function /pat": list functions matching pattern.
17432 */
17433 if (*eap->arg == '/')
17434 {
17435 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17436 if (!eap->skip)
17437 {
17438 regmatch_T regmatch;
17439
17440 c = *p;
17441 *p = NUL;
17442 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17443 *p = c;
17444 if (regmatch.regprog != NULL)
17445 {
17446 regmatch.rm_ic = p_ic;
17447
17448 todo = func_hashtab.ht_used;
17449 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17450 {
17451 if (!HASHITEM_EMPTY(hi))
17452 {
17453 --todo;
17454 fp = HI2UF(hi);
17455 if (!isdigit(*fp->uf_name)
17456 && vim_regexec(&regmatch, fp->uf_name, 0))
17457 list_func_head(fp, FALSE);
17458 }
17459 }
17460 }
17461 }
17462 if (*p == '/')
17463 ++p;
17464 eap->nextcmd = check_nextcmd(p);
17465 return;
17466 }
17467
17468 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017469 * Get the function name. There are these situations:
17470 * func normal function name
17471 * "name" == func, "fudi.fd_dict" == NULL
17472 * dict.func new dictionary entry
17473 * "name" == NULL, "fudi.fd_dict" set,
17474 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17475 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017476 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017477 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17478 * dict.func existing dict entry that's not a Funcref
17479 * "name" == NULL, "fudi.fd_dict" set,
17480 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017483 name = trans_function_name(&p, eap->skip, 0, &fudi);
17484 paren = (vim_strchr(p, '(') != NULL);
17485 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 {
17487 /*
17488 * Return on an invalid expression in braces, unless the expression
17489 * evaluation has been cancelled due to an aborting error, an
17490 * interrupt, or an exception.
17491 */
17492 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017493 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017494 if (!eap->skip && fudi.fd_newkey != NULL)
17495 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017496 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 else
17500 eap->skip = TRUE;
17501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 /* An error in a function call during evaluation of an expression in magic
17503 * braces should not cause the function not to be defined. */
17504 saved_did_emsg = did_emsg;
17505 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506
17507 /*
17508 * ":function func" with only function name: list function.
17509 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017510 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 {
17512 if (!ends_excmd(*skipwhite(p)))
17513 {
17514 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017515 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 }
17517 eap->nextcmd = check_nextcmd(p);
17518 if (eap->nextcmd != NULL)
17519 *p = NUL;
17520 if (!eap->skip && !got_int)
17521 {
17522 fp = find_func(name);
17523 if (fp != NULL)
17524 {
17525 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017526 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017528 if (FUNCLINE(fp, j) == NULL)
17529 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 msg_putchar('\n');
17531 msg_outnum((long)(j + 1));
17532 if (j < 9)
17533 msg_putchar(' ');
17534 if (j < 99)
17535 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017536 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 out_flush(); /* show a line at a time */
17538 ui_breakcheck();
17539 }
17540 if (!got_int)
17541 {
17542 msg_putchar('\n');
17543 msg_puts((char_u *)" endfunction");
17544 }
17545 }
17546 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017547 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017549 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 }
17551
17552 /*
17553 * ":function name(arg1, arg2)" Define function.
17554 */
17555 p = skipwhite(p);
17556 if (*p != '(')
17557 {
17558 if (!eap->skip)
17559 {
17560 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017561 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 }
17563 /* attempt to continue by skipping some text */
17564 if (vim_strchr(p, '(') != NULL)
17565 p = vim_strchr(p, '(');
17566 }
17567 p = skipwhite(p + 1);
17568
17569 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17570 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17571
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017572 if (!eap->skip)
17573 {
17574 /* Check the name of the function. */
17575 if (name != NULL)
17576 arg = name;
17577 else
17578 arg = fudi.fd_newkey;
17579 if (arg != NULL)
17580 {
17581 if (*arg == K_SPECIAL)
17582 j = 3;
17583 else
17584 j = 0;
17585 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17586 : eval_isnamec(arg[j])))
17587 ++j;
17588 if (arg[j] != NUL)
17589 emsg_funcname(_(e_invarg2), arg);
17590 }
17591 }
17592
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 /*
17594 * Isolate the arguments: "arg1, arg2, ...)"
17595 */
17596 while (*p != ')')
17597 {
17598 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17599 {
17600 varargs = TRUE;
17601 p += 3;
17602 mustend = TRUE;
17603 }
17604 else
17605 {
17606 arg = p;
17607 while (ASCII_ISALNUM(*p) || *p == '_')
17608 ++p;
17609 if (arg == p || isdigit(*arg)
17610 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17611 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17612 {
17613 if (!eap->skip)
17614 EMSG2(_("E125: Illegal argument: %s"), arg);
17615 break;
17616 }
17617 if (ga_grow(&newargs, 1) == FAIL)
17618 goto erret;
17619 c = *p;
17620 *p = NUL;
17621 arg = vim_strsave(arg);
17622 if (arg == NULL)
17623 goto erret;
17624 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17625 *p = c;
17626 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 if (*p == ',')
17628 ++p;
17629 else
17630 mustend = TRUE;
17631 }
17632 p = skipwhite(p);
17633 if (mustend && *p != ')')
17634 {
17635 if (!eap->skip)
17636 EMSG2(_(e_invarg2), eap->arg);
17637 break;
17638 }
17639 }
17640 ++p; /* skip the ')' */
17641
Bram Moolenaare9a41262005-01-15 22:18:47 +000017642 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 for (;;)
17644 {
17645 p = skipwhite(p);
17646 if (STRNCMP(p, "range", 5) == 0)
17647 {
17648 flags |= FC_RANGE;
17649 p += 5;
17650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017651 else if (STRNCMP(p, "dict", 4) == 0)
17652 {
17653 flags |= FC_DICT;
17654 p += 4;
17655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 else if (STRNCMP(p, "abort", 5) == 0)
17657 {
17658 flags |= FC_ABORT;
17659 p += 5;
17660 }
17661 else
17662 break;
17663 }
17664
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017665 /* When there is a line break use what follows for the function body.
17666 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17667 if (*p == '\n')
17668 line_arg = p + 1;
17669 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 EMSG(_(e_trailing));
17671
17672 /*
17673 * Read the body of the function, until ":endfunction" is found.
17674 */
17675 if (KeyTyped)
17676 {
17677 /* Check if the function already exists, don't let the user type the
17678 * whole function before telling him it doesn't work! For a script we
17679 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017680 if (!eap->skip && !eap->forceit)
17681 {
17682 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17683 EMSG(_(e_funcdict));
17684 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017685 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017688 if (!eap->skip && did_emsg)
17689 goto erret;
17690
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 msg_putchar('\n'); /* don't overwrite the function name */
17692 cmdline_row = msg_row;
17693 }
17694
17695 indent = 2;
17696 nesting = 0;
17697 for (;;)
17698 {
17699 msg_scroll = TRUE;
17700 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017701 sourcing_lnum_off = sourcing_lnum;
17702
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017703 if (line_arg != NULL)
17704 {
17705 /* Use eap->arg, split up in parts by line breaks. */
17706 theline = line_arg;
17707 p = vim_strchr(theline, '\n');
17708 if (p == NULL)
17709 line_arg += STRLEN(line_arg);
17710 else
17711 {
17712 *p = NUL;
17713 line_arg = p + 1;
17714 }
17715 }
17716 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 theline = getcmdline(':', 0L, indent);
17718 else
17719 theline = eap->getline(':', eap->cookie, indent);
17720 if (KeyTyped)
17721 lines_left = Rows - 1;
17722 if (theline == NULL)
17723 {
17724 EMSG(_("E126: Missing :endfunction"));
17725 goto erret;
17726 }
17727
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017728 /* Detect line continuation: sourcing_lnum increased more than one. */
17729 if (sourcing_lnum > sourcing_lnum_off + 1)
17730 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
17731 else
17732 sourcing_lnum_off = 0;
17733
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 if (skip_until != NULL)
17735 {
17736 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17737 * don't check for ":endfunc". */
17738 if (STRCMP(theline, skip_until) == 0)
17739 {
17740 vim_free(skip_until);
17741 skip_until = NULL;
17742 }
17743 }
17744 else
17745 {
17746 /* skip ':' and blanks*/
17747 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17748 ;
17749
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017750 /* Check for "endfunction". */
17751 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017753 if (line_arg == NULL)
17754 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 break;
17756 }
17757
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017758 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 * at "end". */
17760 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17761 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017762 else if (STRNCMP(p, "if", 2) == 0
17763 || STRNCMP(p, "wh", 2) == 0
17764 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 || STRNCMP(p, "try", 3) == 0)
17766 indent += 2;
17767
17768 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017769 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017771 if (*p == '!')
17772 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 p += eval_fname_script(p);
17774 if (ASCII_ISALPHA(*p))
17775 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017776 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 if (*skipwhite(p) == '(')
17778 {
17779 ++nesting;
17780 indent += 2;
17781 }
17782 }
17783 }
17784
17785 /* Check for ":append" or ":insert". */
17786 p = skip_range(p, NULL);
17787 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17788 || (p[0] == 'i'
17789 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17790 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17791 skip_until = vim_strsave((char_u *)".");
17792
17793 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17794 arg = skipwhite(skiptowhite(p));
17795 if (arg[0] == '<' && arg[1] =='<'
17796 && ((p[0] == 'p' && p[1] == 'y'
17797 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17798 || (p[0] == 'p' && p[1] == 'e'
17799 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17800 || (p[0] == 't' && p[1] == 'c'
17801 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17802 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17803 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017804 || (p[0] == 'm' && p[1] == 'z'
17805 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 ))
17807 {
17808 /* ":python <<" continues until a dot, like ":append" */
17809 p = skipwhite(arg + 2);
17810 if (*p == NUL)
17811 skip_until = vim_strsave((char_u *)".");
17812 else
17813 skip_until = vim_strsave(p);
17814 }
17815 }
17816
17817 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017818 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017819 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017820 if (line_arg == NULL)
17821 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017823 }
17824
17825 /* Copy the line to newly allocated memory. get_one_sourceline()
17826 * allocates 250 bytes per line, this saves 80% on average. The cost
17827 * is an extra alloc/free. */
17828 p = vim_strsave(theline);
17829 if (p != NULL)
17830 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017831 if (line_arg == NULL)
17832 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017833 theline = p;
17834 }
17835
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017836 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
17837
17838 /* Add NULL lines for continuation lines, so that the line count is
17839 * equal to the index in the growarray. */
17840 while (sourcing_lnum_off-- > 0)
17841 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017842
17843 /* Check for end of eap->arg. */
17844 if (line_arg != NULL && *line_arg == NUL)
17845 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 }
17847
17848 /* Don't define the function when skipping commands or when an error was
17849 * detected. */
17850 if (eap->skip || did_emsg)
17851 goto erret;
17852
17853 /*
17854 * If there are no errors, add the function
17855 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017856 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017857 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017858 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017859 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017860 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017861 emsg_funcname("E707: Function name conflicts with variable: %s",
17862 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017863 goto erret;
17864 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017865
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017866 fp = find_func(name);
17867 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017869 if (!eap->forceit)
17870 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017871 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017872 goto erret;
17873 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017874 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017875 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017876 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017877 name);
17878 goto erret;
17879 }
17880 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017881 ga_clear_strings(&(fp->uf_args));
17882 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017883 vim_free(name);
17884 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 }
17887 else
17888 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017889 char numbuf[20];
17890
17891 fp = NULL;
17892 if (fudi.fd_newkey == NULL && !eap->forceit)
17893 {
17894 EMSG(_(e_funcdict));
17895 goto erret;
17896 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017897 if (fudi.fd_di == NULL)
17898 {
17899 /* Can't add a function to a locked dictionary */
17900 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17901 goto erret;
17902 }
17903 /* Can't change an existing function if it is locked */
17904 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17905 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017906
17907 /* Give the function a sequential number. Can only be used with a
17908 * Funcref! */
17909 vim_free(name);
17910 sprintf(numbuf, "%d", ++func_nr);
17911 name = vim_strsave((char_u *)numbuf);
17912 if (name == NULL)
17913 goto erret;
17914 }
17915
17916 if (fp == NULL)
17917 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017918 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017919 {
17920 int slen, plen;
17921 char_u *scriptname;
17922
17923 /* Check that the autoload name matches the script name. */
17924 j = FAIL;
17925 if (sourcing_name != NULL)
17926 {
17927 scriptname = autoload_name(name);
17928 if (scriptname != NULL)
17929 {
17930 p = vim_strchr(scriptname, '/');
17931 plen = STRLEN(p);
17932 slen = STRLEN(sourcing_name);
17933 if (slen > plen && fnamecmp(p,
17934 sourcing_name + slen - plen) == 0)
17935 j = OK;
17936 vim_free(scriptname);
17937 }
17938 }
17939 if (j == FAIL)
17940 {
17941 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17942 goto erret;
17943 }
17944 }
17945
17946 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 if (fp == NULL)
17948 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017949
17950 if (fudi.fd_dict != NULL)
17951 {
17952 if (fudi.fd_di == NULL)
17953 {
17954 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017955 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017956 if (fudi.fd_di == NULL)
17957 {
17958 vim_free(fp);
17959 goto erret;
17960 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017961 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17962 {
17963 vim_free(fudi.fd_di);
17964 goto erret;
17965 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017966 }
17967 else
17968 /* overwrite existing dict entry */
17969 clear_tv(&fudi.fd_di->di_tv);
17970 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017971 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017972 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017973 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017974 }
17975
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017977 STRCPY(fp->uf_name, name);
17978 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017980 fp->uf_args = newargs;
17981 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017982#ifdef FEAT_PROFILE
17983 fp->uf_tml_count = NULL;
17984 fp->uf_tml_total = NULL;
17985 fp->uf_tml_self = NULL;
17986 fp->uf_profiling = FALSE;
17987 if (prof_def_func())
17988 func_do_profile(fp);
17989#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017990 fp->uf_varargs = varargs;
17991 fp->uf_flags = flags;
17992 fp->uf_calls = 0;
17993 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017994 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995
17996erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 ga_clear_strings(&newargs);
17998 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017999ret_free:
18000 vim_free(skip_until);
18001 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004}
18005
18006/*
18007 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018008 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018010 * flags:
18011 * TFN_INT: internal function name OK
18012 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 * Advances "pp" to just after the function name (if no error).
18014 */
18015 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018016trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 char_u **pp;
18018 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018019 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018020 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018022 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 char_u *start;
18024 char_u *end;
18025 int lead;
18026 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018029
18030 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018031 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018033
18034 /* Check for hard coded <SNR>: already translated function ID (from a user
18035 * command). */
18036 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18037 && (*pp)[2] == (int)KE_SNR)
18038 {
18039 *pp += 3;
18040 len = get_id_len(pp) + 3;
18041 return vim_strnsave(start, len);
18042 }
18043
18044 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18045 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018047 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018049
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018050 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18051 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018052 if (end == start)
18053 {
18054 if (!skip)
18055 EMSG(_("E129: Function name required"));
18056 goto theend;
18057 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018058 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018059 {
18060 /*
18061 * Report an invalid expression in braces, unless the expression
18062 * evaluation has been cancelled due to an aborting error, an
18063 * interrupt, or an exception.
18064 */
18065 if (!aborting())
18066 {
18067 if (end != NULL)
18068 EMSG2(_(e_invarg2), start);
18069 }
18070 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018071 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018072 goto theend;
18073 }
18074
18075 if (lv.ll_tv != NULL)
18076 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018077 if (fdp != NULL)
18078 {
18079 fdp->fd_dict = lv.ll_dict;
18080 fdp->fd_newkey = lv.ll_newkey;
18081 lv.ll_newkey = NULL;
18082 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018083 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018084 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18085 {
18086 name = vim_strsave(lv.ll_tv->vval.v_string);
18087 *pp = end;
18088 }
18089 else
18090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018091 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18092 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018093 EMSG(_(e_funcref));
18094 else
18095 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018096 name = NULL;
18097 }
18098 goto theend;
18099 }
18100
18101 if (lv.ll_name == NULL)
18102 {
18103 /* Error found, but continue after the function name. */
18104 *pp = end;
18105 goto theend;
18106 }
18107
18108 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018109 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018110 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018111 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18112 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18113 {
18114 /* When there was "s:" already or the name expanded to get a
18115 * leading "s:" then remove it. */
18116 lv.ll_name += 2;
18117 len -= 2;
18118 lead = 2;
18119 }
18120 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018121 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018122 {
18123 if (lead == 2) /* skip over "s:" */
18124 lv.ll_name += 2;
18125 len = (int)(end - lv.ll_name);
18126 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018127
18128 /*
18129 * Copy the function name to allocated memory.
18130 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18131 * Accept <SNR>123_name() outside a script.
18132 */
18133 if (skip)
18134 lead = 0; /* do nothing */
18135 else if (lead > 0)
18136 {
18137 lead = 3;
18138 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18139 {
18140 if (current_SID <= 0)
18141 {
18142 EMSG(_(e_usingsid));
18143 goto theend;
18144 }
18145 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18146 lead += (int)STRLEN(sid_buf);
18147 }
18148 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018149 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018150 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018151 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018152 goto theend;
18153 }
18154 name = alloc((unsigned)(len + lead + 1));
18155 if (name != NULL)
18156 {
18157 if (lead > 0)
18158 {
18159 name[0] = K_SPECIAL;
18160 name[1] = KS_EXTRA;
18161 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018162 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018163 STRCPY(name + 3, sid_buf);
18164 }
18165 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18166 name[len + lead] = NUL;
18167 }
18168 *pp = end;
18169
18170theend:
18171 clear_lval(&lv);
18172 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173}
18174
18175/*
18176 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18177 * Return 2 if "p" starts with "s:".
18178 * Return 0 otherwise.
18179 */
18180 static int
18181eval_fname_script(p)
18182 char_u *p;
18183{
18184 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18185 || STRNICMP(p + 1, "SNR>", 4) == 0))
18186 return 5;
18187 if (p[0] == 's' && p[1] == ':')
18188 return 2;
18189 return 0;
18190}
18191
18192/*
18193 * Return TRUE if "p" starts with "<SID>" or "s:".
18194 * Only works if eval_fname_script() returned non-zero for "p"!
18195 */
18196 static int
18197eval_fname_sid(p)
18198 char_u *p;
18199{
18200 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18201}
18202
18203/*
18204 * List the head of the function: "name(arg1, arg2)".
18205 */
18206 static void
18207list_func_head(fp, indent)
18208 ufunc_T *fp;
18209 int indent;
18210{
18211 int j;
18212
18213 msg_start();
18214 if (indent)
18215 MSG_PUTS(" ");
18216 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018217 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 {
18219 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018220 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 }
18222 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018223 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018225 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 {
18227 if (j)
18228 MSG_PUTS(", ");
18229 msg_puts(FUNCARG(fp, j));
18230 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018231 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 {
18233 if (j)
18234 MSG_PUTS(", ");
18235 MSG_PUTS("...");
18236 }
18237 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018238 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018239 if (p_verbose > 0)
18240 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241}
18242
18243/*
18244 * Find a function by name, return pointer to it in ufuncs.
18245 * Return NULL for unknown function.
18246 */
18247 static ufunc_T *
18248find_func(name)
18249 char_u *name;
18250{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018251 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018253 hi = hash_find(&func_hashtab, name);
18254 if (!HASHITEM_EMPTY(hi))
18255 return HI2UF(hi);
18256 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257}
18258
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018259#if defined(EXITFREE) || defined(PROTO)
18260 void
18261free_all_functions()
18262{
18263 hashitem_T *hi;
18264
18265 /* Need to start all over every time, because func_free() may change the
18266 * hash table. */
18267 while (func_hashtab.ht_used > 0)
18268 for (hi = func_hashtab.ht_array; ; ++hi)
18269 if (!HASHITEM_EMPTY(hi))
18270 {
18271 func_free(HI2UF(hi));
18272 break;
18273 }
18274}
18275#endif
18276
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018277/*
18278 * Return TRUE if a function "name" exists.
18279 */
18280 static int
18281function_exists(name)
18282 char_u *name;
18283{
18284 char_u *p = name;
18285 int n = FALSE;
18286
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018287 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018288 if (p != NULL)
18289 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018290 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018291 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018292 else
18293 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018294 vim_free(p);
18295 }
18296 return n;
18297}
18298
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018299/*
18300 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018301 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018302 */
18303 static int
18304builtin_function(name)
18305 char_u *name;
18306{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018307 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18308 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018309}
18310
Bram Moolenaar05159a02005-02-26 23:04:13 +000018311#if defined(FEAT_PROFILE) || defined(PROTO)
18312/*
18313 * Start profiling function "fp".
18314 */
18315 static void
18316func_do_profile(fp)
18317 ufunc_T *fp;
18318{
18319 fp->uf_tm_count = 0;
18320 profile_zero(&fp->uf_tm_self);
18321 profile_zero(&fp->uf_tm_total);
18322 if (fp->uf_tml_count == NULL)
18323 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18324 (sizeof(int) * fp->uf_lines.ga_len));
18325 if (fp->uf_tml_total == NULL)
18326 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18327 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18328 if (fp->uf_tml_self == NULL)
18329 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18330 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18331 fp->uf_tml_idx = -1;
18332 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18333 || fp->uf_tml_self == NULL)
18334 return; /* out of memory */
18335
18336 fp->uf_profiling = TRUE;
18337}
18338
18339/*
18340 * Dump the profiling results for all functions in file "fd".
18341 */
18342 void
18343func_dump_profile(fd)
18344 FILE *fd;
18345{
18346 hashitem_T *hi;
18347 int todo;
18348 ufunc_T *fp;
18349 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018350 ufunc_T **sorttab;
18351 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018352
18353 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018354 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18355
Bram Moolenaar05159a02005-02-26 23:04:13 +000018356 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18357 {
18358 if (!HASHITEM_EMPTY(hi))
18359 {
18360 --todo;
18361 fp = HI2UF(hi);
18362 if (fp->uf_profiling)
18363 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018364 if (sorttab != NULL)
18365 sorttab[st_len++] = fp;
18366
Bram Moolenaar05159a02005-02-26 23:04:13 +000018367 if (fp->uf_name[0] == K_SPECIAL)
18368 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18369 else
18370 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18371 if (fp->uf_tm_count == 1)
18372 fprintf(fd, "Called 1 time\n");
18373 else
18374 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18375 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18376 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18377 fprintf(fd, "\n");
18378 fprintf(fd, "count total (s) self (s)\n");
18379
18380 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18381 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018382 if (FUNCLINE(fp, i) == NULL)
18383 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018384 prof_func_line(fd, fp->uf_tml_count[i],
18385 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018386 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18387 }
18388 fprintf(fd, "\n");
18389 }
18390 }
18391 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018392
18393 if (sorttab != NULL && st_len > 0)
18394 {
18395 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18396 prof_total_cmp);
18397 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18398 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18399 prof_self_cmp);
18400 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18401 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018402}
Bram Moolenaar73830342005-02-28 22:48:19 +000018403
18404 static void
18405prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18406 FILE *fd;
18407 ufunc_T **sorttab;
18408 int st_len;
18409 char *title;
18410 int prefer_self; /* when equal print only self time */
18411{
18412 int i;
18413 ufunc_T *fp;
18414
18415 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18416 fprintf(fd, "count total (s) self (s) function\n");
18417 for (i = 0; i < 20 && i < st_len; ++i)
18418 {
18419 fp = sorttab[i];
18420 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18421 prefer_self);
18422 if (fp->uf_name[0] == K_SPECIAL)
18423 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18424 else
18425 fprintf(fd, " %s()\n", fp->uf_name);
18426 }
18427 fprintf(fd, "\n");
18428}
18429
18430/*
18431 * Print the count and times for one function or function line.
18432 */
18433 static void
18434prof_func_line(fd, count, total, self, prefer_self)
18435 FILE *fd;
18436 int count;
18437 proftime_T *total;
18438 proftime_T *self;
18439 int prefer_self; /* when equal print only self time */
18440{
18441 if (count > 0)
18442 {
18443 fprintf(fd, "%5d ", count);
18444 if (prefer_self && profile_equal(total, self))
18445 fprintf(fd, " ");
18446 else
18447 fprintf(fd, "%s ", profile_msg(total));
18448 if (!prefer_self && profile_equal(total, self))
18449 fprintf(fd, " ");
18450 else
18451 fprintf(fd, "%s ", profile_msg(self));
18452 }
18453 else
18454 fprintf(fd, " ");
18455}
18456
18457/*
18458 * Compare function for total time sorting.
18459 */
18460 static int
18461#ifdef __BORLANDC__
18462_RTLENTRYF
18463#endif
18464prof_total_cmp(s1, s2)
18465 const void *s1;
18466 const void *s2;
18467{
18468 ufunc_T *p1, *p2;
18469
18470 p1 = *(ufunc_T **)s1;
18471 p2 = *(ufunc_T **)s2;
18472 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18473}
18474
18475/*
18476 * Compare function for self time sorting.
18477 */
18478 static int
18479#ifdef __BORLANDC__
18480_RTLENTRYF
18481#endif
18482prof_self_cmp(s1, s2)
18483 const void *s1;
18484 const void *s2;
18485{
18486 ufunc_T *p1, *p2;
18487
18488 p1 = *(ufunc_T **)s1;
18489 p2 = *(ufunc_T **)s2;
18490 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18491}
18492
Bram Moolenaar05159a02005-02-26 23:04:13 +000018493#endif
18494
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018495/* The names of packages that once were loaded is remembered. */
18496static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18497
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018498/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018499 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018500 * Return TRUE if a package was loaded.
18501 */
18502 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018503script_autoload(name, reload)
18504 char_u *name;
18505 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018506{
18507 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018508 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018509 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018510 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018511
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018512 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018513 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018514 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018515 return FALSE;
18516
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018517 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018518
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018519 /* Find the name in the list of previously loaded package names. Skip
18520 * "autoload/", it's always the same. */
18521 for (i = 0; i < ga_loaded.ga_len; ++i)
18522 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18523 break;
18524 if (!reload && i < ga_loaded.ga_len)
18525 ret = FALSE; /* was loaded already */
18526 else
18527 {
18528 /* Remember the name if it wasn't loaded already. */
18529 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18530 {
18531 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18532 tofree = NULL;
18533 }
18534
18535 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018536 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018537 ret = TRUE;
18538 }
18539
18540 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018541 return ret;
18542}
18543
18544/*
18545 * Return the autoload script name for a function or variable name.
18546 * Returns NULL when out of memory.
18547 */
18548 static char_u *
18549autoload_name(name)
18550 char_u *name;
18551{
18552 char_u *p;
18553 char_u *scriptname;
18554
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018555 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018556 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18557 if (scriptname == NULL)
18558 return FALSE;
18559 STRCPY(scriptname, "autoload/");
18560 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018561 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018562 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018563 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018564 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018565 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018566}
18567
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18569
18570/*
18571 * Function given to ExpandGeneric() to obtain the list of user defined
18572 * function names.
18573 */
18574 char_u *
18575get_user_func_name(xp, idx)
18576 expand_T *xp;
18577 int idx;
18578{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018579 static long_u done;
18580 static hashitem_T *hi;
18581 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582
18583 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018585 done = 0;
18586 hi = func_hashtab.ht_array;
18587 }
18588 if (done < func_hashtab.ht_used)
18589 {
18590 if (done++ > 0)
18591 ++hi;
18592 while (HASHITEM_EMPTY(hi))
18593 ++hi;
18594 fp = HI2UF(hi);
18595
18596 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18597 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598
18599 cat_func_name(IObuff, fp);
18600 if (xp->xp_context != EXPAND_USER_FUNC)
18601 {
18602 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018603 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 STRCAT(IObuff, ")");
18605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 return IObuff;
18607 }
18608 return NULL;
18609}
18610
18611#endif /* FEAT_CMDL_COMPL */
18612
18613/*
18614 * Copy the function name of "fp" to buffer "buf".
18615 * "buf" must be able to hold the function name plus three bytes.
18616 * Takes care of script-local function names.
18617 */
18618 static void
18619cat_func_name(buf, fp)
18620 char_u *buf;
18621 ufunc_T *fp;
18622{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018623 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 {
18625 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018626 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 }
18628 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018629 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630}
18631
18632/*
18633 * ":delfunction {name}"
18634 */
18635 void
18636ex_delfunction(eap)
18637 exarg_T *eap;
18638{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018639 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640 char_u *p;
18641 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018642 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643
18644 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018645 name = trans_function_name(&p, eap->skip, 0, &fudi);
18646 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018648 {
18649 if (fudi.fd_dict != NULL && !eap->skip)
18650 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 if (!ends_excmd(*skipwhite(p)))
18654 {
18655 vim_free(name);
18656 EMSG(_(e_trailing));
18657 return;
18658 }
18659 eap->nextcmd = check_nextcmd(p);
18660 if (eap->nextcmd != NULL)
18661 *p = NUL;
18662
18663 if (!eap->skip)
18664 fp = find_func(name);
18665 vim_free(name);
18666
18667 if (!eap->skip)
18668 {
18669 if (fp == NULL)
18670 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018671 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 return;
18673 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018674 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 {
18676 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18677 return;
18678 }
18679
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018680 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018682 /* Delete the dict item that refers to the function, it will
18683 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018684 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018686 else
18687 func_free(fp);
18688 }
18689}
18690
18691/*
18692 * Free a function and remove it from the list of functions.
18693 */
18694 static void
18695func_free(fp)
18696 ufunc_T *fp;
18697{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018698 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018699
18700 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018701 ga_clear_strings(&(fp->uf_args));
18702 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018703#ifdef FEAT_PROFILE
18704 vim_free(fp->uf_tml_count);
18705 vim_free(fp->uf_tml_total);
18706 vim_free(fp->uf_tml_self);
18707#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018708
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018709 /* remove the function from the function hashtable */
18710 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18711 if (HASHITEM_EMPTY(hi))
18712 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018713 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018714 hash_remove(&func_hashtab, hi);
18715
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018716 vim_free(fp);
18717}
18718
18719/*
18720 * Unreference a Function: decrement the reference count and free it when it
18721 * becomes zero. Only for numbered functions.
18722 */
18723 static void
18724func_unref(name)
18725 char_u *name;
18726{
18727 ufunc_T *fp;
18728
18729 if (name != NULL && isdigit(*name))
18730 {
18731 fp = find_func(name);
18732 if (fp == NULL)
18733 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018734 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018735 {
18736 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018737 * when "uf_calls" becomes zero. */
18738 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018739 func_free(fp);
18740 }
18741 }
18742}
18743
18744/*
18745 * Count a reference to a Function.
18746 */
18747 static void
18748func_ref(name)
18749 char_u *name;
18750{
18751 ufunc_T *fp;
18752
18753 if (name != NULL && isdigit(*name))
18754 {
18755 fp = find_func(name);
18756 if (fp == NULL)
18757 EMSG2(_(e_intern2), "func_ref()");
18758 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018759 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 }
18761}
18762
18763/*
18764 * Call a user function.
18765 */
18766 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018767call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 ufunc_T *fp; /* pointer to function */
18769 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018770 typval_T *argvars; /* arguments */
18771 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 linenr_T firstline; /* first line of range */
18773 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018774 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775{
Bram Moolenaar33570922005-01-25 22:26:29 +000018776 char_u *save_sourcing_name;
18777 linenr_T save_sourcing_lnum;
18778 scid_T save_current_SID;
18779 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018780 int save_did_emsg;
18781 static int depth = 0;
18782 dictitem_T *v;
18783 int fixvar_idx = 0; /* index in fixvar[] */
18784 int i;
18785 int ai;
18786 char_u numbuf[NUMBUFLEN];
18787 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018788#ifdef FEAT_PROFILE
18789 proftime_T wait_start;
18790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791
18792 /* If depth of calling is getting too high, don't execute the function */
18793 if (depth >= p_mfd)
18794 {
18795 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018796 rettv->v_type = VAR_NUMBER;
18797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 return;
18799 }
18800 ++depth;
18801
18802 line_breakcheck(); /* check for CTRL-C hit */
18803
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018804 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018807 fc.rettv = rettv;
18808 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 fc.linenr = 0;
18810 fc.returned = FALSE;
18811 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018813 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 fc.dbg_tick = debug_tick;
18815
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 /*
18817 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18818 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18819 * each argument variable and saves a lot of time.
18820 */
18821 /*
18822 * Init l: variables.
18823 */
18824 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018825 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018826 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018827 /* Set l:self to "selfdict". */
18828 v = &fc.fixvar[fixvar_idx++].var;
18829 STRCPY(v->di_key, "self");
18830 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18831 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18832 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018833 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018834 v->di_tv.vval.v_dict = selfdict;
18835 ++selfdict->dv_refcount;
18836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018837
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 /*
18839 * Init a: variables.
18840 * Set a:0 to "argcount".
18841 * Set a:000 to a list with room for the "..." arguments.
18842 */
18843 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18844 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018845 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018846 v = &fc.fixvar[fixvar_idx++].var;
18847 STRCPY(v->di_key, "000");
18848 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18849 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18850 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018851 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018852 v->di_tv.vval.v_list = &fc.l_varlist;
18853 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18854 fc.l_varlist.lv_refcount = 99999;
18855
18856 /*
18857 * Set a:firstline to "firstline" and a:lastline to "lastline".
18858 * Set a:name to named arguments.
18859 * Set a:N to the "..." arguments.
18860 */
18861 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18862 (varnumber_T)firstline);
18863 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18864 (varnumber_T)lastline);
18865 for (i = 0; i < argcount; ++i)
18866 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018867 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018868 if (ai < 0)
18869 /* named argument a:name */
18870 name = FUNCARG(fp, i);
18871 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018872 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018873 /* "..." argument a:1, a:2, etc. */
18874 sprintf((char *)numbuf, "%d", ai + 1);
18875 name = numbuf;
18876 }
18877 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18878 {
18879 v = &fc.fixvar[fixvar_idx++].var;
18880 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18881 }
18882 else
18883 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018884 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18885 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018886 if (v == NULL)
18887 break;
18888 v->di_flags = DI_FLAGS_RO;
18889 }
18890 STRCPY(v->di_key, name);
18891 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18892
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018893 /* Note: the values are copied directly to avoid alloc/free.
18894 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018895 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018896 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018897
18898 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18899 {
18900 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18901 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018902 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018903 }
18904 }
18905
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 /* Don't redraw while executing the function. */
18907 ++RedrawingDisabled;
18908 save_sourcing_name = sourcing_name;
18909 save_sourcing_lnum = sourcing_lnum;
18910 sourcing_lnum = 1;
18911 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018912 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 if (sourcing_name != NULL)
18914 {
18915 if (save_sourcing_name != NULL
18916 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18917 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18918 else
18919 STRCPY(sourcing_name, "function ");
18920 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18921
18922 if (p_verbose >= 12)
18923 {
18924 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018925 verbose_enter_scroll();
18926
Bram Moolenaar555b2802005-05-19 21:08:39 +000018927 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 if (p_verbose >= 14)
18929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018931 char_u numbuf[NUMBUFLEN];
18932 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933
18934 msg_puts((char_u *)"(");
18935 for (i = 0; i < argcount; ++i)
18936 {
18937 if (i > 0)
18938 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018939 if (argvars[i].v_type == VAR_NUMBER)
18940 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 else
18942 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018943 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018944 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018946 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947 }
18948 }
18949 msg_puts((char_u *)")");
18950 }
18951 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018952
18953 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 --no_wait_return;
18955 }
18956 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018957#ifdef FEAT_PROFILE
18958 if (do_profiling)
18959 {
18960 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18961 func_do_profile(fp);
18962 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018963 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018964 {
18965 ++fp->uf_tm_count;
18966 profile_start(&fp->uf_tm_start);
18967 profile_zero(&fp->uf_tm_children);
18968 }
18969 script_prof_save(&wait_start);
18970 }
18971#endif
18972
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018974 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 save_did_emsg = did_emsg;
18976 did_emsg = FALSE;
18977
18978 /* call do_cmdline() to execute the lines */
18979 do_cmdline(NULL, get_func_line, (void *)&fc,
18980 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18981
18982 --RedrawingDisabled;
18983
18984 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018985 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987 clear_tv(rettv);
18988 rettv->v_type = VAR_NUMBER;
18989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 }
18991
Bram Moolenaar05159a02005-02-26 23:04:13 +000018992#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018993 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018994 {
18995 profile_end(&fp->uf_tm_start);
18996 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18997 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18998 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18999 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019000 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019001 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019002 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19003 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019004 }
19005 }
19006#endif
19007
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 /* when being verbose, mention the return value */
19009 if (p_verbose >= 12)
19010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019012 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019015 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019016 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019017 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19018 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019021 char_u buf[MSG_BUF_LEN];
19022 char_u numbuf[NUMBUFLEN];
19023 char_u *tofree;
19024
Bram Moolenaar555b2802005-05-19 21:08:39 +000019025 /* The value may be very long. Skip the middle part, so that we
19026 * have some idea how it starts and ends. smsg() would always
19027 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019028 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019029 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019030 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019031 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 }
19033 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019034
19035 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 --no_wait_return;
19037 }
19038
19039 vim_free(sourcing_name);
19040 sourcing_name = save_sourcing_name;
19041 sourcing_lnum = save_sourcing_lnum;
19042 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019043#ifdef FEAT_PROFILE
19044 if (do_profiling)
19045 script_prof_restore(&wait_start);
19046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047
19048 if (p_verbose >= 12 && sourcing_name != NULL)
19049 {
19050 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019051 verbose_enter_scroll();
19052
Bram Moolenaar555b2802005-05-19 21:08:39 +000019053 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019055
19056 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 --no_wait_return;
19058 }
19059
19060 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019061 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 /* The a: variables typevals were not alloced, only free the allocated
19064 * variables. */
19065 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19066
19067 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 --depth;
19069}
19070
19071/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019072 * Add a number variable "name" to dict "dp" with value "nr".
19073 */
19074 static void
19075add_nr_var(dp, v, name, nr)
19076 dict_T *dp;
19077 dictitem_T *v;
19078 char *name;
19079 varnumber_T nr;
19080{
19081 STRCPY(v->di_key, name);
19082 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19083 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19084 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019085 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019086 v->di_tv.vval.v_number = nr;
19087}
19088
19089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 * ":return [expr]"
19091 */
19092 void
19093ex_return(eap)
19094 exarg_T *eap;
19095{
19096 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019097 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 int returning = FALSE;
19099
19100 if (current_funccal == NULL)
19101 {
19102 EMSG(_("E133: :return not inside a function"));
19103 return;
19104 }
19105
19106 if (eap->skip)
19107 ++emsg_skip;
19108
19109 eap->nextcmd = NULL;
19110 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019111 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 {
19113 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019114 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019116 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 }
19118 /* It's safer to return also on error. */
19119 else if (!eap->skip)
19120 {
19121 /*
19122 * Return unless the expression evaluation has been cancelled due to an
19123 * aborting error, an interrupt, or an exception.
19124 */
19125 if (!aborting())
19126 returning = do_return(eap, FALSE, TRUE, NULL);
19127 }
19128
19129 /* When skipping or the return gets pending, advance to the next command
19130 * in this line (!returning). Otherwise, ignore the rest of the line.
19131 * Following lines will be ignored by get_func_line(). */
19132 if (returning)
19133 eap->nextcmd = NULL;
19134 else if (eap->nextcmd == NULL) /* no argument */
19135 eap->nextcmd = check_nextcmd(arg);
19136
19137 if (eap->skip)
19138 --emsg_skip;
19139}
19140
19141/*
19142 * Return from a function. Possibly makes the return pending. Also called
19143 * for a pending return at the ":endtry" or after returning from an extra
19144 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019146 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 * FALSE when the return gets pending.
19148 */
19149 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019150do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 exarg_T *eap;
19152 int reanimate;
19153 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019154 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155{
19156 int idx;
19157 struct condstack *cstack = eap->cstack;
19158
19159 if (reanimate)
19160 /* Undo the return. */
19161 current_funccal->returned = FALSE;
19162
19163 /*
19164 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19165 * not in its finally clause (which then is to be executed next) is found.
19166 * In this case, make the ":return" pending for execution at the ":endtry".
19167 * Otherwise, return normally.
19168 */
19169 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19170 if (idx >= 0)
19171 {
19172 cstack->cs_pending[idx] = CSTP_RETURN;
19173
19174 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 /* A pending return again gets pending. "rettv" points to an
19176 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 else
19180 {
19181 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019182 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019184 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 {
19188 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019189 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019190 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 else
19192 EMSG(_(e_outofmem));
19193 }
19194 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019195 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196
19197 if (reanimate)
19198 {
19199 /* The pending return value could be overwritten by a ":return"
19200 * without argument in a finally clause; reset the default
19201 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019202 current_funccal->rettv->v_type = VAR_NUMBER;
19203 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 }
19205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019206 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 }
19208 else
19209 {
19210 current_funccal->returned = TRUE;
19211
19212 /* If the return is carried out now, store the return value. For
19213 * a return immediately after reanimation, the value is already
19214 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019215 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019218 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019220 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 }
19222 }
19223
19224 return idx < 0;
19225}
19226
19227/*
19228 * Free the variable with a pending return value.
19229 */
19230 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231discard_pending_return(rettv)
19232 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233{
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235}
19236
19237/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019238 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 * is an allocated string. Used by report_pending() for verbose messages.
19240 */
19241 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019242get_return_cmd(rettv)
19243 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019245 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019246 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019247 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019249 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019250 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019251 if (s == NULL)
19252 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019253
19254 STRCPY(IObuff, ":return ");
19255 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19256 if (STRLEN(s) + 8 >= IOSIZE)
19257 STRCPY(IObuff + IOSIZE - 4, "...");
19258 vim_free(tofree);
19259 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260}
19261
19262/*
19263 * Get next function line.
19264 * Called by do_cmdline() to get the next line.
19265 * Returns allocated string, or NULL for end of function.
19266 */
19267/* ARGSUSED */
19268 char_u *
19269get_func_line(c, cookie, indent)
19270 int c; /* not used */
19271 void *cookie;
19272 int indent; /* not used */
19273{
Bram Moolenaar33570922005-01-25 22:26:29 +000019274 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019275 ufunc_T *fp = fcp->func;
19276 char_u *retval;
19277 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278
19279 /* If breakpoints have been added/deleted need to check for it. */
19280 if (fcp->dbg_tick != debug_tick)
19281 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019282 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283 sourcing_lnum);
19284 fcp->dbg_tick = debug_tick;
19285 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019286#ifdef FEAT_PROFILE
19287 if (do_profiling)
19288 func_line_end(cookie);
19289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290
Bram Moolenaar05159a02005-02-26 23:04:13 +000019291 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019292 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19293 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 retval = NULL;
19295 else
19296 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019297 /* Skip NULL lines (continuation lines). */
19298 while (fcp->linenr < gap->ga_len
19299 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19300 ++fcp->linenr;
19301 if (fcp->linenr >= gap->ga_len)
19302 retval = NULL;
19303 else
19304 {
19305 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19306 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019307#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019308 if (do_profiling)
19309 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019310#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 }
19313
19314 /* Did we encounter a breakpoint? */
19315 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19316 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019317 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019319 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 sourcing_lnum);
19321 fcp->dbg_tick = debug_tick;
19322 }
19323
19324 return retval;
19325}
19326
Bram Moolenaar05159a02005-02-26 23:04:13 +000019327#if defined(FEAT_PROFILE) || defined(PROTO)
19328/*
19329 * Called when starting to read a function line.
19330 * "sourcing_lnum" must be correct!
19331 * When skipping lines it may not actually be executed, but we won't find out
19332 * until later and we need to store the time now.
19333 */
19334 void
19335func_line_start(cookie)
19336 void *cookie;
19337{
19338 funccall_T *fcp = (funccall_T *)cookie;
19339 ufunc_T *fp = fcp->func;
19340
19341 if (fp->uf_profiling && sourcing_lnum >= 1
19342 && sourcing_lnum <= fp->uf_lines.ga_len)
19343 {
19344 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019345 /* Skip continuation lines. */
19346 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19347 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019348 fp->uf_tml_execed = FALSE;
19349 profile_start(&fp->uf_tml_start);
19350 profile_zero(&fp->uf_tml_children);
19351 profile_get_wait(&fp->uf_tml_wait);
19352 }
19353}
19354
19355/*
19356 * Called when actually executing a function line.
19357 */
19358 void
19359func_line_exec(cookie)
19360 void *cookie;
19361{
19362 funccall_T *fcp = (funccall_T *)cookie;
19363 ufunc_T *fp = fcp->func;
19364
19365 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19366 fp->uf_tml_execed = TRUE;
19367}
19368
19369/*
19370 * Called when done with a function line.
19371 */
19372 void
19373func_line_end(cookie)
19374 void *cookie;
19375{
19376 funccall_T *fcp = (funccall_T *)cookie;
19377 ufunc_T *fp = fcp->func;
19378
19379 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19380 {
19381 if (fp->uf_tml_execed)
19382 {
19383 ++fp->uf_tml_count[fp->uf_tml_idx];
19384 profile_end(&fp->uf_tml_start);
19385 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19386 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19387 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19388 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19389 }
19390 fp->uf_tml_idx = -1;
19391 }
19392}
19393#endif
19394
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395/*
19396 * Return TRUE if the currently active function should be ended, because a
19397 * return was encountered or an error occured. Used inside a ":while".
19398 */
19399 int
19400func_has_ended(cookie)
19401 void *cookie;
19402{
Bram Moolenaar33570922005-01-25 22:26:29 +000019403 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404
19405 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19406 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019407 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 || fcp->returned);
19409}
19410
19411/*
19412 * return TRUE if cookie indicates a function which "abort"s on errors.
19413 */
19414 int
19415func_has_abort(cookie)
19416 void *cookie;
19417{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019418 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419}
19420
19421#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19422typedef enum
19423{
19424 VAR_FLAVOUR_DEFAULT,
19425 VAR_FLAVOUR_SESSION,
19426 VAR_FLAVOUR_VIMINFO
19427} var_flavour_T;
19428
19429static var_flavour_T var_flavour __ARGS((char_u *varname));
19430
19431 static var_flavour_T
19432var_flavour(varname)
19433 char_u *varname;
19434{
19435 char_u *p = varname;
19436
19437 if (ASCII_ISUPPER(*p))
19438 {
19439 while (*(++p))
19440 if (ASCII_ISLOWER(*p))
19441 return VAR_FLAVOUR_SESSION;
19442 return VAR_FLAVOUR_VIMINFO;
19443 }
19444 else
19445 return VAR_FLAVOUR_DEFAULT;
19446}
19447#endif
19448
19449#if defined(FEAT_VIMINFO) || defined(PROTO)
19450/*
19451 * Restore global vars that start with a capital from the viminfo file
19452 */
19453 int
19454read_viminfo_varlist(virp, writing)
19455 vir_T *virp;
19456 int writing;
19457{
19458 char_u *tab;
19459 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019460 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461
19462 if (!writing && (find_viminfo_parameter('!') != NULL))
19463 {
19464 tab = vim_strchr(virp->vir_line + 1, '\t');
19465 if (tab != NULL)
19466 {
19467 *tab++ = '\0'; /* isolate the variable name */
19468 if (*tab == 'S') /* string var */
19469 is_string = TRUE;
19470
19471 tab = vim_strchr(tab, '\t');
19472 if (tab != NULL)
19473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 if (is_string)
19475 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019476 tv.v_type = VAR_STRING;
19477 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 }
19480 else
19481 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019482 tv.v_type = VAR_NUMBER;
19483 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019485 set_var(virp->vir_line + 1, &tv, FALSE);
19486 if (is_string)
19487 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 }
19489 }
19490 }
19491
19492 return viminfo_readline(virp);
19493}
19494
19495/*
19496 * Write global vars that start with a capital to the viminfo file
19497 */
19498 void
19499write_viminfo_varlist(fp)
19500 FILE *fp;
19501{
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 hashitem_T *hi;
19503 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019504 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019505 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019506 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019508 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509
19510 if (find_viminfo_parameter('!') == NULL)
19511 return;
19512
19513 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019514
Bram Moolenaar33570922005-01-25 22:26:29 +000019515 todo = globvarht.ht_used;
19516 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019518 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019520 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019521 this_var = HI2DI(hi);
19522 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019525 {
19526 case VAR_STRING: s = "STR"; break;
19527 case VAR_NUMBER: s = "NUM"; break;
19528 default: continue;
19529 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019531 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019532 if (p != NULL)
19533 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019534 vim_free(tofree);
19535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 }
19537 }
19538}
19539#endif
19540
19541#if defined(FEAT_SESSION) || defined(PROTO)
19542 int
19543store_session_globals(fd)
19544 FILE *fd;
19545{
Bram Moolenaar33570922005-01-25 22:26:29 +000019546 hashitem_T *hi;
19547 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019548 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 char_u *p, *t;
19550
Bram Moolenaar33570922005-01-25 22:26:29 +000019551 todo = globvarht.ht_used;
19552 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019554 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019556 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019557 this_var = HI2DI(hi);
19558 if ((this_var->di_tv.v_type == VAR_NUMBER
19559 || this_var->di_tv.v_type == VAR_STRING)
19560 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019561 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019562 /* Escape special characters with a backslash. Turn a LF and
19563 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019564 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019565 (char_u *)"\\\"\n\r");
19566 if (p == NULL) /* out of memory */
19567 break;
19568 for (t = p; *t != NUL; ++t)
19569 if (*t == '\n')
19570 *t = 'n';
19571 else if (*t == '\r')
19572 *t = 'r';
19573 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019574 this_var->di_key,
19575 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19576 : ' ',
19577 p,
19578 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19579 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019580 || put_eol(fd) == FAIL)
19581 {
19582 vim_free(p);
19583 return FAIL;
19584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 vim_free(p);
19586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 }
19588 }
19589 return OK;
19590}
19591#endif
19592
Bram Moolenaar661b1822005-07-28 22:36:45 +000019593/*
19594 * Display script name where an item was last set.
19595 * Should only be invoked when 'verbose' is non-zero.
19596 */
19597 void
19598last_set_msg(scriptID)
19599 scid_T scriptID;
19600{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019601 char_u *p;
19602
Bram Moolenaar661b1822005-07-28 22:36:45 +000019603 if (scriptID != 0)
19604 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019605 p = home_replace_save(NULL, get_scriptname(scriptID));
19606 if (p != NULL)
19607 {
19608 verbose_enter();
19609 MSG_PUTS(_("\n\tLast set from "));
19610 MSG_PUTS(p);
19611 vim_free(p);
19612 verbose_leave();
19613 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019614 }
19615}
19616
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617#endif /* FEAT_EVAL */
19618
19619#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19620
19621
19622#ifdef WIN3264
19623/*
19624 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19625 */
19626static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19627static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19628static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19629
19630/*
19631 * Get the short pathname of a file.
19632 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19633 */
19634 static int
19635get_short_pathname(fnamep, bufp, fnamelen)
19636 char_u **fnamep;
19637 char_u **bufp;
19638 int *fnamelen;
19639{
19640 int l,len;
19641 char_u *newbuf;
19642
19643 len = *fnamelen;
19644
19645 l = GetShortPathName(*fnamep, *fnamep, len);
19646 if (l > len - 1)
19647 {
19648 /* If that doesn't work (not enough space), then save the string
19649 * and try again with a new buffer big enough
19650 */
19651 newbuf = vim_strnsave(*fnamep, l);
19652 if (newbuf == NULL)
19653 return 0;
19654
19655 vim_free(*bufp);
19656 *fnamep = *bufp = newbuf;
19657
19658 l = GetShortPathName(*fnamep,*fnamep,l+1);
19659
19660 /* Really should always succeed, as the buffer is big enough */
19661 }
19662
19663 *fnamelen = l;
19664 return 1;
19665}
19666
19667/*
19668 * Create a short path name. Returns the length of the buffer it needs.
19669 * Doesn't copy over the end of the buffer passed in.
19670 */
19671 static int
19672shortpath_for_invalid_fname(fname, bufp, fnamelen)
19673 char_u **fname;
19674 char_u **bufp;
19675 int *fnamelen;
19676{
19677 char_u *s, *p, *pbuf2, *pbuf3;
19678 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019679 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680
19681 /* Make a copy */
19682 len2 = *fnamelen;
19683 pbuf2 = vim_strnsave(*fname, len2);
19684 pbuf3 = NULL;
19685
19686 s = pbuf2 + len2 - 1; /* Find the end */
19687 slen = 1;
19688 plen = len2;
19689
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019690 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 {
19692 --s;
19693 ++slen;
19694 --plen;
19695 }
19696
19697 do
19698 {
19699 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019700 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 {
19702 --s;
19703 ++slen;
19704 --plen;
19705 }
19706 if (s <= pbuf2)
19707 break;
19708
19709 /* Remeber the character that is about to be blatted */
19710 ch = *s;
19711 *s = 0; /* get_short_pathname requires a null-terminated string */
19712
19713 /* Try it in situ */
19714 p = pbuf2;
19715 if (!get_short_pathname(&p, &pbuf3, &plen))
19716 {
19717 vim_free(pbuf2);
19718 return -1;
19719 }
19720 *s = ch; /* Preserve the string */
19721 } while (plen == 0);
19722
19723 if (plen > 0)
19724 {
19725 /* Remeber the length of the new string. */
19726 *fnamelen = len = plen + slen;
19727 vim_free(*bufp);
19728 if (len > len2)
19729 {
19730 /* If there's not enough space in the currently allocated string,
19731 * then copy it to a buffer big enough.
19732 */
19733 *fname= *bufp = vim_strnsave(p, len);
19734 if (*fname == NULL)
19735 return -1;
19736 }
19737 else
19738 {
19739 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19740 *fname = *bufp = pbuf2;
19741 if (p != pbuf2)
19742 strncpy(*fname, p, plen);
19743 pbuf2 = NULL;
19744 }
19745 /* Concat the next bit */
19746 strncpy(*fname + plen, s, slen);
19747 (*fname)[len] = '\0';
19748 }
19749 vim_free(pbuf3);
19750 vim_free(pbuf2);
19751 return 0;
19752}
19753
19754/*
19755 * Get a pathname for a partial path.
19756 */
19757 static int
19758shortpath_for_partial(fnamep, bufp, fnamelen)
19759 char_u **fnamep;
19760 char_u **bufp;
19761 int *fnamelen;
19762{
19763 int sepcount, len, tflen;
19764 char_u *p;
19765 char_u *pbuf, *tfname;
19766 int hasTilde;
19767
19768 /* Count up the path seperators from the RHS.. so we know which part
19769 * of the path to return.
19770 */
19771 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019772 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 if (vim_ispathsep(*p))
19774 ++sepcount;
19775
19776 /* Need full path first (use expand_env() to remove a "~/") */
19777 hasTilde = (**fnamep == '~');
19778 if (hasTilde)
19779 pbuf = tfname = expand_env_save(*fnamep);
19780 else
19781 pbuf = tfname = FullName_save(*fnamep, FALSE);
19782
19783 len = tflen = STRLEN(tfname);
19784
19785 if (!get_short_pathname(&tfname, &pbuf, &len))
19786 return -1;
19787
19788 if (len == 0)
19789 {
19790 /* Don't have a valid filename, so shorten the rest of the
19791 * path if we can. This CAN give us invalid 8.3 filenames, but
19792 * there's not a lot of point in guessing what it might be.
19793 */
19794 len = tflen;
19795 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19796 return -1;
19797 }
19798
19799 /* Count the paths backward to find the beginning of the desired string. */
19800 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019801 {
19802#ifdef FEAT_MBYTE
19803 if (has_mbyte)
19804 p -= mb_head_off(tfname, p);
19805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 if (vim_ispathsep(*p))
19807 {
19808 if (sepcount == 0 || (hasTilde && sepcount == 1))
19809 break;
19810 else
19811 sepcount --;
19812 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 if (hasTilde)
19815 {
19816 --p;
19817 if (p >= tfname)
19818 *p = '~';
19819 else
19820 return -1;
19821 }
19822 else
19823 ++p;
19824
19825 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19826 vim_free(*bufp);
19827 *fnamelen = (int)STRLEN(p);
19828 *bufp = pbuf;
19829 *fnamep = p;
19830
19831 return 0;
19832}
19833#endif /* WIN3264 */
19834
19835/*
19836 * Adjust a filename, according to a string of modifiers.
19837 * *fnamep must be NUL terminated when called. When returning, the length is
19838 * determined by *fnamelen.
19839 * Returns valid flags.
19840 * When there is an error, *fnamep is set to NULL.
19841 */
19842 int
19843modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19844 char_u *src; /* string with modifiers */
19845 int *usedlen; /* characters after src that are used */
19846 char_u **fnamep; /* file name so far */
19847 char_u **bufp; /* buffer for allocated file name or NULL */
19848 int *fnamelen; /* length of fnamep */
19849{
19850 int valid = 0;
19851 char_u *tail;
19852 char_u *s, *p, *pbuf;
19853 char_u dirname[MAXPATHL];
19854 int c;
19855 int has_fullname = 0;
19856#ifdef WIN3264
19857 int has_shortname = 0;
19858#endif
19859
19860repeat:
19861 /* ":p" - full path/file_name */
19862 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19863 {
19864 has_fullname = 1;
19865
19866 valid |= VALID_PATH;
19867 *usedlen += 2;
19868
19869 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19870 if ((*fnamep)[0] == '~'
19871#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19872 && ((*fnamep)[1] == '/'
19873# ifdef BACKSLASH_IN_FILENAME
19874 || (*fnamep)[1] == '\\'
19875# endif
19876 || (*fnamep)[1] == NUL)
19877
19878#endif
19879 )
19880 {
19881 *fnamep = expand_env_save(*fnamep);
19882 vim_free(*bufp); /* free any allocated file name */
19883 *bufp = *fnamep;
19884 if (*fnamep == NULL)
19885 return -1;
19886 }
19887
19888 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019889 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 {
19891 if (vim_ispathsep(*p)
19892 && p[1] == '.'
19893 && (p[2] == NUL
19894 || vim_ispathsep(p[2])
19895 || (p[2] == '.'
19896 && (p[3] == NUL || vim_ispathsep(p[3])))))
19897 break;
19898 }
19899
19900 /* FullName_save() is slow, don't use it when not needed. */
19901 if (*p != NUL || !vim_isAbsName(*fnamep))
19902 {
19903 *fnamep = FullName_save(*fnamep, *p != NUL);
19904 vim_free(*bufp); /* free any allocated file name */
19905 *bufp = *fnamep;
19906 if (*fnamep == NULL)
19907 return -1;
19908 }
19909
19910 /* Append a path separator to a directory. */
19911 if (mch_isdir(*fnamep))
19912 {
19913 /* Make room for one or two extra characters. */
19914 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19915 vim_free(*bufp); /* free any allocated file name */
19916 *bufp = *fnamep;
19917 if (*fnamep == NULL)
19918 return -1;
19919 add_pathsep(*fnamep);
19920 }
19921 }
19922
19923 /* ":." - path relative to the current directory */
19924 /* ":~" - path relative to the home directory */
19925 /* ":8" - shortname path - postponed till after */
19926 while (src[*usedlen] == ':'
19927 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19928 {
19929 *usedlen += 2;
19930 if (c == '8')
19931 {
19932#ifdef WIN3264
19933 has_shortname = 1; /* Postpone this. */
19934#endif
19935 continue;
19936 }
19937 pbuf = NULL;
19938 /* Need full path first (use expand_env() to remove a "~/") */
19939 if (!has_fullname)
19940 {
19941 if (c == '.' && **fnamep == '~')
19942 p = pbuf = expand_env_save(*fnamep);
19943 else
19944 p = pbuf = FullName_save(*fnamep, FALSE);
19945 }
19946 else
19947 p = *fnamep;
19948
19949 has_fullname = 0;
19950
19951 if (p != NULL)
19952 {
19953 if (c == '.')
19954 {
19955 mch_dirname(dirname, MAXPATHL);
19956 s = shorten_fname(p, dirname);
19957 if (s != NULL)
19958 {
19959 *fnamep = s;
19960 if (pbuf != NULL)
19961 {
19962 vim_free(*bufp); /* free any allocated file name */
19963 *bufp = pbuf;
19964 pbuf = NULL;
19965 }
19966 }
19967 }
19968 else
19969 {
19970 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19971 /* Only replace it when it starts with '~' */
19972 if (*dirname == '~')
19973 {
19974 s = vim_strsave(dirname);
19975 if (s != NULL)
19976 {
19977 *fnamep = s;
19978 vim_free(*bufp);
19979 *bufp = s;
19980 }
19981 }
19982 }
19983 vim_free(pbuf);
19984 }
19985 }
19986
19987 tail = gettail(*fnamep);
19988 *fnamelen = (int)STRLEN(*fnamep);
19989
19990 /* ":h" - head, remove "/file_name", can be repeated */
19991 /* Don't remove the first "/" or "c:\" */
19992 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19993 {
19994 valid |= VALID_HEAD;
19995 *usedlen += 2;
19996 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019997 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 --tail;
19999 *fnamelen = (int)(tail - *fnamep);
20000#ifdef VMS
20001 if (*fnamelen > 0)
20002 *fnamelen += 1; /* the path separator is part of the path */
20003#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020004 while (tail > s && !after_pathsep(s, tail))
20005 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 }
20007
20008 /* ":8" - shortname */
20009 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20010 {
20011 *usedlen += 2;
20012#ifdef WIN3264
20013 has_shortname = 1;
20014#endif
20015 }
20016
20017#ifdef WIN3264
20018 /* Check shortname after we have done 'heads' and before we do 'tails'
20019 */
20020 if (has_shortname)
20021 {
20022 pbuf = NULL;
20023 /* Copy the string if it is shortened by :h */
20024 if (*fnamelen < (int)STRLEN(*fnamep))
20025 {
20026 p = vim_strnsave(*fnamep, *fnamelen);
20027 if (p == 0)
20028 return -1;
20029 vim_free(*bufp);
20030 *bufp = *fnamep = p;
20031 }
20032
20033 /* Split into two implementations - makes it easier. First is where
20034 * there isn't a full name already, second is where there is.
20035 */
20036 if (!has_fullname && !vim_isAbsName(*fnamep))
20037 {
20038 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20039 return -1;
20040 }
20041 else
20042 {
20043 int l;
20044
20045 /* Simple case, already have the full-name
20046 * Nearly always shorter, so try first time. */
20047 l = *fnamelen;
20048 if (!get_short_pathname(fnamep, bufp, &l))
20049 return -1;
20050
20051 if (l == 0)
20052 {
20053 /* Couldn't find the filename.. search the paths.
20054 */
20055 l = *fnamelen;
20056 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20057 return -1;
20058 }
20059 *fnamelen = l;
20060 }
20061 }
20062#endif /* WIN3264 */
20063
20064 /* ":t" - tail, just the basename */
20065 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20066 {
20067 *usedlen += 2;
20068 *fnamelen -= (int)(tail - *fnamep);
20069 *fnamep = tail;
20070 }
20071
20072 /* ":e" - extension, can be repeated */
20073 /* ":r" - root, without extension, can be repeated */
20074 while (src[*usedlen] == ':'
20075 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20076 {
20077 /* find a '.' in the tail:
20078 * - for second :e: before the current fname
20079 * - otherwise: The last '.'
20080 */
20081 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20082 s = *fnamep - 2;
20083 else
20084 s = *fnamep + *fnamelen - 1;
20085 for ( ; s > tail; --s)
20086 if (s[0] == '.')
20087 break;
20088 if (src[*usedlen + 1] == 'e') /* :e */
20089 {
20090 if (s > tail)
20091 {
20092 *fnamelen += (int)(*fnamep - (s + 1));
20093 *fnamep = s + 1;
20094#ifdef VMS
20095 /* cut version from the extension */
20096 s = *fnamep + *fnamelen - 1;
20097 for ( ; s > *fnamep; --s)
20098 if (s[0] == ';')
20099 break;
20100 if (s > *fnamep)
20101 *fnamelen = s - *fnamep;
20102#endif
20103 }
20104 else if (*fnamep <= tail)
20105 *fnamelen = 0;
20106 }
20107 else /* :r */
20108 {
20109 if (s > tail) /* remove one extension */
20110 *fnamelen = (int)(s - *fnamep);
20111 }
20112 *usedlen += 2;
20113 }
20114
20115 /* ":s?pat?foo?" - substitute */
20116 /* ":gs?pat?foo?" - global substitute */
20117 if (src[*usedlen] == ':'
20118 && (src[*usedlen + 1] == 's'
20119 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20120 {
20121 char_u *str;
20122 char_u *pat;
20123 char_u *sub;
20124 int sep;
20125 char_u *flags;
20126 int didit = FALSE;
20127
20128 flags = (char_u *)"";
20129 s = src + *usedlen + 2;
20130 if (src[*usedlen + 1] == 'g')
20131 {
20132 flags = (char_u *)"g";
20133 ++s;
20134 }
20135
20136 sep = *s++;
20137 if (sep)
20138 {
20139 /* find end of pattern */
20140 p = vim_strchr(s, sep);
20141 if (p != NULL)
20142 {
20143 pat = vim_strnsave(s, (int)(p - s));
20144 if (pat != NULL)
20145 {
20146 s = p + 1;
20147 /* find end of substitution */
20148 p = vim_strchr(s, sep);
20149 if (p != NULL)
20150 {
20151 sub = vim_strnsave(s, (int)(p - s));
20152 str = vim_strnsave(*fnamep, *fnamelen);
20153 if (sub != NULL && str != NULL)
20154 {
20155 *usedlen = (int)(p + 1 - src);
20156 s = do_string_sub(str, pat, sub, flags);
20157 if (s != NULL)
20158 {
20159 *fnamep = s;
20160 *fnamelen = (int)STRLEN(s);
20161 vim_free(*bufp);
20162 *bufp = s;
20163 didit = TRUE;
20164 }
20165 }
20166 vim_free(sub);
20167 vim_free(str);
20168 }
20169 vim_free(pat);
20170 }
20171 }
20172 /* after using ":s", repeat all the modifiers */
20173 if (didit)
20174 goto repeat;
20175 }
20176 }
20177
20178 return valid;
20179}
20180
20181/*
20182 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20183 * "flags" can be "g" to do a global substitute.
20184 * Returns an allocated string, NULL for error.
20185 */
20186 char_u *
20187do_string_sub(str, pat, sub, flags)
20188 char_u *str;
20189 char_u *pat;
20190 char_u *sub;
20191 char_u *flags;
20192{
20193 int sublen;
20194 regmatch_T regmatch;
20195 int i;
20196 int do_all;
20197 char_u *tail;
20198 garray_T ga;
20199 char_u *ret;
20200 char_u *save_cpo;
20201
20202 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20203 save_cpo = p_cpo;
20204 p_cpo = (char_u *)"";
20205
20206 ga_init2(&ga, 1, 200);
20207
20208 do_all = (flags[0] == 'g');
20209
20210 regmatch.rm_ic = p_ic;
20211 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20212 if (regmatch.regprog != NULL)
20213 {
20214 tail = str;
20215 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20216 {
20217 /*
20218 * Get some space for a temporary buffer to do the substitution
20219 * into. It will contain:
20220 * - The text up to where the match is.
20221 * - The substituted text.
20222 * - The text after the match.
20223 */
20224 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20225 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20226 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20227 {
20228 ga_clear(&ga);
20229 break;
20230 }
20231
20232 /* copy the text up to where the match is */
20233 i = (int)(regmatch.startp[0] - tail);
20234 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20235 /* add the substituted text */
20236 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20237 + ga.ga_len + i, TRUE, TRUE, FALSE);
20238 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 /* avoid getting stuck on a match with an empty string */
20240 if (tail == regmatch.endp[0])
20241 {
20242 if (*tail == NUL)
20243 break;
20244 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20245 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 }
20247 else
20248 {
20249 tail = regmatch.endp[0];
20250 if (*tail == NUL)
20251 break;
20252 }
20253 if (!do_all)
20254 break;
20255 }
20256
20257 if (ga.ga_data != NULL)
20258 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20259
20260 vim_free(regmatch.regprog);
20261 }
20262
20263 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20264 ga_clear(&ga);
20265 p_cpo = save_cpo;
20266
20267 return ret;
20268}
20269
20270#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */