blob: 53ebc78451fea5e594ac33510ab54971d244508d [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)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#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 */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tm_children; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count; /* nr of times line was executed */
172 proftime_T *uf_tml_total; /* time spend in a line + children */
173 proftime_T *uf_tml_self; /* time spend in a line itself */
174 proftime_T uf_tml_start; /* start time for current line */
175 proftime_T uf_tml_children; /* time spent in children for this line */
176 proftime_T uf_tml_wait; /* start wait time for current line */
177 int uf_tml_idx; /* index of line being timed; -1 if none */
178 int uf_tml_execed; /* line being timed was executed */
179#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000180 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000182 int uf_refcount; /* for numbered function: reference count */
183 char_u uf_name[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
185 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186};
187
188/* function flags */
189#define FC_ABORT 1 /* abort function on error */
190#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000191#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000194 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000196static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000198/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000199static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
200
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 Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000345 {VV_NAME("mouse_win", VAR_NUMBER), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000348};
349
350/* shorthand */
351#define vv_type vv_di.di_tv.v_type
352#define vv_nr vv_di.di_tv.vval.v_number
353#define vv_str vv_di.di_tv.vval.v_string
354#define vv_tv vv_di.di_tv
355
356/*
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
359 */
360static dict_T vimvardict;
361static dictitem_T vimvars_var;
362#define vimvarht vimvardict.dv_hashtab
363
Bram Moolenaara40058a2005-07-11 22:42:07 +0000364static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
365static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
366#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
368#endif
369static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
370static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
371static char_u *skip_var_one __ARGS((char_u *arg));
372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
373static void list_glob_vars __ARGS((void));
374static void list_buf_vars __ARGS((void));
375static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
377static void list_tab_vars __ARGS((void));
378#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000380static void list_script_vars __ARGS((void));
381static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000438static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_alloc __ARGS((char_u *key));
440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
442static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int dict_add __ARGS((dict_T *d, dictitem_T *item));
445static long dict_len __ARGS((dict_T *d));
446static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
450static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static char_u *string_quote __ARGS((char_u *str, int function));
452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
454static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
455static 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));
456static 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 Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000480static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
489static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000502static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000516static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000518static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000524static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000532static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000533static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000536static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000557static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000563static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000579static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585#ifdef vim_mkdir
586static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
587#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000591static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000593static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000594static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000596static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000597static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000610static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000612static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000619static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000620static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000621static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000623static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000625static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000628static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000629static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000632static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633#ifdef HAVE_STRFTIME
634static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
636static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000648static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000649static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000650static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000651static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000652static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000654static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000668static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000671static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000673static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
674static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static int get_env_len __ARGS((char_u **arg));
676static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000678static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
679#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
680#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
681 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000682static 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 +0000683static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000684static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000685static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
686static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static typval_T *alloc_tv __ARGS((void));
688static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void init_tv __ARGS((typval_T *varp));
690static long get_tv_number __ARGS((typval_T *varp));
691static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000692static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static char_u *get_tv_string __ARGS((typval_T *varp));
694static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000695static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000697static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
699static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
700static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
701static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
702static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
703static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
704static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000705static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000706static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000708static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
710static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
711static int eval_fname_script __ARGS((char_u *p));
712static int eval_fname_sid __ARGS((char_u *p));
713static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static ufunc_T *find_func __ARGS((char_u *name));
715static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000716static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000717#ifdef FEAT_PROFILE
718static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000719static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
720static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
721static int
722# ifdef __BORLANDC__
723 _RTLENTRYF
724# endif
725 prof_total_cmp __ARGS((const void *s1, const void *s2));
726static int
727# ifdef __BORLANDC__
728 _RTLENTRYF
729# endif
730 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000731#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000732static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000733static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000734static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void func_free __ARGS((ufunc_T *fp));
736static void func_unref __ARGS((char_u *name));
737static void func_ref __ARGS((char_u *name));
738static 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));
739static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000740static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
741static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000742static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000743static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000744static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000746/* Character used as separated in autoload function/variable names. */
747#define AUTOLOAD_CHAR '#'
748
Bram Moolenaar33570922005-01-25 22:26:29 +0000749/*
750 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000751 */
752 void
753eval_init()
754{
Bram Moolenaar33570922005-01-25 22:26:29 +0000755 int i;
756 struct vimvar *p;
757
758 init_var_dict(&globvardict, &globvars_var);
759 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000760 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000761 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
763 for (i = 0; i < VV_LEN; ++i)
764 {
765 p = &vimvars[i];
766 STRCPY(p->vv_di.di_key, p->vv_name);
767 if (p->vv_flags & VV_RO)
768 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
769 else if (p->vv_flags & VV_RO_SBX)
770 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
771 else
772 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000773
774 /* add to v: scope dict, unless the value is not always available */
775 if (p->vv_type != VAR_UNKNOWN)
776 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000777 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000778 /* add to compat scope dict */
779 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000781}
782
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000783#if defined(EXITFREE) || defined(PROTO)
784 void
785eval_clear()
786{
787 int i;
788 struct vimvar *p;
789
790 for (i = 0; i < VV_LEN; ++i)
791 {
792 p = &vimvars[i];
793 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000795 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000796 p->vv_di.di_tv.vval.v_string = NULL;
797 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000798 }
799 hash_clear(&vimvarht);
800 hash_clear(&compat_hashtab);
801
802 /* script-local variables */
803 for (i = 1; i <= ga_scripts.ga_len; ++i)
804 vars_clear(&SCRIPT_VARS(i));
805 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000806 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000807
808 /* global variables */
809 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000810
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000811 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000812 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000813 hash_clear(&func_hashtab);
814
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000815 /* autoloaded script names */
816 ga_clear_strings(&ga_loaded);
817
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000818 /* unreferenced lists and dicts */
819 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000820}
821#endif
822
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 * Return the name of the executed function.
825 */
826 char_u *
827func_name(cookie)
828 void *cookie;
829{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000830 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832
833/*
834 * Return the address holding the next breakpoint line for a funccall cookie.
835 */
836 linenr_T *
837func_breakpoint(cookie)
838 void *cookie;
839{
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
843/*
844 * Return the address holding the debug tick for a funccall cookie.
845 */
846 int *
847func_dbg_tick(cookie)
848 void *cookie;
849{
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851}
852
853/*
854 * Return the nesting level for a funccall cookie.
855 */
856 int
857func_level(cookie)
858 void *cookie;
859{
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
863/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000864funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866/*
867 * Return TRUE when a function was ended by a ":return" command.
868 */
869 int
870current_func_returned()
871{
872 return current_funccal->returned;
873}
874
875
876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * Set an internal variable to a string value. Creates the variable if it does
878 * not already exist.
879 */
880 void
881set_internal_string_var(name, value)
882 char_u *name;
883 char_u *value;
884{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000885 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000886 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 val = vim_strsave(value);
889 if (val != NULL)
890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000892 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000894 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897 }
898}
899
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900static lval_T *redir_lval = NULL;
901static char_u *redir_endp = NULL;
902static char_u *redir_varname = NULL;
903
904/*
905 * Start recording command output to a variable
906 * Returns OK if successfully completed the setup. FAIL otherwise.
907 */
908 int
909var_redir_start(name, append)
910 char_u *name;
911 int append; /* append to an existing variable */
912{
913 int save_emsg;
914 int err;
915 typval_T tv;
916
917 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000918 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 {
920 EMSG(_(e_invarg));
921 return FAIL;
922 }
923
924 redir_varname = vim_strsave(name);
925 if (redir_varname == NULL)
926 return FAIL;
927
928 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
929 if (redir_lval == NULL)
930 {
931 var_redir_stop();
932 return FAIL;
933 }
934
935 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000936 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
937 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000938 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
939 {
940 if (redir_endp != NULL && *redir_endp != NUL)
941 /* Trailing characters are present after the variable name */
942 EMSG(_(e_trailing));
943 else
944 EMSG(_(e_invarg));
945 var_redir_stop();
946 return FAIL;
947 }
948
949 /* check if we can write to the variable: set it to or append an empty
950 * string */
951 save_emsg = did_emsg;
952 did_emsg = FALSE;
953 tv.v_type = VAR_STRING;
954 tv.vval.v_string = (char_u *)"";
955 if (append)
956 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
957 else
958 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
959 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000960 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000961 if (err)
962 {
963 var_redir_stop();
964 return FAIL;
965 }
966 if (redir_lval->ll_newkey != NULL)
967 {
968 /* Dictionary item was created, don't do it again. */
969 vim_free(redir_lval->ll_newkey);
970 redir_lval->ll_newkey = NULL;
971 }
972
973 return OK;
974}
975
976/*
977 * Append "value[len]" to the variable set by var_redir_start().
978 */
979 void
980var_redir_str(value, len)
981 char_u *value;
982 int len;
983{
984 char_u *val;
985 typval_T tv;
986 int save_emsg;
987 int err;
988
989 if (redir_lval == NULL)
990 return;
991
992 if (len == -1)
993 /* Append the entire string */
994 val = vim_strsave(value);
995 else
996 /* Append only the specified number of characters */
997 val = vim_strnsave(value, len);
998 if (val == NULL)
999 return;
1000
1001 tv.v_type = VAR_STRING;
1002 tv.vval.v_string = val;
1003
1004 save_emsg = did_emsg;
1005 did_emsg = FALSE;
1006 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1007 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001008 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009 if (err)
1010 var_redir_stop();
1011
1012 vim_free(tv.vval.v_string);
1013}
1014
1015/*
1016 * Stop redirecting command output to a variable.
1017 */
1018 void
1019var_redir_stop()
1020{
1021 if (redir_lval != NULL)
1022 {
1023 clear_lval(redir_lval);
1024 vim_free(redir_lval);
1025 redir_lval = NULL;
1026 }
1027 vim_free(redir_varname);
1028 redir_varname = NULL;
1029}
1030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031# if defined(FEAT_MBYTE) || defined(PROTO)
1032 int
1033eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1034 char_u *enc_from;
1035 char_u *enc_to;
1036 char_u *fname_from;
1037 char_u *fname_to;
1038{
1039 int err = FALSE;
1040
1041 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1042 set_vim_var_string(VV_CC_TO, enc_to, -1);
1043 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1044 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1045 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1046 err = TRUE;
1047 set_vim_var_string(VV_CC_FROM, NULL, -1);
1048 set_vim_var_string(VV_CC_TO, NULL, -1);
1049 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1050 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1051
1052 if (err)
1053 return FAIL;
1054 return OK;
1055}
1056# endif
1057
1058# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1059 int
1060eval_printexpr(fname, args)
1061 char_u *fname;
1062 char_u *args;
1063{
1064 int err = FALSE;
1065
1066 set_vim_var_string(VV_FNAME_IN, fname, -1);
1067 set_vim_var_string(VV_CMDARG, args, -1);
1068 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1069 err = TRUE;
1070 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1071 set_vim_var_string(VV_CMDARG, NULL, -1);
1072
1073 if (err)
1074 {
1075 mch_remove(fname);
1076 return FAIL;
1077 }
1078 return OK;
1079}
1080# endif
1081
1082# if defined(FEAT_DIFF) || defined(PROTO)
1083 void
1084eval_diff(origfile, newfile, outfile)
1085 char_u *origfile;
1086 char_u *newfile;
1087 char_u *outfile;
1088{
1089 int err = FALSE;
1090
1091 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1092 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1093 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1094 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1095 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1096 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1097 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1098}
1099
1100 void
1101eval_patch(origfile, difffile, outfile)
1102 char_u *origfile;
1103 char_u *difffile;
1104 char_u *outfile;
1105{
1106 int err;
1107
1108 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1109 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1110 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1111 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1112 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1113 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1114 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1115}
1116# endif
1117
1118/*
1119 * Top level evaluation function, returning a boolean.
1120 * Sets "error" to TRUE if there was an error.
1121 * Return TRUE or FALSE.
1122 */
1123 int
1124eval_to_bool(arg, error, nextcmd, skip)
1125 char_u *arg;
1126 int *error;
1127 char_u **nextcmd;
1128 int skip; /* only parse, don't execute */
1129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int retval = FALSE;
1132
1133 if (skip)
1134 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 else
1138 {
1139 *error = FALSE;
1140 if (!skip)
1141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001142 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 }
1146 if (skip)
1147 --emsg_skip;
1148
1149 return retval;
1150}
1151
1152/*
1153 * Top level evaluation function, returning a string. If "skip" is TRUE,
1154 * only parsing to "nextcmd" is done, without reporting errors. Return
1155 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1156 */
1157 char_u *
1158eval_to_string_skip(arg, nextcmd, skip)
1159 char_u *arg;
1160 char_u **nextcmd;
1161 int skip; /* only parse, don't execute */
1162{
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 char_u *retval;
1165
1166 if (skip)
1167 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 retval = NULL;
1170 else
1171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 retval = vim_strsave(get_tv_string(&tv));
1173 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 if (skip)
1176 --emsg_skip;
1177
1178 return retval;
1179}
1180
1181/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001182 * Skip over an expression at "*pp".
1183 * Return FAIL for an error, OK otherwise.
1184 */
1185 int
1186skip_expr(pp)
1187 char_u **pp;
1188{
Bram Moolenaar33570922005-01-25 22:26:29 +00001189 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001190
1191 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001192 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001193}
1194
1195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 * Top level evaluation function, returning a string.
1197 * Return pointer to allocated memory, or NULL for failure.
1198 */
1199 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001200eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 char_u *arg;
1202 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001203 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
Bram Moolenaar33570922005-01-25 22:26:29 +00001205 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001207 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 retval = NULL;
1211 else
1212 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001213 if (dolist && tv.v_type == VAR_LIST)
1214 {
1215 ga_init2(&ga, (int)sizeof(char), 80);
1216 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1217 ga_append(&ga, NUL);
1218 retval = (char_u *)ga.ga_data;
1219 }
1220 else
1221 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224
1225 return retval;
1226}
1227
1228/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001229 * Call eval_to_string() without using current local variables and using
1230 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 */
1232 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001233eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 char_u *arg;
1235 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001236 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 char_u *retval;
1239 void *save_funccalp;
1240
1241 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001242 if (use_sandbox)
1243 ++sandbox;
1244 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001245 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001246 if (use_sandbox)
1247 --sandbox;
1248 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 restore_funccal(save_funccalp);
1250 return retval;
1251}
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253/*
1254 * Top level evaluation function, returning a number.
1255 * Evaluates "expr" silently.
1256 * Returns -1 for an error.
1257 */
1258 int
1259eval_to_number(expr)
1260 char_u *expr;
1261{
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001264 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 ++emsg_off;
1267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 retval = -1;
1270 else
1271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001272 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 --emsg_off;
1276
1277 return retval;
1278}
1279
Bram Moolenaara40058a2005-07-11 22:42:07 +00001280/*
1281 * Prepare v: variable "idx" to be used.
1282 * Save the current typeval in "save_tv".
1283 * When not used yet add the variable to the v: hashtable.
1284 */
1285 static void
1286prepare_vimvar(idx, save_tv)
1287 int idx;
1288 typval_T *save_tv;
1289{
1290 *save_tv = vimvars[idx].vv_tv;
1291 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1292 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1293}
1294
1295/*
1296 * Restore v: variable "idx" to typeval "save_tv".
1297 * When no longer defined, remove the variable from the v: hashtable.
1298 */
1299 static void
1300restore_vimvar(idx, save_tv)
1301 int idx;
1302 typval_T *save_tv;
1303{
1304 hashitem_T *hi;
1305
1306 clear_tv(&vimvars[idx].vv_tv);
1307 vimvars[idx].vv_tv = *save_tv;
1308 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1309 {
1310 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1311 if (HASHITEM_EMPTY(hi))
1312 EMSG2(_(e_intern2), "restore_vimvar()");
1313 else
1314 hash_remove(&vimvarht, hi);
1315 }
1316}
1317
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001318#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001319/*
1320 * Evaluate an expression to a list with suggestions.
1321 * For the "expr:" part of 'spellsuggest'.
1322 */
1323 list_T *
1324eval_spell_expr(badword, expr)
1325 char_u *badword;
1326 char_u *expr;
1327{
1328 typval_T save_val;
1329 typval_T rettv;
1330 list_T *list = NULL;
1331 char_u *p = skipwhite(expr);
1332
1333 /* Set "v:val" to the bad word. */
1334 prepare_vimvar(VV_VAL, &save_val);
1335 vimvars[VV_VAL].vv_type = VAR_STRING;
1336 vimvars[VV_VAL].vv_str = badword;
1337 if (p_verbose == 0)
1338 ++emsg_off;
1339
1340 if (eval1(&p, &rettv, TRUE) == OK)
1341 {
1342 if (rettv.v_type != VAR_LIST)
1343 clear_tv(&rettv);
1344 else
1345 list = rettv.vval.v_list;
1346 }
1347
1348 if (p_verbose == 0)
1349 --emsg_off;
1350 vimvars[VV_VAL].vv_str = NULL;
1351 restore_vimvar(VV_VAL, &save_val);
1352
1353 return list;
1354}
1355
1356/*
1357 * "list" is supposed to contain two items: a word and a number. Return the
1358 * word in "pp" and the number as the return value.
1359 * Return -1 if anything isn't right.
1360 * Used to get the good word and score from the eval_spell_expr() result.
1361 */
1362 int
1363get_spellword(list, pp)
1364 list_T *list;
1365 char_u **pp;
1366{
1367 listitem_T *li;
1368
1369 li = list->lv_first;
1370 if (li == NULL)
1371 return -1;
1372 *pp = get_tv_string(&li->li_tv);
1373
1374 li = li->li_next;
1375 if (li == NULL)
1376 return -1;
1377 return get_tv_number(&li->li_tv);
1378}
1379#endif
1380
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001381/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001382 * Top level evaluation function.
1383 * Returns an allocated typval_T with the result.
1384 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001385 */
1386 typval_T *
1387eval_expr(arg, nextcmd)
1388 char_u *arg;
1389 char_u **nextcmd;
1390{
1391 typval_T *tv;
1392
1393 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001394 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395 {
1396 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001397 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001398 }
1399
1400 return tv;
1401}
1402
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1405/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001410 static int
1411call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 char_u *func;
1413 int argc;
1414 char_u **argv;
1415 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 long n;
1420 int len;
1421 int i;
1422 int doesrange;
1423 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001426 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 for (i = 0; i < argc; i++)
1431 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001432 /* Pass a NULL or empty argument as an empty string */
1433 if (argv[i] == NULL || *argv[i] == NUL)
1434 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001435 argvars[i].v_type = VAR_STRING;
1436 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001437 continue;
1438 }
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /* Recognize a number argument, the others must be strings. */
1441 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1442 if (len != 0 && len == (int)STRLEN(argv[i]))
1443 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001444 argvars[i].v_type = VAR_NUMBER;
1445 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447 else
1448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001449 argvars[i].v_type = VAR_STRING;
1450 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 }
1453
1454 if (safe)
1455 {
1456 save_funccalp = save_funccal();
1457 ++sandbox;
1458 }
1459
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001460 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1461 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001463 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (safe)
1465 {
1466 --sandbox;
1467 restore_funccal(save_funccalp);
1468 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 vim_free(argvars);
1470
1471 if (ret == FAIL)
1472 clear_tv(rettv);
1473
1474 return ret;
1475}
1476
1477/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001478 * Call vimL function "func" and return the result as a string.
1479 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480 * Uses argv[argc] for the function arguments.
1481 */
1482 void *
1483call_func_retstr(func, argc, argv, safe)
1484 char_u *func;
1485 int argc;
1486 char_u **argv;
1487 int safe; /* use the sandbox */
1488{
1489 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001490 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001491
1492 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1493 return NULL;
1494
1495 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return retval;
1498}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001499
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001500#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001501/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001502 * Call vimL function "func" and return the result as a number.
1503 * Returns -1 when calling the function fails.
1504 * Uses argv[argc] for the function arguments.
1505 */
1506 long
1507call_func_retnr(func, argc, argv, safe)
1508 char_u *func;
1509 int argc;
1510 char_u **argv;
1511 int safe; /* use the sandbox */
1512{
1513 typval_T rettv;
1514 long retval;
1515
1516 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1517 return -1;
1518
1519 retval = get_tv_number_chk(&rettv, NULL);
1520 clear_tv(&rettv);
1521 return retval;
1522}
1523#endif
1524
1525/*
1526 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001527 * Uses argv[argc] for the function arguments.
1528 */
1529 void *
1530call_func_retlist(func, argc, argv, safe)
1531 char_u *func;
1532 int argc;
1533 char_u **argv;
1534 int safe; /* use the sandbox */
1535{
1536 typval_T rettv;
1537
1538 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1539 return NULL;
1540
1541 if (rettv.v_type != VAR_LIST)
1542 {
1543 clear_tv(&rettv);
1544 return NULL;
1545 }
1546
1547 return rettv.vval.v_list;
1548}
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#endif
1551
1552/*
1553 * Save the current function call pointer, and set it to NULL.
1554 * Used when executing autocommands and for ":source".
1555 */
1556 void *
1557save_funccal()
1558{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001559 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 current_funccal = NULL;
1562 return (void *)fc;
1563}
1564
1565 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001566restore_funccal(vfc)
1567 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001569 funccall_T *fc = (funccall_T *)vfc;
1570
1571 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572}
1573
Bram Moolenaar05159a02005-02-26 23:04:13 +00001574#if defined(FEAT_PROFILE) || defined(PROTO)
1575/*
1576 * Prepare profiling for entering a child or something else that is not
1577 * counted for the script/function itself.
1578 * Should always be called in pair with prof_child_exit().
1579 */
1580 void
1581prof_child_enter(tm)
1582 proftime_T *tm; /* place to store waittime */
1583{
1584 funccall_T *fc = current_funccal;
1585
1586 if (fc != NULL && fc->func->uf_profiling)
1587 profile_start(&fc->prof_child);
1588 script_prof_save(tm);
1589}
1590
1591/*
1592 * Take care of time spent in a child.
1593 * Should always be called after prof_child_enter().
1594 */
1595 void
1596prof_child_exit(tm)
1597 proftime_T *tm; /* where waittime was stored */
1598{
1599 funccall_T *fc = current_funccal;
1600
1601 if (fc != NULL && fc->func->uf_profiling)
1602 {
1603 profile_end(&fc->prof_child);
1604 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1605 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1606 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1607 }
1608 script_prof_restore(tm);
1609}
1610#endif
1611
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#ifdef FEAT_FOLDING
1614/*
1615 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1616 * it in "*cp". Doesn't give error messages.
1617 */
1618 int
1619eval_foldexpr(arg, cp)
1620 char_u *arg;
1621 int *cp;
1622{
Bram Moolenaar33570922005-01-25 22:26:29 +00001623 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 int retval;
1625 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001626 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1627 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001630 if (use_sandbox)
1631 ++sandbox;
1632 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 retval = 0;
1636 else
1637 {
1638 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 if (tv.v_type == VAR_NUMBER)
1640 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001641 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 retval = 0;
1643 else
1644 {
1645 /* If the result is a string, check if there is a non-digit before
1646 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (!VIM_ISDIGIT(*s) && *s != '-')
1649 *cp = *s++;
1650 retval = atol((char *)s);
1651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001655 if (use_sandbox)
1656 --sandbox;
1657 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 return retval;
1660}
1661#endif
1662
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 * ":let" list all variable values
1665 * ":let var1 var2" list variable values
1666 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 * ":let var += expr" assignment command.
1668 * ":let var -= expr" assignment command.
1669 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 */
1672 void
1673ex_let(eap)
1674 exarg_T *eap;
1675{
1676 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001678 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 int var_count = 0;
1681 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001682 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001683 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaardb552d602006-03-23 22:59:57 +00001685 argend = skip_var_list(arg, &var_count, &semicolon);
1686 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001688 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1689 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001690 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001693 /*
1694 * ":let" without "=": list variables
1695 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 if (*arg == '[')
1697 EMSG(_(e_invarg));
1698 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 /* ":let var1 var2" */
1700 arg = list_arg_vars(eap, arg);
1701 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001703 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001704 list_glob_vars();
1705 list_buf_vars();
1706 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001707#ifdef FEAT_WINDOWS
1708 list_tab_vars();
1709#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001710 list_script_vars();
1711 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001712 list_vim_vars();
1713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 eap->nextcmd = check_nextcmd(arg);
1715 }
1716 else
1717 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001718 op[0] = '=';
1719 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001720 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 {
1722 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1723 op[0] = expr[-1]; /* +=, -= or .= */
1724 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (eap->skip)
1728 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (eap->skip)
1731 {
1732 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 --emsg_skip;
1735 }
1736 else if (i != FAIL)
1737 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
1742 }
1743}
1744
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745/*
1746 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1747 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 * When "nextchars" is not NULL it points to a string with characters that
1749 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1750 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 * Returns OK or FAIL;
1752 */
1753 static int
1754ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1755 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001756 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 int copy; /* copy values from "tv", don't move */
1758 int semicolon; /* from skip_var_list() */
1759 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761{
1762 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001765 listitem_T *item;
1766 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767
1768 if (*arg != '[')
1769 {
1770 /*
1771 * ":let var = expr" or ":for var in list"
1772 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 return FAIL;
1775 return OK;
1776 }
1777
1778 /*
1779 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1780 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001781 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 {
1783 EMSG(_(e_listreq));
1784 return FAIL;
1785 }
1786
1787 i = list_len(l);
1788 if (semicolon == 0 && var_count < i)
1789 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001790 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 return FAIL;
1792 }
1793 if (var_count - semicolon > i)
1794 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001795 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 return FAIL;
1797 }
1798
1799 item = l->lv_first;
1800 while (*arg != ']')
1801 {
1802 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 item = item->li_next;
1805 if (arg == NULL)
1806 return FAIL;
1807
1808 arg = skipwhite(arg);
1809 if (*arg == ';')
1810 {
1811 /* Put the rest of the list (may be empty) in the var after ';'.
1812 * Create a new list for this. */
1813 l = list_alloc();
1814 if (l == NULL)
1815 return FAIL;
1816 while (item != NULL)
1817 {
1818 list_append_tv(l, &item->li_tv);
1819 item = item->li_next;
1820 }
1821
1822 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001823 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 ltv.vval.v_list = l;
1825 l->lv_refcount = 1;
1826
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1828 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 clear_tv(&ltv);
1830 if (arg == NULL)
1831 return FAIL;
1832 break;
1833 }
1834 else if (*arg != ',' && *arg != ']')
1835 {
1836 EMSG2(_(e_intern2), "ex_let_vars()");
1837 return FAIL;
1838 }
1839 }
1840
1841 return OK;
1842}
1843
1844/*
1845 * Skip over assignable variable "var" or list of variables "[var, var]".
1846 * Used for ":let varvar = expr" and ":for varvar in expr".
1847 * For "[var, var]" increment "*var_count" for each variable.
1848 * for "[var, var; var]" set "semicolon".
1849 * Return NULL for an error.
1850 */
1851 static char_u *
1852skip_var_list(arg, var_count, semicolon)
1853 char_u *arg;
1854 int *var_count;
1855 int *semicolon;
1856{
1857 char_u *p, *s;
1858
1859 if (*arg == '[')
1860 {
1861 /* "[var, var]": find the matching ']'. */
1862 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001863 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 {
1865 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1866 s = skip_var_one(p);
1867 if (s == p)
1868 {
1869 EMSG2(_(e_invarg2), p);
1870 return NULL;
1871 }
1872 ++*var_count;
1873
1874 p = skipwhite(s);
1875 if (*p == ']')
1876 break;
1877 else if (*p == ';')
1878 {
1879 if (*semicolon == 1)
1880 {
1881 EMSG(_("Double ; in list of variables"));
1882 return NULL;
1883 }
1884 *semicolon = 1;
1885 }
1886 else if (*p != ',')
1887 {
1888 EMSG2(_(e_invarg2), p);
1889 return NULL;
1890 }
1891 }
1892 return p + 1;
1893 }
1894 else
1895 return skip_var_one(arg);
1896}
1897
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001898/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001899 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1900 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001901 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 static char_u *
1903skip_var_one(arg)
1904 char_u *arg;
1905{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001906 if (*arg == '@' && arg[1] != NUL)
1907 return arg + 2;
1908 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1909 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910}
1911
Bram Moolenaara7043832005-01-21 11:56:39 +00001912/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 * List variables for hashtab "ht" with prefix "prefix".
1914 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001915 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001917list_hashtable_vars(ht, prefix, empty)
1918 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001921{
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 hashitem_T *hi;
1923 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001924 int todo;
1925
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001926 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001927 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1928 {
1929 if (!HASHITEM_EMPTY(hi))
1930 {
1931 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 di = HI2DI(hi);
1933 if (empty || di->di_tv.v_type != VAR_STRING
1934 || di->di_tv.vval.v_string != NULL)
1935 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 }
1937 }
1938}
1939
1940/*
1941 * List global variables.
1942 */
1943 static void
1944list_glob_vars()
1945{
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001947}
1948
1949/*
1950 * List buffer variables.
1951 */
1952 static void
1953list_buf_vars()
1954{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001955 char_u numbuf[NUMBUFLEN];
1956
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958
1959 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1960 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001961}
1962
1963/*
1964 * List window variables.
1965 */
1966 static void
1967list_win_vars()
1968{
Bram Moolenaar33570922005-01-25 22:26:29 +00001969 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001970}
1971
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001972#ifdef FEAT_WINDOWS
1973/*
1974 * List tab page variables.
1975 */
1976 static void
1977list_tab_vars()
1978{
1979 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1980}
1981#endif
1982
Bram Moolenaara7043832005-01-21 11:56:39 +00001983/*
1984 * List Vim variables.
1985 */
1986 static void
1987list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988{
Bram Moolenaar33570922005-01-25 22:26:29 +00001989 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990}
1991
1992/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001993 * List script-local variables, if there is a script.
1994 */
1995 static void
1996list_script_vars()
1997{
1998 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1999 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
2000}
2001
2002/*
2003 * List function variables, if there is a function.
2004 */
2005 static void
2006list_func_vars()
2007{
2008 if (current_funccal != NULL)
2009 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2010 (char_u *)"l:", FALSE);
2011}
2012
2013/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 * List variables in "arg".
2015 */
2016 static char_u *
2017list_arg_vars(eap, arg)
2018 exarg_T *eap;
2019 char_u *arg;
2020{
2021 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002024 char_u *name_start;
2025 char_u *arg_subsc;
2026 char_u *tofree;
2027 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002028
2029 while (!ends_excmd(*arg) && !got_int)
2030 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002031 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002033 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2035 {
2036 emsg_severe = TRUE;
2037 EMSG(_(e_trailing));
2038 break;
2039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 /* get_name_len() takes care of expanding curly braces */
2044 name_start = name = arg;
2045 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2046 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002048 /* This is mainly to keep test 49 working: when expanding
2049 * curly braces fails overrule the exception error message. */
2050 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052 emsg_severe = TRUE;
2053 EMSG2(_(e_invarg2), arg);
2054 break;
2055 }
2056 error = TRUE;
2057 }
2058 else
2059 {
2060 if (tofree != NULL)
2061 name = tofree;
2062 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 else
2065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 /* handle d.key, l[idx], f(expr) */
2067 arg_subsc = arg;
2068 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 case 'g': list_glob_vars(); break;
2077 case 'b': list_buf_vars(); break;
2078 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002079#ifdef FEAT_WINDOWS
2080 case 't': list_tab_vars(); break;
2081#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002083 case 's': list_script_vars(); break;
2084 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 default:
2086 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 }
2089 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002090 {
2091 char_u numbuf[NUMBUFLEN];
2092 char_u *tf;
2093 int c;
2094 char_u *s;
2095
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002096 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097 c = *arg;
2098 *arg = NUL;
2099 list_one_var_a((char_u *)"",
2100 arg == arg_subsc ? name : name_start,
2101 tv.v_type, s == NULL ? (char_u *)"" : s);
2102 *arg = c;
2103 vim_free(tf);
2104 }
2105 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 }
2108 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109
2110 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112
2113 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 }
2115
2116 return arg;
2117}
2118
2119/*
2120 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2121 * Returns a pointer to the char just after the var name.
2122 * Returns NULL if there is an error.
2123 */
2124 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002125ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131{
2132 int c1;
2133 char_u *name;
2134 char_u *p;
2135 char_u *arg_end = NULL;
2136 int len;
2137 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002138 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139
2140 /*
2141 * ":let $VAR = expr": Set environment variable.
2142 */
2143 if (*arg == '$')
2144 {
2145 /* Find the end of the name. */
2146 ++arg;
2147 name = arg;
2148 len = get_env_len(&arg);
2149 if (len == 0)
2150 EMSG2(_(e_invarg2), name - 1);
2151 else
2152 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002153 if (op != NULL && (*op == '+' || *op == '-'))
2154 EMSG2(_(e_letwrong), op);
2155 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002156 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 EMSG(_(e_letunexp));
2158 else
2159 {
2160 c1 = name[len];
2161 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 p = get_tv_string_chk(tv);
2163 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 {
2165 int mustfree = FALSE;
2166 char_u *s = vim_getenv(name, &mustfree);
2167
2168 if (s != NULL)
2169 {
2170 p = tofree = concat_str(s, p);
2171 if (mustfree)
2172 vim_free(s);
2173 }
2174 }
2175 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002178 if (STRICMP(name, "HOME") == 0)
2179 init_homedir();
2180 else if (didset_vim && STRICMP(name, "VIM") == 0)
2181 didset_vim = FALSE;
2182 else if (didset_vimruntime
2183 && STRICMP(name, "VIMRUNTIME") == 0)
2184 didset_vimruntime = FALSE;
2185 arg_end = arg;
2186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 }
2190 }
2191 }
2192
2193 /*
2194 * ":let &option = expr": Set option value.
2195 * ":let &l:option = expr": Set local option value.
2196 * ":let &g:option = expr": Set global option value.
2197 */
2198 else if (*arg == '&')
2199 {
2200 /* Find the end of the name. */
2201 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002202 if (p == NULL || (endchars != NULL
2203 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 EMSG(_(e_letunexp));
2205 else
2206 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 long n;
2208 int opt_type;
2209 long numval;
2210 char_u *stringval = NULL;
2211 char_u *s;
2212
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 c1 = *p;
2214 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002215
2216 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002217 s = get_tv_string_chk(tv); /* != NULL if number or string */
2218 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002219 {
2220 opt_type = get_option_value(arg, &numval,
2221 &stringval, opt_flags);
2222 if ((opt_type == 1 && *op == '.')
2223 || (opt_type == 0 && *op != '.'))
2224 EMSG2(_(e_letwrong), op);
2225 else
2226 {
2227 if (opt_type == 1) /* number */
2228 {
2229 if (*op == '+')
2230 n = numval + n;
2231 else
2232 n = numval - n;
2233 }
2234 else if (opt_type == 0 && stringval != NULL) /* string */
2235 {
2236 s = concat_str(stringval, s);
2237 vim_free(stringval);
2238 stringval = s;
2239 }
2240 }
2241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002242 if (s != NULL)
2243 {
2244 set_option_value(arg, n, s, opt_flags);
2245 arg_end = p;
2246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 }
2250 }
2251
2252 /*
2253 * ":let @r = expr": Set register contents.
2254 */
2255 else if (*arg == '@')
2256 {
2257 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002258 if (op != NULL && (*op == '+' || *op == '-'))
2259 EMSG2(_(e_letwrong), op);
2260 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002261 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 EMSG(_(e_letunexp));
2263 else
2264 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002265 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 char_u *s;
2267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002268 p = get_tv_string_chk(tv);
2269 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002271 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 if (s != NULL)
2273 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002274 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002275 vim_free(s);
2276 }
2277 }
2278 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002281 arg_end = arg + 1;
2282 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002283 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
2286
2287 /*
2288 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002291 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002293 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002295 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2299 EMSG(_(e_letunexp));
2300 else
2301 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 arg_end = p;
2304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308
2309 else
2310 EMSG2(_(e_invarg2), arg);
2311
2312 return arg_end;
2313}
2314
2315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002316 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2317 */
2318 static int
2319check_changedtick(arg)
2320 char_u *arg;
2321{
2322 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2323 {
2324 EMSG2(_(e_readonlyvar), arg);
2325 return TRUE;
2326 }
2327 return FALSE;
2328}
2329
2330/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 * Get an lval: variable, Dict item or List item that can be assigned a value
2332 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2333 * "name.key", "name.key[expr]" etc.
2334 * Indexing only works if "name" is an existing List or Dictionary.
2335 * "name" points to the start of the name.
2336 * If "rettv" is not NULL it points to the value to be assigned.
2337 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2338 * wrong; must end in space or cmd separator.
2339 *
2340 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002341 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 */
2344 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002345get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002347 typval_T *rettv;
2348 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 int unlet;
2350 int skip;
2351 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002352 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 char_u *expr_start, *expr_end;
2356 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002357 dictitem_T *v;
2358 typval_T var1;
2359 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002361 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002364 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002367 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368
2369 if (skip)
2370 {
2371 /* When skipping just find the end of the name. */
2372 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002373 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 }
2375
2376 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002377 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (expr_start != NULL)
2379 {
2380 /* Don't expand the name when we already know there is an error. */
2381 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2382 && *p != '[' && *p != '.')
2383 {
2384 EMSG(_(e_trailing));
2385 return NULL;
2386 }
2387
2388 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2389 if (lp->ll_exp_name == NULL)
2390 {
2391 /* Report an invalid expression in braces, unless the
2392 * expression evaluation has been cancelled due to an
2393 * aborting error, an interrupt, or an exception. */
2394 if (!aborting() && !quiet)
2395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002396 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 EMSG2(_(e_invarg2), name);
2398 return NULL;
2399 }
2400 }
2401 lp->ll_name = lp->ll_exp_name;
2402 }
2403 else
2404 lp->ll_name = name;
2405
2406 /* Without [idx] or .key we are done. */
2407 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2408 return p;
2409
2410 cc = *p;
2411 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002412 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (v == NULL && !quiet)
2414 EMSG2(_(e_undefvar), lp->ll_name);
2415 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 if (v == NULL)
2417 return NULL;
2418
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 /*
2420 * Loop until no more [idx] or .key is following.
2421 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002422 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2426 && !(lp->ll_tv->v_type == VAR_DICT
2427 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 if (!quiet)
2430 EMSG(_("E689: Can only index a List or Dictionary"));
2431 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (!quiet)
2436 EMSG(_("E708: [:] must come last"));
2437 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002439
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 len = -1;
2441 if (*p == '.')
2442 {
2443 key = p + 1;
2444 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2445 ;
2446 if (len == 0)
2447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (!quiet)
2449 EMSG(_(e_emptykey));
2450 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452 p = key + len;
2453 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002454 else
2455 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002457 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 if (*p == ':')
2459 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002460 else
2461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 empty1 = FALSE;
2463 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 if (get_tv_string_chk(&var1) == NULL)
2466 {
2467 /* not a number or string */
2468 clear_tv(&var1);
2469 return NULL;
2470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 }
2472
2473 /* Optionally get the second index [ :expr]. */
2474 if (*p == ':')
2475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002480 if (!empty1)
2481 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002483 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (rettv != NULL && (rettv->v_type != VAR_LIST
2485 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (!quiet)
2488 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 if (!empty1)
2490 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 }
2493 p = skipwhite(p + 1);
2494 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 else
2497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2500 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 if (!empty1)
2502 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 if (get_tv_string_chk(&var2) == NULL)
2506 {
2507 /* not a number or string */
2508 if (!empty1)
2509 clear_tv(&var1);
2510 clear_tv(&var2);
2511 return NULL;
2512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (!quiet)
2522 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 if (!empty1)
2524 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 }
2529
2530 /* Skip to past ']'. */
2531 ++p;
2532 }
2533
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 {
2536 if (len == -1)
2537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002539 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 if (*key == NUL)
2541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 if (!quiet)
2543 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 }
2547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548 lp->ll_list = NULL;
2549 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002550 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002553 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (len == -1)
2559 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 }
2562 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 if (len == -1)
2567 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 p = NULL;
2570 break;
2571 }
2572 if (len == -1)
2573 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 }
2576 else
2577 {
2578 /*
2579 * Get the number and item for the only or first index of the List.
2580 */
2581 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 else
2584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002585 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 clear_tv(&var1);
2587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002588 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 lp->ll_list = lp->ll_tv->vval.v_list;
2590 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2591 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002593 if (lp->ll_n1 < 0)
2594 {
2595 lp->ll_n1 = 0;
2596 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2597 }
2598 }
2599 if (lp->ll_li == NULL)
2600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 }
2605
2606 /*
2607 * May need to find the item or absolute index for the second
2608 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 * When no index given: "lp->ll_empty2" is TRUE.
2610 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002614 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2625 if (lp->ll_n1 < 0)
2626 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2627 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 }
2630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 }
2634
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return p;
2636}
2637
2638/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 */
2641 static void
2642clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644{
2645 vim_free(lp->ll_exp_name);
2646 vim_free(lp->ll_newkey);
2647}
2648
2649/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002650 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002652 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 */
2654 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002658 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661{
2662 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002663 listitem_T *ri;
2664 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665
2666 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 cc = *endp;
2671 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002672 if (op != NULL && *op != '=')
2673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002675
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002676 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002677 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002678 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002679 {
2680 if (tv_op(&tv, rettv, op) == OK)
2681 set_var(lp->ll_name, &tv, FALSE);
2682 clear_tv(&tv);
2683 }
2684 }
2685 else
2686 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002690 else if (tv_check_lock(lp->ll_newkey == NULL
2691 ? lp->ll_tv->v_lock
2692 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2693 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 else if (lp->ll_range)
2695 {
2696 /*
2697 * Assign the List values to the list items.
2698 */
2699 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002701 if (op != NULL && *op != '=')
2702 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2703 else
2704 {
2705 clear_tv(&lp->ll_li->li_tv);
2706 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 ri = ri->li_next;
2709 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2710 break;
2711 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002714 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 ri = NULL;
2717 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_li = lp->ll_li->li_next;
2721 ++lp->ll_n1;
2722 }
2723 if (ri != NULL)
2724 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 else if (lp->ll_empty2
2726 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 : lp->ll_n1 != lp->ll_n2)
2728 EMSG(_("E711: List value has not enough items"));
2729 }
2730 else
2731 {
2732 /*
2733 * Assign to a List or Dictionary item.
2734 */
2735 if (lp->ll_newkey != NULL)
2736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 if (op != NULL && *op != '=')
2738 {
2739 EMSG2(_(e_letwrong), op);
2740 return;
2741 }
2742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002744 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (di == NULL)
2746 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002747 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2748 {
2749 vim_free(di);
2750 return;
2751 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002754 else if (op != NULL && *op != '=')
2755 {
2756 tv_op(lp->ll_tv, rettv, op);
2757 return;
2758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002759 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 /*
2763 * Assign the value to the variable or list item.
2764 */
2765 if (copy)
2766 copy_tv(rettv, lp->ll_tv);
2767 else
2768 {
2769 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002770 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002772 }
2773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002774}
2775
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002777 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2778 * Returns OK or FAIL.
2779 */
2780 static int
2781tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002782 typval_T *tv1;
2783 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002784 char_u *op;
2785{
2786 long n;
2787 char_u numbuf[NUMBUFLEN];
2788 char_u *s;
2789
2790 /* Can't do anything with a Funcref or a Dict on the right. */
2791 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2792 {
2793 switch (tv1->v_type)
2794 {
2795 case VAR_DICT:
2796 case VAR_FUNC:
2797 break;
2798
2799 case VAR_LIST:
2800 if (*op != '+' || tv2->v_type != VAR_LIST)
2801 break;
2802 /* List += List */
2803 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2804 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2805 return OK;
2806
2807 case VAR_NUMBER:
2808 case VAR_STRING:
2809 if (tv2->v_type == VAR_LIST)
2810 break;
2811 if (*op == '+' || *op == '-')
2812 {
2813 /* nr += nr or nr -= nr*/
2814 n = get_tv_number(tv1);
2815 if (*op == '+')
2816 n += get_tv_number(tv2);
2817 else
2818 n -= get_tv_number(tv2);
2819 clear_tv(tv1);
2820 tv1->v_type = VAR_NUMBER;
2821 tv1->vval.v_number = n;
2822 }
2823 else
2824 {
2825 /* str .= str */
2826 s = get_tv_string(tv1);
2827 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2828 clear_tv(tv1);
2829 tv1->v_type = VAR_STRING;
2830 tv1->vval.v_string = s;
2831 }
2832 return OK;
2833 }
2834 }
2835
2836 EMSG2(_(e_letwrong), op);
2837 return FAIL;
2838}
2839
2840/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002841 * Add a watcher to a list.
2842 */
2843 static void
2844list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 list_T *l;
2846 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847{
2848 lw->lw_next = l->lv_watch;
2849 l->lv_watch = lw;
2850}
2851
2852/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002853 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 * No warning when it isn't found...
2855 */
2856 static void
2857list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002858 list_T *l;
2859 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860{
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002862
2863 lwp = &l->lv_watch;
2864 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2865 {
2866 if (lw == lwrem)
2867 {
2868 *lwp = lw->lw_next;
2869 break;
2870 }
2871 lwp = &lw->lw_next;
2872 }
2873}
2874
2875/*
2876 * Just before removing an item from a list: advance watchers to the next
2877 * item.
2878 */
2879 static void
2880list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 list_T *l;
2882 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883{
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002885
2886 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2887 if (lw->lw_item == item)
2888 lw->lw_item = item->li_next;
2889}
2890
2891/*
2892 * Evaluate the expression used in a ":for var in expr" command.
2893 * "arg" points to "var".
2894 * Set "*errp" to TRUE for an error, FALSE otherwise;
2895 * Return a pointer that holds the info. Null when there is an error.
2896 */
2897 void *
2898eval_for_line(arg, errp, nextcmdp, skip)
2899 char_u *arg;
2900 int *errp;
2901 char_u **nextcmdp;
2902 int skip;
2903{
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002906 typval_T tv;
2907 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908
2909 *errp = TRUE; /* default: there is an error */
2910
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 if (fi == NULL)
2913 return NULL;
2914
2915 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2916 if (expr == NULL)
2917 return fi;
2918
2919 expr = skipwhite(expr);
2920 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2921 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002922 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 return fi;
2924 }
2925
2926 if (skip)
2927 ++emsg_skip;
2928 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2929 {
2930 *errp = FALSE;
2931 if (!skip)
2932 {
2933 l = tv.vval.v_list;
2934 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002935 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002936 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002937 clear_tv(&tv);
2938 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002939 else
2940 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002941 /* No need to increment the refcount, it's already set for the
2942 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943 fi->fi_list = l;
2944 list_add_watch(l, &fi->fi_lw);
2945 fi->fi_lw.lw_item = l->lv_first;
2946 }
2947 }
2948 }
2949 if (skip)
2950 --emsg_skip;
2951
2952 return fi;
2953}
2954
2955/*
2956 * Use the first item in a ":for" list. Advance to the next.
2957 * Assign the values to the variable (list). "arg" points to the first one.
2958 * Return TRUE when a valid item was found, FALSE when at end of list or
2959 * something wrong.
2960 */
2961 int
2962next_for_item(fi_void, arg)
2963 void *fi_void;
2964 char_u *arg;
2965{
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002968 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969
2970 item = fi->fi_lw.lw_item;
2971 if (item == NULL)
2972 result = FALSE;
2973 else
2974 {
2975 fi->fi_lw.lw_item = item->li_next;
2976 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2977 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2978 }
2979 return result;
2980}
2981
2982/*
2983 * Free the structure used to store info used by ":for".
2984 */
2985 void
2986free_for_info(fi_void)
2987 void *fi_void;
2988{
Bram Moolenaar33570922005-01-25 22:26:29 +00002989 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002990
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002991 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002992 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002993 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002994 list_unref(fi->fi_list);
2995 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002996 vim_free(fi);
2997}
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3000
3001 void
3002set_context_for_expression(xp, arg, cmdidx)
3003 expand_T *xp;
3004 char_u *arg;
3005 cmdidx_T cmdidx;
3006{
3007 int got_eq = FALSE;
3008 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011 if (cmdidx == CMD_let)
3012 {
3013 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003014 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003015 {
3016 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003017 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003018 {
3019 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021 if (vim_iswhite(*p))
3022 break;
3023 }
3024 return;
3025 }
3026 }
3027 else
3028 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3029 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 while ((xp->xp_pattern = vim_strpbrk(arg,
3031 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3032 {
3033 c = *xp->xp_pattern;
3034 if (c == '&')
3035 {
3036 c = xp->xp_pattern[1];
3037 if (c == '&')
3038 {
3039 ++xp->xp_pattern;
3040 xp->xp_context = cmdidx != CMD_let || got_eq
3041 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3042 }
3043 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003046 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3047 xp->xp_pattern += 2;
3048
3049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051 else if (c == '$')
3052 {
3053 /* environment variable */
3054 xp->xp_context = EXPAND_ENV_VARS;
3055 }
3056 else if (c == '=')
3057 {
3058 got_eq = TRUE;
3059 xp->xp_context = EXPAND_EXPRESSION;
3060 }
3061 else if (c == '<'
3062 && xp->xp_context == EXPAND_FUNCTIONS
3063 && vim_strchr(xp->xp_pattern, '(') == NULL)
3064 {
3065 /* Function name can start with "<SNR>" */
3066 break;
3067 }
3068 else if (cmdidx != CMD_let || got_eq)
3069 {
3070 if (c == '"') /* string */
3071 {
3072 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3073 if (c == '\\' && xp->xp_pattern[1] != NUL)
3074 ++xp->xp_pattern;
3075 xp->xp_context = EXPAND_NOTHING;
3076 }
3077 else if (c == '\'') /* literal string */
3078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003079 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3081 /* skip */ ;
3082 xp->xp_context = EXPAND_NOTHING;
3083 }
3084 else if (c == '|')
3085 {
3086 if (xp->xp_pattern[1] == '|')
3087 {
3088 ++xp->xp_pattern;
3089 xp->xp_context = EXPAND_EXPRESSION;
3090 }
3091 else
3092 xp->xp_context = EXPAND_COMMANDS;
3093 }
3094 else
3095 xp->xp_context = EXPAND_EXPRESSION;
3096 }
3097 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003098 /* Doesn't look like something valid, expand as an expression
3099 * anyway. */
3100 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 arg = xp->xp_pattern;
3102 if (*arg != NUL)
3103 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3104 /* skip */ ;
3105 }
3106 xp->xp_pattern = arg;
3107}
3108
3109#endif /* FEAT_CMDL_COMPL */
3110
3111/*
3112 * ":1,25call func(arg1, arg2)" function call.
3113 */
3114 void
3115ex_call(eap)
3116 exarg_T *eap;
3117{
3118 char_u *arg = eap->arg;
3119 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 linenr_T lnum;
3125 int doesrange;
3126 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003129 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003130 if (fudi.fd_newkey != NULL)
3131 {
3132 /* Still need to give an error message for missing key. */
3133 EMSG2(_(e_dictkey), fudi.fd_newkey);
3134 vim_free(fudi.fd_newkey);
3135 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003136 if (tofree == NULL)
3137 return;
3138
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003139 /* Increase refcount on dictionary, it could get deleted when evaluating
3140 * the arguments. */
3141 if (fudi.fd_dict != NULL)
3142 ++fudi.fd_dict->dv_refcount;
3143
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003145 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
Bram Moolenaar532c7802005-01-27 14:44:31 +00003148 /* Skip white space to allow ":call func ()". Not good, but required for
3149 * backward compatibility. */
3150 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003151 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 if (*startarg != '(')
3154 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003155 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 goto end;
3157 }
3158
3159 /*
3160 * When skipping, evaluate the function once, to find the end of the
3161 * arguments.
3162 * When the function takes a range, this is discovered after the first
3163 * call, and the loop is broken.
3164 */
3165 if (eap->skip)
3166 {
3167 ++emsg_skip;
3168 lnum = eap->line2; /* do it once, also with an invalid range */
3169 }
3170 else
3171 lnum = eap->line1;
3172 for ( ; lnum <= eap->line2; ++lnum)
3173 {
3174 if (!eap->skip && eap->addr_count > 0)
3175 {
3176 curwin->w_cursor.lnum = lnum;
3177 curwin->w_cursor.col = 0;
3178 }
3179 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003180 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003181 eap->line1, eap->line2, &doesrange,
3182 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 failed = TRUE;
3185 break;
3186 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003187
3188 /* Handle a function returning a Funcref, Dictionary or List. */
3189 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3190 {
3191 failed = TRUE;
3192 break;
3193 }
3194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003195 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (doesrange || eap->skip)
3197 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003200 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003201 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003202 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (aborting())
3204 break;
3205 }
3206 if (eap->skip)
3207 --emsg_skip;
3208
3209 if (!failed)
3210 {
3211 /* Check for trailing illegal characters and a following command. */
3212 if (!ends_excmd(*arg))
3213 {
3214 emsg_severe = TRUE;
3215 EMSG(_(e_trailing));
3216 }
3217 else
3218 eap->nextcmd = check_nextcmd(arg);
3219 }
3220
3221end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003222 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003223 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226/*
3227 * ":unlet[!] var1 ... " command.
3228 */
3229 void
3230ex_unlet(eap)
3231 exarg_T *eap;
3232{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003233 ex_unletlock(eap, eap->arg, 0);
3234}
3235
3236/*
3237 * ":lockvar" and ":unlockvar" commands
3238 */
3239 void
3240ex_lockvar(eap)
3241 exarg_T *eap;
3242{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003244 int deep = 2;
3245
3246 if (eap->forceit)
3247 deep = -1;
3248 else if (vim_isdigit(*arg))
3249 {
3250 deep = getdigits(&arg);
3251 arg = skipwhite(arg);
3252 }
3253
3254 ex_unletlock(eap, arg, deep);
3255}
3256
3257/*
3258 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3259 */
3260 static void
3261ex_unletlock(eap, argstart, deep)
3262 exarg_T *eap;
3263 char_u *argstart;
3264 int deep;
3265{
3266 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003269 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
3271 do
3272 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003273 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003274 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3275 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003276 if (lv.ll_name == NULL)
3277 error = TRUE; /* error but continue parsing */
3278 if (name_end == NULL || (!vim_iswhite(*name_end)
3279 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003281 if (name_end != NULL)
3282 {
3283 emsg_severe = TRUE;
3284 EMSG(_(e_trailing));
3285 }
3286 if (!(eap->skip || error))
3287 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 break;
3289 }
3290
3291 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003292 {
3293 if (eap->cmdidx == CMD_unlet)
3294 {
3295 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3296 error = TRUE;
3297 }
3298 else
3299 {
3300 if (do_lock_var(&lv, name_end, deep,
3301 eap->cmdidx == CMD_lockvar) == FAIL)
3302 error = TRUE;
3303 }
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 if (!eap->skip)
3307 clear_lval(&lv);
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 arg = skipwhite(name_end);
3310 } while (!ends_excmd(*arg));
3311
3312 eap->nextcmd = check_nextcmd(arg);
3313}
3314
Bram Moolenaar8c711452005-01-14 21:53:12 +00003315 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003316do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003318 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003319 int forceit;
3320{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003321 int ret = OK;
3322 int cc;
3323
3324 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003326 cc = *name_end;
3327 *name_end = NUL;
3328
3329 /* Normal name or expanded name. */
3330 if (check_changedtick(lp->ll_name))
3331 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003332 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003333 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003334 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003335 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003336 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3337 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003338 else if (lp->ll_range)
3339 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003340 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003341
3342 /* Delete a range of List items. */
3343 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3344 {
3345 li = lp->ll_li->li_next;
3346 listitem_remove(lp->ll_list, lp->ll_li);
3347 lp->ll_li = li;
3348 ++lp->ll_n1;
3349 }
3350 }
3351 else
3352 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003353 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003354 /* unlet a List item. */
3355 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003356 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003357 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003358 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003359 }
3360
3361 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362}
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364/*
3365 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003366 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 */
3368 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003369do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003371 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 hashtab_T *ht;
3374 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003375 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
Bram Moolenaar33570922005-01-25 22:26:29 +00003377 ht = find_var_ht(name, &varname);
3378 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003380 hi = hash_find(ht, varname);
3381 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003382 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003383 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3384 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003385 if (var_check_ro(HI2DI(hi)->di_flags, name))
3386 return FAIL;
3387 delete_var(ht, hi);
3388 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003391 if (forceit)
3392 return OK;
3393 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return FAIL;
3395}
3396
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003397/*
3398 * Lock or unlock variable indicated by "lp".
3399 * "deep" is the levels to go (-1 for unlimited);
3400 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3401 */
3402 static int
3403do_lock_var(lp, name_end, deep, lock)
3404 lval_T *lp;
3405 char_u *name_end;
3406 int deep;
3407 int lock;
3408{
3409 int ret = OK;
3410 int cc;
3411 dictitem_T *di;
3412
3413 if (deep == 0) /* nothing to do */
3414 return OK;
3415
3416 if (lp->ll_tv == NULL)
3417 {
3418 cc = *name_end;
3419 *name_end = NUL;
3420
3421 /* Normal name or expanded name. */
3422 if (check_changedtick(lp->ll_name))
3423 ret = FAIL;
3424 else
3425 {
3426 di = find_var(lp->ll_name, NULL);
3427 if (di == NULL)
3428 ret = FAIL;
3429 else
3430 {
3431 if (lock)
3432 di->di_flags |= DI_FLAGS_LOCK;
3433 else
3434 di->di_flags &= ~DI_FLAGS_LOCK;
3435 item_lock(&di->di_tv, deep, lock);
3436 }
3437 }
3438 *name_end = cc;
3439 }
3440 else if (lp->ll_range)
3441 {
3442 listitem_T *li = lp->ll_li;
3443
3444 /* (un)lock a range of List items. */
3445 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3446 {
3447 item_lock(&li->li_tv, deep, lock);
3448 li = li->li_next;
3449 ++lp->ll_n1;
3450 }
3451 }
3452 else if (lp->ll_list != NULL)
3453 /* (un)lock a List item. */
3454 item_lock(&lp->ll_li->li_tv, deep, lock);
3455 else
3456 /* un(lock) a Dictionary item. */
3457 item_lock(&lp->ll_di->di_tv, deep, lock);
3458
3459 return ret;
3460}
3461
3462/*
3463 * Lock or unlock an item. "deep" is nr of levels to go.
3464 */
3465 static void
3466item_lock(tv, deep, lock)
3467 typval_T *tv;
3468 int deep;
3469 int lock;
3470{
3471 static int recurse = 0;
3472 list_T *l;
3473 listitem_T *li;
3474 dict_T *d;
3475 hashitem_T *hi;
3476 int todo;
3477
3478 if (recurse >= DICT_MAXNEST)
3479 {
3480 EMSG(_("E743: variable nested too deep for (un)lock"));
3481 return;
3482 }
3483 if (deep == 0)
3484 return;
3485 ++recurse;
3486
3487 /* lock/unlock the item itself */
3488 if (lock)
3489 tv->v_lock |= VAR_LOCKED;
3490 else
3491 tv->v_lock &= ~VAR_LOCKED;
3492
3493 switch (tv->v_type)
3494 {
3495 case VAR_LIST:
3496 if ((l = tv->vval.v_list) != NULL)
3497 {
3498 if (lock)
3499 l->lv_lock |= VAR_LOCKED;
3500 else
3501 l->lv_lock &= ~VAR_LOCKED;
3502 if (deep < 0 || deep > 1)
3503 /* recursive: lock/unlock the items the List contains */
3504 for (li = l->lv_first; li != NULL; li = li->li_next)
3505 item_lock(&li->li_tv, deep - 1, lock);
3506 }
3507 break;
3508 case VAR_DICT:
3509 if ((d = tv->vval.v_dict) != NULL)
3510 {
3511 if (lock)
3512 d->dv_lock |= VAR_LOCKED;
3513 else
3514 d->dv_lock &= ~VAR_LOCKED;
3515 if (deep < 0 || deep > 1)
3516 {
3517 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003518 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3520 {
3521 if (!HASHITEM_EMPTY(hi))
3522 {
3523 --todo;
3524 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3525 }
3526 }
3527 }
3528 }
3529 }
3530 --recurse;
3531}
3532
Bram Moolenaara40058a2005-07-11 22:42:07 +00003533/*
3534 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3535 * it refers to a List or Dictionary that is locked.
3536 */
3537 static int
3538tv_islocked(tv)
3539 typval_T *tv;
3540{
3541 return (tv->v_lock & VAR_LOCKED)
3542 || (tv->v_type == VAR_LIST
3543 && tv->vval.v_list != NULL
3544 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3545 || (tv->v_type == VAR_DICT
3546 && tv->vval.v_dict != NULL
3547 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3548}
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3551/*
3552 * Delete all "menutrans_" variables.
3553 */
3554 void
3555del_menutrans_vars()
3556{
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
Bram Moolenaar33570922005-01-25 22:26:29 +00003560 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003561 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003563 {
3564 if (!HASHITEM_EMPTY(hi))
3565 {
3566 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003567 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3568 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003569 }
3570 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
3573#endif
3574
3575#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3576
3577/*
3578 * Local string buffer for the next two functions to store a variable name
3579 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3580 * get_user_var_name().
3581 */
3582
3583static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3584
3585static char_u *varnamebuf = NULL;
3586static int varnamebuflen = 0;
3587
3588/*
3589 * Function to concatenate a prefix and a variable name.
3590 */
3591 static char_u *
3592cat_prefix_varname(prefix, name)
3593 int prefix;
3594 char_u *name;
3595{
3596 int len;
3597
3598 len = (int)STRLEN(name) + 3;
3599 if (len > varnamebuflen)
3600 {
3601 vim_free(varnamebuf);
3602 len += 10; /* some additional space */
3603 varnamebuf = alloc(len);
3604 if (varnamebuf == NULL)
3605 {
3606 varnamebuflen = 0;
3607 return NULL;
3608 }
3609 varnamebuflen = len;
3610 }
3611 *varnamebuf = prefix;
3612 varnamebuf[1] = ':';
3613 STRCPY(varnamebuf + 2, name);
3614 return varnamebuf;
3615}
3616
3617/*
3618 * Function given to ExpandGeneric() to obtain the list of user defined
3619 * (global/buffer/window/built-in) variable names.
3620 */
3621/*ARGSUSED*/
3622 char_u *
3623get_user_var_name(xp, idx)
3624 expand_T *xp;
3625 int idx;
3626{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003627 static long_u gdone;
3628 static long_u bdone;
3629 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003630#ifdef FEAT_WINDOWS
3631 static long_u tdone;
3632#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003633 static int vidx;
3634 static hashitem_T *hi;
3635 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003638 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003639 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003640#ifdef FEAT_WINDOWS
3641 tdone = 0;
3642#endif
3643 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003644
3645 /* Global variables */
3646 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003649 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003650 else
3651 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 while (HASHITEM_EMPTY(hi))
3653 ++hi;
3654 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3655 return cat_prefix_varname('g', hi->hi_key);
3656 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003658
3659 /* b: variables */
3660 ht = &curbuf->b_vars.dv_hashtab;
3661 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003663 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003665 else
3666 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 while (HASHITEM_EMPTY(hi))
3668 ++hi;
3669 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 return (char_u *)"b:changedtick";
3675 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003676
3677 /* w: variables */
3678 ht = &curwin->w_vars.dv_hashtab;
3679 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003681 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003683 else
3684 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 while (HASHITEM_EMPTY(hi))
3686 ++hi;
3687 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003689
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003690#ifdef FEAT_WINDOWS
3691 /* t: variables */
3692 ht = &curtab->tp_vars.dv_hashtab;
3693 if (tdone < ht->ht_used)
3694 {
3695 if (tdone++ == 0)
3696 hi = ht->ht_array;
3697 else
3698 ++hi;
3699 while (HASHITEM_EMPTY(hi))
3700 ++hi;
3701 return cat_prefix_varname('t', hi->hi_key);
3702 }
3703#endif
3704
Bram Moolenaar33570922005-01-25 22:26:29 +00003705 /* v: variables */
3706 if (vidx < VV_LEN)
3707 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 vim_free(varnamebuf);
3710 varnamebuf = NULL;
3711 varnamebuflen = 0;
3712 return NULL;
3713}
3714
3715#endif /* FEAT_CMDL_COMPL */
3716
3717/*
3718 * types for expressions.
3719 */
3720typedef enum
3721{
3722 TYPE_UNKNOWN = 0
3723 , TYPE_EQUAL /* == */
3724 , TYPE_NEQUAL /* != */
3725 , TYPE_GREATER /* > */
3726 , TYPE_GEQUAL /* >= */
3727 , TYPE_SMALLER /* < */
3728 , TYPE_SEQUAL /* <= */
3729 , TYPE_MATCH /* =~ */
3730 , TYPE_NOMATCH /* !~ */
3731} exptype_T;
3732
3733/*
3734 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003735 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3737 */
3738
3739/*
3740 * Handle zero level expression.
3741 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003743 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 * Return OK or FAIL.
3745 */
3746 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003747eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 char_u **nextcmd;
3751 int evaluate;
3752{
3753 int ret;
3754 char_u *p;
3755
3756 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 if (ret == FAIL || !ends_excmd(*p))
3759 {
3760 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003761 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 /*
3763 * Report the invalid expression unless the expression evaluation has
3764 * been cancelled due to an aborting error, an interrupt, or an
3765 * exception.
3766 */
3767 if (!aborting())
3768 EMSG2(_(e_invexpr2), arg);
3769 ret = FAIL;
3770 }
3771 if (nextcmd != NULL)
3772 *nextcmd = check_nextcmd(p);
3773
3774 return ret;
3775}
3776
3777/*
3778 * Handle top level expression:
3779 * expr1 ? expr0 : expr0
3780 *
3781 * "arg" must point to the first non-white of the expression.
3782 * "arg" is advanced to the next non-white after the recognized expression.
3783 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003784 * Note: "rettv.v_lock" is not set.
3785 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 * Return OK or FAIL.
3787 */
3788 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003789eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 int evaluate;
3793{
3794 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003795 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /*
3798 * Get the first variable.
3799 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802
3803 if ((*arg)[0] == '?')
3804 {
3805 result = FALSE;
3806 if (evaluate)
3807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003808 int error = FALSE;
3809
3810 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 if (error)
3814 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816
3817 /*
3818 * Get the second variable.
3819 */
3820 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003821 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FAIL;
3823
3824 /*
3825 * Check for the ":".
3826 */
3827 if ((*arg)[0] != ':')
3828 {
3829 EMSG(_("E109: Missing ':' after '?'"));
3830 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return FAIL;
3833 }
3834
3835 /*
3836 * Get the third variable.
3837 */
3838 *arg = skipwhite(*arg + 1);
3839 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3840 {
3841 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003842 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 return FAIL;
3844 }
3845 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848
3849 return OK;
3850}
3851
3852/*
3853 * Handle first level expression:
3854 * expr2 || expr2 || expr2 logical OR
3855 *
3856 * "arg" must point to the first non-white of the expression.
3857 * "arg" is advanced to the next non-white after the recognized expression.
3858 *
3859 * Return OK or FAIL.
3860 */
3861 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003862eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 int evaluate;
3866{
Bram Moolenaar33570922005-01-25 22:26:29 +00003867 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 long result;
3869 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003870 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
3872 /*
3873 * Get the first variable.
3874 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return FAIL;
3877
3878 /*
3879 * Repeat until there is no following "||".
3880 */
3881 first = TRUE;
3882 result = FALSE;
3883 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3884 {
3885 if (evaluate && first)
3886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003887 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003890 if (error)
3891 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 first = FALSE;
3893 }
3894
3895 /*
3896 * Get the second variable.
3897 */
3898 *arg = skipwhite(*arg + 2);
3899 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3900 return FAIL;
3901
3902 /*
3903 * Compute the result.
3904 */
3905 if (evaluate && !result)
3906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003907 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003910 if (error)
3911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 if (evaluate)
3914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915 rettv->v_type = VAR_NUMBER;
3916 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 }
3919
3920 return OK;
3921}
3922
3923/*
3924 * Handle second level expression:
3925 * expr3 && expr3 && expr3 logical AND
3926 *
3927 * "arg" must point to the first non-white of the expression.
3928 * "arg" is advanced to the next non-white after the recognized expression.
3929 *
3930 * Return OK or FAIL.
3931 */
3932 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 int evaluate;
3937{
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 long result;
3940 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003941 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 /*
3944 * Get the first variable.
3945 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003946 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 return FAIL;
3948
3949 /*
3950 * Repeat until there is no following "&&".
3951 */
3952 first = TRUE;
3953 result = TRUE;
3954 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3955 {
3956 if (evaluate && first)
3957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003958 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003961 if (error)
3962 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 first = FALSE;
3964 }
3965
3966 /*
3967 * Get the second variable.
3968 */
3969 *arg = skipwhite(*arg + 2);
3970 if (eval4(arg, &var2, evaluate && result) == FAIL)
3971 return FAIL;
3972
3973 /*
3974 * Compute the result.
3975 */
3976 if (evaluate && result)
3977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003978 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003980 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003981 if (error)
3982 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984 if (evaluate)
3985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003986 rettv->v_type = VAR_NUMBER;
3987 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 }
3990
3991 return OK;
3992}
3993
3994/*
3995 * Handle third level expression:
3996 * var1 == var2
3997 * var1 =~ var2
3998 * var1 != var2
3999 * var1 !~ var2
4000 * var1 > var2
4001 * var1 >= var2
4002 * var1 < var2
4003 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004004 * var1 is var2
4005 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 *
4007 * "arg" must point to the first non-white of the expression.
4008 * "arg" is advanced to the next non-white after the recognized expression.
4009 *
4010 * Return OK or FAIL.
4011 */
4012 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 int evaluate;
4017{
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 char_u *p;
4020 int i;
4021 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004022 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 int len = 2;
4024 long n1, n2;
4025 char_u *s1, *s2;
4026 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4027 regmatch_T regmatch;
4028 int ic;
4029 char_u *save_cpo;
4030
4031 /*
4032 * Get the first variable.
4033 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 return FAIL;
4036
4037 p = *arg;
4038 switch (p[0])
4039 {
4040 case '=': if (p[1] == '=')
4041 type = TYPE_EQUAL;
4042 else if (p[1] == '~')
4043 type = TYPE_MATCH;
4044 break;
4045 case '!': if (p[1] == '=')
4046 type = TYPE_NEQUAL;
4047 else if (p[1] == '~')
4048 type = TYPE_NOMATCH;
4049 break;
4050 case '>': if (p[1] != '=')
4051 {
4052 type = TYPE_GREATER;
4053 len = 1;
4054 }
4055 else
4056 type = TYPE_GEQUAL;
4057 break;
4058 case '<': if (p[1] != '=')
4059 {
4060 type = TYPE_SMALLER;
4061 len = 1;
4062 }
4063 else
4064 type = TYPE_SEQUAL;
4065 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004066 case 'i': if (p[1] == 's')
4067 {
4068 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4069 len = 5;
4070 if (!vim_isIDc(p[len]))
4071 {
4072 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4073 type_is = TRUE;
4074 }
4075 }
4076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078
4079 /*
4080 * If there is a comparitive operator, use it.
4081 */
4082 if (type != TYPE_UNKNOWN)
4083 {
4084 /* extra question mark appended: ignore case */
4085 if (p[len] == '?')
4086 {
4087 ic = TRUE;
4088 ++len;
4089 }
4090 /* extra '#' appended: match case */
4091 else if (p[len] == '#')
4092 {
4093 ic = FALSE;
4094 ++len;
4095 }
4096 /* nothing appened: use 'ignorecase' */
4097 else
4098 ic = p_ic;
4099
4100 /*
4101 * Get the second variable.
4102 */
4103 *arg = skipwhite(p + len);
4104 if (eval5(arg, &var2, evaluate) == FAIL)
4105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 return FAIL;
4108 }
4109
4110 if (evaluate)
4111 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004112 if (type_is && rettv->v_type != var2.v_type)
4113 {
4114 /* For "is" a different type always means FALSE, for "notis"
4115 * it means TRUE. */
4116 n1 = (type == TYPE_NEQUAL);
4117 }
4118 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4119 {
4120 if (type_is)
4121 {
4122 n1 = (rettv->v_type == var2.v_type
4123 && rettv->vval.v_list == var2.vval.v_list);
4124 if (type == TYPE_NEQUAL)
4125 n1 = !n1;
4126 }
4127 else if (rettv->v_type != var2.v_type
4128 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4129 {
4130 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004131 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004132 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004133 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004134 clear_tv(rettv);
4135 clear_tv(&var2);
4136 return FAIL;
4137 }
4138 else
4139 {
4140 /* Compare two Lists for being equal or unequal. */
4141 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4142 if (type == TYPE_NEQUAL)
4143 n1 = !n1;
4144 }
4145 }
4146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004147 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4148 {
4149 if (type_is)
4150 {
4151 n1 = (rettv->v_type == var2.v_type
4152 && rettv->vval.v_dict == var2.vval.v_dict);
4153 if (type == TYPE_NEQUAL)
4154 n1 = !n1;
4155 }
4156 else if (rettv->v_type != var2.v_type
4157 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4158 {
4159 if (rettv->v_type != var2.v_type)
4160 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4161 else
4162 EMSG(_("E736: Invalid operation for Dictionary"));
4163 clear_tv(rettv);
4164 clear_tv(&var2);
4165 return FAIL;
4166 }
4167 else
4168 {
4169 /* Compare two Dictionaries for being equal or unequal. */
4170 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4171 if (type == TYPE_NEQUAL)
4172 n1 = !n1;
4173 }
4174 }
4175
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004176 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4177 {
4178 if (rettv->v_type != var2.v_type
4179 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4180 {
4181 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004182 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004183 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004184 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004185 clear_tv(rettv);
4186 clear_tv(&var2);
4187 return FAIL;
4188 }
4189 else
4190 {
4191 /* Compare two Funcrefs for being equal or unequal. */
4192 if (rettv->vval.v_string == NULL
4193 || var2.vval.v_string == NULL)
4194 n1 = FALSE;
4195 else
4196 n1 = STRCMP(rettv->vval.v_string,
4197 var2.vval.v_string) == 0;
4198 if (type == TYPE_NEQUAL)
4199 n1 = !n1;
4200 }
4201 }
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 /*
4204 * If one of the two variables is a number, compare as a number.
4205 * When using "=~" or "!~", always compare as string.
4206 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004207 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 n1 = get_tv_number(rettv);
4211 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 switch (type)
4213 {
4214 case TYPE_EQUAL: n1 = (n1 == n2); break;
4215 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4216 case TYPE_GREATER: n1 = (n1 > n2); break;
4217 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4218 case TYPE_SMALLER: n1 = (n1 < n2); break;
4219 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4220 case TYPE_UNKNOWN:
4221 case TYPE_MATCH:
4222 case TYPE_NOMATCH: break; /* avoid gcc warning */
4223 }
4224 }
4225 else
4226 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 s1 = get_tv_string_buf(rettv, buf1);
4228 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4230 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4231 else
4232 i = 0;
4233 n1 = FALSE;
4234 switch (type)
4235 {
4236 case TYPE_EQUAL: n1 = (i == 0); break;
4237 case TYPE_NEQUAL: n1 = (i != 0); break;
4238 case TYPE_GREATER: n1 = (i > 0); break;
4239 case TYPE_GEQUAL: n1 = (i >= 0); break;
4240 case TYPE_SMALLER: n1 = (i < 0); break;
4241 case TYPE_SEQUAL: n1 = (i <= 0); break;
4242
4243 case TYPE_MATCH:
4244 case TYPE_NOMATCH:
4245 /* avoid 'l' flag in 'cpoptions' */
4246 save_cpo = p_cpo;
4247 p_cpo = (char_u *)"";
4248 regmatch.regprog = vim_regcomp(s2,
4249 RE_MAGIC + RE_STRING);
4250 regmatch.rm_ic = ic;
4251 if (regmatch.regprog != NULL)
4252 {
4253 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4254 vim_free(regmatch.regprog);
4255 if (type == TYPE_NOMATCH)
4256 n1 = !n1;
4257 }
4258 p_cpo = save_cpo;
4259 break;
4260
4261 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4262 }
4263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 clear_tv(rettv);
4265 clear_tv(&var2);
4266 rettv->v_type = VAR_NUMBER;
4267 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * Handle fourth level expression:
4276 * + number addition
4277 * - number subtraction
4278 * . string concatenation
4279 *
4280 * "arg" must point to the first non-white of the expression.
4281 * "arg" is advanced to the next non-white after the recognized expression.
4282 *
4283 * Return OK or FAIL.
4284 */
4285 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int evaluate;
4290{
Bram Moolenaar33570922005-01-25 22:26:29 +00004291 typval_T var2;
4292 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 int op;
4294 long n1, n2;
4295 char_u *s1, *s2;
4296 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4297 char_u *p;
4298
4299 /*
4300 * Get the first variable.
4301 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 return FAIL;
4304
4305 /*
4306 * Repeat computing, until no '+', '-' or '.' is following.
4307 */
4308 for (;;)
4309 {
4310 op = **arg;
4311 if (op != '+' && op != '-' && op != '.')
4312 break;
4313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 if (op != '+' || rettv->v_type != VAR_LIST)
4315 {
4316 /* For "list + ...", an illegal use of the first operand as
4317 * a number cannot be determined before evaluating the 2nd
4318 * operand: if this is also a list, all is ok.
4319 * For "something . ...", "something - ..." or "non-list + ...",
4320 * we know that the first operand needs to be a string or number
4321 * without evaluating the 2nd operand. So check before to avoid
4322 * side effects after an error. */
4323 if (evaluate && get_tv_string_chk(rettv) == NULL)
4324 {
4325 clear_tv(rettv);
4326 return FAIL;
4327 }
4328 }
4329
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 /*
4331 * Get the second variable.
4332 */
4333 *arg = skipwhite(*arg + 1);
4334 if (eval6(arg, &var2, evaluate) == FAIL)
4335 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return FAIL;
4338 }
4339
4340 if (evaluate)
4341 {
4342 /*
4343 * Compute the result.
4344 */
4345 if (op == '.')
4346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4348 s2 = get_tv_string_buf_chk(&var2, buf2);
4349 if (s2 == NULL) /* type error ? */
4350 {
4351 clear_tv(rettv);
4352 clear_tv(&var2);
4353 return FAIL;
4354 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004355 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 clear_tv(rettv);
4357 rettv->v_type = VAR_STRING;
4358 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004360 else if (op == '+' && rettv->v_type == VAR_LIST
4361 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004362 {
4363 /* concatenate Lists */
4364 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4365 &var3) == FAIL)
4366 {
4367 clear_tv(rettv);
4368 clear_tv(&var2);
4369 return FAIL;
4370 }
4371 clear_tv(rettv);
4372 *rettv = var3;
4373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 else
4375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004376 int error = FALSE;
4377
4378 n1 = get_tv_number_chk(rettv, &error);
4379 if (error)
4380 {
4381 /* This can only happen for "list + non-list".
4382 * For "non-list + ..." or "something - ...", we returned
4383 * before evaluating the 2nd operand. */
4384 clear_tv(rettv);
4385 return FAIL;
4386 }
4387 n2 = get_tv_number_chk(&var2, &error);
4388 if (error)
4389 {
4390 clear_tv(rettv);
4391 clear_tv(&var2);
4392 return FAIL;
4393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (op == '+')
4396 n1 = n1 + n2;
4397 else
4398 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004399 rettv->v_type = VAR_NUMBER;
4400 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 }
4405 return OK;
4406}
4407
4408/*
4409 * Handle fifth level expression:
4410 * * number multiplication
4411 * / number division
4412 * % number modulo
4413 *
4414 * "arg" must point to the first non-white of the expression.
4415 * "arg" is advanced to the next non-white after the recognized expression.
4416 *
4417 * Return OK or FAIL.
4418 */
4419 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 int evaluate;
4424{
Bram Moolenaar33570922005-01-25 22:26:29 +00004425 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 int op;
4427 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004428 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
4430 /*
4431 * Get the first variable.
4432 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004433 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 return FAIL;
4435
4436 /*
4437 * Repeat computing, until no '*', '/' or '%' is following.
4438 */
4439 for (;;)
4440 {
4441 op = **arg;
4442 if (op != '*' && op != '/' && op != '%')
4443 break;
4444
4445 if (evaluate)
4446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004447 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004448 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004449 if (error)
4450 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452 else
4453 n1 = 0;
4454
4455 /*
4456 * Get the second variable.
4457 */
4458 *arg = skipwhite(*arg + 1);
4459 if (eval7(arg, &var2, evaluate) == FAIL)
4460 return FAIL;
4461
4462 if (evaluate)
4463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004464 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004466 if (error)
4467 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 /*
4470 * Compute the result.
4471 */
4472 if (op == '*')
4473 n1 = n1 * n2;
4474 else if (op == '/')
4475 {
4476 if (n2 == 0) /* give an error message? */
4477 n1 = 0x7fffffffL;
4478 else
4479 n1 = n1 / n2;
4480 }
4481 else
4482 {
4483 if (n2 == 0) /* give an error message? */
4484 n1 = 0;
4485 else
4486 n1 = n1 % n2;
4487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004488 rettv->v_type = VAR_NUMBER;
4489 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 }
4492
4493 return OK;
4494}
4495
4496/*
4497 * Handle sixth level expression:
4498 * number number constant
4499 * "string" string contstant
4500 * 'string' literal string contstant
4501 * &option-name option value
4502 * @r register contents
4503 * identifier variable value
4504 * function() function call
4505 * $VAR environment variable
4506 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004507 * [expr, expr] List
4508 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 *
4510 * Also handle:
4511 * ! in front logical NOT
4512 * - in front unary minus
4513 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 * trailing [] subscript in String or List
4515 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 *
4517 * "arg" must point to the first non-white of the expression.
4518 * "arg" is advanced to the next non-white after the recognized expression.
4519 *
4520 * Return OK or FAIL.
4521 */
4522 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 int evaluate;
4527{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 long n;
4529 int len;
4530 char_u *s;
4531 int val;
4532 char_u *start_leader, *end_leader;
4533 int ret = OK;
4534 char_u *alias;
4535
4536 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004537 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004538 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
4542 /*
4543 * Skip '!' and '-' characters. They are handled later.
4544 */
4545 start_leader = *arg;
4546 while (**arg == '!' || **arg == '-' || **arg == '+')
4547 *arg = skipwhite(*arg + 1);
4548 end_leader = *arg;
4549
4550 switch (**arg)
4551 {
4552 /*
4553 * Number constant.
4554 */
4555 case '0':
4556 case '1':
4557 case '2':
4558 case '3':
4559 case '4':
4560 case '5':
4561 case '6':
4562 case '7':
4563 case '8':
4564 case '9':
4565 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4566 *arg += len;
4567 if (evaluate)
4568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004569 rettv->v_type = VAR_NUMBER;
4570 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
4572 break;
4573
4574 /*
4575 * String constant: "string".
4576 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 break;
4579
4580 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004581 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584 break;
4585
4586 /*
4587 * List: [expr, expr]
4588 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004589 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 break;
4591
4592 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004593 * Dictionary: {key: val, key: val}
4594 */
4595 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4596 break;
4597
4598 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004599 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004601 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 break;
4603
4604 /*
4605 * Environment variable: $VAR.
4606 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 break;
4609
4610 /*
4611 * Register contents: @r.
4612 */
4613 case '@': ++*arg;
4614 if (evaluate)
4615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004617 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619 if (**arg != NUL)
4620 ++*arg;
4621 break;
4622
4623 /*
4624 * nested expression: (expression).
4625 */
4626 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (**arg == ')')
4629 ++*arg;
4630 else if (ret == OK)
4631 {
4632 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 ret = FAIL;
4635 }
4636 break;
4637
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 break;
4640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004641
4642 if (ret == NOTDONE)
4643 {
4644 /*
4645 * Must be a variable or function name.
4646 * Can also be a curly-braces kind of name: {expr}.
4647 */
4648 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004649 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004650 if (alias != NULL)
4651 s = alias;
4652
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004653 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004654 ret = FAIL;
4655 else
4656 {
4657 if (**arg == '(') /* recursive! */
4658 {
4659 /* If "s" is the name of a variable of type VAR_FUNC
4660 * use its contents. */
4661 s = deref_func_name(s, &len);
4662
4663 /* Invoke the function. */
4664 ret = get_func_tv(s, len, rettv, arg,
4665 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004666 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667 /* Stop the expression evaluation when immediately
4668 * aborting on error, or when an interrupt occurred or
4669 * an exception was thrown but not caught. */
4670 if (aborting())
4671 {
4672 if (ret == OK)
4673 clear_tv(rettv);
4674 ret = FAIL;
4675 }
4676 }
4677 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004678 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004679 else
4680 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 }
4682
4683 if (alias != NULL)
4684 vim_free(alias);
4685 }
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 *arg = skipwhite(*arg);
4688
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004689 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4690 * expr(expr). */
4691 if (ret == OK)
4692 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693
4694 /*
4695 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4696 */
4697 if (ret == OK && evaluate && end_leader > start_leader)
4698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 int error = FALSE;
4700
4701 val = get_tv_number_chk(rettv, &error);
4702 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 clear_tv(rettv);
4705 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 else
4708 {
4709 while (end_leader > start_leader)
4710 {
4711 --end_leader;
4712 if (*end_leader == '!')
4713 val = !val;
4714 else if (*end_leader == '-')
4715 val = -val;
4716 }
4717 clear_tv(rettv);
4718 rettv->v_type = VAR_NUMBER;
4719 rettv->vval.v_number = val;
4720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722
4723 return ret;
4724}
4725
4726/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004727 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4728 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4730 */
4731 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004732eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004734 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004736 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737{
4738 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004739 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 long len = -1;
4742 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004748 if (verbose)
4749 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 return FAIL;
4751 }
4752
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 /*
4756 * dict.name
4757 */
4758 key = *arg + 1;
4759 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4760 ;
4761 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004763 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 }
4765 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767 /*
4768 * something[idx]
4769 *
4770 * Get the (first) variable from inside the [].
4771 */
4772 *arg = skipwhite(*arg + 1);
4773 if (**arg == ':')
4774 empty1 = TRUE;
4775 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4776 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4778 {
4779 /* not a number or string */
4780 clear_tv(&var1);
4781 return FAIL;
4782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004783
4784 /*
4785 * Get the second variable from inside the [:].
4786 */
4787 if (**arg == ':')
4788 {
4789 range = TRUE;
4790 *arg = skipwhite(*arg + 1);
4791 if (**arg == ']')
4792 empty2 = TRUE;
4793 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 if (!empty1)
4796 clear_tv(&var1);
4797 return FAIL;
4798 }
4799 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4800 {
4801 /* not a number or string */
4802 if (!empty1)
4803 clear_tv(&var1);
4804 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 return FAIL;
4806 }
4807 }
4808
4809 /* Check for the ']'. */
4810 if (**arg != ']')
4811 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004812 if (verbose)
4813 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 clear_tv(&var1);
4815 if (range)
4816 clear_tv(&var2);
4817 return FAIL;
4818 }
4819 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 }
4821
4822 if (evaluate)
4823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004824 n1 = 0;
4825 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 n1 = get_tv_number(&var1);
4828 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 }
4830 if (range)
4831 {
4832 if (empty2)
4833 n2 = -1;
4834 else
4835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 n2 = get_tv_number(&var2);
4837 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 }
4839 }
4840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004842 {
4843 case VAR_NUMBER:
4844 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 len = (long)STRLEN(s);
4847 if (range)
4848 {
4849 /* The resulting variable is a substring. If the indexes
4850 * are out of range the result is empty. */
4851 if (n1 < 0)
4852 {
4853 n1 = len + n1;
4854 if (n1 < 0)
4855 n1 = 0;
4856 }
4857 if (n2 < 0)
4858 n2 = len + n2;
4859 else if (n2 >= len)
4860 n2 = len;
4861 if (n1 >= len || n2 < 0 || n1 > n2)
4862 s = NULL;
4863 else
4864 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4865 }
4866 else
4867 {
4868 /* The resulting variable is a string of a single
4869 * character. If the index is too big or negative the
4870 * result is empty. */
4871 if (n1 >= len || n1 < 0)
4872 s = NULL;
4873 else
4874 s = vim_strnsave(s + n1, 1);
4875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 clear_tv(rettv);
4877 rettv->v_type = VAR_STRING;
4878 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 break;
4880
4881 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883 if (n1 < 0)
4884 n1 = len + n1;
4885 if (!empty1 && (n1 < 0 || n1 >= len))
4886 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004887 /* For a range we allow invalid values and return an empty
4888 * list. A list index out of range is an error. */
4889 if (!range)
4890 {
4891 if (verbose)
4892 EMSGN(_(e_listidx), n1);
4893 return FAIL;
4894 }
4895 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004896 }
4897 if (range)
4898 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004899 list_T *l;
4900 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901
4902 if (n2 < 0)
4903 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004904 else if (n2 >= len)
4905 n2 = len - 1;
4906 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004907 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004908 l = list_alloc();
4909 if (l == NULL)
4910 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912 n1 <= n2; ++n1)
4913 {
4914 if (list_append_tv(l, &item->li_tv) == FAIL)
4915 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004916 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 return FAIL;
4918 }
4919 item = item->li_next;
4920 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 clear_tv(rettv);
4922 rettv->v_type = VAR_LIST;
4923 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004924 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004925 }
4926 else
4927 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004928 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 clear_tv(rettv);
4930 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004931 }
4932 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004933
4934 case VAR_DICT:
4935 if (range)
4936 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004937 if (verbose)
4938 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004939 if (len == -1)
4940 clear_tv(&var1);
4941 return FAIL;
4942 }
4943 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004944 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945
4946 if (len == -1)
4947 {
4948 key = get_tv_string(&var1);
4949 if (*key == NUL)
4950 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004951 if (verbose)
4952 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 clear_tv(&var1);
4954 return FAIL;
4955 }
4956 }
4957
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004958 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004960 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004961 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 if (len == -1)
4963 clear_tv(&var1);
4964 if (item == NULL)
4965 return FAIL;
4966
4967 copy_tv(&item->di_tv, &var1);
4968 clear_tv(rettv);
4969 *rettv = var1;
4970 }
4971 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004972 }
4973 }
4974
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004975 return OK;
4976}
4977
4978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 * Get an option value.
4980 * "arg" points to the '&' or '+' before the option name.
4981 * "arg" is advanced to character after the option name.
4982 * Return OK or FAIL.
4983 */
4984 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004985get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004987 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 int evaluate;
4989{
4990 char_u *option_end;
4991 long numval;
4992 char_u *stringval;
4993 int opt_type;
4994 int c;
4995 int working = (**arg == '+'); /* has("+option") */
4996 int ret = OK;
4997 int opt_flags;
4998
4999 /*
5000 * Isolate the option name and find its value.
5001 */
5002 option_end = find_option_end(arg, &opt_flags);
5003 if (option_end == NULL)
5004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 EMSG2(_("E112: Option name missing: %s"), *arg);
5007 return FAIL;
5008 }
5009
5010 if (!evaluate)
5011 {
5012 *arg = option_end;
5013 return OK;
5014 }
5015
5016 c = *option_end;
5017 *option_end = NUL;
5018 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005019 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
5021 if (opt_type == -3) /* invalid name */
5022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 EMSG2(_("E113: Unknown option: %s"), *arg);
5025 ret = FAIL;
5026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005027 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
5029 if (opt_type == -2) /* hidden string option */
5030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 rettv->v_type = VAR_STRING;
5032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034 else if (opt_type == -1) /* hidden number option */
5035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005036 rettv->v_type = VAR_NUMBER;
5037 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039 else if (opt_type == 1) /* number option */
5040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 rettv->v_type = VAR_NUMBER;
5042 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 else /* string option */
5045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046 rettv->v_type = VAR_STRING;
5047 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049 }
5050 else if (working && (opt_type == -2 || opt_type == -1))
5051 ret = FAIL;
5052
5053 *option_end = c; /* put back for error messages */
5054 *arg = option_end;
5055
5056 return ret;
5057}
5058
5059/*
5060 * Allocate a variable for a string constant.
5061 * Return OK or FAIL.
5062 */
5063 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 int evaluate;
5068{
5069 char_u *p;
5070 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 int extra = 0;
5072
5073 /*
5074 * Find the end of the string, skipping backslashed characters.
5075 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 if (*p == '\\' && p[1] != NUL)
5079 {
5080 ++p;
5081 /* A "\<x>" form occupies at least 4 characters, and produces up
5082 * to 6 characters: reserve space for 2 extra */
5083 if (*p == '<')
5084 extra += 2;
5085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087
5088 if (*p != '"')
5089 {
5090 EMSG2(_("E114: Missing quote: %s"), *arg);
5091 return FAIL;
5092 }
5093
5094 /* If only parsing, set *arg and return here */
5095 if (!evaluate)
5096 {
5097 *arg = p + 1;
5098 return OK;
5099 }
5100
5101 /*
5102 * Copy the string into allocated memory, handling backslashed
5103 * characters.
5104 */
5105 name = alloc((unsigned)(p - *arg + extra));
5106 if (name == NULL)
5107 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 rettv->v_type = VAR_STRING;
5109 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 if (*p == '\\')
5114 {
5115 switch (*++p)
5116 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 case 'b': *name++ = BS; ++p; break;
5118 case 'e': *name++ = ESC; ++p; break;
5119 case 'f': *name++ = FF; ++p; break;
5120 case 'n': *name++ = NL; ++p; break;
5121 case 'r': *name++ = CAR; ++p; break;
5122 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 case 'X': /* hex: "\x1", "\x12" */
5125 case 'x':
5126 case 'u': /* Unicode: "\u0023" */
5127 case 'U':
5128 if (vim_isxdigit(p[1]))
5129 {
5130 int n, nr;
5131 int c = toupper(*p);
5132
5133 if (c == 'X')
5134 n = 2;
5135 else
5136 n = 4;
5137 nr = 0;
5138 while (--n >= 0 && vim_isxdigit(p[1]))
5139 {
5140 ++p;
5141 nr = (nr << 4) + hex2nr(*p);
5142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144#ifdef FEAT_MBYTE
5145 /* For "\u" store the number according to
5146 * 'encoding'. */
5147 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 else
5150#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154
5155 /* octal: "\1", "\12", "\123" */
5156 case '0':
5157 case '1':
5158 case '2':
5159 case '3':
5160 case '4':
5161 case '5':
5162 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 case '7': *name = *p++ - '0';
5164 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 *name = (*name << 3) + *p++ - '0';
5167 if (*p >= '0' && *p <= '7')
5168 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172
5173 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (extra != 0)
5176 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 break;
5179 }
5180 /* FALLTHROUGH */
5181
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184 }
5185 }
5186 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005190 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 *arg = p + 1;
5192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 return OK;
5194}
5195
5196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 * Return OK or FAIL.
5199 */
5200 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 int evaluate;
5205{
5206 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005207 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005208 int reduce = 0;
5209
5210 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005212 */
5213 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5214 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005216 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005218 break;
5219 ++reduce;
5220 ++p;
5221 }
5222 }
5223
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005225 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005227 return FAIL;
5228 }
5229
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005231 if (!evaluate)
5232 {
5233 *arg = p + 1;
5234 return OK;
5235 }
5236
5237 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005239 */
5240 str = alloc((unsigned)((p - *arg) - reduce));
5241 if (str == NULL)
5242 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 rettv->v_type = VAR_STRING;
5244 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005247 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005251 break;
5252 ++p;
5253 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005255 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005257 *arg = p + 1;
5258
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005259 return OK;
5260}
5261
5262/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 * Allocate a variable for a List and fill it from "*arg".
5264 * Return OK or FAIL.
5265 */
5266 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005267get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005269 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 int evaluate;
5271{
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 list_T *l = NULL;
5273 typval_T tv;
5274 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275
5276 if (evaluate)
5277 {
5278 l = list_alloc();
5279 if (l == NULL)
5280 return FAIL;
5281 }
5282
5283 *arg = skipwhite(*arg + 1);
5284 while (**arg != ']' && **arg != NUL)
5285 {
5286 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5287 goto failret;
5288 if (evaluate)
5289 {
5290 item = listitem_alloc();
5291 if (item != NULL)
5292 {
5293 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005294 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 list_append(l, item);
5296 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005297 else
5298 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 }
5300
5301 if (**arg == ']')
5302 break;
5303 if (**arg != ',')
5304 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 goto failret;
5307 }
5308 *arg = skipwhite(*arg + 1);
5309 }
5310
5311 if (**arg != ']')
5312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314failret:
5315 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005316 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 return FAIL;
5318 }
5319
5320 *arg = skipwhite(*arg + 1);
5321 if (evaluate)
5322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005323 rettv->v_type = VAR_LIST;
5324 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 ++l->lv_refcount;
5326 }
5327
5328 return OK;
5329}
5330
5331/*
5332 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005333 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005335 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336list_alloc()
5337{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005338 list_T *l;
5339
5340 l = (list_T *)alloc_clear(sizeof(list_T));
5341 if (l != NULL)
5342 {
5343 /* Prepend the list to the list of lists for garbage collection. */
5344 if (first_list != NULL)
5345 first_list->lv_used_prev = l;
5346 l->lv_used_prev = NULL;
5347 l->lv_used_next = first_list;
5348 first_list = l;
5349 }
5350 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351}
5352
5353/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005354 * Allocate an empty list for a return value.
5355 * Returns OK or FAIL.
5356 */
5357 static int
5358rettv_list_alloc(rettv)
5359 typval_T *rettv;
5360{
5361 list_T *l = list_alloc();
5362
5363 if (l == NULL)
5364 return FAIL;
5365
5366 rettv->vval.v_list = l;
5367 rettv->v_type = VAR_LIST;
5368 ++l->lv_refcount;
5369 return OK;
5370}
5371
5372/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 * Unreference a list: decrement the reference count and free it when it
5374 * becomes zero.
5375 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005376 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005378 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005380 if (l != NULL && --l->lv_refcount <= 0)
5381 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382}
5383
5384/*
5385 * Free a list, including all items it points to.
5386 * Ignores the reference count.
5387 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005388 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005389list_free(l, recurse)
5390 list_T *l;
5391 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392{
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005395 /* Remove the list from the list of lists for garbage collection. */
5396 if (l->lv_used_prev == NULL)
5397 first_list = l->lv_used_next;
5398 else
5399 l->lv_used_prev->lv_used_next = l->lv_used_next;
5400 if (l->lv_used_next != NULL)
5401 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5402
Bram Moolenaard9fba312005-06-26 22:34:35 +00005403 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005405 /* Remove the item before deleting it. */
5406 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005407 if (recurse || (item->li_tv.v_type != VAR_LIST
5408 && item->li_tv.v_type != VAR_DICT))
5409 clear_tv(&item->li_tv);
5410 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 }
5412 vim_free(l);
5413}
5414
5415/*
5416 * Allocate a list item.
5417 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005418 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419listitem_alloc()
5420{
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422}
5423
5424/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005425 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 */
5427 static void
5428listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005431 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 vim_free(item);
5433}
5434
5435/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005436 * Remove a list item from a List and free it. Also clears the value.
5437 */
5438 static void
5439listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 list_T *l;
5441 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005442{
5443 list_remove(l, item, item);
5444 listitem_free(item);
5445}
5446
5447/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 * Get the number of items in a list.
5449 */
5450 static long
5451list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 if (l == NULL)
5455 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005456 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457}
5458
5459/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005460 * Return TRUE when two lists have exactly the same values.
5461 */
5462 static int
5463list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 list_T *l1;
5465 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005466 int ic; /* ignore case for strings */
5467{
Bram Moolenaar33570922005-01-25 22:26:29 +00005468 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005469
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005470 if (l1 == l2)
5471 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005472 if (list_len(l1) != list_len(l2))
5473 return FALSE;
5474
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005475 for (item1 = l1->lv_first, item2 = l2->lv_first;
5476 item1 != NULL && item2 != NULL;
5477 item1 = item1->li_next, item2 = item2->li_next)
5478 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5479 return FALSE;
5480 return item1 == NULL && item2 == NULL;
5481}
5482
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005483#if defined(FEAT_PYTHON) || defined(PROTO)
5484/*
5485 * Return the dictitem that an entry in a hashtable points to.
5486 */
5487 dictitem_T *
5488dict_lookup(hi)
5489 hashitem_T *hi;
5490{
5491 return HI2DI(hi);
5492}
5493#endif
5494
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005495/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005496 * Return TRUE when two dictionaries have exactly the same key/values.
5497 */
5498 static int
5499dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 dict_T *d1;
5501 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005502 int ic; /* ignore case for strings */
5503{
Bram Moolenaar33570922005-01-25 22:26:29 +00005504 hashitem_T *hi;
5505 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005506 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005507
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005508 if (d1 == d2)
5509 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005510 if (dict_len(d1) != dict_len(d2))
5511 return FALSE;
5512
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005513 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 if (!HASHITEM_EMPTY(hi))
5517 {
5518 item2 = dict_find(d2, hi->hi_key, -1);
5519 if (item2 == NULL)
5520 return FALSE;
5521 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5522 return FALSE;
5523 --todo;
5524 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005525 }
5526 return TRUE;
5527}
5528
5529/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005530 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005531 * Compares the items just like "==" would compare them, but strings and
5532 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005533 */
5534 static int
5535tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005536 typval_T *tv1;
5537 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005538 int ic; /* ignore case */
5539{
5540 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005541 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005542 static int recursive = 0; /* cach recursive loops */
5543 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005544
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005545 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005546 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005547 /* Catch lists and dicts that have an endless loop by limiting
5548 * recursiveness to 1000. We guess they are equal then. */
5549 if (recursive >= 1000)
5550 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005551
5552 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005553 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005554 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005555 ++recursive;
5556 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5557 --recursive;
5558 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005559
5560 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005561 ++recursive;
5562 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5563 --recursive;
5564 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005565
5566 case VAR_FUNC:
5567 return (tv1->vval.v_string != NULL
5568 && tv2->vval.v_string != NULL
5569 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5570
5571 case VAR_NUMBER:
5572 return tv1->vval.v_number == tv2->vval.v_number;
5573
5574 case VAR_STRING:
5575 s1 = get_tv_string_buf(tv1, buf1);
5576 s2 = get_tv_string_buf(tv2, buf2);
5577 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005578 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005579
5580 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005581 return TRUE;
5582}
5583
5584/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 * Locate item with index "n" in list "l" and return it.
5586 * A negative index is counted from the end; -1 is the last item.
5587 * Returns NULL when "n" is out of range.
5588 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005589 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005592 long n;
5593{
Bram Moolenaar33570922005-01-25 22:26:29 +00005594 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 long idx;
5596
5597 if (l == NULL)
5598 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005599
5600 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005602 n = l->lv_len + n;
5603
5604 /* Check for index out of range. */
5605 if (n < 0 || n >= l->lv_len)
5606 return NULL;
5607
5608 /* When there is a cached index may start search from there. */
5609 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005611 if (n < l->lv_idx / 2)
5612 {
5613 /* closest to the start of the list */
5614 item = l->lv_first;
5615 idx = 0;
5616 }
5617 else if (n > (l->lv_idx + l->lv_len) / 2)
5618 {
5619 /* closest to the end of the list */
5620 item = l->lv_last;
5621 idx = l->lv_len - 1;
5622 }
5623 else
5624 {
5625 /* closest to the cached index */
5626 item = l->lv_idx_item;
5627 idx = l->lv_idx;
5628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005629 }
5630 else
5631 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005632 if (n < l->lv_len / 2)
5633 {
5634 /* closest to the start of the list */
5635 item = l->lv_first;
5636 idx = 0;
5637 }
5638 else
5639 {
5640 /* closest to the end of the list */
5641 item = l->lv_last;
5642 idx = l->lv_len - 1;
5643 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005645
5646 while (n > idx)
5647 {
5648 /* search forward */
5649 item = item->li_next;
5650 ++idx;
5651 }
5652 while (n < idx)
5653 {
5654 /* search backward */
5655 item = item->li_prev;
5656 --idx;
5657 }
5658
5659 /* cache the used index */
5660 l->lv_idx = idx;
5661 l->lv_idx_item = item;
5662
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005663 return item;
5664}
5665
5666/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005667 * Get list item "l[idx]" as a number.
5668 */
5669 static long
5670list_find_nr(l, idx, errorp)
5671 list_T *l;
5672 long idx;
5673 int *errorp; /* set to TRUE when something wrong */
5674{
5675 listitem_T *li;
5676
5677 li = list_find(l, idx);
5678 if (li == NULL)
5679 {
5680 if (errorp != NULL)
5681 *errorp = TRUE;
5682 return -1L;
5683 }
5684 return get_tv_number_chk(&li->li_tv, errorp);
5685}
5686
5687/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005688 * Locate "item" list "l" and return its index.
5689 * Returns -1 when "item" is not in the list.
5690 */
5691 static long
5692list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 list_T *l;
5694 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005695{
5696 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005697 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005698
5699 if (l == NULL)
5700 return -1;
5701 idx = 0;
5702 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5703 ++idx;
5704 if (li == NULL)
5705 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005706 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005707}
5708
5709/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005710 * Append item "item" to the end of list "l".
5711 */
5712 static void
5713list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005714 list_T *l;
5715 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716{
5717 if (l->lv_last == NULL)
5718 {
5719 /* empty list */
5720 l->lv_first = item;
5721 l->lv_last = item;
5722 item->li_prev = NULL;
5723 }
5724 else
5725 {
5726 l->lv_last->li_next = item;
5727 item->li_prev = l->lv_last;
5728 l->lv_last = item;
5729 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005730 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731 item->li_next = NULL;
5732}
5733
5734/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005735 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005736 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 */
5738 static int
5739list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005740 list_T *l;
5741 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005743 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744
Bram Moolenaar05159a02005-02-26 23:04:13 +00005745 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005747 copy_tv(tv, &li->li_tv);
5748 list_append(l, li);
5749 return OK;
5750}
5751
5752/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005753 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005754 * Return FAIL when out of memory.
5755 */
5756 int
5757list_append_dict(list, dict)
5758 list_T *list;
5759 dict_T *dict;
5760{
5761 listitem_T *li = listitem_alloc();
5762
5763 if (li == NULL)
5764 return FAIL;
5765 li->li_tv.v_type = VAR_DICT;
5766 li->li_tv.v_lock = 0;
5767 li->li_tv.vval.v_dict = dict;
5768 list_append(list, li);
5769 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 return OK;
5771}
5772
5773/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005774 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005775 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005776 * Returns FAIL when out of memory.
5777 */
5778 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005779list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005780 list_T *l;
5781 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005782 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005783{
5784 listitem_T *li = listitem_alloc();
5785
5786 if (li == NULL)
5787 return FAIL;
5788 list_append(l, li);
5789 li->li_tv.v_type = VAR_STRING;
5790 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005791 if (str == NULL)
5792 li->li_tv.vval.v_string = NULL;
5793 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005794 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005795 return FAIL;
5796 return OK;
5797}
5798
5799/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005800 * Append "n" to list "l".
5801 * Returns FAIL when out of memory.
5802 */
5803 static int
5804list_append_number(l, n)
5805 list_T *l;
5806 varnumber_T n;
5807{
5808 listitem_T *li;
5809
5810 li = listitem_alloc();
5811 if (li == NULL)
5812 return FAIL;
5813 li->li_tv.v_type = VAR_NUMBER;
5814 li->li_tv.v_lock = 0;
5815 li->li_tv.vval.v_number = n;
5816 list_append(l, li);
5817 return OK;
5818}
5819
5820/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005822 * If "item" is NULL append at the end.
5823 * Return FAIL when out of memory.
5824 */
5825 static int
5826list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 list_T *l;
5828 typval_T *tv;
5829 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005830{
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005832
5833 if (ni == NULL)
5834 return FAIL;
5835 copy_tv(tv, &ni->li_tv);
5836 if (item == NULL)
5837 /* Append new item at end of list. */
5838 list_append(l, ni);
5839 else
5840 {
5841 /* Insert new item before existing item. */
5842 ni->li_prev = item->li_prev;
5843 ni->li_next = item;
5844 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005845 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005846 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005847 ++l->lv_idx;
5848 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005849 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005850 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005851 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005852 l->lv_idx_item = NULL;
5853 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005854 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005855 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005856 }
5857 return OK;
5858}
5859
5860/*
5861 * Extend "l1" with "l2".
5862 * If "bef" is NULL append at the end, otherwise insert before this item.
5863 * Returns FAIL when out of memory.
5864 */
5865 static int
5866list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005867 list_T *l1;
5868 list_T *l2;
5869 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005870{
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005872
5873 for (item = l2->lv_first; item != NULL; item = item->li_next)
5874 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5875 return FAIL;
5876 return OK;
5877}
5878
5879/*
5880 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5881 * Return FAIL when out of memory.
5882 */
5883 static int
5884list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *l1;
5886 list_T *l2;
5887 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005888{
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005890
5891 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005892 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005893 if (l == NULL)
5894 return FAIL;
5895 tv->v_type = VAR_LIST;
5896 tv->vval.v_list = l;
5897
5898 /* append all items from the second list */
5899 return list_extend(l, l2, NULL);
5900}
5901
5902/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005903 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005905 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 * Returns NULL when out of memory.
5907 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005908 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005909list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005910 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005912 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913{
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *copy;
5915 listitem_T *item;
5916 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917
5918 if (orig == NULL)
5919 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920
5921 copy = list_alloc();
5922 if (copy != NULL)
5923 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005924 if (copyID != 0)
5925 {
5926 /* Do this before adding the items, because one of the items may
5927 * refer back to this list. */
5928 orig->lv_copyID = copyID;
5929 orig->lv_copylist = copy;
5930 }
5931 for (item = orig->lv_first; item != NULL && !got_int;
5932 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 {
5934 ni = listitem_alloc();
5935 if (ni == NULL)
5936 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005937 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005938 {
5939 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5940 {
5941 vim_free(ni);
5942 break;
5943 }
5944 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005946 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 list_append(copy, ni);
5948 }
5949 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005950 if (item != NULL)
5951 {
5952 list_unref(copy);
5953 copy = NULL;
5954 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 }
5956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 return copy;
5958}
5959
5960/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005961 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005962 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005965list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 list_T *l;
5967 listitem_T *item;
5968 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005969{
Bram Moolenaar33570922005-01-25 22:26:29 +00005970 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005972 /* notify watchers */
5973 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005975 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005976 list_fix_watch(l, ip);
5977 if (ip == item2)
5978 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005980
5981 if (item2->li_next == NULL)
5982 l->lv_last = item->li_prev;
5983 else
5984 item2->li_next->li_prev = item->li_prev;
5985 if (item->li_prev == NULL)
5986 l->lv_first = item2->li_next;
5987 else
5988 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005989 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990}
5991
5992/*
5993 * Return an allocated string with the string representation of a list.
5994 * May return NULL.
5995 */
5996 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005997list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005999 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
6001 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002
6003 if (tv->vval.v_list == NULL)
6004 return NULL;
6005 ga_init2(&ga, (int)sizeof(char), 80);
6006 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006008 {
6009 vim_free(ga.ga_data);
6010 return NULL;
6011 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 ga_append(&ga, ']');
6013 ga_append(&ga, NUL);
6014 return (char_u *)ga.ga_data;
6015}
6016
6017/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006018 * Join list "l" into a string in "*gap", using separator "sep".
6019 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006020 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006021 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006022 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006023list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006024 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006026 char_u *sep;
6027 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006028 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006029{
6030 int first = TRUE;
6031 char_u *tofree;
6032 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006034 char_u *s;
6035
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006036 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006037 {
6038 if (first)
6039 first = FALSE;
6040 else
6041 ga_concat(gap, sep);
6042
6043 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006044 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006045 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006046 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006047 if (s != NULL)
6048 ga_concat(gap, s);
6049 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006050 if (s == NULL)
6051 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006052 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054}
6055
6056/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006057 * Garbage collection for lists and dictionaries.
6058 *
6059 * We use reference counts to be able to free most items right away when they
6060 * are no longer used. But for composite items it's possible that it becomes
6061 * unused while the reference count is > 0: When there is a recursive
6062 * reference. Example:
6063 * :let l = [1, 2, 3]
6064 * :let d = {9: l}
6065 * :let l[1] = d
6066 *
6067 * Since this is quite unusual we handle this with garbage collection: every
6068 * once in a while find out which lists and dicts are not referenced from any
6069 * variable.
6070 *
6071 * Here is a good reference text about garbage collection (refers to Python
6072 * but it applies to all reference-counting mechanisms):
6073 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006074 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006075
6076/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006077 * Do garbage collection for lists and dicts.
6078 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006079 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006080 int
6081garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006082{
6083 dict_T *dd;
6084 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006085 int copyID = ++current_copyID;
6086 buf_T *buf;
6087 win_T *wp;
6088 int i;
6089 funccall_T *fc;
6090 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006091#ifdef FEAT_WINDOWS
6092 tabpage_T *tp;
6093#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006094
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006095 /* Only do this once. */
6096 want_garbage_collect = FALSE;
6097 may_garbage_collect = FALSE;
6098
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006099 /*
6100 * 1. Go through all accessible variables and mark all lists and dicts
6101 * with copyID.
6102 */
6103 /* script-local variables */
6104 for (i = 1; i <= ga_scripts.ga_len; ++i)
6105 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6106
6107 /* buffer-local variables */
6108 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6109 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6110
6111 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006112 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6114
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006115#ifdef FEAT_WINDOWS
6116 /* tabpage-local variables */
6117 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6118 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6119#endif
6120
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 /* global variables */
6122 set_ref_in_ht(&globvarht, copyID);
6123
6124 /* function-local variables */
6125 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6126 {
6127 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6128 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6129 }
6130
6131 /*
6132 * 2. Go through the list of dicts and free items without the copyID.
6133 */
6134 for (dd = first_dict; dd != NULL; )
6135 if (dd->dv_copyID != copyID)
6136 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006137 /* Free the Dictionary and ordinary items it contains, but don't
6138 * recurse into Lists and Dictionaries, they will be in the list
6139 * of dicts or list of lists. */
6140 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006141 did_free = TRUE;
6142
6143 /* restart, next dict may also have been freed */
6144 dd = first_dict;
6145 }
6146 else
6147 dd = dd->dv_used_next;
6148
6149 /*
6150 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006151 * But don't free a list that has a watcher (used in a for loop), these
6152 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006153 */
6154 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006155 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006156 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006157 /* Free the List and ordinary items it contains, but don't recurse
6158 * into Lists and Dictionaries, they will be in the list of dicts
6159 * or list of lists. */
6160 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006161 did_free = TRUE;
6162
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006163 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006164 ll = first_list;
6165 }
6166 else
6167 ll = ll->lv_used_next;
6168
6169 return did_free;
6170}
6171
6172/*
6173 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6174 */
6175 static void
6176set_ref_in_ht(ht, copyID)
6177 hashtab_T *ht;
6178 int copyID;
6179{
6180 int todo;
6181 hashitem_T *hi;
6182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006183 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006184 for (hi = ht->ht_array; todo > 0; ++hi)
6185 if (!HASHITEM_EMPTY(hi))
6186 {
6187 --todo;
6188 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6189 }
6190}
6191
6192/*
6193 * Mark all lists and dicts referenced through list "l" with "copyID".
6194 */
6195 static void
6196set_ref_in_list(l, copyID)
6197 list_T *l;
6198 int copyID;
6199{
6200 listitem_T *li;
6201
6202 for (li = l->lv_first; li != NULL; li = li->li_next)
6203 set_ref_in_item(&li->li_tv, copyID);
6204}
6205
6206/*
6207 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6208 */
6209 static void
6210set_ref_in_item(tv, copyID)
6211 typval_T *tv;
6212 int copyID;
6213{
6214 dict_T *dd;
6215 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006216
6217 switch (tv->v_type)
6218 {
6219 case VAR_DICT:
6220 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006221 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006222 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006223 /* Didn't see this dict yet. */
6224 dd->dv_copyID = copyID;
6225 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006226 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006227 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006228
6229 case VAR_LIST:
6230 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006231 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006232 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006233 /* Didn't see this list yet. */
6234 ll->lv_copyID = copyID;
6235 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006236 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006237 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006238 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006239 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006240}
6241
6242/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006243 * Allocate an empty header for a dictionary.
6244 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006245 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006246dict_alloc()
6247{
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249
Bram Moolenaar33570922005-01-25 22:26:29 +00006250 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006252 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006253 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006254 if (first_dict != NULL)
6255 first_dict->dv_used_prev = d;
6256 d->dv_used_next = first_dict;
6257 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006258 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006259
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006261 d->dv_lock = 0;
6262 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006263 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006264 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006265 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006266}
6267
6268/*
6269 * Unreference a Dictionary: decrement the reference count and free it when it
6270 * becomes zero.
6271 */
6272 static void
6273dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006275{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006276 if (d != NULL && --d->dv_refcount <= 0)
6277 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006278}
6279
6280/*
6281 * Free a Dictionary, including all items it contains.
6282 * Ignores the reference count.
6283 */
6284 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006285dict_free(d, recurse)
6286 dict_T *d;
6287 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006288{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006291 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006292
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006293 /* Remove the dict from the list of dicts for garbage collection. */
6294 if (d->dv_used_prev == NULL)
6295 first_dict = d->dv_used_next;
6296 else
6297 d->dv_used_prev->dv_used_next = d->dv_used_next;
6298 if (d->dv_used_next != NULL)
6299 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6300
6301 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006302 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006303 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006306 if (!HASHITEM_EMPTY(hi))
6307 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006308 /* Remove the item before deleting it, just in case there is
6309 * something recursive causing trouble. */
6310 di = HI2DI(hi);
6311 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006312 if (recurse || (di->di_tv.v_type != VAR_LIST
6313 && di->di_tv.v_type != VAR_DICT))
6314 clear_tv(&di->di_tv);
6315 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316 --todo;
6317 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006318 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006319 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006320 vim_free(d);
6321}
6322
6323/*
6324 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 * The "key" is copied to the new item.
6326 * Note that the value of the item "di_tv" still needs to be initialized!
6327 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006328 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330dictitem_alloc(key)
6331 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006332{
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006335 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006336 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006337 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006338 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006339 di->di_flags = 0;
6340 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006341 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342}
6343
6344/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006345 * Make a copy of a Dictionary item.
6346 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006348dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350{
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006353 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6354 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355 if (di != NULL)
6356 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006357 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006358 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359 copy_tv(&org->di_tv, &di->di_tv);
6360 }
6361 return di;
6362}
6363
6364/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365 * Remove item "item" from Dictionary "dict" and free it.
6366 */
6367 static void
6368dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 dict_T *dict;
6370 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371{
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 if (HASHITEM_EMPTY(hi))
6376 EMSG2(_(e_intern2), "dictitem_remove()");
6377 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 dictitem_free(item);
6380}
6381
6382/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006383 * Free a dict item. Also clears the value.
6384 */
6385 static void
6386dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006388{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006389 clear_tv(&item->di_tv);
6390 vim_free(item);
6391}
6392
6393/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6395 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006396 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 * Returns NULL when out of memory.
6398 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006402 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006403 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006404{
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 dict_T *copy;
6406 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006409
6410 if (orig == NULL)
6411 return NULL;
6412
6413 copy = dict_alloc();
6414 if (copy != NULL)
6415 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006416 if (copyID != 0)
6417 {
6418 orig->dv_copyID = copyID;
6419 orig->dv_copydict = copy;
6420 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006421 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006422 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006423 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006424 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006425 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006426 --todo;
6427
6428 di = dictitem_alloc(hi->hi_key);
6429 if (di == NULL)
6430 break;
6431 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006432 {
6433 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6434 copyID) == FAIL)
6435 {
6436 vim_free(di);
6437 break;
6438 }
6439 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006440 else
6441 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6442 if (dict_add(copy, di) == FAIL)
6443 {
6444 dictitem_free(di);
6445 break;
6446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006448 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006449
Bram Moolenaare9a41262005-01-15 22:18:47 +00006450 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 if (todo > 0)
6452 {
6453 dict_unref(copy);
6454 copy = NULL;
6455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 }
6457
6458 return copy;
6459}
6460
6461/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006462 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006463 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006464 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006465 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006466dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006467 dict_T *d;
6468 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006469{
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006471}
6472
Bram Moolenaar8c711452005-01-14 21:53:12 +00006473/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006474 * Add a number or string entry to dictionary "d".
6475 * When "str" is NULL use number "nr", otherwise use "str".
6476 * Returns FAIL when out of memory and when key already exists.
6477 */
6478 int
6479dict_add_nr_str(d, key, nr, str)
6480 dict_T *d;
6481 char *key;
6482 long nr;
6483 char_u *str;
6484{
6485 dictitem_T *item;
6486
6487 item = dictitem_alloc((char_u *)key);
6488 if (item == NULL)
6489 return FAIL;
6490 item->di_tv.v_lock = 0;
6491 if (str == NULL)
6492 {
6493 item->di_tv.v_type = VAR_NUMBER;
6494 item->di_tv.vval.v_number = nr;
6495 }
6496 else
6497 {
6498 item->di_tv.v_type = VAR_STRING;
6499 item->di_tv.vval.v_string = vim_strsave(str);
6500 }
6501 if (dict_add(d, item) == FAIL)
6502 {
6503 dictitem_free(item);
6504 return FAIL;
6505 }
6506 return OK;
6507}
6508
6509/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 * Get the number of items in a Dictionary.
6511 */
6512 static long
6513dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 if (d == NULL)
6517 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006518 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006519}
6520
6521/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006522 * Find item "key[len]" in Dictionary "d".
6523 * If "len" is negative use strlen(key).
6524 * Returns NULL when not found.
6525 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006526 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006527dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006528 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529 char_u *key;
6530 int len;
6531{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006532#define AKEYLEN 200
6533 char_u buf[AKEYLEN];
6534 char_u *akey;
6535 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006537
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006538 if (len < 0)
6539 akey = key;
6540 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006541 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006542 tofree = akey = vim_strnsave(key, len);
6543 if (akey == NULL)
6544 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006545 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006546 else
6547 {
6548 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006549 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006550 akey = buf;
6551 }
6552
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006554 vim_free(tofree);
6555 if (HASHITEM_EMPTY(hi))
6556 return NULL;
6557 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006558}
6559
6560/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006561 * Get a string item from a dictionary.
6562 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006563 * Returns NULL if the entry doesn't exist or out of memory.
6564 */
6565 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006566get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006567 dict_T *d;
6568 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006569 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006570{
6571 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006572 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006573
6574 di = dict_find(d, key, -1);
6575 if (di == NULL)
6576 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006577 s = get_tv_string(&di->di_tv);
6578 if (save && s != NULL)
6579 s = vim_strsave(s);
6580 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006581}
6582
6583/*
6584 * Get a number item from a dictionary.
6585 * Returns 0 if the entry doesn't exist or out of memory.
6586 */
6587 long
6588get_dict_number(d, key)
6589 dict_T *d;
6590 char_u *key;
6591{
6592 dictitem_T *di;
6593
6594 di = dict_find(d, key, -1);
6595 if (di == NULL)
6596 return 0;
6597 return get_tv_number(&di->di_tv);
6598}
6599
6600/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601 * Return an allocated string with the string representation of a Dictionary.
6602 * May return NULL.
6603 */
6604 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006605dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006606 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006607 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608{
6609 garray_T ga;
6610 int first = TRUE;
6611 char_u *tofree;
6612 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006613 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006614 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006616 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006618 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006619 return NULL;
6620 ga_init2(&ga, (int)sizeof(char), 80);
6621 ga_append(&ga, '{');
6622
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006623 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006624 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006625 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006626 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006627 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006628 --todo;
6629
6630 if (first)
6631 first = FALSE;
6632 else
6633 ga_concat(&ga, (char_u *)", ");
6634
6635 tofree = string_quote(hi->hi_key, FALSE);
6636 if (tofree != NULL)
6637 {
6638 ga_concat(&ga, tofree);
6639 vim_free(tofree);
6640 }
6641 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006642 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006643 if (s != NULL)
6644 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006645 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006646 if (s == NULL)
6647 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006649 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006650 if (todo > 0)
6651 {
6652 vim_free(ga.ga_data);
6653 return NULL;
6654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655
6656 ga_append(&ga, '}');
6657 ga_append(&ga, NUL);
6658 return (char_u *)ga.ga_data;
6659}
6660
6661/*
6662 * Allocate a variable for a Dictionary and fill it from "*arg".
6663 * Return OK or FAIL. Returns NOTDONE for {expr}.
6664 */
6665 static int
6666get_dict_tv(arg, rettv, evaluate)
6667 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 int evaluate;
6670{
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 dict_T *d = NULL;
6672 typval_T tvkey;
6673 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006674 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006675 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006676 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006677 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678
6679 /*
6680 * First check if it's not a curly-braces thing: {expr}.
6681 * Must do this without evaluating, otherwise a function may be called
6682 * twice. Unfortunately this means we need to call eval1() twice for the
6683 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006684 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006686 if (*start != '}')
6687 {
6688 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6689 return FAIL;
6690 if (*start == '}')
6691 return NOTDONE;
6692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006693
6694 if (evaluate)
6695 {
6696 d = dict_alloc();
6697 if (d == NULL)
6698 return FAIL;
6699 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006700 tvkey.v_type = VAR_UNKNOWN;
6701 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006702
6703 *arg = skipwhite(*arg + 1);
6704 while (**arg != '}' && **arg != NUL)
6705 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006706 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707 goto failret;
6708 if (**arg != ':')
6709 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006710 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006711 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712 goto failret;
6713 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006714 key = get_tv_string_buf_chk(&tvkey, buf);
6715 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006717 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6718 if (key != NULL)
6719 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006720 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006721 goto failret;
6722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006723
6724 *arg = skipwhite(*arg + 1);
6725 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6726 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006727 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006728 goto failret;
6729 }
6730 if (evaluate)
6731 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006732 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006733 if (item != NULL)
6734 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006735 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006736 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006737 clear_tv(&tv);
6738 goto failret;
6739 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006740 item = dictitem_alloc(key);
6741 clear_tv(&tvkey);
6742 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006744 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006745 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006746 if (dict_add(d, item) == FAIL)
6747 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748 }
6749 }
6750
6751 if (**arg == '}')
6752 break;
6753 if (**arg != ',')
6754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006755 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006756 goto failret;
6757 }
6758 *arg = skipwhite(*arg + 1);
6759 }
6760
6761 if (**arg != '}')
6762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006763 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006764failret:
6765 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006766 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006767 return FAIL;
6768 }
6769
6770 *arg = skipwhite(*arg + 1);
6771 if (evaluate)
6772 {
6773 rettv->v_type = VAR_DICT;
6774 rettv->vval.v_dict = d;
6775 ++d->dv_refcount;
6776 }
6777
6778 return OK;
6779}
6780
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782 * Return a string with the string representation of a variable.
6783 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006784 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006785 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006786 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006787 * May return NULL;
6788 */
6789 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006790echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006793 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006794 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006795{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006796 static int recurse = 0;
6797 char_u *r = NULL;
6798
Bram Moolenaar33570922005-01-25 22:26:29 +00006799 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006800 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006801 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 *tofree = NULL;
6803 return NULL;
6804 }
6805 ++recurse;
6806
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006807 switch (tv->v_type)
6808 {
6809 case VAR_FUNC:
6810 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006811 r = tv->vval.v_string;
6812 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006813
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006814 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006815 if (tv->vval.v_list == NULL)
6816 {
6817 *tofree = NULL;
6818 r = NULL;
6819 }
6820 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6821 {
6822 *tofree = NULL;
6823 r = (char_u *)"[...]";
6824 }
6825 else
6826 {
6827 tv->vval.v_list->lv_copyID = copyID;
6828 *tofree = list2string(tv, copyID);
6829 r = *tofree;
6830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006831 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006832
Bram Moolenaar8c711452005-01-14 21:53:12 +00006833 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006834 if (tv->vval.v_dict == NULL)
6835 {
6836 *tofree = NULL;
6837 r = NULL;
6838 }
6839 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6840 {
6841 *tofree = NULL;
6842 r = (char_u *)"{...}";
6843 }
6844 else
6845 {
6846 tv->vval.v_dict->dv_copyID = copyID;
6847 *tofree = dict2string(tv, copyID);
6848 r = *tofree;
6849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006852 case VAR_STRING:
6853 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006854 *tofree = NULL;
6855 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006858 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006859 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006862
6863 --recurse;
6864 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006865}
6866
6867/*
6868 * Return a string with the string representation of a variable.
6869 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6870 * "numbuf" is used for a number.
6871 * Puts quotes around strings, so that they can be parsed back by eval().
6872 * May return NULL;
6873 */
6874 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006875tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006876 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006877 char_u **tofree;
6878 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006879 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006880{
6881 switch (tv->v_type)
6882 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006883 case VAR_FUNC:
6884 *tofree = string_quote(tv->vval.v_string, TRUE);
6885 return *tofree;
6886 case VAR_STRING:
6887 *tofree = string_quote(tv->vval.v_string, FALSE);
6888 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006892 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006893 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006894 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006895 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006896 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006897}
6898
6899/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 * Return string "str" in ' quotes, doubling ' characters.
6901 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006902 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006903 */
6904 static char_u *
6905string_quote(str, function)
6906 char_u *str;
6907 int function;
6908{
Bram Moolenaar33570922005-01-25 22:26:29 +00006909 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006910 char_u *p, *r, *s;
6911
Bram Moolenaar33570922005-01-25 22:26:29 +00006912 len = (function ? 13 : 3);
6913 if (str != NULL)
6914 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006915 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 for (p = str; *p != NUL; mb_ptr_adv(p))
6917 if (*p == '\'')
6918 ++len;
6919 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006920 s = r = alloc(len);
6921 if (r != NULL)
6922 {
6923 if (function)
6924 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006925 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006926 r += 10;
6927 }
6928 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006929 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006930 if (str != NULL)
6931 for (p = str; *p != NUL; )
6932 {
6933 if (*p == '\'')
6934 *r++ = '\'';
6935 MB_COPY_CHAR(p, r);
6936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006937 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006938 if (function)
6939 *r++ = ')';
6940 *r++ = NUL;
6941 }
6942 return s;
6943}
6944
6945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 * Get the value of an environment variable.
6947 * "arg" is pointing to the '$'. It is advanced to after the name.
6948 * If the environment variable was not set, silently assume it is empty.
6949 * Always return OK.
6950 */
6951 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006952get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 int evaluate;
6956{
6957 char_u *string = NULL;
6958 int len;
6959 int cc;
6960 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006961 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
6963 ++*arg;
6964 name = *arg;
6965 len = get_env_len(arg);
6966 if (evaluate)
6967 {
6968 if (len != 0)
6969 {
6970 cc = name[len];
6971 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006972 /* first try vim_getenv(), fast for normal environment vars */
6973 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006975 {
6976 if (!mustfree)
6977 string = vim_strsave(string);
6978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 else
6980 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006981 if (mustfree)
6982 vim_free(string);
6983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 /* next try expanding things like $VIM and ${HOME} */
6985 string = expand_env_save(name - 1);
6986 if (string != NULL && *string == '$')
6987 {
6988 vim_free(string);
6989 string = NULL;
6990 }
6991 }
6992 name[len] = cc;
6993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994 rettv->v_type = VAR_STRING;
6995 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997
6998 return OK;
6999}
7000
7001/*
7002 * Array with names and number of arguments of all internal functions
7003 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7004 */
7005static struct fst
7006{
7007 char *f_name; /* function name */
7008 char f_min_argc; /* minimal number of arguments */
7009 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007011 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012} functions[] =
7013{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007014 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {"append", 2, 2, f_append},
7016 {"argc", 0, 0, f_argc},
7017 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007018 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007020 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {"bufexists", 1, 1, f_bufexists},
7022 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7023 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7024 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7025 {"buflisted", 1, 1, f_buflisted},
7026 {"bufloaded", 1, 1, f_bufloaded},
7027 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007028 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"bufwinnr", 1, 1, f_bufwinnr},
7030 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007031 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007032 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007033 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {"char2nr", 1, 1, f_char2nr},
7035 {"cindent", 1, 1, f_cindent},
7036 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007037#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007038 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007039 {"complete_add", 1, 1, f_complete_add},
7040 {"complete_check", 0, 0, f_complete_check},
7041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007043 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007044 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007046 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007047 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {"delete", 1, 1, f_delete},
7049 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007050 {"diff_filler", 1, 1, f_diff_filler},
7051 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007052 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007054 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {"eventhandler", 0, 0, f_eventhandler},
7056 {"executable", 1, 1, f_executable},
7057 {"exists", 1, 1, f_exists},
7058 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007059 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007060 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7062 {"filereadable", 1, 1, f_filereadable},
7063 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007064 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007065 {"finddir", 1, 3, f_finddir},
7066 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {"fnamemodify", 2, 2, f_fnamemodify},
7068 {"foldclosed", 1, 1, f_foldclosed},
7069 {"foldclosedend", 1, 1, f_foldclosedend},
7070 {"foldlevel", 1, 1, f_foldlevel},
7071 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007072 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007074 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007076 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007077 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {"getbufvar", 2, 2, f_getbufvar},
7079 {"getchar", 0, 1, f_getchar},
7080 {"getcharmod", 0, 0, f_getcharmod},
7081 {"getcmdline", 0, 0, f_getcmdline},
7082 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007083 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007085 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007086 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"getfsize", 1, 1, f_getfsize},
7088 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007089 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007090 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007091 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007092 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007093 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007094 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007096 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"getwinposx", 0, 0, f_getwinposx},
7098 {"getwinposy", 0, 0, f_getwinposy},
7099 {"getwinvar", 2, 2, f_getwinvar},
7100 {"glob", 1, 1, f_glob},
7101 {"globpath", 2, 2, f_globpath},
7102 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007104 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7106 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7107 {"histadd", 2, 2, f_histadd},
7108 {"histdel", 1, 2, f_histdel},
7109 {"histget", 1, 2, f_histget},
7110 {"histnr", 1, 1, f_histnr},
7111 {"hlID", 1, 1, f_hlID},
7112 {"hlexists", 1, 1, f_hlexists},
7113 {"hostname", 0, 0, f_hostname},
7114 {"iconv", 3, 3, f_iconv},
7115 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007116 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007117 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007119 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {"inputrestore", 0, 0, f_inputrestore},
7121 {"inputsave", 0, 0, f_inputsave},
7122 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007123 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007126 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007127 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007130 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {"libcall", 3, 3, f_libcall},
7132 {"libcallnr", 3, 3, f_libcallnr},
7133 {"line", 1, 1, f_line},
7134 {"line2byte", 1, 1, f_line2byte},
7135 {"lispindent", 1, 1, f_lispindent},
7136 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007137 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007138 {"maparg", 1, 3, f_maparg},
7139 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007140 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007141 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007142 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007143 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007144 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007145 {"max", 1, 1, f_max},
7146 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007147#ifdef vim_mkdir
7148 {"mkdir", 1, 3, f_mkdir},
7149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {"mode", 0, 0, f_mode},
7151 {"nextnonblank", 1, 1, f_nextnonblank},
7152 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007153 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007155 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007156 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007157 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007158 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007159 {"reltime", 0, 2, f_reltime},
7160 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"remote_expr", 2, 3, f_remote_expr},
7162 {"remote_foreground", 1, 1, f_remote_foreground},
7163 {"remote_peek", 1, 2, f_remote_peek},
7164 {"remote_read", 1, 1, f_remote_read},
7165 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007166 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007168 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007170 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007171 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007172 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007173 {"searchpair", 3, 6, f_searchpair},
7174 {"searchpairpos", 3, 6, f_searchpairpos},
7175 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {"server2client", 2, 2, f_server2client},
7177 {"serverlist", 0, 0, f_serverlist},
7178 {"setbufvar", 3, 3, f_setbufvar},
7179 {"setcmdpos", 1, 1, f_setcmdpos},
7180 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007181 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007182 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007183 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007185 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007187 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007189 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007190 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007191 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007192 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007193 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007194 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195#ifdef HAVE_STRFTIME
7196 {"strftime", 1, 2, f_strftime},
7197#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007198 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007199 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {"strlen", 1, 1, f_strlen},
7201 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007202 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {"strtrans", 1, 1, f_strtrans},
7204 {"submatch", 1, 1, f_submatch},
7205 {"substitute", 4, 4, f_substitute},
7206 {"synID", 3, 3, f_synID},
7207 {"synIDattr", 2, 3, f_synIDattr},
7208 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007209 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007210 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007211 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007213 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007214 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007216 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {"tolower", 1, 1, f_tolower},
7218 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007219 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {"virtcol", 1, 1, f_virtcol},
7223 {"visualmode", 0, 1, f_visualmode},
7224 {"winbufnr", 1, 1, f_winbufnr},
7225 {"wincol", 0, 0, f_wincol},
7226 {"winheight", 1, 1, f_winheight},
7227 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007228 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007230 {"winrestview", 1, 1, f_winrestview},
7231 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007233 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234};
7235
7236#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7237
7238/*
7239 * Function given to ExpandGeneric() to obtain the list of internal
7240 * or user defined function names.
7241 */
7242 char_u *
7243get_function_name(xp, idx)
7244 expand_T *xp;
7245 int idx;
7246{
7247 static int intidx = -1;
7248 char_u *name;
7249
7250 if (idx == 0)
7251 intidx = -1;
7252 if (intidx < 0)
7253 {
7254 name = get_user_func_name(xp, idx);
7255 if (name != NULL)
7256 return name;
7257 }
7258 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7259 {
7260 STRCPY(IObuff, functions[intidx].f_name);
7261 STRCAT(IObuff, "(");
7262 if (functions[intidx].f_max_argc == 0)
7263 STRCAT(IObuff, ")");
7264 return IObuff;
7265 }
7266
7267 return NULL;
7268}
7269
7270/*
7271 * Function given to ExpandGeneric() to obtain the list of internal or
7272 * user defined variable or function names.
7273 */
7274/*ARGSUSED*/
7275 char_u *
7276get_expr_name(xp, idx)
7277 expand_T *xp;
7278 int idx;
7279{
7280 static int intidx = -1;
7281 char_u *name;
7282
7283 if (idx == 0)
7284 intidx = -1;
7285 if (intidx < 0)
7286 {
7287 name = get_function_name(xp, idx);
7288 if (name != NULL)
7289 return name;
7290 }
7291 return get_user_var_name(xp, ++intidx);
7292}
7293
7294#endif /* FEAT_CMDL_COMPL */
7295
7296/*
7297 * Find internal function in table above.
7298 * Return index, or -1 if not found
7299 */
7300 static int
7301find_internal_func(name)
7302 char_u *name; /* name of the function */
7303{
7304 int first = 0;
7305 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7306 int cmp;
7307 int x;
7308
7309 /*
7310 * Find the function name in the table. Binary search.
7311 */
7312 while (first <= last)
7313 {
7314 x = first + ((unsigned)(last - first) >> 1);
7315 cmp = STRCMP(name, functions[x].f_name);
7316 if (cmp < 0)
7317 last = x - 1;
7318 else if (cmp > 0)
7319 first = x + 1;
7320 else
7321 return x;
7322 }
7323 return -1;
7324}
7325
7326/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007327 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7328 * name it contains, otherwise return "name".
7329 */
7330 static char_u *
7331deref_func_name(name, lenp)
7332 char_u *name;
7333 int *lenp;
7334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007336 int cc;
7337
7338 cc = name[*lenp];
7339 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007340 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007341 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007343 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 {
7346 *lenp = 0;
7347 return (char_u *)""; /* just in case */
7348 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007349 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007351 }
7352
7353 return name;
7354}
7355
7356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 * Allocate a variable for the result of a function.
7358 * Return OK or FAIL.
7359 */
7360 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7362 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 char_u *name; /* name of the function */
7364 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 char_u **arg; /* argument, pointing to the '(' */
7367 linenr_T firstline; /* first line of range */
7368 linenr_T lastline; /* last line of range */
7369 int *doesrange; /* return: function handled range */
7370 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 char_u *argp;
7374 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007375 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 int argcount = 0; /* number of arguments found */
7377
7378 /*
7379 * Get the arguments.
7380 */
7381 argp = *arg;
7382 while (argcount < MAX_FUNC_ARGS)
7383 {
7384 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7385 if (*argp == ')' || *argp == ',' || *argp == NUL)
7386 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7388 {
7389 ret = FAIL;
7390 break;
7391 }
7392 ++argcount;
7393 if (*argp != ',')
7394 break;
7395 }
7396 if (*argp == ')')
7397 ++argp;
7398 else
7399 ret = FAIL;
7400
7401 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007402 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 {
7406 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007409 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411
7412 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007413 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
7415 *arg = skipwhite(argp);
7416 return ret;
7417}
7418
7419
7420/*
7421 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007422 * Return OK when the function can't be called, FAIL otherwise.
7423 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 */
7425 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 char_u *name; /* name of the function */
7429 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007432 typval_T *argvars; /* vars for arguments, must have "argcount"
7433 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 linenr_T firstline; /* first line of range */
7435 linenr_T lastline; /* last line of range */
7436 int *doesrange; /* return: function handled range */
7437 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441#define ERROR_UNKNOWN 0
7442#define ERROR_TOOMANY 1
7443#define ERROR_TOOFEW 2
7444#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007445#define ERROR_DICT 4
7446#define ERROR_NONE 5
7447#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 int error = ERROR_NONE;
7449 int i;
7450 int llen;
7451 ufunc_T *fp;
7452 int cc;
7453#define FLEN_FIXED 40
7454 char_u fname_buf[FLEN_FIXED + 1];
7455 char_u *fname;
7456
7457 /*
7458 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7459 * Change <SNR>123_name() to K_SNR 123_name().
7460 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7461 */
7462 cc = name[len];
7463 name[len] = NUL;
7464 llen = eval_fname_script(name);
7465 if (llen > 0)
7466 {
7467 fname_buf[0] = K_SPECIAL;
7468 fname_buf[1] = KS_EXTRA;
7469 fname_buf[2] = (int)KE_SNR;
7470 i = 3;
7471 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7472 {
7473 if (current_SID <= 0)
7474 error = ERROR_SCRIPT;
7475 else
7476 {
7477 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7478 i = (int)STRLEN(fname_buf);
7479 }
7480 }
7481 if (i + STRLEN(name + llen) < FLEN_FIXED)
7482 {
7483 STRCPY(fname_buf + i, name + llen);
7484 fname = fname_buf;
7485 }
7486 else
7487 {
7488 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7489 if (fname == NULL)
7490 error = ERROR_OTHER;
7491 else
7492 {
7493 mch_memmove(fname, fname_buf, (size_t)i);
7494 STRCPY(fname + i, name + llen);
7495 }
7496 }
7497 }
7498 else
7499 fname = name;
7500
7501 *doesrange = FALSE;
7502
7503
7504 /* execute the function if no errors detected and executing */
7505 if (evaluate && error == ERROR_NONE)
7506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 error = ERROR_UNKNOWN;
7509
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007510 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 {
7512 /*
7513 * User defined function.
7514 */
7515 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007516
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007518 /* Trigger FuncUndefined event, may load the function. */
7519 if (fp == NULL
7520 && apply_autocmds(EVENT_FUNCUNDEFINED,
7521 fname, fname, TRUE, NULL)
7522 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007524 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 fp = find_func(fname);
7526 }
7527#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007528 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007529 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007530 {
7531 /* loaded a package, search for the function again */
7532 fp = find_func(fname);
7533 }
7534
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 if (fp != NULL)
7536 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007537 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007539 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007541 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007543 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007544 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 else
7546 {
7547 /*
7548 * Call the user function.
7549 * Save and restore search patterns, script variables and
7550 * redo buffer.
7551 */
7552 save_search_patterns();
7553 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007554 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007556 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007557 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7558 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7559 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007560 /* Function was unreferenced while being used, free it
7561 * now. */
7562 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 restoreRedobuff();
7564 restore_search_patterns();
7565 error = ERROR_NONE;
7566 }
7567 }
7568 }
7569 else
7570 {
7571 /*
7572 * Find the function name in the table, call its implementation.
7573 */
7574 i = find_internal_func(fname);
7575 if (i >= 0)
7576 {
7577 if (argcount < functions[i].f_min_argc)
7578 error = ERROR_TOOFEW;
7579 else if (argcount > functions[i].f_max_argc)
7580 error = ERROR_TOOMANY;
7581 else
7582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007583 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007584 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 error = ERROR_NONE;
7586 }
7587 }
7588 }
7589 /*
7590 * The function call (or "FuncUndefined" autocommand sequence) might
7591 * have been aborted by an error, an interrupt, or an explicitly thrown
7592 * exception that has not been caught so far. This situation can be
7593 * tested for by calling aborting(). For an error in an internal
7594 * function or for the "E132" error in call_user_func(), however, the
7595 * throw point at which the "force_abort" flag (temporarily reset by
7596 * emsg()) is normally updated has not been reached yet. We need to
7597 * update that flag first to make aborting() reliable.
7598 */
7599 update_force_abort();
7600 }
7601 if (error == ERROR_NONE)
7602 ret = OK;
7603
7604 /*
7605 * Report an error unless the argument evaluation or function call has been
7606 * cancelled due to an aborting error, an interrupt, or an exception.
7607 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 if (!aborting())
7609 {
7610 switch (error)
7611 {
7612 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007613 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 break;
7615 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007616 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007617 break;
7618 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007619 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 name);
7621 break;
7622 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007623 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 name);
7625 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007626 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007627 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628 name);
7629 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 }
7631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
7633 name[len] = cc;
7634 if (fname != name && fname != fname_buf)
7635 vim_free(fname);
7636
7637 return ret;
7638}
7639
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007640/*
7641 * Give an error message with a function name. Handle <SNR> things.
7642 */
7643 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007644emsg_funcname(ermsg, name)
7645 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007646 char_u *name;
7647{
7648 char_u *p;
7649
7650 if (*name == K_SPECIAL)
7651 p = concat_str((char_u *)"<SNR>", name + 3);
7652 else
7653 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007654 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007655 if (p != name)
7656 vim_free(p);
7657}
7658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659/*********************************************
7660 * Implementation of the built-in functions
7661 */
7662
7663/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007664 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 */
7666 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007667f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 typval_T *argvars;
7669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007674 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007676 if ((l = argvars[0].vval.v_list) != NULL
7677 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7678 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007680 }
7681 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007682 EMSG(_(e_listreq));
7683}
7684
7685/*
7686 * "append(lnum, string/list)" function
7687 */
7688 static void
7689f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 typval_T *argvars;
7691 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007692{
7693 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007694 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 list_T *l = NULL;
7696 listitem_T *li = NULL;
7697 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007698 long added = 0;
7699
Bram Moolenaar0d660222005-01-07 21:51:51 +00007700 lnum = get_tv_lnum(argvars);
7701 if (lnum >= 0
7702 && lnum <= curbuf->b_ml.ml_line_count
7703 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007705 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007706 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007707 l = argvars[1].vval.v_list;
7708 if (l == NULL)
7709 return;
7710 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007711 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007712 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007713 for (;;)
7714 {
7715 if (l == NULL)
7716 tv = &argvars[1]; /* append a string */
7717 else if (li == NULL)
7718 break; /* end of list */
7719 else
7720 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007721 line = get_tv_string_chk(tv);
7722 if (line == NULL) /* type error */
7723 {
7724 rettv->vval.v_number = 1; /* Failed */
7725 break;
7726 }
7727 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007728 ++added;
7729 if (l == NULL)
7730 break;
7731 li = li->li_next;
7732 }
7733
7734 appended_lines_mark(lnum, added);
7735 if (curwin->w_cursor.lnum > lnum)
7736 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007738 else
7739 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740}
7741
7742/*
7743 * "argc()" function
7744 */
7745/* ARGSUSED */
7746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007747f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 typval_T *argvars;
7749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007751 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752}
7753
7754/*
7755 * "argidx()" function
7756 */
7757/* ARGSUSED */
7758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 typval_T *argvars;
7761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764}
7765
7766/*
7767 * "argv(nr)" function
7768 */
7769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 typval_T *argvars;
7772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 int idx;
7775
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007776 if (argvars[0].v_type != VAR_UNKNOWN)
7777 {
7778 idx = get_tv_number_chk(&argvars[0], NULL);
7779 if (idx >= 0 && idx < ARGCOUNT)
7780 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7781 else
7782 rettv->vval.v_string = NULL;
7783 rettv->v_type = VAR_STRING;
7784 }
7785 else if (rettv_list_alloc(rettv) == OK)
7786 for (idx = 0; idx < ARGCOUNT; ++idx)
7787 list_append_string(rettv->vval.v_list,
7788 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
7790
7791/*
7792 * "browse(save, title, initdir, default)" function
7793 */
7794/* ARGSUSED */
7795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 typval_T *argvars;
7798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800#ifdef FEAT_BROWSE
7801 int save;
7802 char_u *title;
7803 char_u *initdir;
7804 char_u *defname;
7805 char_u buf[NUMBUFLEN];
7806 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007807 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007809 save = get_tv_number_chk(&argvars[0], &error);
7810 title = get_tv_string_chk(&argvars[1]);
7811 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7812 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007814 if (error || title == NULL || initdir == NULL || defname == NULL)
7815 rettv->vval.v_string = NULL;
7816 else
7817 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007818 do_browse(save ? BROWSE_SAVE : 0,
7819 title, defname, NULL, initdir, NULL, curbuf);
7820#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007821 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007822#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007823 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007824}
7825
7826/*
7827 * "browsedir(title, initdir)" function
7828 */
7829/* ARGSUSED */
7830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007831f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 typval_T *argvars;
7833 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007834{
7835#ifdef FEAT_BROWSE
7836 char_u *title;
7837 char_u *initdir;
7838 char_u buf[NUMBUFLEN];
7839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007840 title = get_tv_string_chk(&argvars[0]);
7841 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007843 if (title == NULL || initdir == NULL)
7844 rettv->vval.v_string = NULL;
7845 else
7846 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007847 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
Bram Moolenaar33570922005-01-25 22:26:29 +00007854static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007855
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856/*
7857 * Find a buffer by number or exact name.
7858 */
7859 static buf_T *
7860find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 if (avar->v_type == VAR_NUMBER)
7866 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007867 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007870 if (buf == NULL)
7871 {
7872 /* No full path name match, try a match with a URL or a "nofile"
7873 * buffer, these don't use the full path. */
7874 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7875 if (buf->b_fname != NULL
7876 && (path_with_url(buf->b_fname)
7877#ifdef FEAT_QUICKFIX
7878 || bt_nofile(buf)
7879#endif
7880 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007882 break;
7883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 }
7885 return buf;
7886}
7887
7888/*
7889 * "bufexists(expr)" function
7890 */
7891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007893 typval_T *argvars;
7894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897}
7898
7899/*
7900 * "buflisted(expr)" function
7901 */
7902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 typval_T *argvars;
7905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 buf_T *buf;
7908
7909 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007910 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911}
7912
7913/*
7914 * "bufloaded(expr)" function
7915 */
7916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007917f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 typval_T *argvars;
7919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
7921 buf_T *buf;
7922
7923 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925}
7926
Bram Moolenaar33570922005-01-25 22:26:29 +00007927static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007928
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929/*
7930 * Get buffer by number or pattern.
7931 */
7932 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007934 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 int save_magic;
7938 char_u *save_cpo;
7939 buf_T *buf;
7940
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 if (tv->v_type == VAR_NUMBER)
7942 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007943 if (tv->v_type != VAR_STRING)
7944 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 if (name == NULL || *name == NUL)
7946 return curbuf;
7947 if (name[0] == '$' && name[1] == NUL)
7948 return lastbuf;
7949
7950 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7951 save_magic = p_magic;
7952 p_magic = TRUE;
7953 save_cpo = p_cpo;
7954 p_cpo = (char_u *)"";
7955
7956 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7957 TRUE, FALSE));
7958
7959 p_magic = save_magic;
7960 p_cpo = save_cpo;
7961
7962 /* If not found, try expanding the name, like done for bufexists(). */
7963 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965
7966 return buf;
7967}
7968
7969/*
7970 * "bufname(expr)" function
7971 */
7972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007974 typval_T *argvars;
7975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 buf_T *buf;
7978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007979 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981 buf = get_buf_tv(&argvars[0]);
7982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 --emsg_off;
7988}
7989
7990/*
7991 * "bufnr(expr)" function
7992 */
7993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007994f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007995 typval_T *argvars;
7996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007999 int error = FALSE;
8000 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008002 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008005 --emsg_off;
8006
8007 /* If the buffer isn't found and the second argument is not zero create a
8008 * new buffer. */
8009 if (buf == NULL
8010 && argvars[1].v_type != VAR_UNKNOWN
8011 && get_tv_number_chk(&argvars[1], &error) != 0
8012 && !error
8013 && (name = get_tv_string_chk(&argvars[0])) != NULL
8014 && !error)
8015 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8016
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021}
8022
8023/*
8024 * "bufwinnr(nr)" function
8025 */
8026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008028 typval_T *argvars;
8029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
8031#ifdef FEAT_WINDOWS
8032 win_T *wp;
8033 int winnr = 0;
8034#endif
8035 buf_T *buf;
8036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008037 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008039 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040#ifdef FEAT_WINDOWS
8041 for (wp = firstwin; wp; wp = wp->w_next)
8042 {
8043 ++winnr;
8044 if (wp->w_buffer == buf)
8045 break;
8046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050#endif
8051 --emsg_off;
8052}
8053
8054/*
8055 * "byte2line(byte)" function
8056 */
8057/*ARGSUSED*/
8058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008059f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008060 typval_T *argvars;
8061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065#else
8066 long boff = 0;
8067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008068 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 (linenr_T)0, &boff);
8074#endif
8075}
8076
8077/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008078 * "byteidx()" function
8079 */
8080/*ARGSUSED*/
8081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008082f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 typval_T *argvars;
8084 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008085{
8086#ifdef FEAT_MBYTE
8087 char_u *t;
8088#endif
8089 char_u *str;
8090 long idx;
8091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008092 str = get_tv_string_chk(&argvars[0]);
8093 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008094 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008095 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008096 return;
8097
8098#ifdef FEAT_MBYTE
8099 t = str;
8100 for ( ; idx > 0; idx--)
8101 {
8102 if (*t == NUL) /* EOL reached */
8103 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008104 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008105 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008106 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008107#else
8108 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008110#endif
8111}
8112
8113/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008114 * "call(func, arglist)" function
8115 */
8116 static void
8117f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008118 typval_T *argvars;
8119 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008120{
8121 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008122 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008123 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008124 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008126 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008127
8128 rettv->vval.v_number = 0;
8129 if (argvars[1].v_type != VAR_LIST)
8130 {
8131 EMSG(_(e_listreq));
8132 return;
8133 }
8134 if (argvars[1].vval.v_list == NULL)
8135 return;
8136
8137 if (argvars[0].v_type == VAR_FUNC)
8138 func = argvars[0].vval.v_string;
8139 else
8140 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008141 if (*func == NUL)
8142 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008143
Bram Moolenaare9a41262005-01-15 22:18:47 +00008144 if (argvars[2].v_type != VAR_UNKNOWN)
8145 {
8146 if (argvars[2].v_type != VAR_DICT)
8147 {
8148 EMSG(_(e_dictreq));
8149 return;
8150 }
8151 selfdict = argvars[2].vval.v_dict;
8152 }
8153
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008154 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8155 item = item->li_next)
8156 {
8157 if (argc == MAX_FUNC_ARGS)
8158 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008159 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008160 break;
8161 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008162 /* Make a copy of each argument. This is needed to be able to set
8163 * v_lock to VAR_FIXED in the copy without changing the original list.
8164 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008165 copy_tv(&item->li_tv, &argv[argc++]);
8166 }
8167
8168 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008169 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008170 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8171 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008172
8173 /* Free the arguments. */
8174 while (argc > 0)
8175 clear_tv(&argv[--argc]);
8176}
8177
8178/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008179 * "changenr()" function
8180 */
8181/*ARGSUSED*/
8182 static void
8183f_changenr(argvars, rettv)
8184 typval_T *argvars;
8185 typval_T *rettv;
8186{
8187 rettv->vval.v_number = curbuf->b_u_seq_cur;
8188}
8189
8190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 * "char2nr(string)" function
8192 */
8193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008194f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 typval_T *argvars;
8196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198#ifdef FEAT_MBYTE
8199 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008200 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 else
8202#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204}
8205
8206/*
8207 * "cindent(lnum)" function
8208 */
8209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008210f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008211 typval_T *argvars;
8212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213{
8214#ifdef FEAT_CINDENT
8215 pos_T pos;
8216 linenr_T lnum;
8217
8218 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8221 {
8222 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008223 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 curwin->w_cursor = pos;
8225 }
8226 else
8227#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008228 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229}
8230
8231/*
8232 * "col(string)" function
8233 */
8234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008236 typval_T *argvars;
8237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239 colnr_T col = 0;
8240 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008241 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008243 fp = var2fpos(&argvars[0], FALSE, &fnum);
8244 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {
8246 if (fp->col == MAXCOL)
8247 {
8248 /* '> can be MAXCOL, get the length of the line then */
8249 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008250 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 else
8252 col = MAXCOL;
8253 }
8254 else
8255 {
8256 col = fp->col + 1;
8257#ifdef FEAT_VIRTUALEDIT
8258 /* col(".") when the cursor is on the NUL at the end of the line
8259 * because of "coladd" can be seen as an extra column. */
8260 if (virtual_active() && fp == &curwin->w_cursor)
8261 {
8262 char_u *p = ml_get_cursor();
8263
8264 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8265 curwin->w_virtcol - curwin->w_cursor.coladd))
8266 {
8267# ifdef FEAT_MBYTE
8268 int l;
8269
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008270 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 col += l;
8272# else
8273 if (*p != NUL && p[1] == NUL)
8274 ++col;
8275# endif
8276 }
8277 }
8278#endif
8279 }
8280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008281 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282}
8283
Bram Moolenaar572cb562005-08-05 21:35:02 +00008284#if defined(FEAT_INS_EXPAND)
8285/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008286 * "complete()" function
8287 */
8288/*ARGSUSED*/
8289 static void
8290f_complete(argvars, rettv)
8291 typval_T *argvars;
8292 typval_T *rettv;
8293{
8294 int startcol;
8295
8296 if ((State & INSERT) == 0)
8297 {
8298 EMSG(_("E785: complete() can only be used in Insert mode"));
8299 return;
8300 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008301
8302 /* Check for undo allowed here, because if something was already inserted
8303 * the line was already saved for undo and this check isn't done. */
8304 if (!undo_allowed())
8305 return;
8306
Bram Moolenaarade00832006-03-10 21:46:58 +00008307 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8308 {
8309 EMSG(_(e_invarg));
8310 return;
8311 }
8312
8313 startcol = get_tv_number_chk(&argvars[0], NULL);
8314 if (startcol <= 0)
8315 return;
8316
8317 set_completion(startcol - 1, argvars[1].vval.v_list);
8318}
8319
8320/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008321 * "complete_add()" function
8322 */
8323/*ARGSUSED*/
8324 static void
8325f_complete_add(argvars, rettv)
8326 typval_T *argvars;
8327 typval_T *rettv;
8328{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008329 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008330}
8331
8332/*
8333 * "complete_check()" function
8334 */
8335/*ARGSUSED*/
8336 static void
8337f_complete_check(argvars, rettv)
8338 typval_T *argvars;
8339 typval_T *rettv;
8340{
8341 int saved = RedrawingDisabled;
8342
8343 RedrawingDisabled = 0;
8344 ins_compl_check_keys(0);
8345 rettv->vval.v_number = compl_interrupted;
8346 RedrawingDisabled = saved;
8347}
8348#endif
8349
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350/*
8351 * "confirm(message, buttons[, default [, type]])" function
8352 */
8353/*ARGSUSED*/
8354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008355f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008356 typval_T *argvars;
8357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358{
8359#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8360 char_u *message;
8361 char_u *buttons = NULL;
8362 char_u buf[NUMBUFLEN];
8363 char_u buf2[NUMBUFLEN];
8364 int def = 1;
8365 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008366 char_u *typestr;
8367 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008369 message = get_tv_string_chk(&argvars[0]);
8370 if (message == NULL)
8371 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008372 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008374 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8375 if (buttons == NULL)
8376 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008377 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008379 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008380 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008382 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8383 if (typestr == NULL)
8384 error = TRUE;
8385 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008387 switch (TOUPPER_ASC(*typestr))
8388 {
8389 case 'E': type = VIM_ERROR; break;
8390 case 'Q': type = VIM_QUESTION; break;
8391 case 'I': type = VIM_INFO; break;
8392 case 'W': type = VIM_WARNING; break;
8393 case 'G': type = VIM_GENERIC; break;
8394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 }
8396 }
8397 }
8398 }
8399
8400 if (buttons == NULL || *buttons == NUL)
8401 buttons = (char_u *)_("&Ok");
8402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008403 if (error)
8404 rettv->vval.v_number = 0;
8405 else
8406 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 def, NULL);
8408#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008409 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410#endif
8411}
8412
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008413/*
8414 * "copy()" function
8415 */
8416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 typval_T *argvars;
8419 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008420{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008421 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008422}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423
8424/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008425 * "count()" function
8426 */
8427 static void
8428f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008429 typval_T *argvars;
8430 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008431{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008432 long n = 0;
8433 int ic = FALSE;
8434
Bram Moolenaare9a41262005-01-15 22:18:47 +00008435 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008436 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008437 listitem_T *li;
8438 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008439 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008440
Bram Moolenaare9a41262005-01-15 22:18:47 +00008441 if ((l = argvars[0].vval.v_list) != NULL)
8442 {
8443 li = l->lv_first;
8444 if (argvars[2].v_type != VAR_UNKNOWN)
8445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008446 int error = FALSE;
8447
8448 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008449 if (argvars[3].v_type != VAR_UNKNOWN)
8450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 idx = get_tv_number_chk(&argvars[3], &error);
8452 if (!error)
8453 {
8454 li = list_find(l, idx);
8455 if (li == NULL)
8456 EMSGN(_(e_listidx), idx);
8457 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008459 if (error)
8460 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008461 }
8462
8463 for ( ; li != NULL; li = li->li_next)
8464 if (tv_equal(&li->li_tv, &argvars[1], ic))
8465 ++n;
8466 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008467 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008468 else if (argvars[0].v_type == VAR_DICT)
8469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008470 int todo;
8471 dict_T *d;
8472 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008474 if ((d = argvars[0].vval.v_dict) != NULL)
8475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476 int error = FALSE;
8477
Bram Moolenaare9a41262005-01-15 22:18:47 +00008478 if (argvars[2].v_type != VAR_UNKNOWN)
8479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008480 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008481 if (argvars[3].v_type != VAR_UNKNOWN)
8482 EMSG(_(e_invarg));
8483 }
8484
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008485 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008486 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008487 {
8488 if (!HASHITEM_EMPTY(hi))
8489 {
8490 --todo;
8491 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8492 ++n;
8493 }
8494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 }
8496 }
8497 else
8498 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008499 rettv->vval.v_number = n;
8500}
8501
8502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8504 *
8505 * Checks the existence of a cscope connection.
8506 */
8507/*ARGSUSED*/
8508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 typval_T *argvars;
8511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
8513#ifdef FEAT_CSCOPE
8514 int num = 0;
8515 char_u *dbpath = NULL;
8516 char_u *prepend = NULL;
8517 char_u buf[NUMBUFLEN];
8518
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519 if (argvars[0].v_type != VAR_UNKNOWN
8520 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522 num = (int)get_tv_number(&argvars[0]);
8523 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 }
8527
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008528 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531#endif
8532}
8533
8534/*
8535 * "cursor(lnum, col)" function
8536 *
8537 * Moves the cursor to the specified line and column
8538 */
8539/*ARGSUSED*/
8540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *argvars;
8543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
8545 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008546#ifdef FEAT_VIRTUALEDIT
8547 long coladd = 0;
8548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549
Bram Moolenaara5525202006-03-02 22:52:09 +00008550 if (argvars[1].v_type == VAR_UNKNOWN)
8551 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008552 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008553
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008554 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008555 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008556 line = pos.lnum;
8557 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008558#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008559 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008560#endif
8561 }
8562 else
8563 {
8564 line = get_tv_lnum(argvars);
8565 col = get_tv_number_chk(&argvars[1], NULL);
8566#ifdef FEAT_VIRTUALEDIT
8567 if (argvars[2].v_type != VAR_UNKNOWN)
8568 coladd = get_tv_number_chk(&argvars[2], NULL);
8569#endif
8570 }
8571 if (line < 0 || col < 0
8572#ifdef FEAT_VIRTUALEDIT
8573 || coladd < 0
8574#endif
8575 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008576 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (line > 0)
8578 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 if (col > 0)
8580 curwin->w_cursor.col = col - 1;
8581#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008582 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583#endif
8584
8585 /* Make sure the cursor is in a valid position. */
8586 check_cursor();
8587#ifdef FEAT_MBYTE
8588 /* Correct cursor for multi-byte character. */
8589 if (has_mbyte)
8590 mb_adjust_cursor();
8591#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008592
8593 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594}
8595
8596/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 */
8599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 typval_T *argvars;
8602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008604 int noref = 0;
8605
8606 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008607 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008608 if (noref < 0 || noref > 1)
8609 EMSG(_(e_invarg));
8610 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008611 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612}
8613
8614/*
8615 * "delete()" function
8616 */
8617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008618f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 typval_T *argvars;
8620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
8622 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008623 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626}
8627
8628/*
8629 * "did_filetype()" function
8630 */
8631/*ARGSUSED*/
8632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008633f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 typval_T *argvars;
8635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636{
8637#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008640 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641#endif
8642}
8643
8644/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008645 * "diff_filler()" function
8646 */
8647/*ARGSUSED*/
8648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008650 typval_T *argvars;
8651 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008652{
8653#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008655#endif
8656}
8657
8658/*
8659 * "diff_hlID()" function
8660 */
8661/*ARGSUSED*/
8662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 typval_T *argvars;
8665 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008666{
8667#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008669 static linenr_T prev_lnum = 0;
8670 static int changedtick = 0;
8671 static int fnum = 0;
8672 static int change_start = 0;
8673 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008674 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008675 int filler_lines;
8676 int col;
8677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008678 if (lnum < 0) /* ignore type error in {lnum} arg */
8679 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008680 if (lnum != prev_lnum
8681 || changedtick != curbuf->b_changedtick
8682 || fnum != curbuf->b_fnum)
8683 {
8684 /* New line, buffer, change: need to get the values. */
8685 filler_lines = diff_check(curwin, lnum);
8686 if (filler_lines < 0)
8687 {
8688 if (filler_lines == -1)
8689 {
8690 change_start = MAXCOL;
8691 change_end = -1;
8692 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8693 hlID = HLF_ADD; /* added line */
8694 else
8695 hlID = HLF_CHD; /* changed line */
8696 }
8697 else
8698 hlID = HLF_ADD; /* added line */
8699 }
8700 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008701 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008702 prev_lnum = lnum;
8703 changedtick = curbuf->b_changedtick;
8704 fnum = curbuf->b_fnum;
8705 }
8706
8707 if (hlID == HLF_CHD || hlID == HLF_TXD)
8708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008709 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008710 if (col >= change_start && col <= change_end)
8711 hlID = HLF_TXD; /* changed text */
8712 else
8713 hlID = HLF_CHD; /* changed line */
8714 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008715 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008716#endif
8717}
8718
8719/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008720 * "empty({expr})" function
8721 */
8722 static void
8723f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008726{
8727 int n;
8728
8729 switch (argvars[0].v_type)
8730 {
8731 case VAR_STRING:
8732 case VAR_FUNC:
8733 n = argvars[0].vval.v_string == NULL
8734 || *argvars[0].vval.v_string == NUL;
8735 break;
8736 case VAR_NUMBER:
8737 n = argvars[0].vval.v_number == 0;
8738 break;
8739 case VAR_LIST:
8740 n = argvars[0].vval.v_list == NULL
8741 || argvars[0].vval.v_list->lv_first == NULL;
8742 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008743 case VAR_DICT:
8744 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008745 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008746 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008747 default:
8748 EMSG2(_(e_intern2), "f_empty()");
8749 n = 0;
8750 }
8751
8752 rettv->vval.v_number = n;
8753}
8754
8755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 * "escape({string}, {chars})" function
8757 */
8758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008760 typval_T *argvars;
8761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 char_u buf[NUMBUFLEN];
8764
Bram Moolenaar758711c2005-02-02 23:11:38 +00008765 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8766 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768}
8769
8770/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008771 * "eval()" function
8772 */
8773/*ARGSUSED*/
8774 static void
8775f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 typval_T *argvars;
8777 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008778{
8779 char_u *s;
8780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 s = get_tv_string_chk(&argvars[0]);
8782 if (s != NULL)
8783 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008785 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8786 {
8787 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008788 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008789 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008790 else if (*s != NUL)
8791 EMSG(_(e_trailing));
8792}
8793
8794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 * "eventhandler()" function
8796 */
8797/*ARGSUSED*/
8798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 typval_T *argvars;
8801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804}
8805
8806/*
8807 * "executable()" function
8808 */
8809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 typval_T *argvars;
8812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * "exists()" function
8819 */
8820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *argvars;
8823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 char_u *p;
8826 char_u *name;
8827 int n = FALSE;
8828 int len = 0;
8829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 if (*p == '$') /* environment variable */
8832 {
8833 /* first try "normal" environment variables (fast) */
8834 if (mch_getenv(p + 1) != NULL)
8835 n = TRUE;
8836 else
8837 {
8838 /* try expanding things like $VIM and ${HOME} */
8839 p = expand_env_save(p);
8840 if (p != NULL && *p != '$')
8841 n = TRUE;
8842 vim_free(p);
8843 }
8844 }
8845 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008848 if (*skipwhite(p) != NUL)
8849 n = FALSE; /* trailing garbage */
8850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 else if (*p == '*') /* internal or user defined function */
8852 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008853 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
8855 else if (*p == ':')
8856 {
8857 n = cmd_exists(p + 1);
8858 }
8859 else if (*p == '#')
8860 {
8861#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008862 if (p[1] == '#')
8863 n = autocmd_supported(p + 2);
8864 else
8865 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866#endif
8867 }
8868 else /* internal variable */
8869 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008870 char_u *tofree;
8871 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008873 /* get_name_len() takes care of expanding curly braces */
8874 name = p;
8875 len = get_name_len(&p, &tofree, TRUE, FALSE);
8876 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008878 if (tofree != NULL)
8879 name = tofree;
8880 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8881 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008883 /* handle d.key, l[idx], f(expr) */
8884 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8885 if (n)
8886 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 }
8888 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008889 if (*p != NUL)
8890 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008892 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 }
8894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896}
8897
8898/*
8899 * "expand()" function
8900 */
8901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 typval_T *argvars;
8904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906 char_u *s;
8907 int len;
8908 char_u *errormsg;
8909 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8910 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->v_type = VAR_STRING;
8914 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 if (*s == '%' || *s == '#' || *s == '<')
8916 {
8917 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 --emsg_off;
8920 }
8921 else
8922 {
8923 /* When the optional second argument is non-zero, don't remove matches
8924 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008925 if (argvars[1].v_type != VAR_UNKNOWN
8926 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008928 if (!error)
8929 {
8930 ExpandInit(&xpc);
8931 xpc.xp_context = EXPAND_FILES;
8932 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 }
8934 else
8935 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 }
8937}
8938
8939/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008940 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008941 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008942 */
8943 static void
8944f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 typval_T *argvars;
8946 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008947{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008948 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008949 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008951 list_T *l1, *l2;
8952 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008953 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008955
Bram Moolenaare9a41262005-01-15 22:18:47 +00008956 l1 = argvars[0].vval.v_list;
8957 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008958 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8959 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 {
8961 if (argvars[2].v_type != VAR_UNKNOWN)
8962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 before = get_tv_number_chk(&argvars[2], &error);
8964 if (error)
8965 return; /* type error; errmsg already given */
8966
Bram Moolenaar758711c2005-02-02 23:11:38 +00008967 if (before == l1->lv_len)
8968 item = NULL;
8969 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008970 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008971 item = list_find(l1, before);
8972 if (item == NULL)
8973 {
8974 EMSGN(_(e_listidx), before);
8975 return;
8976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008977 }
8978 }
8979 else
8980 item = NULL;
8981 list_extend(l1, l2, item);
8982
Bram Moolenaare9a41262005-01-15 22:18:47 +00008983 copy_tv(&argvars[0], rettv);
8984 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008985 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008986 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8987 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 dict_T *d1, *d2;
8989 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008990 char_u *action;
8991 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008993 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008994
8995 d1 = argvars[0].vval.v_dict;
8996 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008997 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8998 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 {
9000 /* Check the third argument. */
9001 if (argvars[2].v_type != VAR_UNKNOWN)
9002 {
9003 static char *(av[]) = {"keep", "force", "error"};
9004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 action = get_tv_string_chk(&argvars[2]);
9006 if (action == NULL)
9007 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 for (i = 0; i < 3; ++i)
9009 if (STRCMP(action, av[i]) == 0)
9010 break;
9011 if (i == 3)
9012 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009013 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009014 return;
9015 }
9016 }
9017 else
9018 action = (char_u *)"force";
9019
9020 /* Go over all entries in the second dict and add them to the
9021 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009022 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009023 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009024 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009025 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009027 --todo;
9028 di1 = dict_find(d1, hi2->hi_key, -1);
9029 if (di1 == NULL)
9030 {
9031 di1 = dictitem_copy(HI2DI(hi2));
9032 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9033 dictitem_free(di1);
9034 }
9035 else if (*action == 'e')
9036 {
9037 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9038 break;
9039 }
9040 else if (*action == 'f')
9041 {
9042 clear_tv(&di1->di_tv);
9043 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 }
9046 }
9047
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048 copy_tv(&argvars[0], rettv);
9049 }
9050 }
9051 else
9052 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009053}
9054
9055/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009056 * "feedkeys()" function
9057 */
9058/*ARGSUSED*/
9059 static void
9060f_feedkeys(argvars, rettv)
9061 typval_T *argvars;
9062 typval_T *rettv;
9063{
9064 int remap = TRUE;
9065 char_u *keys, *flags;
9066 char_u nbuf[NUMBUFLEN];
9067 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009068 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009069
9070 rettv->vval.v_number = 0;
9071 keys = get_tv_string(&argvars[0]);
9072 if (*keys != NUL)
9073 {
9074 if (argvars[1].v_type != VAR_UNKNOWN)
9075 {
9076 flags = get_tv_string_buf(&argvars[1], nbuf);
9077 for ( ; *flags != NUL; ++flags)
9078 {
9079 switch (*flags)
9080 {
9081 case 'n': remap = FALSE; break;
9082 case 'm': remap = TRUE; break;
9083 case 't': typed = TRUE; break;
9084 }
9085 }
9086 }
9087
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009088 /* Need to escape K_SPECIAL and CSI before putting the string in the
9089 * typeahead buffer. */
9090 keys_esc = vim_strsave_escape_csi(keys);
9091 if (keys_esc != NULL)
9092 {
9093 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009094 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009095 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009096 if (vgetc_busy)
9097 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009098 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009099 }
9100}
9101
9102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 * "filereadable()" function
9104 */
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110 FILE *fd;
9111 char_u *p;
9112 int n;
9113
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9116 {
9117 n = TRUE;
9118 fclose(fd);
9119 }
9120 else
9121 n = FALSE;
9122
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124}
9125
9126/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009127 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 * rights to write into.
9129 */
9130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 typval_T *argvars;
9133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009135 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136}
9137
Bram Moolenaar33570922005-01-25 22:26:29 +00009138static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009139
9140 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009141findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009142 typval_T *argvars;
9143 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009144 int dir;
9145{
9146#ifdef FEAT_SEARCHPATH
9147 char_u *fname;
9148 char_u *fresult = NULL;
9149 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9150 char_u *p;
9151 char_u pathbuf[NUMBUFLEN];
9152 int count = 1;
9153 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009154 int error = FALSE;
9155#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009156
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009157 rettv->vval.v_string = NULL;
9158 rettv->v_type = VAR_STRING;
9159
9160#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009162
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9166 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009167 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009168 else
9169 {
9170 if (*p != NUL)
9171 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009173 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009174 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009176 }
9177
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009178 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9179 error = TRUE;
9180
9181 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 do
9184 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009185 if (rettv->v_type == VAR_STRING)
9186 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009187 fresult = find_file_in_path_option(first ? fname : NULL,
9188 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009189 0, first, path, dir, NULL,
9190 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009191 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009192
9193 if (fresult != NULL && rettv->v_type == VAR_LIST)
9194 list_append_string(rettv->vval.v_list, fresult, -1);
9195
9196 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009197 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009198
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009199 if (rettv->v_type == VAR_STRING)
9200 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009201#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009202}
9203
Bram Moolenaar33570922005-01-25 22:26:29 +00009204static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9205static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009206
9207/*
9208 * Implementation of map() and filter().
9209 */
9210 static void
9211filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009212 typval_T *argvars;
9213 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009214 int map;
9215{
9216 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009217 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009218 listitem_T *li, *nli;
9219 list_T *l = NULL;
9220 dictitem_T *di;
9221 hashtab_T *ht;
9222 hashitem_T *hi;
9223 dict_T *d = NULL;
9224 typval_T save_val;
9225 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009226 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009227 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009228 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009229 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009230
9231 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009232 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009233 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009234 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009235 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009236 return;
9237 }
9238 else if (argvars[0].v_type == VAR_DICT)
9239 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009240 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009241 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009242 return;
9243 }
9244 else
9245 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009246 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009247 return;
9248 }
9249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 expr = get_tv_string_buf_chk(&argvars[1], buf);
9251 /* On type errors, the preceding call has already displayed an error
9252 * message. Avoid a misleading error message for an empty string that
9253 * was not passed as argument. */
9254 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 prepare_vimvar(VV_VAL, &save_val);
9257 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009258
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009259 /* We reset "did_emsg" to be able to detect whether an error
9260 * occurred during evaluation of the expression. */
9261 save_did_emsg = did_emsg;
9262 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009266 prepare_vimvar(VV_KEY, &save_key);
9267 vimvars[VV_KEY].vv_type = VAR_STRING;
9268
9269 ht = &d->dv_hashtab;
9270 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009271 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009274 if (!HASHITEM_EMPTY(hi))
9275 {
9276 --todo;
9277 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009278 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009279 break;
9280 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009281 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009282 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009283 break;
9284 if (!map && rem)
9285 dictitem_remove(d, di);
9286 clear_tv(&vimvars[VV_KEY].vv_tv);
9287 }
9288 }
9289 hash_unlock(ht);
9290
9291 restore_vimvar(VV_KEY, &save_key);
9292 }
9293 else
9294 {
9295 for (li = l->lv_first; li != NULL; li = nli)
9296 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009297 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009298 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009299 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009300 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009301 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009302 break;
9303 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009304 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009305 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009306 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009309
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009310 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009311 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009312
9313 copy_tv(&argvars[0], rettv);
9314}
9315
9316 static int
9317filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009318 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009319 char_u *expr;
9320 int map;
9321 int *remp;
9322{
Bram Moolenaar33570922005-01-25 22:26:29 +00009323 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009324 char_u *s;
9325
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009327 s = expr;
9328 if (eval1(&s, &rettv, TRUE) == FAIL)
9329 return FAIL;
9330 if (*s != NUL) /* check for trailing chars after expr */
9331 {
9332 EMSG2(_(e_invexpr2), s);
9333 return FAIL;
9334 }
9335 if (map)
9336 {
9337 /* map(): replace the list item value */
9338 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009339 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009340 *tv = rettv;
9341 }
9342 else
9343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 int error = FALSE;
9345
Bram Moolenaare9a41262005-01-15 22:18:47 +00009346 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009348 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009349 /* On type error, nothing has been removed; return FAIL to stop the
9350 * loop. The error message was given by get_tv_number_chk(). */
9351 if (error)
9352 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009353 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009354 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009355 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009356}
9357
9358/*
9359 * "filter()" function
9360 */
9361 static void
9362f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 typval_T *argvars;
9364 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009365{
9366 filter_map(argvars, rettv, FALSE);
9367}
9368
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009369/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009370 * "finddir({fname}[, {path}[, {count}]])" function
9371 */
9372 static void
9373f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009374 typval_T *argvars;
9375 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009376{
9377 findfilendir(argvars, rettv, TRUE);
9378}
9379
9380/*
9381 * "findfile({fname}[, {path}[, {count}]])" function
9382 */
9383 static void
9384f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009385 typval_T *argvars;
9386 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009387{
9388 findfilendir(argvars, rettv, FALSE);
9389}
9390
9391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 * "fnamemodify({fname}, {mods})" function
9393 */
9394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 typval_T *argvars;
9397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398{
9399 char_u *fname;
9400 char_u *mods;
9401 int usedlen = 0;
9402 int len;
9403 char_u *fbuf = NULL;
9404 char_u buf[NUMBUFLEN];
9405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009406 fname = get_tv_string_chk(&argvars[0]);
9407 mods = get_tv_string_buf_chk(&argvars[1], buf);
9408 if (fname == NULL || mods == NULL)
9409 fname = NULL;
9410 else
9411 {
9412 len = (int)STRLEN(fname);
9413 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 vim_free(fbuf);
9422}
9423
Bram Moolenaar33570922005-01-25 22:26:29 +00009424static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425
9426/*
9427 * "foldclosed()" function
9428 */
9429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009431 typval_T *argvars;
9432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 int end;
9434{
9435#ifdef FEAT_FOLDING
9436 linenr_T lnum;
9437 linenr_T first, last;
9438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9441 {
9442 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9443 {
9444 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 return;
9449 }
9450 }
9451#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453}
9454
9455/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009456 * "foldclosed()" function
9457 */
9458 static void
9459f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009462{
9463 foldclosed_both(argvars, rettv, FALSE);
9464}
9465
9466/*
9467 * "foldclosedend()" function
9468 */
9469 static void
9470f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009471 typval_T *argvars;
9472 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009473{
9474 foldclosed_both(argvars, rettv, TRUE);
9475}
9476
9477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 * "foldlevel()" function
9479 */
9480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009482 typval_T *argvars;
9483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485#ifdef FEAT_FOLDING
9486 linenr_T lnum;
9487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 else
9492#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
9497 * "foldtext()" function
9498 */
9499/*ARGSUSED*/
9500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009502 typval_T *argvars;
9503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504{
9505#ifdef FEAT_FOLDING
9506 linenr_T lnum;
9507 char_u *s;
9508 char_u *r;
9509 int len;
9510 char *txt;
9511#endif
9512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->v_type = VAR_STRING;
9514 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009516 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9517 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9518 <= curbuf->b_ml.ml_line_count
9519 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 {
9521 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009522 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9523 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
9525 if (!linewhite(lnum))
9526 break;
9527 ++lnum;
9528 }
9529
9530 /* Find interesting text in this line. */
9531 s = skipwhite(ml_get(lnum));
9532 /* skip C comment-start */
9533 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009536 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009537 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009538 {
9539 s = skipwhite(ml_get(lnum + 1));
9540 if (*s == '*')
9541 s = skipwhite(s + 1);
9542 }
9543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 txt = _("+-%s%3ld lines: ");
9545 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009546 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 + 20 /* for %3ld */
9548 + STRLEN(s))); /* concatenated */
9549 if (r != NULL)
9550 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009551 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9552 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9553 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 len = (int)STRLEN(r);
9555 STRCAT(r, s);
9556 /* remove 'foldmarker' and 'commentstring' */
9557 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 }
9560 }
9561#endif
9562}
9563
9564/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009565 * "foldtextresult(lnum)" function
9566 */
9567/*ARGSUSED*/
9568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009570 typval_T *argvars;
9571 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572{
9573#ifdef FEAT_FOLDING
9574 linenr_T lnum;
9575 char_u *text;
9576 char_u buf[51];
9577 foldinfo_T foldinfo;
9578 int fold_count;
9579#endif
9580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->v_type = VAR_STRING;
9582 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009583#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 /* treat illegal types and illegal string values for {lnum} the same */
9586 if (lnum < 0)
9587 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009588 fold_count = foldedCount(curwin, lnum, &foldinfo);
9589 if (fold_count > 0)
9590 {
9591 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9592 &foldinfo, buf);
9593 if (text == buf)
9594 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009596 }
9597#endif
9598}
9599
9600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * "foreground()" function
9602 */
9603/*ARGSUSED*/
9604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 typval_T *argvars;
9607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610#ifdef FEAT_GUI
9611 if (gui.in_use)
9612 gui_mch_set_foreground();
9613#else
9614# ifdef WIN32
9615 win32_set_foreground();
9616# endif
9617#endif
9618}
9619
9620/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621 * "function()" function
9622 */
9623/*ARGSUSED*/
9624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 typval_T *argvars;
9627 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628{
9629 char_u *s;
9630
Bram Moolenaara7043832005-01-21 11:56:39 +00009631 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009633 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009634 EMSG2(_(e_invarg2), s);
9635 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009636 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009637 else
9638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_string = vim_strsave(s);
9640 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009641 }
9642}
9643
9644/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009645 * "garbagecollect()" function
9646 */
9647/*ARGSUSED*/
9648 static void
9649f_garbagecollect(argvars, rettv)
9650 typval_T *argvars;
9651 typval_T *rettv;
9652{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009653 /* This is postponed until we are back at the toplevel, because we may be
9654 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9655 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009656}
9657
9658/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009659 * "get()" function
9660 */
9661 static void
9662f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009663 typval_T *argvars;
9664 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009665{
Bram Moolenaar33570922005-01-25 22:26:29 +00009666 listitem_T *li;
9667 list_T *l;
9668 dictitem_T *di;
9669 dict_T *d;
9670 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009671
Bram Moolenaare9a41262005-01-15 22:18:47 +00009672 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009673 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009674 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009676 int error = FALSE;
9677
9678 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9679 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009680 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009681 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 else if (argvars[0].v_type == VAR_DICT)
9684 {
9685 if ((d = argvars[0].vval.v_dict) != NULL)
9686 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009687 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009688 if (di != NULL)
9689 tv = &di->di_tv;
9690 }
9691 }
9692 else
9693 EMSG2(_(e_listdictarg), "get()");
9694
9695 if (tv == NULL)
9696 {
9697 if (argvars[2].v_type == VAR_UNKNOWN)
9698 rettv->vval.v_number = 0;
9699 else
9700 copy_tv(&argvars[2], rettv);
9701 }
9702 else
9703 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009704}
9705
Bram Moolenaar342337a2005-07-21 21:11:17 +00009706static 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 +00009707
9708/*
9709 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009710 * Return a range (from start to end) of lines in rettv from the specified
9711 * buffer.
9712 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009713 */
9714 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009715get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009716 buf_T *buf;
9717 linenr_T start;
9718 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009719 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009720 typval_T *rettv;
9721{
9722 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009723
Bram Moolenaar342337a2005-07-21 21:11:17 +00009724 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009725 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009726 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009727 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009728 }
9729 else
9730 rettv->vval.v_number = 0;
9731
9732 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9733 return;
9734
9735 if (!retlist)
9736 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009737 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9738 p = ml_get_buf(buf, start, FALSE);
9739 else
9740 p = (char_u *)"";
9741
9742 rettv->v_type = VAR_STRING;
9743 rettv->vval.v_string = vim_strsave(p);
9744 }
9745 else
9746 {
9747 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009748 return;
9749
9750 if (start < 1)
9751 start = 1;
9752 if (end > buf->b_ml.ml_line_count)
9753 end = buf->b_ml.ml_line_count;
9754 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009755 if (list_append_string(rettv->vval.v_list,
9756 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009757 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009758 }
9759}
9760
9761/*
9762 * "getbufline()" function
9763 */
9764 static void
9765f_getbufline(argvars, rettv)
9766 typval_T *argvars;
9767 typval_T *rettv;
9768{
9769 linenr_T lnum;
9770 linenr_T end;
9771 buf_T *buf;
9772
9773 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9774 ++emsg_off;
9775 buf = get_buf_tv(&argvars[0]);
9776 --emsg_off;
9777
Bram Moolenaar661b1822005-07-28 22:36:45 +00009778 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009779 if (argvars[2].v_type == VAR_UNKNOWN)
9780 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009781 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009782 end = get_tv_lnum_buf(&argvars[2], buf);
9783
Bram Moolenaar342337a2005-07-21 21:11:17 +00009784 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009785}
9786
Bram Moolenaar0d660222005-01-07 21:51:51 +00009787/*
9788 * "getbufvar()" function
9789 */
9790 static void
9791f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009792 typval_T *argvars;
9793 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009794{
9795 buf_T *buf;
9796 buf_T *save_curbuf;
9797 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009798 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009800 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9801 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009802 ++emsg_off;
9803 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009804
9805 rettv->v_type = VAR_STRING;
9806 rettv->vval.v_string = NULL;
9807
9808 if (buf != NULL && varname != NULL)
9809 {
9810 if (*varname == '&') /* buffer-local-option */
9811 {
9812 /* set curbuf to be our buf, temporarily */
9813 save_curbuf = curbuf;
9814 curbuf = buf;
9815
9816 get_option_tv(&varname, rettv, TRUE);
9817
9818 /* restore previous notion of curbuf */
9819 curbuf = save_curbuf;
9820 }
9821 else
9822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 if (*varname == NUL)
9824 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9825 * scope prefix before the NUL byte is required by
9826 * find_var_in_ht(). */
9827 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009828 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009829 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009830 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009832 }
9833 }
9834
9835 --emsg_off;
9836}
9837
9838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 * "getchar()" function
9840 */
9841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 typval_T *argvars;
9844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009847 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009849 /* Position the cursor. Needed after a message that ends in a space. */
9850 windgoto(msg_row, msg_col);
9851
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 ++no_mapping;
9853 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009854 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 /* getchar(): blocking wait. */
9856 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 /* getchar(1): only check if char avail */
9859 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009860 else if (error || vpeekc() == NUL)
9861 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 n = 0;
9863 else
9864 /* getchar(0) and char avail: return char */
9865 n = safe_vgetc();
9866 --no_mapping;
9867 --allow_keys;
9868
Bram Moolenaar219b8702006-11-01 14:32:36 +00009869 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9870 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9871 vimvars[VV_MOUSE_COL].vv_nr = 0;
9872
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 if (IS_SPECIAL(n) || mod_mask != 0)
9875 {
9876 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9877 int i = 0;
9878
9879 /* Turn a special key into three bytes, plus modifier. */
9880 if (mod_mask != 0)
9881 {
9882 temp[i++] = K_SPECIAL;
9883 temp[i++] = KS_MODIFIER;
9884 temp[i++] = mod_mask;
9885 }
9886 if (IS_SPECIAL(n))
9887 {
9888 temp[i++] = K_SPECIAL;
9889 temp[i++] = K_SECOND(n);
9890 temp[i++] = K_THIRD(n);
9891 }
9892#ifdef FEAT_MBYTE
9893 else if (has_mbyte)
9894 i += (*mb_char2bytes)(n, temp + i);
9895#endif
9896 else
9897 temp[i++] = n;
9898 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 rettv->v_type = VAR_STRING;
9900 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009901
9902#ifdef FEAT_MOUSE
9903 if (n == K_LEFTMOUSE
9904 || n == K_LEFTMOUSE_NM
9905 || n == K_LEFTDRAG
9906 || n == K_LEFTRELEASE
9907 || n == K_LEFTRELEASE_NM
9908 || n == K_MIDDLEMOUSE
9909 || n == K_MIDDLEDRAG
9910 || n == K_MIDDLERELEASE
9911 || n == K_RIGHTMOUSE
9912 || n == K_RIGHTDRAG
9913 || n == K_RIGHTRELEASE
9914 || n == K_X1MOUSE
9915 || n == K_X1DRAG
9916 || n == K_X1RELEASE
9917 || n == K_X2MOUSE
9918 || n == K_X2DRAG
9919 || n == K_X2RELEASE
9920 || n == K_MOUSEDOWN
9921 || n == K_MOUSEUP)
9922 {
9923 int row = mouse_row;
9924 int col = mouse_col;
9925 win_T *win;
9926 linenr_T lnum;
9927# ifdef FEAT_WINDOWS
9928 win_T *wp;
9929# endif
9930 int n = 1;
9931
9932 if (row >= 0 && col >= 0)
9933 {
9934 /* Find the window at the mouse coordinates and compute the
9935 * text position. */
9936 win = mouse_find_win(&row, &col);
9937 (void)mouse_comp_pos(win, &row, &col, &lnum);
9938# ifdef FEAT_WINDOWS
9939 for (wp = firstwin; wp != win; wp = wp->w_next)
9940 ++n;
9941# endif
9942 vimvars[VV_MOUSE_WIN].vv_nr = n;
9943 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
9944 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
9945 }
9946 }
9947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 }
9949}
9950
9951/*
9952 * "getcharmod()" function
9953 */
9954/*ARGSUSED*/
9955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961}
9962
9963/*
9964 * "getcmdline()" function
9965 */
9966/*ARGSUSED*/
9967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 typval_T *argvars;
9970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 rettv->v_type = VAR_STRING;
9973 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974}
9975
9976/*
9977 * "getcmdpos()" function
9978 */
9979/*ARGSUSED*/
9980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009981f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009982 typval_T *argvars;
9983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986}
9987
9988/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009989 * "getcmdtype()" function
9990 */
9991/*ARGSUSED*/
9992 static void
9993f_getcmdtype(argvars, rettv)
9994 typval_T *argvars;
9995 typval_T *rettv;
9996{
9997 rettv->v_type = VAR_STRING;
9998 rettv->vval.v_string = alloc(2);
9999 if (rettv->vval.v_string != NULL)
10000 {
10001 rettv->vval.v_string[0] = get_cmdline_type();
10002 rettv->vval.v_string[1] = NUL;
10003 }
10004}
10005
10006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 * "getcwd()" function
10008 */
10009/*ARGSUSED*/
10010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010012 typval_T *argvars;
10013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014{
10015 char_u cwd[MAXPATHL];
10016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 else
10021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010022 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010024 if (rettv->vval.v_string != NULL)
10025 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026#endif
10027 }
10028}
10029
10030/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010031 * "getfontname()" function
10032 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010033/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 typval_T *argvars;
10037 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010038{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->v_type = VAR_STRING;
10040 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010041#ifdef FEAT_GUI
10042 if (gui.in_use)
10043 {
10044 GuiFont font;
10045 char_u *name = NULL;
10046
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010047 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010048 {
10049 /* Get the "Normal" font. Either the name saved by
10050 * hl_set_font_name() or from the font ID. */
10051 font = gui.norm_font;
10052 name = hl_get_font_name();
10053 }
10054 else
10055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010057 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10058 return;
10059 font = gui_mch_get_font(name, FALSE);
10060 if (font == NOFONT)
10061 return; /* Invalid font name, return empty string. */
10062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010064 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010065 gui_mch_free_font(font);
10066 }
10067#endif
10068}
10069
10070/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010071 * "getfperm({fname})" function
10072 */
10073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010075 typval_T *argvars;
10076 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010077{
10078 char_u *fname;
10079 struct stat st;
10080 char_u *perm = NULL;
10081 char_u flags[] = "rwx";
10082 int i;
10083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010087 if (mch_stat((char *)fname, &st) >= 0)
10088 {
10089 perm = vim_strsave((char_u *)"---------");
10090 if (perm != NULL)
10091 {
10092 for (i = 0; i < 9; i++)
10093 {
10094 if (st.st_mode & (1 << (8 - i)))
10095 perm[i] = flags[i % 3];
10096 }
10097 }
10098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010100}
10101
10102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 * "getfsize({fname})" function
10104 */
10105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106f_getfsize(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 *fname;
10111 struct stat st;
10112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
10117 if (mch_stat((char *)fname, &st) >= 0)
10118 {
10119 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 }
10124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126}
10127
10128/*
10129 * "getftime({fname})" function
10130 */
10131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010133 typval_T *argvars;
10134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135{
10136 char_u *fname;
10137 struct stat st;
10138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140
10141 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145}
10146
10147/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010148 * "getftype({fname})" function
10149 */
10150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010152 typval_T *argvars;
10153 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010154{
10155 char_u *fname;
10156 struct stat st;
10157 char_u *type = NULL;
10158 char *t;
10159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010163 if (mch_lstat((char *)fname, &st) >= 0)
10164 {
10165#ifdef S_ISREG
10166 if (S_ISREG(st.st_mode))
10167 t = "file";
10168 else if (S_ISDIR(st.st_mode))
10169 t = "dir";
10170# ifdef S_ISLNK
10171 else if (S_ISLNK(st.st_mode))
10172 t = "link";
10173# endif
10174# ifdef S_ISBLK
10175 else if (S_ISBLK(st.st_mode))
10176 t = "bdev";
10177# endif
10178# ifdef S_ISCHR
10179 else if (S_ISCHR(st.st_mode))
10180 t = "cdev";
10181# endif
10182# ifdef S_ISFIFO
10183 else if (S_ISFIFO(st.st_mode))
10184 t = "fifo";
10185# endif
10186# ifdef S_ISSOCK
10187 else if (S_ISSOCK(st.st_mode))
10188 t = "fifo";
10189# endif
10190 else
10191 t = "other";
10192#else
10193# ifdef S_IFMT
10194 switch (st.st_mode & S_IFMT)
10195 {
10196 case S_IFREG: t = "file"; break;
10197 case S_IFDIR: t = "dir"; break;
10198# ifdef S_IFLNK
10199 case S_IFLNK: t = "link"; break;
10200# endif
10201# ifdef S_IFBLK
10202 case S_IFBLK: t = "bdev"; break;
10203# endif
10204# ifdef S_IFCHR
10205 case S_IFCHR: t = "cdev"; break;
10206# endif
10207# ifdef S_IFIFO
10208 case S_IFIFO: t = "fifo"; break;
10209# endif
10210# ifdef S_IFSOCK
10211 case S_IFSOCK: t = "socket"; break;
10212# endif
10213 default: t = "other";
10214 }
10215# else
10216 if (mch_isdir(fname))
10217 t = "dir";
10218 else
10219 t = "file";
10220# endif
10221#endif
10222 type = vim_strsave((char_u *)t);
10223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010224 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010225}
10226
10227/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010228 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010229 */
10230 static void
10231f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010232 typval_T *argvars;
10233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010234{
10235 linenr_T lnum;
10236 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010237 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010238
10239 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010240 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010241 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010242 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010243 retlist = FALSE;
10244 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010245 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010246 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010247 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010248 retlist = TRUE;
10249 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010250
Bram Moolenaar342337a2005-07-21 21:11:17 +000010251 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010252}
10253
10254/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010255 * "getpos(string)" function
10256 */
10257 static void
10258f_getpos(argvars, rettv)
10259 typval_T *argvars;
10260 typval_T *rettv;
10261{
10262 pos_T *fp;
10263 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010264 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010265
10266 if (rettv_list_alloc(rettv) == OK)
10267 {
10268 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010269 fp = var2fpos(&argvars[0], TRUE, &fnum);
10270 if (fnum != -1)
10271 list_append_number(l, (varnumber_T)fnum);
10272 else
10273 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010274 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10275 : (varnumber_T)0);
10276 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10277 : (varnumber_T)0);
10278 list_append_number(l,
10279#ifdef FEAT_VIRTUALEDIT
10280 (fp != NULL) ? (varnumber_T)fp->coladd :
10281#endif
10282 (varnumber_T)0);
10283 }
10284 else
10285 rettv->vval.v_number = FALSE;
10286}
10287
10288/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010289 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010290 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010291/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010292 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010293f_getqflist(argvars, rettv)
10294 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010295 typval_T *rettv;
10296{
10297#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010298 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010299#endif
10300
10301 rettv->vval.v_number = FALSE;
10302#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010303 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010304 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010305 wp = NULL;
10306 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10307 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010308 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010309 if (wp == NULL)
10310 return;
10311 }
10312
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010313 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010314 }
10315#endif
10316}
10317
10318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 * "getreg()" function
10320 */
10321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010322f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010323 typval_T *argvars;
10324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325{
10326 char_u *strregname;
10327 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010328 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010331 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 strregname = get_tv_string_chk(&argvars[0]);
10334 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010335 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010339 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 regname = (strregname == NULL ? '"' : *strregname);
10341 if (regname == 0)
10342 regname = '"';
10343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 rettv->vval.v_string = error ? NULL :
10346 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347}
10348
10349/*
10350 * "getregtype()" function
10351 */
10352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010353f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010354 typval_T *argvars;
10355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356{
10357 char_u *strregname;
10358 int regname;
10359 char_u buf[NUMBUFLEN + 2];
10360 long reglen = 0;
10361
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010362 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010363 {
10364 strregname = get_tv_string_chk(&argvars[0]);
10365 if (strregname == NULL) /* type error; errmsg already given */
10366 {
10367 rettv->v_type = VAR_STRING;
10368 rettv->vval.v_string = NULL;
10369 return;
10370 }
10371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 else
10373 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010374 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375
10376 regname = (strregname == NULL ? '"' : *strregname);
10377 if (regname == 0)
10378 regname = '"';
10379
10380 buf[0] = NUL;
10381 buf[1] = NUL;
10382 switch (get_reg_type(regname, &reglen))
10383 {
10384 case MLINE: buf[0] = 'V'; break;
10385 case MCHAR: buf[0] = 'v'; break;
10386#ifdef FEAT_VISUAL
10387 case MBLOCK:
10388 buf[0] = Ctrl_V;
10389 sprintf((char *)buf + 1, "%ld", reglen + 1);
10390 break;
10391#endif
10392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 rettv->v_type = VAR_STRING;
10394 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395}
10396
10397/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010398 * "gettabwinvar()" function
10399 */
10400 static void
10401f_gettabwinvar(argvars, rettv)
10402 typval_T *argvars;
10403 typval_T *rettv;
10404{
10405 getwinvar(argvars, rettv, 1);
10406}
10407
10408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 * "getwinposx()" function
10410 */
10411/*ARGSUSED*/
10412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010414 typval_T *argvars;
10415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418#ifdef FEAT_GUI
10419 if (gui.in_use)
10420 {
10421 int x, y;
10422
10423 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010424 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 }
10426#endif
10427}
10428
10429/*
10430 * "getwinposy()" function
10431 */
10432/*ARGSUSED*/
10433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 typval_T *argvars;
10436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439#ifdef FEAT_GUI
10440 if (gui.in_use)
10441 {
10442 int x, y;
10443
10444 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 }
10447#endif
10448}
10449
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010450/*
10451 * Find window specifed by "vp" in tabpage "tp".
10452 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010453 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010454find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010455 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010456 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010457{
10458#ifdef FEAT_WINDOWS
10459 win_T *wp;
10460#endif
10461 int nr;
10462
10463 nr = get_tv_number_chk(vp, NULL);
10464
10465#ifdef FEAT_WINDOWS
10466 if (nr < 0)
10467 return NULL;
10468 if (nr == 0)
10469 return curwin;
10470
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010471 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10472 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010473 if (--nr <= 0)
10474 break;
10475 return wp;
10476#else
10477 if (nr == 0 || nr == 1)
10478 return curwin;
10479 return NULL;
10480#endif
10481}
10482
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483/*
10484 * "getwinvar()" function
10485 */
10486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010488 typval_T *argvars;
10489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010491 getwinvar(argvars, rettv, 0);
10492}
10493
10494/*
10495 * getwinvar() and gettabwinvar()
10496 */
10497 static void
10498getwinvar(argvars, rettv, off)
10499 typval_T *argvars;
10500 typval_T *rettv;
10501 int off; /* 1 for gettabwinvar() */
10502{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 win_T *win, *oldcurwin;
10504 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010505 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010506 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010508#ifdef FEAT_WINDOWS
10509 if (off == 1)
10510 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10511 else
10512 tp = curtab;
10513#endif
10514 win = find_win_by_nr(&argvars[off], tp);
10515 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010518 rettv->v_type = VAR_STRING;
10519 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520
10521 if (win != NULL && varname != NULL)
10522 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010523 /* Set curwin to be our win, temporarily. Also set curbuf, so
10524 * that we can get buffer-local options. */
10525 oldcurwin = curwin;
10526 curwin = win;
10527 curbuf = win->w_buffer;
10528
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 else
10532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010533 if (*varname == NUL)
10534 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10535 * scope prefix before the NUL byte is required by
10536 * find_var_in_ht(). */
10537 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010539 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010541 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010543
10544 /* restore previous notion of curwin */
10545 curwin = oldcurwin;
10546 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 }
10548
10549 --emsg_off;
10550}
10551
10552/*
10553 * "glob()" function
10554 */
10555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010557 typval_T *argvars;
10558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
10560 expand_T xpc;
10561
10562 ExpandInit(&xpc);
10563 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564 rettv->v_type = VAR_STRING;
10565 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567}
10568
10569/*
10570 * "globpath()" function
10571 */
10572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 typval_T *argvars;
10575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576{
10577 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 if (file == NULL)
10582 rettv->vval.v_string = NULL;
10583 else
10584 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585}
10586
10587/*
10588 * "has()" function
10589 */
10590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010591f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010592 typval_T *argvars;
10593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594{
10595 int i;
10596 char_u *name;
10597 int n = FALSE;
10598 static char *(has_list[]) =
10599 {
10600#ifdef AMIGA
10601 "amiga",
10602# ifdef FEAT_ARP
10603 "arp",
10604# endif
10605#endif
10606#ifdef __BEOS__
10607 "beos",
10608#endif
10609#ifdef MSDOS
10610# ifdef DJGPP
10611 "dos32",
10612# else
10613 "dos16",
10614# endif
10615#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010616#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 "mac",
10618#endif
10619#if defined(MACOS_X_UNIX)
10620 "macunix",
10621#endif
10622#ifdef OS2
10623 "os2",
10624#endif
10625#ifdef __QNX__
10626 "qnx",
10627#endif
10628#ifdef RISCOS
10629 "riscos",
10630#endif
10631#ifdef UNIX
10632 "unix",
10633#endif
10634#ifdef VMS
10635 "vms",
10636#endif
10637#ifdef WIN16
10638 "win16",
10639#endif
10640#ifdef WIN32
10641 "win32",
10642#endif
10643#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10644 "win32unix",
10645#endif
10646#ifdef WIN64
10647 "win64",
10648#endif
10649#ifdef EBCDIC
10650 "ebcdic",
10651#endif
10652#ifndef CASE_INSENSITIVE_FILENAME
10653 "fname_case",
10654#endif
10655#ifdef FEAT_ARABIC
10656 "arabic",
10657#endif
10658#ifdef FEAT_AUTOCMD
10659 "autocmd",
10660#endif
10661#ifdef FEAT_BEVAL
10662 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010663# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10664 "balloon_multiline",
10665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666#endif
10667#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10668 "builtin_terms",
10669# ifdef ALL_BUILTIN_TCAPS
10670 "all_builtin_terms",
10671# endif
10672#endif
10673#ifdef FEAT_BYTEOFF
10674 "byte_offset",
10675#endif
10676#ifdef FEAT_CINDENT
10677 "cindent",
10678#endif
10679#ifdef FEAT_CLIENTSERVER
10680 "clientserver",
10681#endif
10682#ifdef FEAT_CLIPBOARD
10683 "clipboard",
10684#endif
10685#ifdef FEAT_CMDL_COMPL
10686 "cmdline_compl",
10687#endif
10688#ifdef FEAT_CMDHIST
10689 "cmdline_hist",
10690#endif
10691#ifdef FEAT_COMMENTS
10692 "comments",
10693#endif
10694#ifdef FEAT_CRYPT
10695 "cryptv",
10696#endif
10697#ifdef FEAT_CSCOPE
10698 "cscope",
10699#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010700#ifdef CURSOR_SHAPE
10701 "cursorshape",
10702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703#ifdef DEBUG
10704 "debug",
10705#endif
10706#ifdef FEAT_CON_DIALOG
10707 "dialog_con",
10708#endif
10709#ifdef FEAT_GUI_DIALOG
10710 "dialog_gui",
10711#endif
10712#ifdef FEAT_DIFF
10713 "diff",
10714#endif
10715#ifdef FEAT_DIGRAPHS
10716 "digraphs",
10717#endif
10718#ifdef FEAT_DND
10719 "dnd",
10720#endif
10721#ifdef FEAT_EMACS_TAGS
10722 "emacs_tags",
10723#endif
10724 "eval", /* always present, of course! */
10725#ifdef FEAT_EX_EXTRA
10726 "ex_extra",
10727#endif
10728#ifdef FEAT_SEARCH_EXTRA
10729 "extra_search",
10730#endif
10731#ifdef FEAT_FKMAP
10732 "farsi",
10733#endif
10734#ifdef FEAT_SEARCHPATH
10735 "file_in_path",
10736#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010737#if defined(UNIX) && !defined(USE_SYSTEM)
10738 "filterpipe",
10739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740#ifdef FEAT_FIND_ID
10741 "find_in_path",
10742#endif
10743#ifdef FEAT_FOLDING
10744 "folding",
10745#endif
10746#ifdef FEAT_FOOTER
10747 "footer",
10748#endif
10749#if !defined(USE_SYSTEM) && defined(UNIX)
10750 "fork",
10751#endif
10752#ifdef FEAT_GETTEXT
10753 "gettext",
10754#endif
10755#ifdef FEAT_GUI
10756 "gui",
10757#endif
10758#ifdef FEAT_GUI_ATHENA
10759# ifdef FEAT_GUI_NEXTAW
10760 "gui_neXtaw",
10761# else
10762 "gui_athena",
10763# endif
10764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765#ifdef FEAT_GUI_GTK
10766 "gui_gtk",
10767# ifdef HAVE_GTK2
10768 "gui_gtk2",
10769# endif
10770#endif
10771#ifdef FEAT_GUI_MAC
10772 "gui_mac",
10773#endif
10774#ifdef FEAT_GUI_MOTIF
10775 "gui_motif",
10776#endif
10777#ifdef FEAT_GUI_PHOTON
10778 "gui_photon",
10779#endif
10780#ifdef FEAT_GUI_W16
10781 "gui_win16",
10782#endif
10783#ifdef FEAT_GUI_W32
10784 "gui_win32",
10785#endif
10786#ifdef FEAT_HANGULIN
10787 "hangul_input",
10788#endif
10789#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10790 "iconv",
10791#endif
10792#ifdef FEAT_INS_EXPAND
10793 "insert_expand",
10794#endif
10795#ifdef FEAT_JUMPLIST
10796 "jumplist",
10797#endif
10798#ifdef FEAT_KEYMAP
10799 "keymap",
10800#endif
10801#ifdef FEAT_LANGMAP
10802 "langmap",
10803#endif
10804#ifdef FEAT_LIBCALL
10805 "libcall",
10806#endif
10807#ifdef FEAT_LINEBREAK
10808 "linebreak",
10809#endif
10810#ifdef FEAT_LISP
10811 "lispindent",
10812#endif
10813#ifdef FEAT_LISTCMDS
10814 "listcmds",
10815#endif
10816#ifdef FEAT_LOCALMAP
10817 "localmap",
10818#endif
10819#ifdef FEAT_MENU
10820 "menu",
10821#endif
10822#ifdef FEAT_SESSION
10823 "mksession",
10824#endif
10825#ifdef FEAT_MODIFY_FNAME
10826 "modify_fname",
10827#endif
10828#ifdef FEAT_MOUSE
10829 "mouse",
10830#endif
10831#ifdef FEAT_MOUSESHAPE
10832 "mouseshape",
10833#endif
10834#if defined(UNIX) || defined(VMS)
10835# ifdef FEAT_MOUSE_DEC
10836 "mouse_dec",
10837# endif
10838# ifdef FEAT_MOUSE_GPM
10839 "mouse_gpm",
10840# endif
10841# ifdef FEAT_MOUSE_JSB
10842 "mouse_jsbterm",
10843# endif
10844# ifdef FEAT_MOUSE_NET
10845 "mouse_netterm",
10846# endif
10847# ifdef FEAT_MOUSE_PTERM
10848 "mouse_pterm",
10849# endif
10850# ifdef FEAT_MOUSE_XTERM
10851 "mouse_xterm",
10852# endif
10853#endif
10854#ifdef FEAT_MBYTE
10855 "multi_byte",
10856#endif
10857#ifdef FEAT_MBYTE_IME
10858 "multi_byte_ime",
10859#endif
10860#ifdef FEAT_MULTI_LANG
10861 "multi_lang",
10862#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010863#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010864#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010865 "mzscheme",
10866#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868#ifdef FEAT_OLE
10869 "ole",
10870#endif
10871#ifdef FEAT_OSFILETYPE
10872 "osfiletype",
10873#endif
10874#ifdef FEAT_PATH_EXTRA
10875 "path_extra",
10876#endif
10877#ifdef FEAT_PERL
10878#ifndef DYNAMIC_PERL
10879 "perl",
10880#endif
10881#endif
10882#ifdef FEAT_PYTHON
10883#ifndef DYNAMIC_PYTHON
10884 "python",
10885#endif
10886#endif
10887#ifdef FEAT_POSTSCRIPT
10888 "postscript",
10889#endif
10890#ifdef FEAT_PRINTER
10891 "printer",
10892#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010893#ifdef FEAT_PROFILE
10894 "profile",
10895#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010896#ifdef FEAT_RELTIME
10897 "reltime",
10898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899#ifdef FEAT_QUICKFIX
10900 "quickfix",
10901#endif
10902#ifdef FEAT_RIGHTLEFT
10903 "rightleft",
10904#endif
10905#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10906 "ruby",
10907#endif
10908#ifdef FEAT_SCROLLBIND
10909 "scrollbind",
10910#endif
10911#ifdef FEAT_CMDL_INFO
10912 "showcmd",
10913 "cmdline_info",
10914#endif
10915#ifdef FEAT_SIGNS
10916 "signs",
10917#endif
10918#ifdef FEAT_SMARTINDENT
10919 "smartindent",
10920#endif
10921#ifdef FEAT_SNIFF
10922 "sniff",
10923#endif
10924#ifdef FEAT_STL_OPT
10925 "statusline",
10926#endif
10927#ifdef FEAT_SUN_WORKSHOP
10928 "sun_workshop",
10929#endif
10930#ifdef FEAT_NETBEANS_INTG
10931 "netbeans_intg",
10932#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010933#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010934 "spell",
10935#endif
10936#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 "syntax",
10938#endif
10939#if defined(USE_SYSTEM) || !defined(UNIX)
10940 "system",
10941#endif
10942#ifdef FEAT_TAG_BINS
10943 "tag_binary",
10944#endif
10945#ifdef FEAT_TAG_OLDSTATIC
10946 "tag_old_static",
10947#endif
10948#ifdef FEAT_TAG_ANYWHITE
10949 "tag_any_white",
10950#endif
10951#ifdef FEAT_TCL
10952# ifndef DYNAMIC_TCL
10953 "tcl",
10954# endif
10955#endif
10956#ifdef TERMINFO
10957 "terminfo",
10958#endif
10959#ifdef FEAT_TERMRESPONSE
10960 "termresponse",
10961#endif
10962#ifdef FEAT_TEXTOBJ
10963 "textobjects",
10964#endif
10965#ifdef HAVE_TGETENT
10966 "tgetent",
10967#endif
10968#ifdef FEAT_TITLE
10969 "title",
10970#endif
10971#ifdef FEAT_TOOLBAR
10972 "toolbar",
10973#endif
10974#ifdef FEAT_USR_CMDS
10975 "user-commands", /* was accidentally included in 5.4 */
10976 "user_commands",
10977#endif
10978#ifdef FEAT_VIMINFO
10979 "viminfo",
10980#endif
10981#ifdef FEAT_VERTSPLIT
10982 "vertsplit",
10983#endif
10984#ifdef FEAT_VIRTUALEDIT
10985 "virtualedit",
10986#endif
10987#ifdef FEAT_VISUAL
10988 "visual",
10989#endif
10990#ifdef FEAT_VISUALEXTRA
10991 "visualextra",
10992#endif
10993#ifdef FEAT_VREPLACE
10994 "vreplace",
10995#endif
10996#ifdef FEAT_WILDIGN
10997 "wildignore",
10998#endif
10999#ifdef FEAT_WILDMENU
11000 "wildmenu",
11001#endif
11002#ifdef FEAT_WINDOWS
11003 "windows",
11004#endif
11005#ifdef FEAT_WAK
11006 "winaltkeys",
11007#endif
11008#ifdef FEAT_WRITEBACKUP
11009 "writebackup",
11010#endif
11011#ifdef FEAT_XIM
11012 "xim",
11013#endif
11014#ifdef FEAT_XFONTSET
11015 "xfontset",
11016#endif
11017#ifdef USE_XSMP
11018 "xsmp",
11019#endif
11020#ifdef USE_XSMP_INTERACT
11021 "xsmp_interact",
11022#endif
11023#ifdef FEAT_XCLIPBOARD
11024 "xterm_clipboard",
11025#endif
11026#ifdef FEAT_XTERM_SAVE
11027 "xterm_save",
11028#endif
11029#if defined(UNIX) && defined(FEAT_X11)
11030 "X11",
11031#endif
11032 NULL
11033 };
11034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 for (i = 0; has_list[i] != NULL; ++i)
11037 if (STRICMP(name, has_list[i]) == 0)
11038 {
11039 n = TRUE;
11040 break;
11041 }
11042
11043 if (n == FALSE)
11044 {
11045 if (STRNICMP(name, "patch", 5) == 0)
11046 n = has_patch(atoi((char *)name + 5));
11047 else if (STRICMP(name, "vim_starting") == 0)
11048 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011049#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11050 else if (STRICMP(name, "balloon_multiline") == 0)
11051 n = multiline_balloon_available();
11052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053#ifdef DYNAMIC_TCL
11054 else if (STRICMP(name, "tcl") == 0)
11055 n = tcl_enabled(FALSE);
11056#endif
11057#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11058 else if (STRICMP(name, "iconv") == 0)
11059 n = iconv_enabled(FALSE);
11060#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011061#ifdef DYNAMIC_MZSCHEME
11062 else if (STRICMP(name, "mzscheme") == 0)
11063 n = mzscheme_enabled(FALSE);
11064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065#ifdef DYNAMIC_RUBY
11066 else if (STRICMP(name, "ruby") == 0)
11067 n = ruby_enabled(FALSE);
11068#endif
11069#ifdef DYNAMIC_PYTHON
11070 else if (STRICMP(name, "python") == 0)
11071 n = python_enabled(FALSE);
11072#endif
11073#ifdef DYNAMIC_PERL
11074 else if (STRICMP(name, "perl") == 0)
11075 n = perl_enabled(FALSE);
11076#endif
11077#ifdef FEAT_GUI
11078 else if (STRICMP(name, "gui_running") == 0)
11079 n = (gui.in_use || gui.starting);
11080# ifdef FEAT_GUI_W32
11081 else if (STRICMP(name, "gui_win32s") == 0)
11082 n = gui_is_win32s();
11083# endif
11084# ifdef FEAT_BROWSE
11085 else if (STRICMP(name, "browse") == 0)
11086 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11087# endif
11088#endif
11089#ifdef FEAT_SYN_HL
11090 else if (STRICMP(name, "syntax_items") == 0)
11091 n = syntax_present(curbuf);
11092#endif
11093#if defined(WIN3264)
11094 else if (STRICMP(name, "win95") == 0)
11095 n = mch_windows95();
11096#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011097#ifdef FEAT_NETBEANS_INTG
11098 else if (STRICMP(name, "netbeans_enabled") == 0)
11099 n = usingNetbeans;
11100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 }
11102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104}
11105
11106/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 * "has_key()" function
11108 */
11109 static void
11110f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 typval_T *argvars;
11112 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113{
11114 rettv->vval.v_number = 0;
11115 if (argvars[0].v_type != VAR_DICT)
11116 {
11117 EMSG(_(e_dictreq));
11118 return;
11119 }
11120 if (argvars[0].vval.v_dict == NULL)
11121 return;
11122
11123 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011124 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125}
11126
11127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 * "hasmapto()" function
11129 */
11130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131f_hasmapto(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 char_u *name;
11136 char_u *mode;
11137 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011138 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011141 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 mode = (char_u *)"nvo";
11143 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011146 if (argvars[2].v_type != VAR_UNKNOWN)
11147 abbr = get_tv_number(&argvars[2]);
11148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149
Bram Moolenaar2c932302006-03-18 21:42:09 +000011150 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154}
11155
11156/*
11157 * "histadd()" function
11158 */
11159/*ARGSUSED*/
11160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011162 typval_T *argvars;
11163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164{
11165#ifdef FEAT_CMDHIST
11166 int histype;
11167 char_u *str;
11168 char_u buf[NUMBUFLEN];
11169#endif
11170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 if (check_restricted() || check_secure())
11173 return;
11174#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11176 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 if (histype >= 0)
11178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 if (*str != NUL)
11181 {
11182 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 return;
11185 }
11186 }
11187#endif
11188}
11189
11190/*
11191 * "histdel()" function
11192 */
11193/*ARGSUSED*/
11194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 typval_T *argvars;
11197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198{
11199#ifdef FEAT_CMDHIST
11200 int n;
11201 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11205 if (str == NULL)
11206 n = 0;
11207 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011210 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011212 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 else
11215 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 get_tv_string_buf(&argvars[1], buf));
11218 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221#endif
11222}
11223
11224/*
11225 * "histget()" function
11226 */
11227/*ARGSUSED*/
11228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011230 typval_T *argvars;
11231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232{
11233#ifdef FEAT_CMDHIST
11234 int type;
11235 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011236 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011238 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11239 if (str == NULL)
11240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242 {
11243 type = get_histtype(str);
11244 if (argvars[1].v_type == VAR_UNKNOWN)
11245 idx = get_history_idx(type);
11246 else
11247 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11248 /* -1 on type error */
11249 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255}
11256
11257/*
11258 * "histnr()" function
11259 */
11260/*ARGSUSED*/
11261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *argvars;
11264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265{
11266 int i;
11267
11268#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 char_u *history = get_tv_string_chk(&argvars[0]);
11270
11271 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 if (i >= HIST_CMD && i < HIST_COUNT)
11273 i = get_history_idx(i);
11274 else
11275#endif
11276 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
11280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 * "highlightID(name)" function
11282 */
11283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011285 typval_T *argvars;
11286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289}
11290
11291/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011292 * "highlight_exists()" function
11293 */
11294 static void
11295f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *argvars;
11297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298{
11299 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11300}
11301
11302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 * "hostname()" function
11304 */
11305/*ARGSUSED*/
11306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *argvars;
11309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310{
11311 char_u hostname[256];
11312
11313 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 rettv->v_type = VAR_STRING;
11315 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316}
11317
11318/*
11319 * iconv() function
11320 */
11321/*ARGSUSED*/
11322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *argvars;
11325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326{
11327#ifdef FEAT_MBYTE
11328 char_u buf1[NUMBUFLEN];
11329 char_u buf2[NUMBUFLEN];
11330 char_u *from, *to, *str;
11331 vimconv_T vimconv;
11332#endif
11333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 rettv->v_type = VAR_STRING;
11335 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336
11337#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 str = get_tv_string(&argvars[0]);
11339 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11340 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 vimconv.vc_type = CONV_NONE;
11342 convert_setup(&vimconv, from, to);
11343
11344 /* If the encodings are equal, no conversion needed. */
11345 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349
11350 convert_setup(&vimconv, NULL, NULL);
11351 vim_free(from);
11352 vim_free(to);
11353#endif
11354}
11355
11356/*
11357 * "indent()" function
11358 */
11359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011361 typval_T *argvars;
11362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363{
11364 linenr_T lnum;
11365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371}
11372
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011373/*
11374 * "index()" function
11375 */
11376 static void
11377f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011378 typval_T *argvars;
11379 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011380{
Bram Moolenaar33570922005-01-25 22:26:29 +000011381 list_T *l;
11382 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011383 long idx = 0;
11384 int ic = FALSE;
11385
11386 rettv->vval.v_number = -1;
11387 if (argvars[0].v_type != VAR_LIST)
11388 {
11389 EMSG(_(e_listreq));
11390 return;
11391 }
11392 l = argvars[0].vval.v_list;
11393 if (l != NULL)
11394 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011395 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011396 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 int error = FALSE;
11399
Bram Moolenaar758711c2005-02-02 23:11:38 +000011400 /* Start at specified item. Use the cached index that list_find()
11401 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011403 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011404 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 ic = get_tv_number_chk(&argvars[3], &error);
11406 if (error)
11407 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011408 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011409
Bram Moolenaar758711c2005-02-02 23:11:38 +000011410 for ( ; item != NULL; item = item->li_next, ++idx)
11411 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011412 {
11413 rettv->vval.v_number = idx;
11414 break;
11415 }
11416 }
11417}
11418
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419static int inputsecret_flag = 0;
11420
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011421static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11422
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011424 * This function is used by f_input() and f_inputdialog() functions. The third
11425 * argument to f_input() specifies the type of completion to use at the
11426 * prompt. The third argument to f_inputdialog() specifies the value to return
11427 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 */
11429 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011430get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 typval_T *argvars;
11432 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011433 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 char_u *p = NULL;
11437 int c;
11438 char_u buf[NUMBUFLEN];
11439 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011440 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011441 int xp_type = EXPAND_NOTHING;
11442 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445
11446#ifdef NO_CONSOLE_INPUT
11447 /* While starting up, there is no place to enter text. */
11448 if (no_console_input())
11449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 return;
11452 }
11453#endif
11454
11455 cmd_silent = FALSE; /* Want to see the prompt. */
11456 if (prompt != NULL)
11457 {
11458 /* Only the part of the message after the last NL is considered as
11459 * prompt for the command line */
11460 p = vim_strrchr(prompt, '\n');
11461 if (p == NULL)
11462 p = prompt;
11463 else
11464 {
11465 ++p;
11466 c = *p;
11467 *p = NUL;
11468 msg_start();
11469 msg_clr_eos();
11470 msg_puts_attr(prompt, echo_attr);
11471 msg_didout = FALSE;
11472 msg_starthere();
11473 *p = c;
11474 }
11475 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 if (argvars[1].v_type != VAR_UNKNOWN)
11478 {
11479 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11480 if (defstr != NULL)
11481 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011483 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011484 {
11485 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011486 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011487 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011488
Bram Moolenaar4463f292005-09-25 22:20:24 +000011489 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011490
Bram Moolenaar4463f292005-09-25 22:20:24 +000011491 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11492 if (xp_name == NULL)
11493 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011494
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011495 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011496
Bram Moolenaar4463f292005-09-25 22:20:24 +000011497 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11498 &xp_arg) == FAIL)
11499 return;
11500 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011501 }
11502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011503 if (defstr != NULL)
11504 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011505 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11506 xp_type, xp_arg);
11507
11508 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011510 /* since the user typed this, no need to wait for return */
11511 need_wait_return = FALSE;
11512 msg_didout = FALSE;
11513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 cmd_silent = cmd_silent_save;
11515}
11516
11517/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011518 * "input()" function
11519 * Also handles inputsecret() when inputsecret is set.
11520 */
11521 static void
11522f_input(argvars, rettv)
11523 typval_T *argvars;
11524 typval_T *rettv;
11525{
11526 get_user_input(argvars, rettv, FALSE);
11527}
11528
11529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 * "inputdialog()" function
11531 */
11532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011534 typval_T *argvars;
11535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536{
11537#if defined(FEAT_GUI_TEXTDIALOG)
11538 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11539 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11540 {
11541 char_u *message;
11542 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 message = get_tv_string_chk(&argvars[0]);
11546 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011547 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011548 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 else
11550 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011551 if (message != NULL && defstr != NULL
11552 && do_dialog(VIM_QUESTION, NULL, message,
11553 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 else
11556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011557 if (message != NULL && defstr != NULL
11558 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011559 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_string = vim_strsave(
11561 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 }
11567 else
11568#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011569 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570}
11571
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011572/*
11573 * "inputlist()" function
11574 */
11575 static void
11576f_inputlist(argvars, rettv)
11577 typval_T *argvars;
11578 typval_T *rettv;
11579{
11580 listitem_T *li;
11581 int selected;
11582 int mouse_used;
11583
11584 rettv->vval.v_number = 0;
11585#ifdef NO_CONSOLE_INPUT
11586 /* While starting up, there is no place to enter text. */
11587 if (no_console_input())
11588 return;
11589#endif
11590 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11591 {
11592 EMSG2(_(e_listarg), "inputlist()");
11593 return;
11594 }
11595
11596 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011597 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011598 lines_left = Rows; /* avoid more prompt */
11599 msg_scroll = TRUE;
11600 msg_clr_eos();
11601
11602 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11603 {
11604 msg_puts(get_tv_string(&li->li_tv));
11605 msg_putchar('\n');
11606 }
11607
11608 /* Ask for choice. */
11609 selected = prompt_for_number(&mouse_used);
11610 if (mouse_used)
11611 selected -= lines_left;
11612
11613 rettv->vval.v_number = selected;
11614}
11615
11616
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11618
11619/*
11620 * "inputrestore()" function
11621 */
11622/*ARGSUSED*/
11623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011625 typval_T *argvars;
11626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627{
11628 if (ga_userinput.ga_len > 0)
11629 {
11630 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11632 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 }
11635 else if (p_verbose > 1)
11636 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011637 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 }
11640}
11641
11642/*
11643 * "inputsave()" function
11644 */
11645/*ARGSUSED*/
11646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *argvars;
11649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650{
11651 /* Add an entry to the stack of typehead storage. */
11652 if (ga_grow(&ga_userinput, 1) == OK)
11653 {
11654 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11655 + ga_userinput.ga_len);
11656 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 }
11659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011660 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661}
11662
11663/*
11664 * "inputsecret()" function
11665 */
11666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011668 typval_T *argvars;
11669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670{
11671 ++cmdline_star;
11672 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 --cmdline_star;
11675 --inputsecret_flag;
11676}
11677
11678/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011679 * "insert()" function
11680 */
11681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 typval_T *argvars;
11684 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011685{
11686 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011687 listitem_T *item;
11688 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011690
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011691 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011692 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011693 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011694 else if ((l = argvars[0].vval.v_list) != NULL
11695 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011696 {
11697 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011698 before = get_tv_number_chk(&argvars[2], &error);
11699 if (error)
11700 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011701
Bram Moolenaar758711c2005-02-02 23:11:38 +000011702 if (before == l->lv_len)
11703 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011704 else
11705 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011706 item = list_find(l, before);
11707 if (item == NULL)
11708 {
11709 EMSGN(_(e_listidx), before);
11710 l = NULL;
11711 }
11712 }
11713 if (l != NULL)
11714 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011715 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011716 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011717 }
11718 }
11719}
11720
11721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 * "isdirectory()" function
11723 */
11724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011726 typval_T *argvars;
11727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730}
11731
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011732/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011733 * "islocked()" function
11734 */
11735 static void
11736f_islocked(argvars, rettv)
11737 typval_T *argvars;
11738 typval_T *rettv;
11739{
11740 lval_T lv;
11741 char_u *end;
11742 dictitem_T *di;
11743
11744 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011745 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11746 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011747 if (end != NULL && lv.ll_name != NULL)
11748 {
11749 if (*end != NUL)
11750 EMSG(_(e_trailing));
11751 else
11752 {
11753 if (lv.ll_tv == NULL)
11754 {
11755 if (check_changedtick(lv.ll_name))
11756 rettv->vval.v_number = 1; /* always locked */
11757 else
11758 {
11759 di = find_var(lv.ll_name, NULL);
11760 if (di != NULL)
11761 {
11762 /* Consider a variable locked when:
11763 * 1. the variable itself is locked
11764 * 2. the value of the variable is locked.
11765 * 3. the List or Dict value is locked.
11766 */
11767 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11768 || tv_islocked(&di->di_tv));
11769 }
11770 }
11771 }
11772 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011773 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011774 else if (lv.ll_newkey != NULL)
11775 EMSG2(_(e_dictkey), lv.ll_newkey);
11776 else if (lv.ll_list != NULL)
11777 /* List item. */
11778 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11779 else
11780 /* Dictionary item. */
11781 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11782 }
11783 }
11784
11785 clear_lval(&lv);
11786}
11787
Bram Moolenaar33570922005-01-25 22:26:29 +000011788static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011789
11790/*
11791 * Turn a dict into a list:
11792 * "what" == 0: list of keys
11793 * "what" == 1: list of values
11794 * "what" == 2: list of items
11795 */
11796 static void
11797dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011798 typval_T *argvars;
11799 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011800 int what;
11801{
Bram Moolenaar33570922005-01-25 22:26:29 +000011802 list_T *l2;
11803 dictitem_T *di;
11804 hashitem_T *hi;
11805 listitem_T *li;
11806 listitem_T *li2;
11807 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011808 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011809
11810 rettv->vval.v_number = 0;
11811 if (argvars[0].v_type != VAR_DICT)
11812 {
11813 EMSG(_(e_dictreq));
11814 return;
11815 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011816 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011817 return;
11818
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011819 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011820 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011821
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011822 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011823 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011824 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011825 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011826 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011827 --todo;
11828 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011829
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011830 li = listitem_alloc();
11831 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011832 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011833 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011834
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011835 if (what == 0)
11836 {
11837 /* keys() */
11838 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011839 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011840 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11841 }
11842 else if (what == 1)
11843 {
11844 /* values() */
11845 copy_tv(&di->di_tv, &li->li_tv);
11846 }
11847 else
11848 {
11849 /* items() */
11850 l2 = list_alloc();
11851 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011852 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011853 li->li_tv.vval.v_list = l2;
11854 if (l2 == NULL)
11855 break;
11856 ++l2->lv_refcount;
11857
11858 li2 = listitem_alloc();
11859 if (li2 == NULL)
11860 break;
11861 list_append(l2, li2);
11862 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011863 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011864 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11865
11866 li2 = listitem_alloc();
11867 if (li2 == NULL)
11868 break;
11869 list_append(l2, li2);
11870 copy_tv(&di->di_tv, &li2->li_tv);
11871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011872 }
11873 }
11874}
11875
11876/*
11877 * "items(dict)" function
11878 */
11879 static void
11880f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011881 typval_T *argvars;
11882 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011883{
11884 dict_list(argvars, rettv, 2);
11885}
11886
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011888 * "join()" function
11889 */
11890 static void
11891f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011892 typval_T *argvars;
11893 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011894{
11895 garray_T ga;
11896 char_u *sep;
11897
11898 rettv->vval.v_number = 0;
11899 if (argvars[0].v_type != VAR_LIST)
11900 {
11901 EMSG(_(e_listreq));
11902 return;
11903 }
11904 if (argvars[0].vval.v_list == NULL)
11905 return;
11906 if (argvars[1].v_type == VAR_UNKNOWN)
11907 sep = (char_u *)" ";
11908 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011909 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011910
11911 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912
11913 if (sep != NULL)
11914 {
11915 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011916 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011917 ga_append(&ga, NUL);
11918 rettv->vval.v_string = (char_u *)ga.ga_data;
11919 }
11920 else
11921 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011922}
11923
11924/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011925 * "keys()" function
11926 */
11927 static void
11928f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011929 typval_T *argvars;
11930 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011931{
11932 dict_list(argvars, rettv, 0);
11933}
11934
11935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 * "last_buffer_nr()" function.
11937 */
11938/*ARGSUSED*/
11939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011941 typval_T *argvars;
11942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943{
11944 int n = 0;
11945 buf_T *buf;
11946
11947 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11948 if (n < buf->b_fnum)
11949 n = buf->b_fnum;
11950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011952}
11953
11954/*
11955 * "len()" function
11956 */
11957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011959 typval_T *argvars;
11960 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011961{
11962 switch (argvars[0].v_type)
11963 {
11964 case VAR_STRING:
11965 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966 rettv->vval.v_number = (varnumber_T)STRLEN(
11967 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011968 break;
11969 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011971 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011972 case VAR_DICT:
11973 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11974 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011975 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011976 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011977 break;
11978 }
11979}
11980
Bram Moolenaar33570922005-01-25 22:26:29 +000011981static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011982
11983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011985 typval_T *argvars;
11986 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011987 int type;
11988{
11989#ifdef FEAT_LIBCALL
11990 char_u *string_in;
11991 char_u **string_result;
11992 int nr_result;
11993#endif
11994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011996 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011998 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012000
12001 if (check_restricted() || check_secure())
12002 return;
12003
12004#ifdef FEAT_LIBCALL
12005 /* The first two args must be strings, otherwise its meaningless */
12006 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12007 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012008 string_in = NULL;
12009 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012010 string_in = argvars[2].vval.v_string;
12011 if (type == VAR_NUMBER)
12012 string_result = NULL;
12013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012015 if (mch_libcall(argvars[0].vval.v_string,
12016 argvars[1].vval.v_string,
12017 string_in,
12018 argvars[2].vval.v_number,
12019 string_result,
12020 &nr_result) == OK
12021 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012023 }
12024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025}
12026
12027/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012028 * "libcall()" function
12029 */
12030 static void
12031f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012032 typval_T *argvars;
12033 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012034{
12035 libcall_common(argvars, rettv, VAR_STRING);
12036}
12037
12038/*
12039 * "libcallnr()" function
12040 */
12041 static void
12042f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012043 typval_T *argvars;
12044 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012045{
12046 libcall_common(argvars, rettv, VAR_NUMBER);
12047}
12048
12049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 * "line(string)" function
12051 */
12052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012054 typval_T *argvars;
12055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056{
12057 linenr_T lnum = 0;
12058 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012059 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012061 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 if (fp != NULL)
12063 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065}
12066
12067/*
12068 * "line2byte(lnum)" function
12069 */
12070/*ARGSUSED*/
12071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012073 typval_T *argvars;
12074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075{
12076#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078#else
12079 linenr_T lnum;
12080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12086 if (rettv->vval.v_number >= 0)
12087 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088#endif
12089}
12090
12091/*
12092 * "lispindent(lnum)" function
12093 */
12094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
12099#ifdef FEAT_LISP
12100 pos_T pos;
12101 linenr_T lnum;
12102
12103 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012104 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12106 {
12107 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 curwin->w_cursor = pos;
12110 }
12111 else
12112#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114}
12115
12116/*
12117 * "localtime()" function
12118 */
12119/*ARGSUSED*/
12120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012122 typval_T *argvars;
12123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126}
12127
Bram Moolenaar33570922005-01-25 22:26:29 +000012128static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129
12130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 typval_T *argvars;
12133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 int exact;
12135{
12136 char_u *keys;
12137 char_u *which;
12138 char_u buf[NUMBUFLEN];
12139 char_u *keys_buf = NULL;
12140 char_u *rhs;
12141 int mode;
12142 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012143 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144
12145 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 rettv->v_type = VAR_STRING;
12147 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 if (*keys == NUL)
12151 return;
12152
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012153 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012155 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012156 if (argvars[2].v_type != VAR_UNKNOWN)
12157 abbr = get_tv_number(&argvars[2]);
12158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 else
12160 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012161 if (which == NULL)
12162 return;
12163
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 mode = get_map_mode(&which, 0);
12165
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012166 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012167 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 vim_free(keys_buf);
12169 if (rhs != NULL)
12170 {
12171 ga_init(&ga);
12172 ga.ga_itemsize = 1;
12173 ga.ga_growsize = 40;
12174
12175 while (*rhs != NUL)
12176 ga_concat(&ga, str2special(&rhs, FALSE));
12177
12178 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 }
12181}
12182
12183/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012184 * "map()" function
12185 */
12186 static void
12187f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012188 typval_T *argvars;
12189 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012190{
12191 filter_map(argvars, rettv, TRUE);
12192}
12193
12194/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196 */
12197 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012198f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012199 typval_T *argvars;
12200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012202 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203}
12204
12205/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 */
12208 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012209f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012210 typval_T *argvars;
12211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214}
12215
Bram Moolenaar33570922005-01-25 22:26:29 +000012216static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217
12218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012220 typval_T *argvars;
12221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 int type;
12223{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012224 char_u *str = NULL;
12225 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 char_u *pat;
12227 regmatch_T regmatch;
12228 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012229 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 char_u *save_cpo;
12231 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012232 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012233 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012234 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012235 list_T *l = NULL;
12236 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012237 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012238 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239
12240 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12241 save_cpo = p_cpo;
12242 p_cpo = (char_u *)"";
12243
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012244 rettv->vval.v_number = -1;
12245 if (type == 3)
12246 {
12247 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012248 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012249 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012250 }
12251 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012253 rettv->v_type = VAR_STRING;
12254 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012257 if (argvars[0].v_type == VAR_LIST)
12258 {
12259 if ((l = argvars[0].vval.v_list) == NULL)
12260 goto theend;
12261 li = l->lv_first;
12262 }
12263 else
12264 expr = str = get_tv_string(&argvars[0]);
12265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012266 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12267 if (pat == NULL)
12268 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012269
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012272 int error = FALSE;
12273
12274 start = get_tv_number_chk(&argvars[2], &error);
12275 if (error)
12276 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012277 if (l != NULL)
12278 {
12279 li = list_find(l, start);
12280 if (li == NULL)
12281 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012282 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012283 }
12284 else
12285 {
12286 if (start < 0)
12287 start = 0;
12288 if (start > (long)STRLEN(str))
12289 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012290 /* When "count" argument is there ignore matches before "start",
12291 * otherwise skip part of the string. Differs when pattern is "^"
12292 * or "\<". */
12293 if (argvars[3].v_type != VAR_UNKNOWN)
12294 startcol = start;
12295 else
12296 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012297 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012298
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012299 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012300 nth = get_tv_number_chk(&argvars[3], &error);
12301 if (error)
12302 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 }
12304
12305 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12306 if (regmatch.regprog != NULL)
12307 {
12308 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012309
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012310 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012311 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012312 if (l != NULL)
12313 {
12314 if (li == NULL)
12315 {
12316 match = FALSE;
12317 break;
12318 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012319 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012320 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012321 if (str == NULL)
12322 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012323 }
12324
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012325 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012326
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012327 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012328 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012329 if (l == NULL && !match)
12330 break;
12331
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012332 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012333 if (l != NULL)
12334 {
12335 li = li->li_next;
12336 ++idx;
12337 }
12338 else
12339 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012340#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012341 startcol = (colnr_T)(regmatch.startp[0]
12342 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012343#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012344 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012345#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012346 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012347 }
12348
12349 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012351 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012352 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012353 int i;
12354
12355 /* return list with matched string and submatches */
12356 for (i = 0; i < NSUBEXP; ++i)
12357 {
12358 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012359 {
12360 if (list_append_string(rettv->vval.v_list,
12361 (char_u *)"", 0) == FAIL)
12362 break;
12363 }
12364 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012365 regmatch.startp[i],
12366 (int)(regmatch.endp[i] - regmatch.startp[i]))
12367 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012368 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012369 }
12370 }
12371 else if (type == 2)
12372 {
12373 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012374 if (l != NULL)
12375 copy_tv(&li->li_tv, rettv);
12376 else
12377 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012379 }
12380 else if (l != NULL)
12381 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 else
12383 {
12384 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 (varnumber_T)(regmatch.startp[0] - str);
12387 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012390 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 }
12392 }
12393 vim_free(regmatch.regprog);
12394 }
12395
12396theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012397 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 p_cpo = save_cpo;
12399}
12400
12401/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012402 * "match()" function
12403 */
12404 static void
12405f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012406 typval_T *argvars;
12407 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012408{
12409 find_some_match(argvars, rettv, 1);
12410}
12411
12412/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012413 * "matcharg()" function
12414 */
12415 static void
12416f_matcharg(argvars, rettv)
12417 typval_T *argvars;
12418 typval_T *rettv;
12419{
12420 if (rettv_list_alloc(rettv) == OK)
12421 {
12422#ifdef FEAT_SEARCH_EXTRA
12423 int mi = get_tv_number(&argvars[0]);
12424
12425 if (mi >= 1 && mi <= 3)
12426 {
12427 list_append_string(rettv->vval.v_list,
12428 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12429 list_append_string(rettv->vval.v_list,
12430 curwin->w_match_pat[mi - 1], -1);
12431 }
12432#endif
12433 }
12434}
12435
12436/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012437 * "matchend()" function
12438 */
12439 static void
12440f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012441 typval_T *argvars;
12442 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443{
12444 find_some_match(argvars, rettv, 0);
12445}
12446
12447/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012448 * "matchlist()" function
12449 */
12450 static void
12451f_matchlist(argvars, rettv)
12452 typval_T *argvars;
12453 typval_T *rettv;
12454{
12455 find_some_match(argvars, rettv, 3);
12456}
12457
12458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012459 * "matchstr()" function
12460 */
12461 static void
12462f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012463 typval_T *argvars;
12464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012465{
12466 find_some_match(argvars, rettv, 2);
12467}
12468
Bram Moolenaar33570922005-01-25 22:26:29 +000012469static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012470
12471 static void
12472max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012473 typval_T *argvars;
12474 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012475 int domax;
12476{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012477 long n = 0;
12478 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012479 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012480
12481 if (argvars[0].v_type == VAR_LIST)
12482 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012483 list_T *l;
12484 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012485
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012486 l = argvars[0].vval.v_list;
12487 if (l != NULL)
12488 {
12489 li = l->lv_first;
12490 if (li != NULL)
12491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012492 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012493 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012494 {
12495 li = li->li_next;
12496 if (li == NULL)
12497 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012498 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012499 if (domax ? i > n : i < n)
12500 n = i;
12501 }
12502 }
12503 }
12504 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012505 else if (argvars[0].v_type == VAR_DICT)
12506 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012507 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012508 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012509 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012510 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012511
12512 d = argvars[0].vval.v_dict;
12513 if (d != NULL)
12514 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012515 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012516 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012518 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012520 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012521 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012522 if (first)
12523 {
12524 n = i;
12525 first = FALSE;
12526 }
12527 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012528 n = i;
12529 }
12530 }
12531 }
12532 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012533 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012534 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012535 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012536}
12537
12538/*
12539 * "max()" function
12540 */
12541 static void
12542f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012543 typval_T *argvars;
12544 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012545{
12546 max_min(argvars, rettv, TRUE);
12547}
12548
12549/*
12550 * "min()" function
12551 */
12552 static void
12553f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012554 typval_T *argvars;
12555 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012556{
12557 max_min(argvars, rettv, FALSE);
12558}
12559
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012560static int mkdir_recurse __ARGS((char_u *dir, int prot));
12561
12562/*
12563 * Create the directory in which "dir" is located, and higher levels when
12564 * needed.
12565 */
12566 static int
12567mkdir_recurse(dir, prot)
12568 char_u *dir;
12569 int prot;
12570{
12571 char_u *p;
12572 char_u *updir;
12573 int r = FAIL;
12574
12575 /* Get end of directory name in "dir".
12576 * We're done when it's "/" or "c:/". */
12577 p = gettail_sep(dir);
12578 if (p <= get_past_head(dir))
12579 return OK;
12580
12581 /* If the directory exists we're done. Otherwise: create it.*/
12582 updir = vim_strnsave(dir, (int)(p - dir));
12583 if (updir == NULL)
12584 return FAIL;
12585 if (mch_isdir(updir))
12586 r = OK;
12587 else if (mkdir_recurse(updir, prot) == OK)
12588 r = vim_mkdir_emsg(updir, prot);
12589 vim_free(updir);
12590 return r;
12591}
12592
12593#ifdef vim_mkdir
12594/*
12595 * "mkdir()" function
12596 */
12597 static void
12598f_mkdir(argvars, rettv)
12599 typval_T *argvars;
12600 typval_T *rettv;
12601{
12602 char_u *dir;
12603 char_u buf[NUMBUFLEN];
12604 int prot = 0755;
12605
12606 rettv->vval.v_number = FAIL;
12607 if (check_restricted() || check_secure())
12608 return;
12609
12610 dir = get_tv_string_buf(&argvars[0], buf);
12611 if (argvars[1].v_type != VAR_UNKNOWN)
12612 {
12613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012614 prot = get_tv_number_chk(&argvars[2], NULL);
12615 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012616 mkdir_recurse(dir, prot);
12617 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012618 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012619}
12620#endif
12621
Bram Moolenaar0d660222005-01-07 21:51:51 +000012622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623 * "mode()" function
12624 */
12625/*ARGSUSED*/
12626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012627f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012628 typval_T *argvars;
12629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630{
12631 char_u buf[2];
12632
12633#ifdef FEAT_VISUAL
12634 if (VIsual_active)
12635 {
12636 if (VIsual_select)
12637 buf[0] = VIsual_mode + 's' - 'v';
12638 else
12639 buf[0] = VIsual_mode;
12640 }
12641 else
12642#endif
12643 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12644 buf[0] = 'r';
12645 else if (State & INSERT)
12646 {
12647 if (State & REPLACE_FLAG)
12648 buf[0] = 'R';
12649 else
12650 buf[0] = 'i';
12651 }
12652 else if (State & CMDLINE)
12653 buf[0] = 'c';
12654 else
12655 buf[0] = 'n';
12656
12657 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658 rettv->vval.v_string = vim_strsave(buf);
12659 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660}
12661
12662/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012663 * "nextnonblank()" function
12664 */
12665 static void
12666f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012667 typval_T *argvars;
12668 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012669{
12670 linenr_T lnum;
12671
12672 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675 {
12676 lnum = 0;
12677 break;
12678 }
12679 if (*skipwhite(ml_get(lnum)) != NUL)
12680 break;
12681 }
12682 rettv->vval.v_number = lnum;
12683}
12684
12685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686 * "nr2char()" function
12687 */
12688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 typval_T *argvars;
12691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692{
12693 char_u buf[NUMBUFLEN];
12694
12695#ifdef FEAT_MBYTE
12696 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 else
12699#endif
12700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 buf[1] = NUL;
12703 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 rettv->v_type = VAR_STRING;
12705 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012706}
12707
12708/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012709 * "pathshorten()" function
12710 */
12711 static void
12712f_pathshorten(argvars, rettv)
12713 typval_T *argvars;
12714 typval_T *rettv;
12715{
12716 char_u *p;
12717
12718 rettv->v_type = VAR_STRING;
12719 p = get_tv_string_chk(&argvars[0]);
12720 if (p == NULL)
12721 rettv->vval.v_string = NULL;
12722 else
12723 {
12724 p = vim_strsave(p);
12725 rettv->vval.v_string = p;
12726 if (p != NULL)
12727 shorten_dir(p);
12728 }
12729}
12730
12731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012732 * "prevnonblank()" function
12733 */
12734 static void
12735f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012736 typval_T *argvars;
12737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012738{
12739 linenr_T lnum;
12740
12741 lnum = get_tv_lnum(argvars);
12742 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12743 lnum = 0;
12744 else
12745 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12746 --lnum;
12747 rettv->vval.v_number = lnum;
12748}
12749
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012750#ifdef HAVE_STDARG_H
12751/* This dummy va_list is here because:
12752 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12753 * - locally in the function results in a "used before set" warning
12754 * - using va_start() to initialize it gives "function with fixed args" error */
12755static va_list ap;
12756#endif
12757
Bram Moolenaar8c711452005-01-14 21:53:12 +000012758/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012759 * "printf()" function
12760 */
12761 static void
12762f_printf(argvars, rettv)
12763 typval_T *argvars;
12764 typval_T *rettv;
12765{
12766 rettv->v_type = VAR_STRING;
12767 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012768#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012769 {
12770 char_u buf[NUMBUFLEN];
12771 int len;
12772 char_u *s;
12773 int saved_did_emsg = did_emsg;
12774 char *fmt;
12775
12776 /* Get the required length, allocate the buffer and do it for real. */
12777 did_emsg = FALSE;
12778 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012779 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012780 if (!did_emsg)
12781 {
12782 s = alloc(len + 1);
12783 if (s != NULL)
12784 {
12785 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012786 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012787 }
12788 }
12789 did_emsg |= saved_did_emsg;
12790 }
12791#endif
12792}
12793
12794/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012795 * "pumvisible()" function
12796 */
12797/*ARGSUSED*/
12798 static void
12799f_pumvisible(argvars, rettv)
12800 typval_T *argvars;
12801 typval_T *rettv;
12802{
12803 rettv->vval.v_number = 0;
12804#ifdef FEAT_INS_EXPAND
12805 if (pum_visible())
12806 rettv->vval.v_number = 1;
12807#endif
12808}
12809
12810/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012811 * "range()" function
12812 */
12813 static void
12814f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012815 typval_T *argvars;
12816 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012817{
12818 long start;
12819 long end;
12820 long stride = 1;
12821 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012822 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012825 if (argvars[1].v_type == VAR_UNKNOWN)
12826 {
12827 end = start - 1;
12828 start = 0;
12829 }
12830 else
12831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012834 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012835 }
12836
12837 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012838 if (error)
12839 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012840 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012841 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012842 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012843 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012844 else
12845 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012846 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012847 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012848 if (list_append_number(rettv->vval.v_list,
12849 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012850 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012851 }
12852}
12853
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012854/*
12855 * "readfile()" function
12856 */
12857 static void
12858f_readfile(argvars, rettv)
12859 typval_T *argvars;
12860 typval_T *rettv;
12861{
12862 int binary = FALSE;
12863 char_u *fname;
12864 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012865 listitem_T *li;
12866#define FREAD_SIZE 200 /* optimized for text lines */
12867 char_u buf[FREAD_SIZE];
12868 int readlen; /* size of last fread() */
12869 int buflen; /* nr of valid chars in buf[] */
12870 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12871 int tolist; /* first byte in buf[] still to be put in list */
12872 int chop; /* how many CR to chop off */
12873 char_u *prev = NULL; /* previously read bytes, if any */
12874 int prevlen = 0; /* length of "prev" if not NULL */
12875 char_u *s;
12876 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012877 long maxline = MAXLNUM;
12878 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012879
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012880 if (argvars[1].v_type != VAR_UNKNOWN)
12881 {
12882 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12883 binary = TRUE;
12884 if (argvars[2].v_type != VAR_UNKNOWN)
12885 maxline = get_tv_number(&argvars[2]);
12886 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012887
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012888 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012889 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012890
12891 /* Always open the file in binary mode, library functions have a mind of
12892 * their own about CR-LF conversion. */
12893 fname = get_tv_string(&argvars[0]);
12894 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12895 {
12896 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12897 return;
12898 }
12899
12900 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012901 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012902 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012903 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012904 buflen = filtd + readlen;
12905 tolist = 0;
12906 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12907 {
12908 if (buf[filtd] == '\n' || readlen <= 0)
12909 {
12910 /* Only when in binary mode add an empty list item when the
12911 * last line ends in a '\n'. */
12912 if (!binary && readlen == 0 && filtd == 0)
12913 break;
12914
12915 /* Found end-of-line or end-of-file: add a text line to the
12916 * list. */
12917 chop = 0;
12918 if (!binary)
12919 while (filtd - chop - 1 >= tolist
12920 && buf[filtd - chop - 1] == '\r')
12921 ++chop;
12922 len = filtd - tolist - chop;
12923 if (prev == NULL)
12924 s = vim_strnsave(buf + tolist, len);
12925 else
12926 {
12927 s = alloc((unsigned)(prevlen + len + 1));
12928 if (s != NULL)
12929 {
12930 mch_memmove(s, prev, prevlen);
12931 vim_free(prev);
12932 prev = NULL;
12933 mch_memmove(s + prevlen, buf + tolist, len);
12934 s[prevlen + len] = NUL;
12935 }
12936 }
12937 tolist = filtd + 1;
12938
12939 li = listitem_alloc();
12940 if (li == NULL)
12941 {
12942 vim_free(s);
12943 break;
12944 }
12945 li->li_tv.v_type = VAR_STRING;
12946 li->li_tv.v_lock = 0;
12947 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012948 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012949
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012950 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012951 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012952 if (readlen <= 0)
12953 break;
12954 }
12955 else if (buf[filtd] == NUL)
12956 buf[filtd] = '\n';
12957 }
12958 if (readlen <= 0)
12959 break;
12960
12961 if (tolist == 0)
12962 {
12963 /* "buf" is full, need to move text to an allocated buffer */
12964 if (prev == NULL)
12965 {
12966 prev = vim_strnsave(buf, buflen);
12967 prevlen = buflen;
12968 }
12969 else
12970 {
12971 s = alloc((unsigned)(prevlen + buflen));
12972 if (s != NULL)
12973 {
12974 mch_memmove(s, prev, prevlen);
12975 mch_memmove(s + prevlen, buf, buflen);
12976 vim_free(prev);
12977 prev = s;
12978 prevlen += buflen;
12979 }
12980 }
12981 filtd = 0;
12982 }
12983 else
12984 {
12985 mch_memmove(buf, buf + tolist, buflen - tolist);
12986 filtd -= tolist;
12987 }
12988 }
12989
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012990 /*
12991 * For a negative line count use only the lines at the end of the file,
12992 * free the rest.
12993 */
12994 if (maxline < 0)
12995 while (cnt > -maxline)
12996 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012997 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012998 --cnt;
12999 }
13000
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013001 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013002 fclose(fd);
13003}
13004
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013005#if defined(FEAT_RELTIME)
13006static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13007
13008/*
13009 * Convert a List to proftime_T.
13010 * Return FAIL when there is something wrong.
13011 */
13012 static int
13013list2proftime(arg, tm)
13014 typval_T *arg;
13015 proftime_T *tm;
13016{
13017 long n1, n2;
13018 int error = FALSE;
13019
13020 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13021 || arg->vval.v_list->lv_len != 2)
13022 return FAIL;
13023 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13024 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13025# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013026 tm->HighPart = n1;
13027 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013028# else
13029 tm->tv_sec = n1;
13030 tm->tv_usec = n2;
13031# endif
13032 return error ? FAIL : OK;
13033}
13034#endif /* FEAT_RELTIME */
13035
13036/*
13037 * "reltime()" function
13038 */
13039 static void
13040f_reltime(argvars, rettv)
13041 typval_T *argvars;
13042 typval_T *rettv;
13043{
13044#ifdef FEAT_RELTIME
13045 proftime_T res;
13046 proftime_T start;
13047
13048 if (argvars[0].v_type == VAR_UNKNOWN)
13049 {
13050 /* No arguments: get current time. */
13051 profile_start(&res);
13052 }
13053 else if (argvars[1].v_type == VAR_UNKNOWN)
13054 {
13055 if (list2proftime(&argvars[0], &res) == FAIL)
13056 return;
13057 profile_end(&res);
13058 }
13059 else
13060 {
13061 /* Two arguments: compute the difference. */
13062 if (list2proftime(&argvars[0], &start) == FAIL
13063 || list2proftime(&argvars[1], &res) == FAIL)
13064 return;
13065 profile_sub(&res, &start);
13066 }
13067
13068 if (rettv_list_alloc(rettv) == OK)
13069 {
13070 long n1, n2;
13071
13072# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013073 n1 = res.HighPart;
13074 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013075# else
13076 n1 = res.tv_sec;
13077 n2 = res.tv_usec;
13078# endif
13079 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13080 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13081 }
13082#endif
13083}
13084
13085/*
13086 * "reltimestr()" function
13087 */
13088 static void
13089f_reltimestr(argvars, rettv)
13090 typval_T *argvars;
13091 typval_T *rettv;
13092{
13093#ifdef FEAT_RELTIME
13094 proftime_T tm;
13095#endif
13096
13097 rettv->v_type = VAR_STRING;
13098 rettv->vval.v_string = NULL;
13099#ifdef FEAT_RELTIME
13100 if (list2proftime(&argvars[0], &tm) == OK)
13101 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13102#endif
13103}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013104
Bram Moolenaar0d660222005-01-07 21:51:51 +000013105#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13106static void make_connection __ARGS((void));
13107static int check_connection __ARGS((void));
13108
13109 static void
13110make_connection()
13111{
13112 if (X_DISPLAY == NULL
13113# ifdef FEAT_GUI
13114 && !gui.in_use
13115# endif
13116 )
13117 {
13118 x_force_connect = TRUE;
13119 setup_term_clip();
13120 x_force_connect = FALSE;
13121 }
13122}
13123
13124 static int
13125check_connection()
13126{
13127 make_connection();
13128 if (X_DISPLAY == NULL)
13129 {
13130 EMSG(_("E240: No connection to Vim server"));
13131 return FAIL;
13132 }
13133 return OK;
13134}
13135#endif
13136
13137#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013138static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139
13140 static void
13141remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013142 typval_T *argvars;
13143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013144 int expr;
13145{
13146 char_u *server_name;
13147 char_u *keys;
13148 char_u *r = NULL;
13149 char_u buf[NUMBUFLEN];
13150# ifdef WIN32
13151 HWND w;
13152# else
13153 Window w;
13154# endif
13155
13156 if (check_restricted() || check_secure())
13157 return;
13158
13159# ifdef FEAT_X11
13160 if (check_connection() == FAIL)
13161 return;
13162# endif
13163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013164 server_name = get_tv_string_chk(&argvars[0]);
13165 if (server_name == NULL)
13166 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013167 keys = get_tv_string_buf(&argvars[1], buf);
13168# ifdef WIN32
13169 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13170# else
13171 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13172 < 0)
13173# endif
13174 {
13175 if (r != NULL)
13176 EMSG(r); /* sending worked but evaluation failed */
13177 else
13178 EMSG2(_("E241: Unable to send to %s"), server_name);
13179 return;
13180 }
13181
13182 rettv->vval.v_string = r;
13183
13184 if (argvars[2].v_type != VAR_UNKNOWN)
13185 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013186 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013187 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013188 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013189
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013190 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013191 v.di_tv.v_type = VAR_STRING;
13192 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013193 idvar = get_tv_string_chk(&argvars[2]);
13194 if (idvar != NULL)
13195 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013196 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013197 }
13198}
13199#endif
13200
13201/*
13202 * "remote_expr()" function
13203 */
13204/*ARGSUSED*/
13205 static void
13206f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 typval_T *argvars;
13208 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013209{
13210 rettv->v_type = VAR_STRING;
13211 rettv->vval.v_string = NULL;
13212#ifdef FEAT_CLIENTSERVER
13213 remote_common(argvars, rettv, TRUE);
13214#endif
13215}
13216
13217/*
13218 * "remote_foreground()" function
13219 */
13220/*ARGSUSED*/
13221 static void
13222f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013223 typval_T *argvars;
13224 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013225{
13226 rettv->vval.v_number = 0;
13227#ifdef FEAT_CLIENTSERVER
13228# ifdef WIN32
13229 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013230 {
13231 char_u *server_name = get_tv_string_chk(&argvars[0]);
13232
13233 if (server_name != NULL)
13234 serverForeground(server_name);
13235 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013236# else
13237 /* Send a foreground() expression to the server. */
13238 argvars[1].v_type = VAR_STRING;
13239 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13240 argvars[2].v_type = VAR_UNKNOWN;
13241 remote_common(argvars, rettv, TRUE);
13242 vim_free(argvars[1].vval.v_string);
13243# endif
13244#endif
13245}
13246
13247/*ARGSUSED*/
13248 static void
13249f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013250 typval_T *argvars;
13251 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013252{
13253#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013254 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013255 char_u *s = NULL;
13256# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013257 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013258# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013259 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013260
13261 if (check_restricted() || check_secure())
13262 {
13263 rettv->vval.v_number = -1;
13264 return;
13265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 serverid = get_tv_string_chk(&argvars[0]);
13267 if (serverid == NULL)
13268 {
13269 rettv->vval.v_number = -1;
13270 return; /* type error; errmsg already given */
13271 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013272# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013273 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013274 if (n == 0)
13275 rettv->vval.v_number = -1;
13276 else
13277 {
13278 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13279 rettv->vval.v_number = (s != NULL);
13280 }
13281# else
13282 rettv->vval.v_number = 0;
13283 if (check_connection() == FAIL)
13284 return;
13285
13286 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013288# endif
13289
13290 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 char_u *retvar;
13293
Bram Moolenaar33570922005-01-25 22:26:29 +000013294 v.di_tv.v_type = VAR_STRING;
13295 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013296 retvar = get_tv_string_chk(&argvars[1]);
13297 if (retvar != NULL)
13298 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013299 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013300 }
13301#else
13302 rettv->vval.v_number = -1;
13303#endif
13304}
13305
13306/*ARGSUSED*/
13307 static void
13308f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013309 typval_T *argvars;
13310 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013311{
13312 char_u *r = NULL;
13313
13314#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013315 char_u *serverid = get_tv_string_chk(&argvars[0]);
13316
13317 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013318 {
13319# ifdef WIN32
13320 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013321 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013323 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013324 if (n != 0)
13325 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13326 if (r == NULL)
13327# else
13328 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013329 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330# endif
13331 EMSG(_("E277: Unable to read a server reply"));
13332 }
13333#endif
13334 rettv->v_type = VAR_STRING;
13335 rettv->vval.v_string = r;
13336}
13337
13338/*
13339 * "remote_send()" function
13340 */
13341/*ARGSUSED*/
13342 static void
13343f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013344 typval_T *argvars;
13345 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013346{
13347 rettv->v_type = VAR_STRING;
13348 rettv->vval.v_string = NULL;
13349#ifdef FEAT_CLIENTSERVER
13350 remote_common(argvars, rettv, FALSE);
13351#endif
13352}
13353
13354/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013355 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013356 */
13357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013358f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013359 typval_T *argvars;
13360 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013361{
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 list_T *l;
13363 listitem_T *item, *item2;
13364 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013365 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013366 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013367 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 dict_T *d;
13369 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013370
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013371 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013372 if (argvars[0].v_type == VAR_DICT)
13373 {
13374 if (argvars[2].v_type != VAR_UNKNOWN)
13375 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013376 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013377 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 key = get_tv_string_chk(&argvars[1]);
13380 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 di = dict_find(d, key, -1);
13383 if (di == NULL)
13384 EMSG2(_(e_dictkey), key);
13385 else
13386 {
13387 *rettv = di->di_tv;
13388 init_tv(&di->di_tv);
13389 dictitem_remove(d, di);
13390 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013391 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013392 }
13393 }
13394 else if (argvars[0].v_type != VAR_LIST)
13395 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013396 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013397 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 int error = FALSE;
13400
13401 idx = get_tv_number_chk(&argvars[1], &error);
13402 if (error)
13403 ; /* type error: do nothing, errmsg already given */
13404 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013405 EMSGN(_(e_listidx), idx);
13406 else
13407 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013408 if (argvars[2].v_type == VAR_UNKNOWN)
13409 {
13410 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013411 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013412 *rettv = item->li_tv;
13413 vim_free(item);
13414 }
13415 else
13416 {
13417 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013418 end = get_tv_number_chk(&argvars[2], &error);
13419 if (error)
13420 ; /* type error: do nothing */
13421 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013422 EMSGN(_(e_listidx), end);
13423 else
13424 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013425 int cnt = 0;
13426
13427 for (li = item; li != NULL; li = li->li_next)
13428 {
13429 ++cnt;
13430 if (li == item2)
13431 break;
13432 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013433 if (li == NULL) /* didn't find "item2" after "item" */
13434 EMSG(_(e_invrange));
13435 else
13436 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013437 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013438 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013439 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013440 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013441 l->lv_first = item;
13442 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013443 item->li_prev = NULL;
13444 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013445 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013446 }
13447 }
13448 }
13449 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013450 }
13451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452}
13453
13454/*
13455 * "rename({from}, {to})" function
13456 */
13457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013458f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013459 typval_T *argvars;
13460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461{
13462 char_u buf[NUMBUFLEN];
13463
13464 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13468 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469}
13470
13471/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013472 * "repeat()" function
13473 */
13474/*ARGSUSED*/
13475 static void
13476f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013477 typval_T *argvars;
13478 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013479{
13480 char_u *p;
13481 int n;
13482 int slen;
13483 int len;
13484 char_u *r;
13485 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013486
13487 n = get_tv_number(&argvars[1]);
13488 if (argvars[0].v_type == VAR_LIST)
13489 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013490 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013491 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013492 if (list_extend(rettv->vval.v_list,
13493 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013494 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013495 }
13496 else
13497 {
13498 p = get_tv_string(&argvars[0]);
13499 rettv->v_type = VAR_STRING;
13500 rettv->vval.v_string = NULL;
13501
13502 slen = (int)STRLEN(p);
13503 len = slen * n;
13504 if (len <= 0)
13505 return;
13506
13507 r = alloc(len + 1);
13508 if (r != NULL)
13509 {
13510 for (i = 0; i < n; i++)
13511 mch_memmove(r + i * slen, p, (size_t)slen);
13512 r[len] = NUL;
13513 }
13514
13515 rettv->vval.v_string = r;
13516 }
13517}
13518
13519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 * "resolve()" function
13521 */
13522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013524 typval_T *argvars;
13525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526{
13527 char_u *p;
13528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530#ifdef FEAT_SHORTCUT
13531 {
13532 char_u *v = NULL;
13533
13534 v = mch_resolve_shortcut(p);
13535 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013536 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 }
13540#else
13541# ifdef HAVE_READLINK
13542 {
13543 char_u buf[MAXPATHL + 1];
13544 char_u *cpy;
13545 int len;
13546 char_u *remain = NULL;
13547 char_u *q;
13548 int is_relative_to_current = FALSE;
13549 int has_trailing_pathsep = FALSE;
13550 int limit = 100;
13551
13552 p = vim_strsave(p);
13553
13554 if (p[0] == '.' && (vim_ispathsep(p[1])
13555 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13556 is_relative_to_current = TRUE;
13557
13558 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013559 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 has_trailing_pathsep = TRUE;
13561
13562 q = getnextcomp(p);
13563 if (*q != NUL)
13564 {
13565 /* Separate the first path component in "p", and keep the
13566 * remainder (beginning with the path separator). */
13567 remain = vim_strsave(q - 1);
13568 q[-1] = NUL;
13569 }
13570
13571 for (;;)
13572 {
13573 for (;;)
13574 {
13575 len = readlink((char *)p, (char *)buf, MAXPATHL);
13576 if (len <= 0)
13577 break;
13578 buf[len] = NUL;
13579
13580 if (limit-- == 0)
13581 {
13582 vim_free(p);
13583 vim_free(remain);
13584 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 goto fail;
13587 }
13588
13589 /* Ensure that the result will have a trailing path separator
13590 * if the argument has one. */
13591 if (remain == NULL && has_trailing_pathsep)
13592 add_pathsep(buf);
13593
13594 /* Separate the first path component in the link value and
13595 * concatenate the remainders. */
13596 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13597 if (*q != NUL)
13598 {
13599 if (remain == NULL)
13600 remain = vim_strsave(q - 1);
13601 else
13602 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013603 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 if (cpy != NULL)
13605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 vim_free(remain);
13607 remain = cpy;
13608 }
13609 }
13610 q[-1] = NUL;
13611 }
13612
13613 q = gettail(p);
13614 if (q > p && *q == NUL)
13615 {
13616 /* Ignore trailing path separator. */
13617 q[-1] = NUL;
13618 q = gettail(p);
13619 }
13620 if (q > p && !mch_isFullName(buf))
13621 {
13622 /* symlink is relative to directory of argument */
13623 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13624 if (cpy != NULL)
13625 {
13626 STRCPY(cpy, p);
13627 STRCPY(gettail(cpy), buf);
13628 vim_free(p);
13629 p = cpy;
13630 }
13631 }
13632 else
13633 {
13634 vim_free(p);
13635 p = vim_strsave(buf);
13636 }
13637 }
13638
13639 if (remain == NULL)
13640 break;
13641
13642 /* Append the first path component of "remain" to "p". */
13643 q = getnextcomp(remain + 1);
13644 len = q - remain - (*q != NUL);
13645 cpy = vim_strnsave(p, STRLEN(p) + len);
13646 if (cpy != NULL)
13647 {
13648 STRNCAT(cpy, remain, len);
13649 vim_free(p);
13650 p = cpy;
13651 }
13652 /* Shorten "remain". */
13653 if (*q != NUL)
13654 STRCPY(remain, q - 1);
13655 else
13656 {
13657 vim_free(remain);
13658 remain = NULL;
13659 }
13660 }
13661
13662 /* If the result is a relative path name, make it explicitly relative to
13663 * the current directory if and only if the argument had this form. */
13664 if (!vim_ispathsep(*p))
13665 {
13666 if (is_relative_to_current
13667 && *p != NUL
13668 && !(p[0] == '.'
13669 && (p[1] == NUL
13670 || vim_ispathsep(p[1])
13671 || (p[1] == '.'
13672 && (p[2] == NUL
13673 || vim_ispathsep(p[2]))))))
13674 {
13675 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013676 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 if (cpy != NULL)
13678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 vim_free(p);
13680 p = cpy;
13681 }
13682 }
13683 else if (!is_relative_to_current)
13684 {
13685 /* Strip leading "./". */
13686 q = p;
13687 while (q[0] == '.' && vim_ispathsep(q[1]))
13688 q += 2;
13689 if (q > p)
13690 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13691 }
13692 }
13693
13694 /* Ensure that the result will have no trailing path separator
13695 * if the argument had none. But keep "/" or "//". */
13696 if (!has_trailing_pathsep)
13697 {
13698 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013699 if (after_pathsep(p, q))
13700 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 }
13702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 }
13705# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013706 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707# endif
13708#endif
13709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711
13712#ifdef HAVE_READLINK
13713fail:
13714#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716}
13717
13718/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013719 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 */
13721 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013722f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013723 typval_T *argvars;
13724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725{
Bram Moolenaar33570922005-01-25 22:26:29 +000013726 list_T *l;
13727 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728
Bram Moolenaar0d660222005-01-07 21:51:51 +000013729 rettv->vval.v_number = 0;
13730 if (argvars[0].v_type != VAR_LIST)
13731 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013732 else if ((l = argvars[0].vval.v_list) != NULL
13733 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013734 {
13735 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013736 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013737 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013738 while (li != NULL)
13739 {
13740 ni = li->li_prev;
13741 list_append(l, li);
13742 li = ni;
13743 }
13744 rettv->vval.v_list = l;
13745 rettv->v_type = VAR_LIST;
13746 ++l->lv_refcount;
13747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748}
13749
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013750#define SP_NOMOVE 0x01 /* don't move cursor */
13751#define SP_REPEAT 0x02 /* repeat to find outer pair */
13752#define SP_RETCOUNT 0x04 /* return matchcount */
13753#define SP_SETPCMARK 0x08 /* set previous context mark */
13754#define SP_START 0x10 /* accept match at start position */
13755#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13756#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013757
Bram Moolenaar33570922005-01-25 22:26:29 +000013758static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013759
13760/*
13761 * Get flags for a search function.
13762 * Possibly sets "p_ws".
13763 * Returns BACKWARD, FORWARD or zero (for an error).
13764 */
13765 static int
13766get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013767 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013768 int *flagsp;
13769{
13770 int dir = FORWARD;
13771 char_u *flags;
13772 char_u nbuf[NUMBUFLEN];
13773 int mask;
13774
13775 if (varp->v_type != VAR_UNKNOWN)
13776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 flags = get_tv_string_buf_chk(varp, nbuf);
13778 if (flags == NULL)
13779 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013780 while (*flags != NUL)
13781 {
13782 switch (*flags)
13783 {
13784 case 'b': dir = BACKWARD; break;
13785 case 'w': p_ws = TRUE; break;
13786 case 'W': p_ws = FALSE; break;
13787 default: mask = 0;
13788 if (flagsp != NULL)
13789 switch (*flags)
13790 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013791 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013792 case 'e': mask = SP_END; break;
13793 case 'm': mask = SP_RETCOUNT; break;
13794 case 'n': mask = SP_NOMOVE; break;
13795 case 'p': mask = SP_SUBPAT; break;
13796 case 'r': mask = SP_REPEAT; break;
13797 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013798 }
13799 if (mask == 0)
13800 {
13801 EMSG2(_(e_invarg2), flags);
13802 dir = 0;
13803 }
13804 else
13805 *flagsp |= mask;
13806 }
13807 if (dir == 0)
13808 break;
13809 ++flags;
13810 }
13811 }
13812 return dir;
13813}
13814
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013816 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013818 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013819search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013820 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013821 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013822 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013824 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 char_u *pat;
13826 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013827 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 int save_p_ws = p_ws;
13829 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013830 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013831 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013832 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013833 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013835 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013836 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013837 if (dir == 0)
13838 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013839 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013840 if (flags & SP_START)
13841 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013842 if (flags & SP_END)
13843 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013844
13845 /* Optional extra argument: line number to stop searching. */
13846 if (argvars[1].v_type != VAR_UNKNOWN
13847 && argvars[2].v_type != VAR_UNKNOWN)
13848 {
13849 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13850 if (lnum_stop < 0)
13851 goto theend;
13852 }
13853
Bram Moolenaar231334e2005-07-25 20:46:57 +000013854 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013855 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013856 * Check to make sure only those flags are set.
13857 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13858 * flags cannot be set. Check for that condition also.
13859 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013860 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013861 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013863 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013864 goto theend;
13865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013867 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013868 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13869 options, RE_SEARCH, (linenr_T)lnum_stop);
13870 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013872 if (flags & SP_SUBPAT)
13873 retval = subpatnum;
13874 else
13875 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013876 if (flags & SP_SETPCMARK)
13877 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013879 if (match_pos != NULL)
13880 {
13881 /* Store the match cursor position */
13882 match_pos->lnum = pos.lnum;
13883 match_pos->col = pos.col + 1;
13884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 /* "/$" will put the cursor after the end of the line, may need to
13886 * correct that here */
13887 check_cursor();
13888 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013889
13890 /* If 'n' flag is used: restore cursor position. */
13891 if (flags & SP_NOMOVE)
13892 curwin->w_cursor = save_cursor;
13893theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013895
13896 return retval;
13897}
13898
13899/*
13900 * "search()" function
13901 */
13902 static void
13903f_search(argvars, rettv)
13904 typval_T *argvars;
13905 typval_T *rettv;
13906{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013907 int flags = 0;
13908
13909 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910}
13911
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013913 * "searchdecl()" function
13914 */
13915 static void
13916f_searchdecl(argvars, rettv)
13917 typval_T *argvars;
13918 typval_T *rettv;
13919{
13920 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013921 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013922 int error = FALSE;
13923 char_u *name;
13924
13925 rettv->vval.v_number = 1; /* default: FAIL */
13926
13927 name = get_tv_string_chk(&argvars[0]);
13928 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013929 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013930 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013931 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13932 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13933 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013934 if (!error && name != NULL)
13935 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013936 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013937}
13938
13939/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013940 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013942 static int
13943searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013944 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013945 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946{
13947 char_u *spat, *mpat, *epat;
13948 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 int dir;
13951 int flags = 0;
13952 char_u nbuf1[NUMBUFLEN];
13953 char_u nbuf2[NUMBUFLEN];
13954 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013955 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013956 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013959 spat = get_tv_string_chk(&argvars[0]);
13960 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13961 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13962 if (spat == NULL || mpat == NULL || epat == NULL)
13963 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 /* Handle the optional fourth argument: flags */
13966 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013967 if (dir == 0)
13968 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013969
13970 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013971 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13972 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013973 if ((flags & (SP_END | SP_SUBPAT)) != 0
13974 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013975 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013976 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013977 goto theend;
13978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013980 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013981 if (argvars[3].v_type == VAR_UNKNOWN
13982 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 skip = (char_u *)"";
13984 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013987 if (argvars[5].v_type != VAR_UNKNOWN)
13988 {
13989 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13990 if (lnum_stop < 0)
13991 goto theend;
13992 }
13993 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994 if (skip == NULL)
13995 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013997 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13998 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013999
14000theend:
14001 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014002
14003 return retval;
14004}
14005
14006/*
14007 * "searchpair()" function
14008 */
14009 static void
14010f_searchpair(argvars, rettv)
14011 typval_T *argvars;
14012 typval_T *rettv;
14013{
14014 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14015}
14016
14017/*
14018 * "searchpairpos()" function
14019 */
14020 static void
14021f_searchpairpos(argvars, rettv)
14022 typval_T *argvars;
14023 typval_T *rettv;
14024{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014025 pos_T match_pos;
14026 int lnum = 0;
14027 int col = 0;
14028
14029 rettv->vval.v_number = 0;
14030
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014031 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014032 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014033
14034 if (searchpair_cmn(argvars, &match_pos) > 0)
14035 {
14036 lnum = match_pos.lnum;
14037 col = match_pos.col;
14038 }
14039
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014040 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14041 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014042}
14043
14044/*
14045 * Search for a start/middle/end thing.
14046 * Used by searchpair(), see its documentation for the details.
14047 * Returns 0 or -1 for no match,
14048 */
14049 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014050do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014051 char_u *spat; /* start pattern */
14052 char_u *mpat; /* middle pattern */
14053 char_u *epat; /* end pattern */
14054 int dir; /* BACKWARD or FORWARD */
14055 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014056 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014057 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014058 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014059{
14060 char_u *save_cpo;
14061 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14062 long retval = 0;
14063 pos_T pos;
14064 pos_T firstpos;
14065 pos_T foundpos;
14066 pos_T save_cursor;
14067 pos_T save_pos;
14068 int n;
14069 int r;
14070 int nest = 1;
14071 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014072 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014073
14074 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14075 save_cpo = p_cpo;
14076 p_cpo = (char_u *)"";
14077
14078 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14079 * start/middle/end (pat3, for the top pair). */
14080 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14081 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14082 if (pat2 == NULL || pat3 == NULL)
14083 goto theend;
14084 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14085 if (*mpat == NUL)
14086 STRCPY(pat3, pat2);
14087 else
14088 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14089 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014090 if (flags & SP_START)
14091 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014092
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 save_cursor = curwin->w_cursor;
14094 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014095 clearpos(&firstpos);
14096 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 pat = pat3;
14098 for (;;)
14099 {
14100 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014101 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14103 /* didn't find it or found the first match again: FAIL */
14104 break;
14105
14106 if (firstpos.lnum == 0)
14107 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014108 if (equalpos(pos, foundpos))
14109 {
14110 /* Found the same position again. Can happen with a pattern that
14111 * has "\zs" at the end and searching backwards. Advance one
14112 * character and try again. */
14113 if (dir == BACKWARD)
14114 decl(&pos);
14115 else
14116 incl(&pos);
14117 }
14118 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119
14120 /* If the skip pattern matches, ignore this match. */
14121 if (*skip != NUL)
14122 {
14123 save_pos = curwin->w_cursor;
14124 curwin->w_cursor = pos;
14125 r = eval_to_bool(skip, &err, NULL, FALSE);
14126 curwin->w_cursor = save_pos;
14127 if (err)
14128 {
14129 /* Evaluating {skip} caused an error, break here. */
14130 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014131 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 break;
14133 }
14134 if (r)
14135 continue;
14136 }
14137
14138 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14139 {
14140 /* Found end when searching backwards or start when searching
14141 * forward: nested pair. */
14142 ++nest;
14143 pat = pat2; /* nested, don't search for middle */
14144 }
14145 else
14146 {
14147 /* Found end when searching forward or start when searching
14148 * backward: end of (nested) pair; or found middle in outer pair. */
14149 if (--nest == 1)
14150 pat = pat3; /* outer level, search for middle */
14151 }
14152
14153 if (nest == 0)
14154 {
14155 /* Found the match: return matchcount or line number. */
14156 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014157 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014159 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014160 if (flags & SP_SETPCMARK)
14161 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 curwin->w_cursor = pos;
14163 if (!(flags & SP_REPEAT))
14164 break;
14165 nest = 1; /* search for next unmatched */
14166 }
14167 }
14168
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014169 if (match_pos != NULL)
14170 {
14171 /* Store the match cursor position */
14172 match_pos->lnum = curwin->w_cursor.lnum;
14173 match_pos->col = curwin->w_cursor.col + 1;
14174 }
14175
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014177 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 curwin->w_cursor = save_cursor;
14179
14180theend:
14181 vim_free(pat2);
14182 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014184
14185 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186}
14187
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014188/*
14189 * "searchpos()" function
14190 */
14191 static void
14192f_searchpos(argvars, rettv)
14193 typval_T *argvars;
14194 typval_T *rettv;
14195{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014196 pos_T match_pos;
14197 int lnum = 0;
14198 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014199 int n;
14200 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014201
14202 rettv->vval.v_number = 0;
14203
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014204 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014205 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014206
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014207 n = search_cmn(argvars, &match_pos, &flags);
14208 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014209 {
14210 lnum = match_pos.lnum;
14211 col = match_pos.col;
14212 }
14213
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014214 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14215 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014216 if (flags & SP_SUBPAT)
14217 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014218}
14219
14220
Bram Moolenaar0d660222005-01-07 21:51:51 +000014221/*ARGSUSED*/
14222 static void
14223f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014224 typval_T *argvars;
14225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227#ifdef FEAT_CLIENTSERVER
14228 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014229 char_u *server = get_tv_string_chk(&argvars[0]);
14230 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231
Bram Moolenaar0d660222005-01-07 21:51:51 +000014232 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 if (server == NULL || reply == NULL)
14234 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014235 if (check_restricted() || check_secure())
14236 return;
14237# ifdef FEAT_X11
14238 if (check_connection() == FAIL)
14239 return;
14240# endif
14241
14242 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244 EMSG(_("E258: Unable to send to client"));
14245 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014247 rettv->vval.v_number = 0;
14248#else
14249 rettv->vval.v_number = -1;
14250#endif
14251}
14252
14253/*ARGSUSED*/
14254 static void
14255f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014256 typval_T *argvars;
14257 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258{
14259 char_u *r = NULL;
14260
14261#ifdef FEAT_CLIENTSERVER
14262# ifdef WIN32
14263 r = serverGetVimNames();
14264# else
14265 make_connection();
14266 if (X_DISPLAY != NULL)
14267 r = serverGetVimNames(X_DISPLAY);
14268# endif
14269#endif
14270 rettv->v_type = VAR_STRING;
14271 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272}
14273
14274/*
14275 * "setbufvar()" function
14276 */
14277/*ARGSUSED*/
14278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014280 typval_T *argvars;
14281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282{
14283 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014286 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 char_u nbuf[NUMBUFLEN];
14288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014289 rettv->vval.v_number = 0;
14290
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 if (check_restricted() || check_secure())
14292 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14294 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014295 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 varp = &argvars[2];
14297
14298 if (buf != NULL && varname != NULL && varp != NULL)
14299 {
14300 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302
14303 if (*varname == '&')
14304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305 long numval;
14306 char_u *strval;
14307 int error = FALSE;
14308
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310 numval = get_tv_number_chk(varp, &error);
14311 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 if (!error && strval != NULL)
14313 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 }
14315 else
14316 {
14317 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14318 if (bufvarname != NULL)
14319 {
14320 STRCPY(bufvarname, "b:");
14321 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014322 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 vim_free(bufvarname);
14324 }
14325 }
14326
14327 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330}
14331
14332/*
14333 * "setcmdpos()" function
14334 */
14335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014337 typval_T *argvars;
14338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 int pos = (int)get_tv_number(&argvars[0]) - 1;
14341
14342 if (pos >= 0)
14343 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344}
14345
14346/*
14347 * "setline()" function
14348 */
14349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014351 typval_T *argvars;
14352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
14354 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014355 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014356 list_T *l = NULL;
14357 listitem_T *li = NULL;
14358 long added = 0;
14359 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014361 lnum = get_tv_lnum(&argvars[0]);
14362 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014364 l = argvars[1].vval.v_list;
14365 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014367 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014368 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014369
14370 rettv->vval.v_number = 0; /* OK */
14371 for (;;)
14372 {
14373 if (l != NULL)
14374 {
14375 /* list argument, get next string */
14376 if (li == NULL)
14377 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014379 li = li->li_next;
14380 }
14381
14382 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014384 break;
14385 if (lnum <= curbuf->b_ml.ml_line_count)
14386 {
14387 /* existing line, replace it */
14388 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14389 {
14390 changed_bytes(lnum, 0);
14391 check_cursor_col();
14392 rettv->vval.v_number = 0; /* OK */
14393 }
14394 }
14395 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14396 {
14397 /* lnum is one past the last line, append the line */
14398 ++added;
14399 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14400 rettv->vval.v_number = 0; /* OK */
14401 }
14402
14403 if (l == NULL) /* only one string argument */
14404 break;
14405 ++lnum;
14406 }
14407
14408 if (added > 0)
14409 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410}
14411
14412/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014413 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014414 */
14415/*ARGSUSED*/
14416 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014417set_qf_ll_list(wp, list_arg, action_arg, rettv)
14418 win_T *wp;
14419 typval_T *list_arg;
14420 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014421 typval_T *rettv;
14422{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014423#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014424 char_u *act;
14425 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014426#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014427
Bram Moolenaar2641f772005-03-25 21:58:17 +000014428 rettv->vval.v_number = -1;
14429
14430#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014431 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014432 EMSG(_(e_listreq));
14433 else
14434 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014435 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014436
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014437 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014438 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014439 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014440 if (act == NULL)
14441 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014442 if (*act == 'a' || *act == 'r')
14443 action = *act;
14444 }
14445
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014446 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014447 rettv->vval.v_number = 0;
14448 }
14449#endif
14450}
14451
14452/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014453 * "setloclist()" function
14454 */
14455/*ARGSUSED*/
14456 static void
14457f_setloclist(argvars, rettv)
14458 typval_T *argvars;
14459 typval_T *rettv;
14460{
14461 win_T *win;
14462
14463 rettv->vval.v_number = -1;
14464
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014465 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014466 if (win != NULL)
14467 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14468}
14469
14470/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014471 * "setpos()" function
14472 */
14473/*ARGSUSED*/
14474 static void
14475f_setpos(argvars, rettv)
14476 typval_T *argvars;
14477 typval_T *rettv;
14478{
14479 pos_T pos;
14480 int fnum;
14481 char_u *name;
14482
14483 name = get_tv_string_chk(argvars);
14484 if (name != NULL)
14485 {
14486 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14487 {
14488 --pos.col;
14489 if (name[0] == '.') /* cursor */
14490 {
14491 if (fnum == curbuf->b_fnum)
14492 {
14493 curwin->w_cursor = pos;
14494 check_cursor();
14495 }
14496 else
14497 EMSG(_(e_invarg));
14498 }
14499 else if (name[0] == '\'') /* mark */
14500 (void)setmark_pos(name[1], &pos, fnum);
14501 else
14502 EMSG(_(e_invarg));
14503 }
14504 }
14505}
14506
14507/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014508 * "setqflist()" function
14509 */
14510/*ARGSUSED*/
14511 static void
14512f_setqflist(argvars, rettv)
14513 typval_T *argvars;
14514 typval_T *rettv;
14515{
14516 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14517}
14518
14519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 * "setreg()" function
14521 */
14522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *argvars;
14525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526{
14527 int regname;
14528 char_u *strregname;
14529 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 int append;
14532 char_u yank_type;
14533 long block_len;
14534
14535 block_len = -1;
14536 yank_type = MAUTO;
14537 append = FALSE;
14538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014539 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542 if (strregname == NULL)
14543 return; /* type error; errmsg already given */
14544 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 if (regname == 0 || regname == '@')
14546 regname = '"';
14547 else if (regname == '=')
14548 return;
14549
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014550 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 stropt = get_tv_string_chk(&argvars[2]);
14553 if (stropt == NULL)
14554 return; /* type error */
14555 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 switch (*stropt)
14557 {
14558 case 'a': case 'A': /* append */
14559 append = TRUE;
14560 break;
14561 case 'v': case 'c': /* character-wise selection */
14562 yank_type = MCHAR;
14563 break;
14564 case 'V': case 'l': /* line-wise selection */
14565 yank_type = MLINE;
14566 break;
14567#ifdef FEAT_VISUAL
14568 case 'b': case Ctrl_V: /* block-wise selection */
14569 yank_type = MBLOCK;
14570 if (VIM_ISDIGIT(stropt[1]))
14571 {
14572 ++stropt;
14573 block_len = getdigits(&stropt) - 1;
14574 --stropt;
14575 }
14576 break;
14577#endif
14578 }
14579 }
14580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014581 strval = get_tv_string_chk(&argvars[1]);
14582 if (strval != NULL)
14583 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586}
14587
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014588/*
14589 * "settabwinvar()" function
14590 */
14591 static void
14592f_settabwinvar(argvars, rettv)
14593 typval_T *argvars;
14594 typval_T *rettv;
14595{
14596 setwinvar(argvars, rettv, 1);
14597}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598
14599/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014600 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014603f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 typval_T *argvars;
14605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014607 setwinvar(argvars, rettv, 0);
14608}
14609
14610/*
14611 * "setwinvar()" and "settabwinvar()" functions
14612 */
14613 static void
14614setwinvar(argvars, rettv, off)
14615 typval_T *argvars;
14616 typval_T *rettv;
14617 int off;
14618{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 win_T *win;
14620#ifdef FEAT_WINDOWS
14621 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014622 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623#endif
14624 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014627 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 rettv->vval.v_number = 0;
14630
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 if (check_restricted() || check_secure())
14632 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014633
14634#ifdef FEAT_WINDOWS
14635 if (off == 1)
14636 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14637 else
14638 tp = curtab;
14639#endif
14640 win = find_win_by_nr(&argvars[off], tp);
14641 varname = get_tv_string_chk(&argvars[off + 1]);
14642 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643
14644 if (win != NULL && varname != NULL && varp != NULL)
14645 {
14646#ifdef FEAT_WINDOWS
14647 /* set curwin to be our win, temporarily */
14648 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014649 save_curtab = curtab;
14650 goto_tabpage_tp(tp);
14651 if (!win_valid(win))
14652 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 curwin = win;
14654 curbuf = curwin->w_buffer;
14655#endif
14656
14657 if (*varname == '&')
14658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014659 long numval;
14660 char_u *strval;
14661 int error = FALSE;
14662
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 numval = get_tv_number_chk(varp, &error);
14665 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 if (!error && strval != NULL)
14667 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 }
14669 else
14670 {
14671 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14672 if (winvarname != NULL)
14673 {
14674 STRCPY(winvarname, "w:");
14675 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014676 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 vim_free(winvarname);
14678 }
14679 }
14680
14681#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014682 /* Restore current tabpage and window, if still valid (autocomands can
14683 * make them invalid). */
14684 if (valid_tabpage(save_curtab))
14685 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 if (win_valid(save_curwin))
14687 {
14688 curwin = save_curwin;
14689 curbuf = curwin->w_buffer;
14690 }
14691#endif
14692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693}
14694
14695/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014696 * "shellescape({string})" function
14697 */
14698 static void
14699f_shellescape(argvars, rettv)
14700 typval_T *argvars;
14701 typval_T *rettv;
14702{
14703 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14704 rettv->v_type = VAR_STRING;
14705}
14706
14707/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 */
14710 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014712 typval_T *argvars;
14713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014715 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716
Bram Moolenaar0d660222005-01-07 21:51:51 +000014717 p = get_tv_string(&argvars[0]);
14718 rettv->vval.v_string = vim_strsave(p);
14719 simplify_filename(rettv->vval.v_string); /* simplify in place */
14720 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721}
14722
Bram Moolenaar0d660222005-01-07 21:51:51 +000014723static int
14724#ifdef __BORLANDC__
14725 _RTLENTRYF
14726#endif
14727 item_compare __ARGS((const void *s1, const void *s2));
14728static int
14729#ifdef __BORLANDC__
14730 _RTLENTRYF
14731#endif
14732 item_compare2 __ARGS((const void *s1, const void *s2));
14733
14734static int item_compare_ic;
14735static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014736static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014737#define ITEM_COMPARE_FAIL 999
14738
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014742 static int
14743#ifdef __BORLANDC__
14744_RTLENTRYF
14745#endif
14746item_compare(s1, s2)
14747 const void *s1;
14748 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750 char_u *p1, *p2;
14751 char_u *tofree1, *tofree2;
14752 int res;
14753 char_u numbuf1[NUMBUFLEN];
14754 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014756 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14757 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014758 if (item_compare_ic)
14759 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014761 res = STRCMP(p1, p2);
14762 vim_free(tofree1);
14763 vim_free(tofree2);
14764 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765}
14766
14767 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014768#ifdef __BORLANDC__
14769_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014771item_compare2(s1, s2)
14772 const void *s1;
14773 const void *s2;
14774{
14775 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014777 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014780 /* shortcut after failure in previous call; compare all items equal */
14781 if (item_compare_func_err)
14782 return 0;
14783
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014784 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14785 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014786 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14787 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014788
14789 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014790 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014791 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014792 clear_tv(&argv[0]);
14793 clear_tv(&argv[1]);
14794
14795 if (res == FAIL)
14796 res = ITEM_COMPARE_FAIL;
14797 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014798 /* return value has wrong type */
14799 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14800 if (item_compare_func_err)
14801 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014802 clear_tv(&rettv);
14803 return res;
14804}
14805
14806/*
14807 * "sort({list})" function
14808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014810f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014811 typval_T *argvars;
14812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813{
Bram Moolenaar33570922005-01-25 22:26:29 +000014814 list_T *l;
14815 listitem_T *li;
14816 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014817 long len;
14818 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819
Bram Moolenaar0d660222005-01-07 21:51:51 +000014820 rettv->vval.v_number = 0;
14821 if (argvars[0].v_type != VAR_LIST)
14822 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 else
14824 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014825 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014826 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014827 return;
14828 rettv->vval.v_list = l;
14829 rettv->v_type = VAR_LIST;
14830 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831
Bram Moolenaar0d660222005-01-07 21:51:51 +000014832 len = list_len(l);
14833 if (len <= 1)
14834 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835
Bram Moolenaar0d660222005-01-07 21:51:51 +000014836 item_compare_ic = FALSE;
14837 item_compare_func = NULL;
14838 if (argvars[1].v_type != VAR_UNKNOWN)
14839 {
14840 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014841 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014842 else
14843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014844 int error = FALSE;
14845
14846 i = get_tv_number_chk(&argvars[1], &error);
14847 if (error)
14848 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014849 if (i == 1)
14850 item_compare_ic = TRUE;
14851 else
14852 item_compare_func = get_tv_string(&argvars[1]);
14853 }
14854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855
Bram Moolenaar0d660222005-01-07 21:51:51 +000014856 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014857 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014858 if (ptrs == NULL)
14859 return;
14860 i = 0;
14861 for (li = l->lv_first; li != NULL; li = li->li_next)
14862 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014864 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014865 /* test the compare function */
14866 if (item_compare_func != NULL
14867 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14868 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014869 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014871 {
14872 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874 item_compare_func == NULL ? item_compare : item_compare2);
14875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014876 if (!item_compare_func_err)
14877 {
14878 /* Clear the List and append the items in the sorted order. */
14879 l->lv_first = l->lv_last = NULL;
14880 l->lv_len = 0;
14881 for (i = 0; i < len; ++i)
14882 list_append(l, ptrs[i]);
14883 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014884 }
14885
14886 vim_free(ptrs);
14887 }
14888}
14889
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014890/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014891 * "soundfold({word})" function
14892 */
14893 static void
14894f_soundfold(argvars, rettv)
14895 typval_T *argvars;
14896 typval_T *rettv;
14897{
14898 char_u *s;
14899
14900 rettv->v_type = VAR_STRING;
14901 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014902#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014903 rettv->vval.v_string = eval_soundfold(s);
14904#else
14905 rettv->vval.v_string = vim_strsave(s);
14906#endif
14907}
14908
14909/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014910 * "spellbadword()" function
14911 */
14912/* ARGSUSED */
14913 static void
14914f_spellbadword(argvars, rettv)
14915 typval_T *argvars;
14916 typval_T *rettv;
14917{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014918 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014919 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014920 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014921
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014922 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014923 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014924
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014925#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014926 if (argvars[0].v_type == VAR_UNKNOWN)
14927 {
14928 /* Find the start and length of the badly spelled word. */
14929 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14930 if (len != 0)
14931 word = ml_get_cursor();
14932 }
14933 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14934 {
14935 char_u *str = get_tv_string_chk(&argvars[0]);
14936 int capcol = -1;
14937
14938 if (str != NULL)
14939 {
14940 /* Check the argument for spelling. */
14941 while (*str != NUL)
14942 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014943 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014944 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014945 {
14946 word = str;
14947 break;
14948 }
14949 str += len;
14950 }
14951 }
14952 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014953#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014954
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014955 list_append_string(rettv->vval.v_list, word, len);
14956 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014957 attr == HLF_SPB ? "bad" :
14958 attr == HLF_SPR ? "rare" :
14959 attr == HLF_SPL ? "local" :
14960 attr == HLF_SPC ? "caps" :
14961 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014962}
14963
14964/*
14965 * "spellsuggest()" function
14966 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014967/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014968 static void
14969f_spellsuggest(argvars, rettv)
14970 typval_T *argvars;
14971 typval_T *rettv;
14972{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014973#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014974 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014975 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014976 int maxcount;
14977 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014978 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014979 listitem_T *li;
14980 int need_capital = FALSE;
14981#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014982
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014983 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014984 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014985
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014986#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014987 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14988 {
14989 str = get_tv_string(&argvars[0]);
14990 if (argvars[1].v_type != VAR_UNKNOWN)
14991 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014992 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014993 if (maxcount <= 0)
14994 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014995 if (argvars[2].v_type != VAR_UNKNOWN)
14996 {
14997 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14998 if (typeerr)
14999 return;
15000 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015001 }
15002 else
15003 maxcount = 25;
15004
Bram Moolenaar4770d092006-01-12 23:22:24 +000015005 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015006
15007 for (i = 0; i < ga.ga_len; ++i)
15008 {
15009 str = ((char_u **)ga.ga_data)[i];
15010
15011 li = listitem_alloc();
15012 if (li == NULL)
15013 vim_free(str);
15014 else
15015 {
15016 li->li_tv.v_type = VAR_STRING;
15017 li->li_tv.v_lock = 0;
15018 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015019 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015020 }
15021 }
15022 ga_clear(&ga);
15023 }
15024#endif
15025}
15026
Bram Moolenaar0d660222005-01-07 21:51:51 +000015027 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015028f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015029 typval_T *argvars;
15030 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015031{
15032 char_u *str;
15033 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015034 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035 regmatch_T regmatch;
15036 char_u patbuf[NUMBUFLEN];
15037 char_u *save_cpo;
15038 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015039 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015040 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015041 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015042
15043 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15044 save_cpo = p_cpo;
15045 p_cpo = (char_u *)"";
15046
15047 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015048 if (argvars[1].v_type != VAR_UNKNOWN)
15049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15051 if (pat == NULL)
15052 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015053 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015054 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015055 }
15056 if (pat == NULL || *pat == NUL)
15057 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015059 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015061 if (typeerr)
15062 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063
Bram Moolenaar0d660222005-01-07 21:51:51 +000015064 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15065 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015068 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015070 if (*str == NUL)
15071 match = FALSE; /* empty item at the end */
15072 else
15073 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074 if (match)
15075 end = regmatch.startp[0];
15076 else
15077 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015078 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15079 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015081 if (list_append_string(rettv->vval.v_list, str,
15082 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 }
15085 if (!match)
15086 break;
15087 /* Advance to just after the match. */
15088 if (regmatch.endp[0] > str)
15089 col = 0;
15090 else
15091 {
15092 /* Don't get stuck at the same match. */
15093#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015094 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095#else
15096 col = 1;
15097#endif
15098 }
15099 str = regmatch.endp[0];
15100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106}
15107
Bram Moolenaar2c932302006-03-18 21:42:09 +000015108/*
15109 * "str2nr()" function
15110 */
15111 static void
15112f_str2nr(argvars, rettv)
15113 typval_T *argvars;
15114 typval_T *rettv;
15115{
15116 int base = 10;
15117 char_u *p;
15118 long n;
15119
15120 if (argvars[1].v_type != VAR_UNKNOWN)
15121 {
15122 base = get_tv_number(&argvars[1]);
15123 if (base != 8 && base != 10 && base != 16)
15124 {
15125 EMSG(_(e_invarg));
15126 return;
15127 }
15128 }
15129
15130 p = skipwhite(get_tv_string(&argvars[0]));
15131 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15132 rettv->vval.v_number = n;
15133}
15134
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135#ifdef HAVE_STRFTIME
15136/*
15137 * "strftime({format}[, {time}])" function
15138 */
15139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015141 typval_T *argvars;
15142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143{
15144 char_u result_buf[256];
15145 struct tm *curtime;
15146 time_t seconds;
15147 char_u *p;
15148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015151 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015152 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 seconds = time(NULL);
15154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015155 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156 curtime = localtime(&seconds);
15157 /* MSVC returns NULL for an invalid value of seconds. */
15158 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 else
15161 {
15162# ifdef FEAT_MBYTE
15163 vimconv_T conv;
15164 char_u *enc;
15165
15166 conv.vc_type = CONV_NONE;
15167 enc = enc_locale();
15168 convert_setup(&conv, p_enc, enc);
15169 if (conv.vc_type != CONV_NONE)
15170 p = string_convert(&conv, p, NULL);
15171# endif
15172 if (p != NULL)
15173 (void)strftime((char *)result_buf, sizeof(result_buf),
15174 (char *)p, curtime);
15175 else
15176 result_buf[0] = NUL;
15177
15178# ifdef FEAT_MBYTE
15179 if (conv.vc_type != CONV_NONE)
15180 vim_free(p);
15181 convert_setup(&conv, enc, p_enc);
15182 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015183 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 else
15185# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015186 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187
15188# ifdef FEAT_MBYTE
15189 /* Release conversion descriptors */
15190 convert_setup(&conv, NULL, NULL);
15191 vim_free(enc);
15192# endif
15193 }
15194}
15195#endif
15196
15197/*
15198 * "stridx()" function
15199 */
15200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015201f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015202 typval_T *argvars;
15203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204{
15205 char_u buf[NUMBUFLEN];
15206 char_u *needle;
15207 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015208 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 needle = get_tv_string_chk(&argvars[1]);
15213 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015214 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215 if (needle == NULL || haystack == NULL)
15216 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 if (argvars[2].v_type != VAR_UNKNOWN)
15219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 int error = FALSE;
15221
15222 start_idx = get_tv_number_chk(&argvars[2], &error);
15223 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015224 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015225 if (start_idx >= 0)
15226 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015227 }
15228
15229 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15230 if (pos != NULL)
15231 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232}
15233
15234/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015235 * "string()" function
15236 */
15237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015238f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015239 typval_T *argvars;
15240 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015241{
15242 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015243 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015245 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015246 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015247 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015248 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249}
15250
15251/*
15252 * "strlen()" function
15253 */
15254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015255f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015256 typval_T *argvars;
15257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259 rettv->vval.v_number = (varnumber_T)(STRLEN(
15260 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261}
15262
15263/*
15264 * "strpart()" function
15265 */
15266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015268 typval_T *argvars;
15269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270{
15271 char_u *p;
15272 int n;
15273 int len;
15274 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015275 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015277 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 slen = (int)STRLEN(p);
15279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015280 n = get_tv_number_chk(&argvars[1], &error);
15281 if (error)
15282 len = 0;
15283 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015284 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285 else
15286 len = slen - n; /* default len: all bytes that are available. */
15287
15288 /*
15289 * Only return the overlap between the specified part and the actual
15290 * string.
15291 */
15292 if (n < 0)
15293 {
15294 len += n;
15295 n = 0;
15296 }
15297 else if (n > slen)
15298 n = slen;
15299 if (len < 0)
15300 len = 0;
15301 else if (n + len > slen)
15302 len = slen - n;
15303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304 rettv->v_type = VAR_STRING;
15305 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306}
15307
15308/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309 * "strridx()" function
15310 */
15311 static void
15312f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015313 typval_T *argvars;
15314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015315{
15316 char_u buf[NUMBUFLEN];
15317 char_u *needle;
15318 char_u *haystack;
15319 char_u *rest;
15320 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015321 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015323 needle = get_tv_string_chk(&argvars[1]);
15324 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325
15326 rettv->vval.v_number = -1;
15327 if (needle == NULL || haystack == NULL)
15328 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015329
15330 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015331 if (argvars[2].v_type != VAR_UNKNOWN)
15332 {
15333 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015334 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015335 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015336 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015337 }
15338 else
15339 end_idx = haystack_len;
15340
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015342 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015344 lastmatch = haystack + end_idx;
15345 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015346 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015347 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348 for (rest = haystack; *rest != '\0'; ++rest)
15349 {
15350 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015351 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015352 break;
15353 lastmatch = rest;
15354 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015355 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015356
15357 if (lastmatch == NULL)
15358 rettv->vval.v_number = -1;
15359 else
15360 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15361}
15362
15363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 * "strtrans()" function
15365 */
15366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015367f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015368 typval_T *argvars;
15369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015371 rettv->v_type = VAR_STRING;
15372 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373}
15374
15375/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015376 * "submatch()" function
15377 */
15378 static void
15379f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015380 typval_T *argvars;
15381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015382{
15383 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015384 rettv->vval.v_string =
15385 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015386}
15387
15388/*
15389 * "substitute()" function
15390 */
15391 static void
15392f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015393 typval_T *argvars;
15394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015395{
15396 char_u patbuf[NUMBUFLEN];
15397 char_u subbuf[NUMBUFLEN];
15398 char_u flagsbuf[NUMBUFLEN];
15399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015400 char_u *str = get_tv_string_chk(&argvars[0]);
15401 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15402 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15403 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15404
Bram Moolenaar0d660222005-01-07 21:51:51 +000015405 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015406 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15407 rettv->vval.v_string = NULL;
15408 else
15409 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410}
15411
15412/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015413 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 */
15415/*ARGSUSED*/
15416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015418 typval_T *argvars;
15419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420{
15421 int id = 0;
15422#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015423 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 long col;
15425 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015426 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015428 lnum = get_tv_lnum(argvars); /* -1 on type error */
15429 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15430 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015432 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015433 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015434 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435#endif
15436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438}
15439
15440/*
15441 * "synIDattr(id, what [, mode])" function
15442 */
15443/*ARGSUSED*/
15444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015446 typval_T *argvars;
15447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448{
15449 char_u *p = NULL;
15450#ifdef FEAT_SYN_HL
15451 int id;
15452 char_u *what;
15453 char_u *mode;
15454 char_u modebuf[NUMBUFLEN];
15455 int modec;
15456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015457 id = get_tv_number(&argvars[0]);
15458 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015459 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 modec = TOLOWER_ASC(mode[0]);
15463 if (modec != 't' && modec != 'c'
15464#ifdef FEAT_GUI
15465 && modec != 'g'
15466#endif
15467 )
15468 modec = 0; /* replace invalid with current */
15469 }
15470 else
15471 {
15472#ifdef FEAT_GUI
15473 if (gui.in_use)
15474 modec = 'g';
15475 else
15476#endif
15477 if (t_colors > 1)
15478 modec = 'c';
15479 else
15480 modec = 't';
15481 }
15482
15483
15484 switch (TOLOWER_ASC(what[0]))
15485 {
15486 case 'b':
15487 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15488 p = highlight_color(id, what, modec);
15489 else /* bold */
15490 p = highlight_has_attr(id, HL_BOLD, modec);
15491 break;
15492
15493 case 'f': /* fg[#] */
15494 p = highlight_color(id, what, modec);
15495 break;
15496
15497 case 'i':
15498 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15499 p = highlight_has_attr(id, HL_INVERSE, modec);
15500 else /* italic */
15501 p = highlight_has_attr(id, HL_ITALIC, modec);
15502 break;
15503
15504 case 'n': /* name */
15505 p = get_highlight_name(NULL, id - 1);
15506 break;
15507
15508 case 'r': /* reverse */
15509 p = highlight_has_attr(id, HL_INVERSE, modec);
15510 break;
15511
15512 case 's': /* standout */
15513 p = highlight_has_attr(id, HL_STANDOUT, modec);
15514 break;
15515
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015516 case 'u':
15517 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15518 /* underline */
15519 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15520 else
15521 /* undercurl */
15522 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 break;
15524 }
15525
15526 if (p != NULL)
15527 p = vim_strsave(p);
15528#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529 rettv->v_type = VAR_STRING;
15530 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531}
15532
15533/*
15534 * "synIDtrans(id)" function
15535 */
15536/*ARGSUSED*/
15537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 typval_T *argvars;
15540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541{
15542 int id;
15543
15544#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546
15547 if (id > 0)
15548 id = syn_get_final_id(id);
15549 else
15550#endif
15551 id = 0;
15552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554}
15555
15556/*
15557 * "system()" function
15558 */
15559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015561 typval_T *argvars;
15562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015564 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015566 char_u *infile = NULL;
15567 char_u buf[NUMBUFLEN];
15568 int err = FALSE;
15569 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015571 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015572 {
15573 /*
15574 * Write the string to a temp file, to be used for input of the shell
15575 * command.
15576 */
15577 if ((infile = vim_tempname('i')) == NULL)
15578 {
15579 EMSG(_(e_notmp));
15580 return;
15581 }
15582
15583 fd = mch_fopen((char *)infile, WRITEBIN);
15584 if (fd == NULL)
15585 {
15586 EMSG2(_(e_notopen), infile);
15587 goto done;
15588 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015589 p = get_tv_string_buf_chk(&argvars[1], buf);
15590 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015591 {
15592 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015593 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015594 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015595 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15596 err = TRUE;
15597 if (fclose(fd) != 0)
15598 err = TRUE;
15599 if (err)
15600 {
15601 EMSG(_("E677: Error writing temp file"));
15602 goto done;
15603 }
15604 }
15605
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015606 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15607 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015608
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609#ifdef USE_CR
15610 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015611 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 {
15613 char_u *s;
15614
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015615 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 {
15617 if (*s == CAR)
15618 *s = NL;
15619 }
15620 }
15621#else
15622# ifdef USE_CRNL
15623 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015624 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 {
15626 char_u *s, *d;
15627
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015628 d = res;
15629 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 {
15631 if (s[0] == CAR && s[1] == NL)
15632 ++s;
15633 *d++ = *s;
15634 }
15635 *d = NUL;
15636 }
15637# endif
15638#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015639
15640done:
15641 if (infile != NULL)
15642 {
15643 mch_remove(infile);
15644 vim_free(infile);
15645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015646 rettv->v_type = VAR_STRING;
15647 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648}
15649
15650/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015651 * "tabpagebuflist()" function
15652 */
15653/* ARGSUSED */
15654 static void
15655f_tabpagebuflist(argvars, rettv)
15656 typval_T *argvars;
15657 typval_T *rettv;
15658{
15659#ifndef FEAT_WINDOWS
15660 rettv->vval.v_number = 0;
15661#else
15662 tabpage_T *tp;
15663 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015664
15665 if (argvars[0].v_type == VAR_UNKNOWN)
15666 wp = firstwin;
15667 else
15668 {
15669 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15670 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015671 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015672 }
15673 if (wp == NULL)
15674 rettv->vval.v_number = 0;
15675 else
15676 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015677 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015678 rettv->vval.v_number = 0;
15679 else
15680 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015681 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015682 if (list_append_number(rettv->vval.v_list,
15683 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015684 break;
15685 }
15686 }
15687#endif
15688}
15689
15690
15691/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015692 * "tabpagenr()" function
15693 */
15694/* ARGSUSED */
15695 static void
15696f_tabpagenr(argvars, rettv)
15697 typval_T *argvars;
15698 typval_T *rettv;
15699{
15700 int nr = 1;
15701#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015702 char_u *arg;
15703
15704 if (argvars[0].v_type != VAR_UNKNOWN)
15705 {
15706 arg = get_tv_string_chk(&argvars[0]);
15707 nr = 0;
15708 if (arg != NULL)
15709 {
15710 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015711 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015712 else
15713 EMSG2(_(e_invexpr2), arg);
15714 }
15715 }
15716 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015717 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015718#endif
15719 rettv->vval.v_number = nr;
15720}
15721
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015722
15723#ifdef FEAT_WINDOWS
15724static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15725
15726/*
15727 * Common code for tabpagewinnr() and winnr().
15728 */
15729 static int
15730get_winnr(tp, argvar)
15731 tabpage_T *tp;
15732 typval_T *argvar;
15733{
15734 win_T *twin;
15735 int nr = 1;
15736 win_T *wp;
15737 char_u *arg;
15738
15739 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15740 if (argvar->v_type != VAR_UNKNOWN)
15741 {
15742 arg = get_tv_string_chk(argvar);
15743 if (arg == NULL)
15744 nr = 0; /* type error; errmsg already given */
15745 else if (STRCMP(arg, "$") == 0)
15746 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15747 else if (STRCMP(arg, "#") == 0)
15748 {
15749 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15750 if (twin == NULL)
15751 nr = 0;
15752 }
15753 else
15754 {
15755 EMSG2(_(e_invexpr2), arg);
15756 nr = 0;
15757 }
15758 }
15759
15760 if (nr > 0)
15761 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15762 wp != twin; wp = wp->w_next)
15763 ++nr;
15764 return nr;
15765}
15766#endif
15767
15768/*
15769 * "tabpagewinnr()" function
15770 */
15771/* ARGSUSED */
15772 static void
15773f_tabpagewinnr(argvars, rettv)
15774 typval_T *argvars;
15775 typval_T *rettv;
15776{
15777 int nr = 1;
15778#ifdef FEAT_WINDOWS
15779 tabpage_T *tp;
15780
15781 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15782 if (tp == NULL)
15783 nr = 0;
15784 else
15785 nr = get_winnr(tp, &argvars[1]);
15786#endif
15787 rettv->vval.v_number = nr;
15788}
15789
15790
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015791/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015792 * "tagfiles()" function
15793 */
15794/*ARGSUSED*/
15795 static void
15796f_tagfiles(argvars, rettv)
15797 typval_T *argvars;
15798 typval_T *rettv;
15799{
15800 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015801 tagname_T tn;
15802 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015803
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015804 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015805 {
15806 rettv->vval.v_number = 0;
15807 return;
15808 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015809
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015810 for (first = TRUE; ; first = FALSE)
15811 if (get_tagfname(&tn, first, fname) == FAIL
15812 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015813 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015814 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015815}
15816
15817/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015818 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015819 */
15820 static void
15821f_taglist(argvars, rettv)
15822 typval_T *argvars;
15823 typval_T *rettv;
15824{
15825 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015826
15827 tag_pattern = get_tv_string(&argvars[0]);
15828
15829 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015830 if (*tag_pattern == NUL)
15831 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015832
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015833 if (rettv_list_alloc(rettv) == OK)
15834 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015835}
15836
15837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 * "tempname()" function
15839 */
15840/*ARGSUSED*/
15841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015842f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015843 typval_T *argvars;
15844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845{
15846 static int x = 'A';
15847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015848 rettv->v_type = VAR_STRING;
15849 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850
15851 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15852 * names. Skip 'I' and 'O', they are used for shell redirection. */
15853 do
15854 {
15855 if (x == 'Z')
15856 x = '0';
15857 else if (x == '9')
15858 x = 'A';
15859 else
15860 {
15861#ifdef EBCDIC
15862 if (x == 'I')
15863 x = 'J';
15864 else if (x == 'R')
15865 x = 'S';
15866 else
15867#endif
15868 ++x;
15869 }
15870 } while (x == 'I' || x == 'O');
15871}
15872
15873/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015874 * "test(list)" function: Just checking the walls...
15875 */
15876/*ARGSUSED*/
15877 static void
15878f_test(argvars, rettv)
15879 typval_T *argvars;
15880 typval_T *rettv;
15881{
15882 /* Used for unit testing. Change the code below to your liking. */
15883#if 0
15884 listitem_T *li;
15885 list_T *l;
15886 char_u *bad, *good;
15887
15888 if (argvars[0].v_type != VAR_LIST)
15889 return;
15890 l = argvars[0].vval.v_list;
15891 if (l == NULL)
15892 return;
15893 li = l->lv_first;
15894 if (li == NULL)
15895 return;
15896 bad = get_tv_string(&li->li_tv);
15897 li = li->li_next;
15898 if (li == NULL)
15899 return;
15900 good = get_tv_string(&li->li_tv);
15901 rettv->vval.v_number = test_edit_score(bad, good);
15902#endif
15903}
15904
15905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 * "tolower(string)" function
15907 */
15908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015909f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015910 typval_T *argvars;
15911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912{
15913 char_u *p;
15914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015915 p = vim_strsave(get_tv_string(&argvars[0]));
15916 rettv->v_type = VAR_STRING;
15917 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918
15919 if (p != NULL)
15920 while (*p != NUL)
15921 {
15922#ifdef FEAT_MBYTE
15923 int l;
15924
15925 if (enc_utf8)
15926 {
15927 int c, lc;
15928
15929 c = utf_ptr2char(p);
15930 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015931 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 /* TODO: reallocate string when byte count changes. */
15933 if (utf_char2len(lc) == l)
15934 utf_char2bytes(lc, p);
15935 p += l;
15936 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015937 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 p += l; /* skip multi-byte character */
15939 else
15940#endif
15941 {
15942 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15943 ++p;
15944 }
15945 }
15946}
15947
15948/*
15949 * "toupper(string)" function
15950 */
15951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015952f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015953 typval_T *argvars;
15954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015956 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015957 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958}
15959
15960/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015961 * "tr(string, fromstr, tostr)" function
15962 */
15963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015964f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015965 typval_T *argvars;
15966 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015967{
15968 char_u *instr;
15969 char_u *fromstr;
15970 char_u *tostr;
15971 char_u *p;
15972#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015973 int inlen;
15974 int fromlen;
15975 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015976 int idx;
15977 char_u *cpstr;
15978 int cplen;
15979 int first = TRUE;
15980#endif
15981 char_u buf[NUMBUFLEN];
15982 char_u buf2[NUMBUFLEN];
15983 garray_T ga;
15984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015985 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015986 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15987 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015988
15989 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015990 rettv->v_type = VAR_STRING;
15991 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015992 if (fromstr == NULL || tostr == NULL)
15993 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015994 ga_init2(&ga, (int)sizeof(char), 80);
15995
15996#ifdef FEAT_MBYTE
15997 if (!has_mbyte)
15998#endif
15999 /* not multi-byte: fromstr and tostr must be the same length */
16000 if (STRLEN(fromstr) != STRLEN(tostr))
16001 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016002#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016003error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016004#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016005 EMSG2(_(e_invarg2), fromstr);
16006 ga_clear(&ga);
16007 return;
16008 }
16009
16010 /* fromstr and tostr have to contain the same number of chars */
16011 while (*instr != NUL)
16012 {
16013#ifdef FEAT_MBYTE
16014 if (has_mbyte)
16015 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016016 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016017 cpstr = instr;
16018 cplen = inlen;
16019 idx = 0;
16020 for (p = fromstr; *p != NUL; p += fromlen)
16021 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016022 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016023 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16024 {
16025 for (p = tostr; *p != NUL; p += tolen)
16026 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016027 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016028 if (idx-- == 0)
16029 {
16030 cplen = tolen;
16031 cpstr = p;
16032 break;
16033 }
16034 }
16035 if (*p == NUL) /* tostr is shorter than fromstr */
16036 goto error;
16037 break;
16038 }
16039 ++idx;
16040 }
16041
16042 if (first && cpstr == instr)
16043 {
16044 /* Check that fromstr and tostr have the same number of
16045 * (multi-byte) characters. Done only once when a character
16046 * of instr doesn't appear in fromstr. */
16047 first = FALSE;
16048 for (p = tostr; *p != NUL; p += tolen)
16049 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016050 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016051 --idx;
16052 }
16053 if (idx != 0)
16054 goto error;
16055 }
16056
16057 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016058 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016059 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016060
16061 instr += inlen;
16062 }
16063 else
16064#endif
16065 {
16066 /* When not using multi-byte chars we can do it faster. */
16067 p = vim_strchr(fromstr, *instr);
16068 if (p != NULL)
16069 ga_append(&ga, tostr[p - fromstr]);
16070 else
16071 ga_append(&ga, *instr);
16072 ++instr;
16073 }
16074 }
16075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016077}
16078
16079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 * "type(expr)" function
16081 */
16082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016083f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016084 typval_T *argvars;
16085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016087 int n;
16088
16089 switch (argvars[0].v_type)
16090 {
16091 case VAR_NUMBER: n = 0; break;
16092 case VAR_STRING: n = 1; break;
16093 case VAR_FUNC: n = 2; break;
16094 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016095 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016096 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16097 }
16098 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099}
16100
16101/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016102 * "values(dict)" function
16103 */
16104 static void
16105f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016106 typval_T *argvars;
16107 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016108{
16109 dict_list(argvars, rettv, 1);
16110}
16111
16112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 * "virtcol(string)" function
16114 */
16115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016116f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016117 typval_T *argvars;
16118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119{
16120 colnr_T vcol = 0;
16121 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016122 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016124 fp = var2fpos(&argvars[0], FALSE, &fnum);
16125 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16126 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127 {
16128 getvvcol(curwin, fp, NULL, NULL, &vcol);
16129 ++vcol;
16130 }
16131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016132 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133}
16134
16135/*
16136 * "visualmode()" function
16137 */
16138/*ARGSUSED*/
16139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016140f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016141 typval_T *argvars;
16142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143{
16144#ifdef FEAT_VISUAL
16145 char_u str[2];
16146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016147 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 str[0] = curbuf->b_visual_mode_eval;
16149 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016150 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151
16152 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016153 if ((argvars[0].v_type == VAR_NUMBER
16154 && argvars[0].vval.v_number != 0)
16155 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016156 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 curbuf->b_visual_mode_eval = NUL;
16158#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016159 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160#endif
16161}
16162
16163/*
16164 * "winbufnr(nr)" function
16165 */
16166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 typval_T *argvars;
16169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170{
16171 win_T *wp;
16172
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016173 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016177 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178}
16179
16180/*
16181 * "wincol()" function
16182 */
16183/*ARGSUSED*/
16184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016186 typval_T *argvars;
16187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188{
16189 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016190 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191}
16192
16193/*
16194 * "winheight(nr)" function
16195 */
16196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016197f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016198 typval_T *argvars;
16199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200{
16201 win_T *wp;
16202
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016203 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208}
16209
16210/*
16211 * "winline()" function
16212 */
16213/*ARGSUSED*/
16214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016215f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016216 typval_T *argvars;
16217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218{
16219 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016220 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221}
16222
16223/*
16224 * "winnr()" function
16225 */
16226/* ARGSUSED */
16227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016228f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016229 typval_T *argvars;
16230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231{
16232 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016233
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016235 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016237 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238}
16239
16240/*
16241 * "winrestcmd()" function
16242 */
16243/* ARGSUSED */
16244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016245f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016246 typval_T *argvars;
16247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248{
16249#ifdef FEAT_WINDOWS
16250 win_T *wp;
16251 int winnr = 1;
16252 garray_T ga;
16253 char_u buf[50];
16254
16255 ga_init2(&ga, (int)sizeof(char), 70);
16256 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16257 {
16258 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16259 ga_concat(&ga, buf);
16260# ifdef FEAT_VERTSPLIT
16261 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16262 ga_concat(&ga, buf);
16263# endif
16264 ++winnr;
16265 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016266 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016272 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273}
16274
16275/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016276 * "winrestview()" function
16277 */
16278/* ARGSUSED */
16279 static void
16280f_winrestview(argvars, rettv)
16281 typval_T *argvars;
16282 typval_T *rettv;
16283{
16284 dict_T *dict;
16285
16286 if (argvars[0].v_type != VAR_DICT
16287 || (dict = argvars[0].vval.v_dict) == NULL)
16288 EMSG(_(e_invarg));
16289 else
16290 {
16291 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16292 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16293#ifdef FEAT_VIRTUALEDIT
16294 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16295#endif
16296 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016297 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016298
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016299 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016300#ifdef FEAT_DIFF
16301 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16302#endif
16303 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16304 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16305
16306 check_cursor();
16307 changed_cline_bef_curs();
16308 invalidate_botline();
16309 redraw_later(VALID);
16310
16311 if (curwin->w_topline == 0)
16312 curwin->w_topline = 1;
16313 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16314 curwin->w_topline = curbuf->b_ml.ml_line_count;
16315#ifdef FEAT_DIFF
16316 check_topfill(curwin, TRUE);
16317#endif
16318 }
16319}
16320
16321/*
16322 * "winsaveview()" function
16323 */
16324/* ARGSUSED */
16325 static void
16326f_winsaveview(argvars, rettv)
16327 typval_T *argvars;
16328 typval_T *rettv;
16329{
16330 dict_T *dict;
16331
16332 dict = dict_alloc();
16333 if (dict == NULL)
16334 return;
16335 rettv->v_type = VAR_DICT;
16336 rettv->vval.v_dict = dict;
16337 ++dict->dv_refcount;
16338
16339 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16340 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16341#ifdef FEAT_VIRTUALEDIT
16342 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16343#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016344 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016345 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16346
16347 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16348#ifdef FEAT_DIFF
16349 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16350#endif
16351 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16352 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16353}
16354
16355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356 * "winwidth(nr)" function
16357 */
16358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016359f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016360 typval_T *argvars;
16361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362{
16363 win_T *wp;
16364
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016365 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016367 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 else
16369#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016372 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373#endif
16374}
16375
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016377 * "writefile()" function
16378 */
16379 static void
16380f_writefile(argvars, rettv)
16381 typval_T *argvars;
16382 typval_T *rettv;
16383{
16384 int binary = FALSE;
16385 char_u *fname;
16386 FILE *fd;
16387 listitem_T *li;
16388 char_u *s;
16389 int ret = 0;
16390 int c;
16391
16392 if (argvars[0].v_type != VAR_LIST)
16393 {
16394 EMSG2(_(e_listarg), "writefile()");
16395 return;
16396 }
16397 if (argvars[0].vval.v_list == NULL)
16398 return;
16399
16400 if (argvars[2].v_type != VAR_UNKNOWN
16401 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16402 binary = TRUE;
16403
16404 /* Always open the file in binary mode, library functions have a mind of
16405 * their own about CR-LF conversion. */
16406 fname = get_tv_string(&argvars[1]);
16407 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16408 {
16409 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16410 ret = -1;
16411 }
16412 else
16413 {
16414 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16415 li = li->li_next)
16416 {
16417 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16418 {
16419 if (*s == '\n')
16420 c = putc(NUL, fd);
16421 else
16422 c = putc(*s, fd);
16423 if (c == EOF)
16424 {
16425 ret = -1;
16426 break;
16427 }
16428 }
16429 if (!binary || li->li_next != NULL)
16430 if (putc('\n', fd) == EOF)
16431 {
16432 ret = -1;
16433 break;
16434 }
16435 if (ret < 0)
16436 {
16437 EMSG(_(e_write));
16438 break;
16439 }
16440 }
16441 fclose(fd);
16442 }
16443
16444 rettv->vval.v_number = ret;
16445}
16446
16447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016449 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 */
16451 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016452var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016453 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016455 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016457 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016459 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460
Bram Moolenaara5525202006-03-02 22:52:09 +000016461 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016462 if (varp->v_type == VAR_LIST)
16463 {
16464 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016465 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016466 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016467
16468 l = varp->vval.v_list;
16469 if (l == NULL)
16470 return NULL;
16471
16472 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016473 pos.lnum = list_find_nr(l, 0L, &error);
16474 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016475 return NULL; /* invalid line number */
16476
16477 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016478 pos.col = list_find_nr(l, 1L, &error);
16479 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016480 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016481 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016482 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016483 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016484 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016485 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016486
Bram Moolenaara5525202006-03-02 22:52:09 +000016487#ifdef FEAT_VIRTUALEDIT
16488 /* Get the virtual offset. Defaults to zero. */
16489 pos.coladd = list_find_nr(l, 2L, &error);
16490 if (error)
16491 pos.coladd = 0;
16492#endif
16493
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016494 return &pos;
16495 }
16496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016497 name = get_tv_string_chk(varp);
16498 if (name == NULL)
16499 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 if (name[0] == '.') /* cursor */
16501 return &curwin->w_cursor;
16502 if (name[0] == '\'') /* mark */
16503 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016504 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16506 return NULL;
16507 return pp;
16508 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016509
16510#ifdef FEAT_VIRTUALEDIT
16511 pos.coladd = 0;
16512#endif
16513
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016514 if (name[0] == 'w' && lnum)
16515 {
16516 pos.col = 0;
16517 if (name[1] == '0') /* "w0": first visible line */
16518 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016519 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016520 pos.lnum = curwin->w_topline;
16521 return &pos;
16522 }
16523 else if (name[1] == '$') /* "w$": last visible line */
16524 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016525 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016526 pos.lnum = curwin->w_botline - 1;
16527 return &pos;
16528 }
16529 }
16530 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 {
16532 if (lnum)
16533 {
16534 pos.lnum = curbuf->b_ml.ml_line_count;
16535 pos.col = 0;
16536 }
16537 else
16538 {
16539 pos.lnum = curwin->w_cursor.lnum;
16540 pos.col = (colnr_T)STRLEN(ml_get_curline());
16541 }
16542 return &pos;
16543 }
16544 return NULL;
16545}
16546
16547/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016548 * Convert list in "arg" into a position and optional file number.
16549 * When "fnump" is NULL there is no file number, only 3 items.
16550 * Note that the column is passed on as-is, the caller may want to decrement
16551 * it to use 1 for the first column.
16552 * Return FAIL when conversion is not possible, doesn't check the position for
16553 * validity.
16554 */
16555 static int
16556list2fpos(arg, posp, fnump)
16557 typval_T *arg;
16558 pos_T *posp;
16559 int *fnump;
16560{
16561 list_T *l = arg->vval.v_list;
16562 long i = 0;
16563 long n;
16564
Bram Moolenaarbde35262006-07-23 20:12:24 +000016565 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16566 * when "fnump" isn't NULL and "coladd" is optional. */
16567 if (arg->v_type != VAR_LIST
16568 || l == NULL
16569 || l->lv_len < (fnump == NULL ? 2 : 3)
16570 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016571 return FAIL;
16572
16573 if (fnump != NULL)
16574 {
16575 n = list_find_nr(l, i++, NULL); /* fnum */
16576 if (n < 0)
16577 return FAIL;
16578 if (n == 0)
16579 n = curbuf->b_fnum; /* current buffer */
16580 *fnump = n;
16581 }
16582
16583 n = list_find_nr(l, i++, NULL); /* lnum */
16584 if (n < 0)
16585 return FAIL;
16586 posp->lnum = n;
16587
16588 n = list_find_nr(l, i++, NULL); /* col */
16589 if (n < 0)
16590 return FAIL;
16591 posp->col = n;
16592
16593#ifdef FEAT_VIRTUALEDIT
16594 n = list_find_nr(l, i, NULL);
16595 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016596 posp->coladd = 0;
16597 else
16598 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016599#endif
16600
16601 return OK;
16602}
16603
16604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 * Get the length of an environment variable name.
16606 * Advance "arg" to the first character after the name.
16607 * Return 0 for error.
16608 */
16609 static int
16610get_env_len(arg)
16611 char_u **arg;
16612{
16613 char_u *p;
16614 int len;
16615
16616 for (p = *arg; vim_isIDc(*p); ++p)
16617 ;
16618 if (p == *arg) /* no name found */
16619 return 0;
16620
16621 len = (int)(p - *arg);
16622 *arg = p;
16623 return len;
16624}
16625
16626/*
16627 * Get the length of the name of a function or internal variable.
16628 * "arg" is advanced to the first non-white character after the name.
16629 * Return 0 if something is wrong.
16630 */
16631 static int
16632get_id_len(arg)
16633 char_u **arg;
16634{
16635 char_u *p;
16636 int len;
16637
16638 /* Find the end of the name. */
16639 for (p = *arg; eval_isnamec(*p); ++p)
16640 ;
16641 if (p == *arg) /* no name found */
16642 return 0;
16643
16644 len = (int)(p - *arg);
16645 *arg = skipwhite(p);
16646
16647 return len;
16648}
16649
16650/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016651 * Get the length of the name of a variable or function.
16652 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016654 * Return -1 if curly braces expansion failed.
16655 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656 * If the name contains 'magic' {}'s, expand them and return the
16657 * expanded name in an allocated string via 'alias' - caller must free.
16658 */
16659 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016660get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 char_u **arg;
16662 char_u **alias;
16663 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016664 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665{
16666 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 char_u *p;
16668 char_u *expr_start;
16669 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670
16671 *alias = NULL; /* default to no alias */
16672
16673 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16674 && (*arg)[2] == (int)KE_SNR)
16675 {
16676 /* hard coded <SNR>, already translated */
16677 *arg += 3;
16678 return get_id_len(arg) + 3;
16679 }
16680 len = eval_fname_script(*arg);
16681 if (len > 0)
16682 {
16683 /* literal "<SID>", "s:" or "<SNR>" */
16684 *arg += len;
16685 }
16686
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016688 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016690 p = find_name_end(*arg, &expr_start, &expr_end,
16691 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 if (expr_start != NULL)
16693 {
16694 char_u *temp_string;
16695
16696 if (!evaluate)
16697 {
16698 len += (int)(p - *arg);
16699 *arg = skipwhite(p);
16700 return len;
16701 }
16702
16703 /*
16704 * Include any <SID> etc in the expanded string:
16705 * Thus the -len here.
16706 */
16707 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16708 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016709 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 *alias = temp_string;
16711 *arg = skipwhite(p);
16712 return (int)STRLEN(temp_string);
16713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714
16715 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016716 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 EMSG2(_(e_invexpr2), *arg);
16718
16719 return len;
16720}
16721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016722/*
16723 * Find the end of a variable or function name, taking care of magic braces.
16724 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16725 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016726 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016727 * Return a pointer to just after the name. Equal to "arg" if there is no
16728 * valid name.
16729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016731find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 char_u *arg;
16733 char_u **expr_start;
16734 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016735 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016737 int mb_nest = 0;
16738 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 char_u *p;
16740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016741 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743 *expr_start = NULL;
16744 *expr_end = NULL;
16745 }
16746
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016747 /* Quick check for valid starting character. */
16748 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16749 return arg;
16750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016751 for (p = arg; *p != NUL
16752 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016753 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016754 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016755 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016756 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016757 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016758 if (*p == '\'')
16759 {
16760 /* skip over 'string' to avoid counting [ and ] inside it. */
16761 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16762 ;
16763 if (*p == NUL)
16764 break;
16765 }
16766 else if (*p == '"')
16767 {
16768 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16769 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16770 if (*p == '\\' && p[1] != NUL)
16771 ++p;
16772 if (*p == NUL)
16773 break;
16774 }
16775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016776 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016778 if (*p == '[')
16779 ++br_nest;
16780 else if (*p == ']')
16781 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016784 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016786 if (*p == '{')
16787 {
16788 mb_nest++;
16789 if (expr_start != NULL && *expr_start == NULL)
16790 *expr_start = p;
16791 }
16792 else if (*p == '}')
16793 {
16794 mb_nest--;
16795 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16796 *expr_end = p;
16797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 }
16800
16801 return p;
16802}
16803
16804/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016805 * Expands out the 'magic' {}'s in a variable/function name.
16806 * Note that this can call itself recursively, to deal with
16807 * constructs like foo{bar}{baz}{bam}
16808 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16809 * "in_start" ^
16810 * "expr_start" ^
16811 * "expr_end" ^
16812 * "in_end" ^
16813 *
16814 * Returns a new allocated string, which the caller must free.
16815 * Returns NULL for failure.
16816 */
16817 static char_u *
16818make_expanded_name(in_start, expr_start, expr_end, in_end)
16819 char_u *in_start;
16820 char_u *expr_start;
16821 char_u *expr_end;
16822 char_u *in_end;
16823{
16824 char_u c1;
16825 char_u *retval = NULL;
16826 char_u *temp_result;
16827 char_u *nextcmd = NULL;
16828
16829 if (expr_end == NULL || in_end == NULL)
16830 return NULL;
16831 *expr_start = NUL;
16832 *expr_end = NUL;
16833 c1 = *in_end;
16834 *in_end = NUL;
16835
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016836 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016837 if (temp_result != NULL && nextcmd == NULL)
16838 {
16839 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16840 + (in_end - expr_end) + 1));
16841 if (retval != NULL)
16842 {
16843 STRCPY(retval, in_start);
16844 STRCAT(retval, temp_result);
16845 STRCAT(retval, expr_end + 1);
16846 }
16847 }
16848 vim_free(temp_result);
16849
16850 *in_end = c1; /* put char back for error messages */
16851 *expr_start = '{';
16852 *expr_end = '}';
16853
16854 if (retval != NULL)
16855 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016856 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016857 if (expr_start != NULL)
16858 {
16859 /* Further expansion! */
16860 temp_result = make_expanded_name(retval, expr_start,
16861 expr_end, temp_result);
16862 vim_free(retval);
16863 retval = temp_result;
16864 }
16865 }
16866
16867 return retval;
16868}
16869
16870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016872 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 */
16874 static int
16875eval_isnamec(c)
16876 int c;
16877{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016878 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16879}
16880
16881/*
16882 * Return TRUE if character "c" can be used as the first character in a
16883 * variable or function name (excluding '{' and '}').
16884 */
16885 static int
16886eval_isnamec1(c)
16887 int c;
16888{
16889 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890}
16891
16892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 * Set number v: variable to "val".
16894 */
16895 void
16896set_vim_var_nr(idx, val)
16897 int idx;
16898 long val;
16899{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016900 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901}
16902
16903/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016904 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 */
16906 long
16907get_vim_var_nr(idx)
16908 int idx;
16909{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016910 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911}
16912
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016913#if defined(FEAT_AUTOCMD) || defined(PROTO)
16914/*
16915 * Get string v: variable value. Uses a static buffer, can only be used once.
16916 */
16917 char_u *
16918get_vim_var_str(idx)
16919 int idx;
16920{
16921 return get_tv_string(&vimvars[idx].vv_tv);
16922}
16923#endif
16924
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925/*
16926 * Set v:count, v:count1 and v:prevcount.
16927 */
16928 void
16929set_vcount(count, count1)
16930 long count;
16931 long count1;
16932{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016933 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16934 vimvars[VV_COUNT].vv_nr = count;
16935 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936}
16937
16938/*
16939 * Set string v: variable to a copy of "val".
16940 */
16941 void
16942set_vim_var_string(idx, val, len)
16943 int idx;
16944 char_u *val;
16945 int len; /* length of "val" to use or -1 (whole string) */
16946{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016947 /* Need to do this (at least) once, since we can't initialize a union.
16948 * Will always be invoked when "v:progname" is set. */
16949 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16950
Bram Moolenaare9a41262005-01-15 22:18:47 +000016951 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016953 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016955 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016957 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958}
16959
16960/*
16961 * Set v:register if needed.
16962 */
16963 void
16964set_reg_var(c)
16965 int c;
16966{
16967 char_u regname;
16968
16969 if (c == 0 || c == ' ')
16970 regname = '"';
16971 else
16972 regname = c;
16973 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016974 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 set_vim_var_string(VV_REG, &regname, 1);
16976}
16977
16978/*
16979 * Get or set v:exception. If "oldval" == NULL, return the current value.
16980 * Otherwise, restore the value to "oldval" and return NULL.
16981 * Must always be called in pairs to save and restore v:exception! Does not
16982 * take care of memory allocations.
16983 */
16984 char_u *
16985v_exception(oldval)
16986 char_u *oldval;
16987{
16988 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016989 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990
Bram Moolenaare9a41262005-01-15 22:18:47 +000016991 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 return NULL;
16993}
16994
16995/*
16996 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16997 * Otherwise, restore the value to "oldval" and return NULL.
16998 * Must always be called in pairs to save and restore v:throwpoint! Does not
16999 * take care of memory allocations.
17000 */
17001 char_u *
17002v_throwpoint(oldval)
17003 char_u *oldval;
17004{
17005 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017006 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007
Bram Moolenaare9a41262005-01-15 22:18:47 +000017008 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 return NULL;
17010}
17011
17012#if defined(FEAT_AUTOCMD) || defined(PROTO)
17013/*
17014 * Set v:cmdarg.
17015 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17016 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17017 * Must always be called in pairs!
17018 */
17019 char_u *
17020set_cmdarg(eap, oldarg)
17021 exarg_T *eap;
17022 char_u *oldarg;
17023{
17024 char_u *oldval;
17025 char_u *newval;
17026 unsigned len;
17027
Bram Moolenaare9a41262005-01-15 22:18:47 +000017028 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017029 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017031 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017032 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017033 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 }
17035
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017036 if (eap->force_bin == FORCE_BIN)
17037 len = 6;
17038 else if (eap->force_bin == FORCE_NOBIN)
17039 len = 8;
17040 else
17041 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017042
17043 if (eap->read_edit)
17044 len += 7;
17045
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017046 if (eap->force_ff != 0)
17047 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17048# ifdef FEAT_MBYTE
17049 if (eap->force_enc != 0)
17050 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017051 if (eap->bad_char != 0)
17052 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017053# endif
17054
17055 newval = alloc(len + 1);
17056 if (newval == NULL)
17057 return NULL;
17058
17059 if (eap->force_bin == FORCE_BIN)
17060 sprintf((char *)newval, " ++bin");
17061 else if (eap->force_bin == FORCE_NOBIN)
17062 sprintf((char *)newval, " ++nobin");
17063 else
17064 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017065
17066 if (eap->read_edit)
17067 STRCAT(newval, " ++edit");
17068
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017069 if (eap->force_ff != 0)
17070 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17071 eap->cmd + eap->force_ff);
17072# ifdef FEAT_MBYTE
17073 if (eap->force_enc != 0)
17074 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17075 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017076 if (eap->bad_char != 0)
17077 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17078 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017079# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017080 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017081 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082}
17083#endif
17084
17085/*
17086 * Get the value of internal variable "name".
17087 * Return OK or FAIL.
17088 */
17089 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017090get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 char_u *name;
17092 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017093 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017094 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095{
17096 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017097 typval_T *tv = NULL;
17098 typval_T atv;
17099 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101
17102 /* truncate the name, so that we can use strcmp() */
17103 cc = name[len];
17104 name[len] = NUL;
17105
17106 /*
17107 * Check for "b:changedtick".
17108 */
17109 if (STRCMP(name, "b:changedtick") == 0)
17110 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017111 atv.v_type = VAR_NUMBER;
17112 atv.vval.v_number = curbuf->b_changedtick;
17113 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 }
17115
17116 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 * Check for user-defined variables.
17118 */
17119 else
17120 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017121 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 }
17125
Bram Moolenaare9a41262005-01-15 22:18:47 +000017126 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017128 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017129 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 ret = FAIL;
17131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017133 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134
17135 name[len] = cc;
17136
17137 return ret;
17138}
17139
17140/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017141 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17142 * Also handle function call with Funcref variable: func(expr)
17143 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17144 */
17145 static int
17146handle_subscript(arg, rettv, evaluate, verbose)
17147 char_u **arg;
17148 typval_T *rettv;
17149 int evaluate; /* do more than finding the end */
17150 int verbose; /* give error messages */
17151{
17152 int ret = OK;
17153 dict_T *selfdict = NULL;
17154 char_u *s;
17155 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017156 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017157
17158 while (ret == OK
17159 && (**arg == '['
17160 || (**arg == '.' && rettv->v_type == VAR_DICT)
17161 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17162 && !vim_iswhite(*(*arg - 1)))
17163 {
17164 if (**arg == '(')
17165 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017166 /* need to copy the funcref so that we can clear rettv */
17167 functv = *rettv;
17168 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017169
17170 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017171 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017172 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017173 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17174 &len, evaluate, selfdict);
17175
17176 /* Clear the funcref afterwards, so that deleting it while
17177 * evaluating the arguments is possible (see test55). */
17178 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017179
17180 /* Stop the expression evaluation when immediately aborting on
17181 * error, or when an interrupt occurred or an exception was thrown
17182 * but not caught. */
17183 if (aborting())
17184 {
17185 if (ret == OK)
17186 clear_tv(rettv);
17187 ret = FAIL;
17188 }
17189 dict_unref(selfdict);
17190 selfdict = NULL;
17191 }
17192 else /* **arg == '[' || **arg == '.' */
17193 {
17194 dict_unref(selfdict);
17195 if (rettv->v_type == VAR_DICT)
17196 {
17197 selfdict = rettv->vval.v_dict;
17198 if (selfdict != NULL)
17199 ++selfdict->dv_refcount;
17200 }
17201 else
17202 selfdict = NULL;
17203 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17204 {
17205 clear_tv(rettv);
17206 ret = FAIL;
17207 }
17208 }
17209 }
17210 dict_unref(selfdict);
17211 return ret;
17212}
17213
17214/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017215 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17216 * value).
17217 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017218 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017219alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017220{
Bram Moolenaar33570922005-01-25 22:26:29 +000017221 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017222}
17223
17224/*
17225 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 * The string "s" must have been allocated, it is consumed.
17227 * Return NULL for out of memory, the variable otherwise.
17228 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017229 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017230alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 char_u *s;
17232{
Bram Moolenaar33570922005-01-25 22:26:29 +000017233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017235 rettv = alloc_tv();
17236 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238 rettv->v_type = VAR_STRING;
17239 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 }
17241 else
17242 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017243 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244}
17245
17246/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017247 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017249 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017250free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017251 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252{
17253 if (varp != NULL)
17254 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017255 switch (varp->v_type)
17256 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017257 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017258 func_unref(varp->vval.v_string);
17259 /*FALLTHROUGH*/
17260 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017261 vim_free(varp->vval.v_string);
17262 break;
17263 case VAR_LIST:
17264 list_unref(varp->vval.v_list);
17265 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017266 case VAR_DICT:
17267 dict_unref(varp->vval.v_dict);
17268 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017269 case VAR_NUMBER:
17270 case VAR_UNKNOWN:
17271 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017272 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017273 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017274 break;
17275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 vim_free(varp);
17277 }
17278}
17279
17280/*
17281 * Free the memory for a variable value and set the value to NULL or 0.
17282 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017283 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017284clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017285 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286{
17287 if (varp != NULL)
17288 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017289 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017291 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017292 func_unref(varp->vval.v_string);
17293 /*FALLTHROUGH*/
17294 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017295 vim_free(varp->vval.v_string);
17296 varp->vval.v_string = NULL;
17297 break;
17298 case VAR_LIST:
17299 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017300 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017301 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017302 case VAR_DICT:
17303 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017304 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017305 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017306 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017307 varp->vval.v_number = 0;
17308 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017309 case VAR_UNKNOWN:
17310 break;
17311 default:
17312 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017314 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 }
17316}
17317
17318/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319 * Set the value of a variable to NULL without freeing items.
17320 */
17321 static void
17322init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017323 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017324{
17325 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017326 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017327}
17328
17329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 * Get the number value of a variable.
17331 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 * For incompatible types, return 0.
17333 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17334 * caller of incompatible types: it sets *denote to TRUE if "denote"
17335 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 */
17337 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017339 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017341 int error = FALSE;
17342
17343 return get_tv_number_chk(varp, &error); /* return 0L on error */
17344}
17345
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017346 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017347get_tv_number_chk(varp, denote)
17348 typval_T *varp;
17349 int *denote;
17350{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017351 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017353 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017355 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017356 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017357 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017358 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017359 break;
17360 case VAR_STRING:
17361 if (varp->vval.v_string != NULL)
17362 vim_str2nr(varp->vval.v_string, NULL, NULL,
17363 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017364 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017365 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017366 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017367 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017368 case VAR_DICT:
17369 EMSG(_("E728: Using a Dictionary as a number"));
17370 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017371 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017372 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017373 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017375 if (denote == NULL) /* useful for values that must be unsigned */
17376 n = -1;
17377 else
17378 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017379 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380}
17381
17382/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017383 * Get the lnum from the first argument.
17384 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017385 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 */
17387 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017389 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390{
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 linenr_T lnum;
17393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017394 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 if (lnum == 0) /* no valid number, try using line() */
17396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017397 rettv.v_type = VAR_NUMBER;
17398 f_line(argvars, &rettv);
17399 lnum = rettv.vval.v_number;
17400 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 }
17402 return lnum;
17403}
17404
17405/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017406 * Get the lnum from the first argument.
17407 * Also accepts "$", then "buf" is used.
17408 * Returns 0 on error.
17409 */
17410 static linenr_T
17411get_tv_lnum_buf(argvars, buf)
17412 typval_T *argvars;
17413 buf_T *buf;
17414{
17415 if (argvars[0].v_type == VAR_STRING
17416 && argvars[0].vval.v_string != NULL
17417 && argvars[0].vval.v_string[0] == '$'
17418 && buf != NULL)
17419 return buf->b_ml.ml_line_count;
17420 return get_tv_number_chk(&argvars[0], NULL);
17421}
17422
17423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 * Get the string value of a variable.
17425 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017426 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17427 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 * If the String variable has never been set, return an empty string.
17429 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017430 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17431 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 */
17433 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017434get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436{
17437 static char_u mybuf[NUMBUFLEN];
17438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017439 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440}
17441
17442 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017443get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017444 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017445 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017447 char_u *res = get_tv_string_buf_chk(varp, buf);
17448
17449 return res != NULL ? res : (char_u *)"";
17450}
17451
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017452 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017453get_tv_string_chk(varp)
17454 typval_T *varp;
17455{
17456 static char_u mybuf[NUMBUFLEN];
17457
17458 return get_tv_string_buf_chk(varp, mybuf);
17459}
17460
17461 static char_u *
17462get_tv_string_buf_chk(varp, buf)
17463 typval_T *varp;
17464 char_u *buf;
17465{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017466 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017468 case VAR_NUMBER:
17469 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17470 return buf;
17471 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017472 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017473 break;
17474 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017475 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017476 break;
17477 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017478 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017479 break;
17480 case VAR_STRING:
17481 if (varp->vval.v_string != NULL)
17482 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017483 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017484 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017485 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017488 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489}
17490
17491/*
17492 * Find variable "name" in the list of variables.
17493 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017494 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017495 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017496 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017499find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017504 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505
Bram Moolenaara7043832005-01-21 11:56:39 +000017506 ht = find_var_ht(name, &varname);
17507 if (htp != NULL)
17508 *htp = ht;
17509 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017511 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512}
17513
17514/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017516 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017518 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017519find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017520 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017521 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017522 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017523{
Bram Moolenaar33570922005-01-25 22:26:29 +000017524 hashitem_T *hi;
17525
17526 if (*varname == NUL)
17527 {
17528 /* Must be something like "s:", otherwise "ht" would be NULL. */
17529 switch (varname[-2])
17530 {
17531 case 's': return &SCRIPT_SV(current_SID).sv_var;
17532 case 'g': return &globvars_var;
17533 case 'v': return &vimvars_var;
17534 case 'b': return &curbuf->b_bufvar;
17535 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017536#ifdef FEAT_WINDOWS
17537 case 't': return &curtab->tp_winvar;
17538#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017539 case 'l': return current_funccal == NULL
17540 ? NULL : &current_funccal->l_vars_var;
17541 case 'a': return current_funccal == NULL
17542 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 }
17544 return NULL;
17545 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017546
17547 hi = hash_find(ht, varname);
17548 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017549 {
17550 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017551 * worked find the variable again. Don't auto-load a script if it was
17552 * loaded already, otherwise it would be loaded every time when
17553 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017554 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017555 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017556 hi = hash_find(ht, varname);
17557 if (HASHITEM_EMPTY(hi))
17558 return NULL;
17559 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017561}
17562
17563/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017565 * Set "varname" to the start of name without ':'.
17566 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017567 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017568find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 char_u *name;
17570 char_u **varname;
17571{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017572 hashitem_T *hi;
17573
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 if (name[1] != ':')
17575 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017576 /* The name must not start with a colon or #. */
17577 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 return NULL;
17579 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017580
17581 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017582 hi = hash_find(&compat_hashtab, name);
17583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017584 return &compat_hashtab;
17585
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017587 return &globvarht; /* global variable */
17588 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 }
17590 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017591 if (*name == 'g') /* global variable */
17592 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017593 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17594 */
17595 if (vim_strchr(name + 2, ':') != NULL
17596 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017597 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017601 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017602#ifdef FEAT_WINDOWS
17603 if (*name == 't') /* tab page variable */
17604 return &curtab->tp_vars.dv_hashtab;
17605#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017606 if (*name == 'v') /* v: variable */
17607 return &vimvarht;
17608 if (*name == 'a' && current_funccal != NULL) /* function argument */
17609 return &current_funccal->l_avars.dv_hashtab;
17610 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17611 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 if (*name == 's' /* script variable */
17613 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17614 return &SCRIPT_VARS(current_SID);
17615 return NULL;
17616}
17617
17618/*
17619 * Get the string value of a (global/local) variable.
17620 * Returns NULL when it doesn't exist.
17621 */
17622 char_u *
17623get_var_value(name)
17624 char_u *name;
17625{
Bram Moolenaar33570922005-01-25 22:26:29 +000017626 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627
Bram Moolenaara7043832005-01-21 11:56:39 +000017628 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 if (v == NULL)
17630 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017631 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632}
17633
17634/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017635 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 * sourcing this script and when executing functions defined in the script.
17637 */
17638 void
17639new_script_vars(id)
17640 scid_T id;
17641{
Bram Moolenaara7043832005-01-21 11:56:39 +000017642 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017643 hashtab_T *ht;
17644 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017645
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17647 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017648 /* Re-allocating ga_data means that an ht_array pointing to
17649 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017650 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017651 for (i = 1; i <= ga_scripts.ga_len; ++i)
17652 {
17653 ht = &SCRIPT_VARS(i);
17654 if (ht->ht_mask == HT_INIT_SIZE - 1)
17655 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017656 sv = &SCRIPT_SV(i);
17657 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017658 }
17659
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 while (ga_scripts.ga_len < id)
17661 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017662 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17663 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 }
17666 }
17667}
17668
17669/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017670 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17671 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 */
17673 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017674init_var_dict(dict, dict_var)
17675 dict_T *dict;
17676 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677{
Bram Moolenaar33570922005-01-25 22:26:29 +000017678 hash_init(&dict->dv_hashtab);
17679 dict->dv_refcount = 99999;
17680 dict_var->di_tv.vval.v_dict = dict;
17681 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017682 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17684 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685}
17686
17687/*
17688 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017689 * Frees all allocated variables and the value they contain.
17690 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 */
17692 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017693vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017694 hashtab_T *ht;
17695{
17696 vars_clear_ext(ht, TRUE);
17697}
17698
17699/*
17700 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17701 */
17702 static void
17703vars_clear_ext(ht, free_val)
17704 hashtab_T *ht;
17705 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706{
Bram Moolenaara7043832005-01-21 11:56:39 +000017707 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017708 hashitem_T *hi;
17709 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017712 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017713 for (hi = ht->ht_array; todo > 0; ++hi)
17714 {
17715 if (!HASHITEM_EMPTY(hi))
17716 {
17717 --todo;
17718
Bram Moolenaar33570922005-01-25 22:26:29 +000017719 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017720 * ht_array might change then. hash_clear() takes care of it
17721 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017722 v = HI2DI(hi);
17723 if (free_val)
17724 clear_tv(&v->di_tv);
17725 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17726 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017727 }
17728 }
17729 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017730 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731}
17732
Bram Moolenaara7043832005-01-21 11:56:39 +000017733/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017734 * Delete a variable from hashtab "ht" at item "hi".
17735 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017738delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 hashtab_T *ht;
17740 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741{
Bram Moolenaar33570922005-01-25 22:26:29 +000017742 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017743
17744 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017745 clear_tv(&di->di_tv);
17746 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747}
17748
17749/*
17750 * List the value of one internal variable.
17751 */
17752 static void
17753list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 char_u *prefix;
17756{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017757 char_u *tofree;
17758 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017759 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017760
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017761 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017762 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017763 s == NULL ? (char_u *)"" : s);
17764 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765}
17766
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 static void
17768list_one_var_a(prefix, name, type, string)
17769 char_u *prefix;
17770 char_u *name;
17771 int type;
17772 char_u *string;
17773{
17774 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17775 if (name != NULL) /* "a:" vars don't have a name stored */
17776 msg_puts(name);
17777 msg_putchar(' ');
17778 msg_advance(22);
17779 if (type == VAR_NUMBER)
17780 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017781 else if (type == VAR_FUNC)
17782 msg_putchar('*');
17783 else if (type == VAR_LIST)
17784 {
17785 msg_putchar('[');
17786 if (*string == '[')
17787 ++string;
17788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017789 else if (type == VAR_DICT)
17790 {
17791 msg_putchar('{');
17792 if (*string == '{')
17793 ++string;
17794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 else
17796 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017797
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017799
17800 if (type == VAR_FUNC)
17801 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802}
17803
17804/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017805 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 * If the variable already exists, the value is updated.
17807 * Otherwise the variable is created.
17808 */
17809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017810set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017812 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017813 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814{
Bram Moolenaar33570922005-01-25 22:26:29 +000017815 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017817 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017818 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017820 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017821 {
17822 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17823 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17824 ? name[2] : name[0]))
17825 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017826 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017827 return;
17828 }
17829 if (function_exists(name))
17830 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017831 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017832 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017833 return;
17834 }
17835 }
17836
Bram Moolenaara7043832005-01-21 11:56:39 +000017837 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017838 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017839 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017840 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017841 return;
17842 }
17843
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017844 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017845 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017848 if (var_check_ro(v->di_flags, name)
17849 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017850 return;
17851 if (v->di_tv.v_type != tv->v_type
17852 && !((v->di_tv.v_type == VAR_STRING
17853 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017854 && (tv->v_type == VAR_STRING
17855 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017856 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017857 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017858 return;
17859 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017860
17861 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017862 * Handle setting internal v: variables separately: we don't change
17863 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017864 */
17865 if (ht == &vimvarht)
17866 {
17867 if (v->di_tv.v_type == VAR_STRING)
17868 {
17869 vim_free(v->di_tv.vval.v_string);
17870 if (copy || tv->v_type != VAR_STRING)
17871 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17872 else
17873 {
17874 /* Take over the string to avoid an extra alloc/free. */
17875 v->di_tv.vval.v_string = tv->vval.v_string;
17876 tv->vval.v_string = NULL;
17877 }
17878 }
17879 else if (v->di_tv.v_type != VAR_NUMBER)
17880 EMSG2(_(e_intern2), "set_var()");
17881 else
17882 v->di_tv.vval.v_number = get_tv_number(tv);
17883 return;
17884 }
17885
17886 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887 }
17888 else /* add a new variable */
17889 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017890 /* Can't add "v:" variable. */
17891 if (ht == &vimvarht)
17892 {
17893 EMSG2(_(e_illvar), name);
17894 return;
17895 }
17896
Bram Moolenaar92124a32005-06-17 22:03:40 +000017897 /* Make sure the variable name is valid. */
17898 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017899 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17900 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017901 {
17902 EMSG2(_(e_illvar), varname);
17903 return;
17904 }
17905
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017906 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17907 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017908 if (v == NULL)
17909 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017910 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017913 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 return;
17915 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017916 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017919 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017920 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017921 else
17922 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017923 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017924 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017925 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927}
17928
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017929/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017930 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017931 * Also give an error message.
17932 */
17933 static int
17934var_check_ro(flags, name)
17935 int flags;
17936 char_u *name;
17937{
17938 if (flags & DI_FLAGS_RO)
17939 {
17940 EMSG2(_(e_readonlyvar), name);
17941 return TRUE;
17942 }
17943 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17944 {
17945 EMSG2(_(e_readonlysbx), name);
17946 return TRUE;
17947 }
17948 return FALSE;
17949}
17950
17951/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017952 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17953 * Also give an error message.
17954 */
17955 static int
17956var_check_fixed(flags, name)
17957 int flags;
17958 char_u *name;
17959{
17960 if (flags & DI_FLAGS_FIX)
17961 {
17962 EMSG2(_("E795: Cannot delete variable %s"), name);
17963 return TRUE;
17964 }
17965 return FALSE;
17966}
17967
17968/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017969 * Return TRUE if typeval "tv" is set to be locked (immutable).
17970 * Also give an error message, using "name".
17971 */
17972 static int
17973tv_check_lock(lock, name)
17974 int lock;
17975 char_u *name;
17976{
17977 if (lock & VAR_LOCKED)
17978 {
17979 EMSG2(_("E741: Value is locked: %s"),
17980 name == NULL ? (char_u *)_("Unknown") : name);
17981 return TRUE;
17982 }
17983 if (lock & VAR_FIXED)
17984 {
17985 EMSG2(_("E742: Cannot change value of %s"),
17986 name == NULL ? (char_u *)_("Unknown") : name);
17987 return TRUE;
17988 }
17989 return FALSE;
17990}
17991
17992/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017993 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017994 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017995 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017998copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017999 typval_T *from;
18000 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018002 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018003 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018004 switch (from->v_type)
18005 {
18006 case VAR_NUMBER:
18007 to->vval.v_number = from->vval.v_number;
18008 break;
18009 case VAR_STRING:
18010 case VAR_FUNC:
18011 if (from->vval.v_string == NULL)
18012 to->vval.v_string = NULL;
18013 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018014 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018015 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018016 if (from->v_type == VAR_FUNC)
18017 func_ref(to->vval.v_string);
18018 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018019 break;
18020 case VAR_LIST:
18021 if (from->vval.v_list == NULL)
18022 to->vval.v_list = NULL;
18023 else
18024 {
18025 to->vval.v_list = from->vval.v_list;
18026 ++to->vval.v_list->lv_refcount;
18027 }
18028 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018029 case VAR_DICT:
18030 if (from->vval.v_dict == NULL)
18031 to->vval.v_dict = NULL;
18032 else
18033 {
18034 to->vval.v_dict = from->vval.v_dict;
18035 ++to->vval.v_dict->dv_refcount;
18036 }
18037 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018038 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018040 break;
18041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042}
18043
18044/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018045 * Make a copy of an item.
18046 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018047 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18048 * reference to an already copied list/dict can be used.
18049 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018050 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018051 static int
18052item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018053 typval_T *from;
18054 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018055 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018056 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018057{
18058 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018059 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018060
Bram Moolenaar33570922005-01-25 22:26:29 +000018061 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018062 {
18063 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018064 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018065 }
18066 ++recurse;
18067
18068 switch (from->v_type)
18069 {
18070 case VAR_NUMBER:
18071 case VAR_STRING:
18072 case VAR_FUNC:
18073 copy_tv(from, to);
18074 break;
18075 case VAR_LIST:
18076 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018077 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018078 if (from->vval.v_list == NULL)
18079 to->vval.v_list = NULL;
18080 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18081 {
18082 /* use the copy made earlier */
18083 to->vval.v_list = from->vval.v_list->lv_copylist;
18084 ++to->vval.v_list->lv_refcount;
18085 }
18086 else
18087 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18088 if (to->vval.v_list == NULL)
18089 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018090 break;
18091 case VAR_DICT:
18092 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018093 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018094 if (from->vval.v_dict == NULL)
18095 to->vval.v_dict = NULL;
18096 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18097 {
18098 /* use the copy made earlier */
18099 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18100 ++to->vval.v_dict->dv_refcount;
18101 }
18102 else
18103 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18104 if (to->vval.v_dict == NULL)
18105 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018106 break;
18107 default:
18108 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018109 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018110 }
18111 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018112 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018113}
18114
18115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 * ":echo expr1 ..." print each argument separated with a space, add a
18117 * newline at the end.
18118 * ":echon expr1 ..." print each argument plain.
18119 */
18120 void
18121ex_echo(eap)
18122 exarg_T *eap;
18123{
18124 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018125 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018126 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 char_u *p;
18128 int needclr = TRUE;
18129 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018130 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131
18132 if (eap->skip)
18133 ++emsg_skip;
18134 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18135 {
18136 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 {
18139 /*
18140 * Report the invalid expression unless the expression evaluation
18141 * has been cancelled due to an aborting error, an interrupt, or an
18142 * exception.
18143 */
18144 if (!aborting())
18145 EMSG2(_(e_invexpr2), p);
18146 break;
18147 }
18148 if (!eap->skip)
18149 {
18150 if (atstart)
18151 {
18152 atstart = FALSE;
18153 /* Call msg_start() after eval1(), evaluating the expression
18154 * may cause a message to appear. */
18155 if (eap->cmdidx == CMD_echo)
18156 msg_start();
18157 }
18158 else if (eap->cmdidx == CMD_echo)
18159 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018160 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018161 if (p != NULL)
18162 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018164 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018166 if (*p != TAB && needclr)
18167 {
18168 /* remove any text still there from the command */
18169 msg_clr_eos();
18170 needclr = FALSE;
18171 }
18172 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 }
18174 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018175 {
18176#ifdef FEAT_MBYTE
18177 if (has_mbyte)
18178 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018179 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018180
18181 (void)msg_outtrans_len_attr(p, i, echo_attr);
18182 p += i - 1;
18183 }
18184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018186 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018189 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018191 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 arg = skipwhite(arg);
18193 }
18194 eap->nextcmd = check_nextcmd(arg);
18195
18196 if (eap->skip)
18197 --emsg_skip;
18198 else
18199 {
18200 /* remove text that may still be there from the command */
18201 if (needclr)
18202 msg_clr_eos();
18203 if (eap->cmdidx == CMD_echo)
18204 msg_end();
18205 }
18206}
18207
18208/*
18209 * ":echohl {name}".
18210 */
18211 void
18212ex_echohl(eap)
18213 exarg_T *eap;
18214{
18215 int id;
18216
18217 id = syn_name2id(eap->arg);
18218 if (id == 0)
18219 echo_attr = 0;
18220 else
18221 echo_attr = syn_id2attr(id);
18222}
18223
18224/*
18225 * ":execute expr1 ..." execute the result of an expression.
18226 * ":echomsg expr1 ..." Print a message
18227 * ":echoerr expr1 ..." Print an error
18228 * Each gets spaces around each argument and a newline at the end for
18229 * echo commands
18230 */
18231 void
18232ex_execute(eap)
18233 exarg_T *eap;
18234{
18235 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018236 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 int ret = OK;
18238 char_u *p;
18239 garray_T ga;
18240 int len;
18241 int save_did_emsg;
18242
18243 ga_init2(&ga, 1, 80);
18244
18245 if (eap->skip)
18246 ++emsg_skip;
18247 while (*arg != NUL && *arg != '|' && *arg != '\n')
18248 {
18249 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018250 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 {
18252 /*
18253 * Report the invalid expression unless the expression evaluation
18254 * has been cancelled due to an aborting error, an interrupt, or an
18255 * exception.
18256 */
18257 if (!aborting())
18258 EMSG2(_(e_invexpr2), p);
18259 ret = FAIL;
18260 break;
18261 }
18262
18263 if (!eap->skip)
18264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018265 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 len = (int)STRLEN(p);
18267 if (ga_grow(&ga, len + 2) == FAIL)
18268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018269 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 ret = FAIL;
18271 break;
18272 }
18273 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 ga.ga_len += len;
18277 }
18278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018279 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 arg = skipwhite(arg);
18281 }
18282
18283 if (ret != FAIL && ga.ga_data != NULL)
18284 {
18285 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018288 out_flush();
18289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 else if (eap->cmdidx == CMD_echoerr)
18291 {
18292 /* We don't want to abort following commands, restore did_emsg. */
18293 save_did_emsg = did_emsg;
18294 EMSG((char_u *)ga.ga_data);
18295 if (!force_abort)
18296 did_emsg = save_did_emsg;
18297 }
18298 else if (eap->cmdidx == CMD_execute)
18299 do_cmdline((char_u *)ga.ga_data,
18300 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18301 }
18302
18303 ga_clear(&ga);
18304
18305 if (eap->skip)
18306 --emsg_skip;
18307
18308 eap->nextcmd = check_nextcmd(arg);
18309}
18310
18311/*
18312 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18313 * "arg" points to the "&" or '+' when called, to "option" when returning.
18314 * Returns NULL when no option name found. Otherwise pointer to the char
18315 * after the option name.
18316 */
18317 static char_u *
18318find_option_end(arg, opt_flags)
18319 char_u **arg;
18320 int *opt_flags;
18321{
18322 char_u *p = *arg;
18323
18324 ++p;
18325 if (*p == 'g' && p[1] == ':')
18326 {
18327 *opt_flags = OPT_GLOBAL;
18328 p += 2;
18329 }
18330 else if (*p == 'l' && p[1] == ':')
18331 {
18332 *opt_flags = OPT_LOCAL;
18333 p += 2;
18334 }
18335 else
18336 *opt_flags = 0;
18337
18338 if (!ASCII_ISALPHA(*p))
18339 return NULL;
18340 *arg = p;
18341
18342 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18343 p += 4; /* termcap option */
18344 else
18345 while (ASCII_ISALPHA(*p))
18346 ++p;
18347 return p;
18348}
18349
18350/*
18351 * ":function"
18352 */
18353 void
18354ex_function(eap)
18355 exarg_T *eap;
18356{
18357 char_u *theline;
18358 int j;
18359 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 char_u *name = NULL;
18362 char_u *p;
18363 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018364 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 garray_T newargs;
18366 garray_T newlines;
18367 int varargs = FALSE;
18368 int mustend = FALSE;
18369 int flags = 0;
18370 ufunc_T *fp;
18371 int indent;
18372 int nesting;
18373 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018374 dictitem_T *v;
18375 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018376 static int func_nr = 0; /* number for nameless function */
18377 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018378 hashtab_T *ht;
18379 int todo;
18380 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018381 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382
18383 /*
18384 * ":function" without argument: list functions.
18385 */
18386 if (ends_excmd(*eap->arg))
18387 {
18388 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018389 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018390 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018391 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018392 {
18393 if (!HASHITEM_EMPTY(hi))
18394 {
18395 --todo;
18396 fp = HI2UF(hi);
18397 if (!isdigit(*fp->uf_name))
18398 list_func_head(fp, FALSE);
18399 }
18400 }
18401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 eap->nextcmd = check_nextcmd(eap->arg);
18403 return;
18404 }
18405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018406 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018407 * ":function /pat": list functions matching pattern.
18408 */
18409 if (*eap->arg == '/')
18410 {
18411 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18412 if (!eap->skip)
18413 {
18414 regmatch_T regmatch;
18415
18416 c = *p;
18417 *p = NUL;
18418 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18419 *p = c;
18420 if (regmatch.regprog != NULL)
18421 {
18422 regmatch.rm_ic = p_ic;
18423
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018424 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018425 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18426 {
18427 if (!HASHITEM_EMPTY(hi))
18428 {
18429 --todo;
18430 fp = HI2UF(hi);
18431 if (!isdigit(*fp->uf_name)
18432 && vim_regexec(&regmatch, fp->uf_name, 0))
18433 list_func_head(fp, FALSE);
18434 }
18435 }
18436 }
18437 }
18438 if (*p == '/')
18439 ++p;
18440 eap->nextcmd = check_nextcmd(p);
18441 return;
18442 }
18443
18444 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018445 * Get the function name. There are these situations:
18446 * func normal function name
18447 * "name" == func, "fudi.fd_dict" == NULL
18448 * dict.func new dictionary entry
18449 * "name" == NULL, "fudi.fd_dict" set,
18450 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18451 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018452 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018453 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18454 * dict.func existing dict entry that's not a Funcref
18455 * "name" == NULL, "fudi.fd_dict" set,
18456 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018459 name = trans_function_name(&p, eap->skip, 0, &fudi);
18460 paren = (vim_strchr(p, '(') != NULL);
18461 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 {
18463 /*
18464 * Return on an invalid expression in braces, unless the expression
18465 * evaluation has been cancelled due to an aborting error, an
18466 * interrupt, or an exception.
18467 */
18468 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018469 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018470 if (!eap->skip && fudi.fd_newkey != NULL)
18471 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018472 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 else
18476 eap->skip = TRUE;
18477 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018478
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 /* An error in a function call during evaluation of an expression in magic
18480 * braces should not cause the function not to be defined. */
18481 saved_did_emsg = did_emsg;
18482 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483
18484 /*
18485 * ":function func" with only function name: list function.
18486 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018487 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 {
18489 if (!ends_excmd(*skipwhite(p)))
18490 {
18491 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018492 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 }
18494 eap->nextcmd = check_nextcmd(p);
18495 if (eap->nextcmd != NULL)
18496 *p = NUL;
18497 if (!eap->skip && !got_int)
18498 {
18499 fp = find_func(name);
18500 if (fp != NULL)
18501 {
18502 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018503 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018505 if (FUNCLINE(fp, j) == NULL)
18506 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 msg_putchar('\n');
18508 msg_outnum((long)(j + 1));
18509 if (j < 9)
18510 msg_putchar(' ');
18511 if (j < 99)
18512 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018513 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 out_flush(); /* show a line at a time */
18515 ui_breakcheck();
18516 }
18517 if (!got_int)
18518 {
18519 msg_putchar('\n');
18520 msg_puts((char_u *)" endfunction");
18521 }
18522 }
18523 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018524 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018526 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 }
18528
18529 /*
18530 * ":function name(arg1, arg2)" Define function.
18531 */
18532 p = skipwhite(p);
18533 if (*p != '(')
18534 {
18535 if (!eap->skip)
18536 {
18537 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018538 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 }
18540 /* attempt to continue by skipping some text */
18541 if (vim_strchr(p, '(') != NULL)
18542 p = vim_strchr(p, '(');
18543 }
18544 p = skipwhite(p + 1);
18545
18546 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18547 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18548
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018549 if (!eap->skip)
18550 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018551 /* Check the name of the function. Unless it's a dictionary function
18552 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018553 if (name != NULL)
18554 arg = name;
18555 else
18556 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018557 if (arg != NULL && (fudi.fd_di == NULL
18558 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018559 {
18560 if (*arg == K_SPECIAL)
18561 j = 3;
18562 else
18563 j = 0;
18564 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18565 : eval_isnamec(arg[j])))
18566 ++j;
18567 if (arg[j] != NUL)
18568 emsg_funcname(_(e_invarg2), arg);
18569 }
18570 }
18571
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 /*
18573 * Isolate the arguments: "arg1, arg2, ...)"
18574 */
18575 while (*p != ')')
18576 {
18577 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18578 {
18579 varargs = TRUE;
18580 p += 3;
18581 mustend = TRUE;
18582 }
18583 else
18584 {
18585 arg = p;
18586 while (ASCII_ISALNUM(*p) || *p == '_')
18587 ++p;
18588 if (arg == p || isdigit(*arg)
18589 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18590 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18591 {
18592 if (!eap->skip)
18593 EMSG2(_("E125: Illegal argument: %s"), arg);
18594 break;
18595 }
18596 if (ga_grow(&newargs, 1) == FAIL)
18597 goto erret;
18598 c = *p;
18599 *p = NUL;
18600 arg = vim_strsave(arg);
18601 if (arg == NULL)
18602 goto erret;
18603 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18604 *p = c;
18605 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 if (*p == ',')
18607 ++p;
18608 else
18609 mustend = TRUE;
18610 }
18611 p = skipwhite(p);
18612 if (mustend && *p != ')')
18613 {
18614 if (!eap->skip)
18615 EMSG2(_(e_invarg2), eap->arg);
18616 break;
18617 }
18618 }
18619 ++p; /* skip the ')' */
18620
Bram Moolenaare9a41262005-01-15 22:18:47 +000018621 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 for (;;)
18623 {
18624 p = skipwhite(p);
18625 if (STRNCMP(p, "range", 5) == 0)
18626 {
18627 flags |= FC_RANGE;
18628 p += 5;
18629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018630 else if (STRNCMP(p, "dict", 4) == 0)
18631 {
18632 flags |= FC_DICT;
18633 p += 4;
18634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 else if (STRNCMP(p, "abort", 5) == 0)
18636 {
18637 flags |= FC_ABORT;
18638 p += 5;
18639 }
18640 else
18641 break;
18642 }
18643
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018644 /* When there is a line break use what follows for the function body.
18645 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18646 if (*p == '\n')
18647 line_arg = p + 1;
18648 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 EMSG(_(e_trailing));
18650
18651 /*
18652 * Read the body of the function, until ":endfunction" is found.
18653 */
18654 if (KeyTyped)
18655 {
18656 /* Check if the function already exists, don't let the user type the
18657 * whole function before telling him it doesn't work! For a script we
18658 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018659 if (!eap->skip && !eap->forceit)
18660 {
18661 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18662 EMSG(_(e_funcdict));
18663 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018664 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018667 if (!eap->skip && did_emsg)
18668 goto erret;
18669
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 msg_putchar('\n'); /* don't overwrite the function name */
18671 cmdline_row = msg_row;
18672 }
18673
18674 indent = 2;
18675 nesting = 0;
18676 for (;;)
18677 {
18678 msg_scroll = TRUE;
18679 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018680 sourcing_lnum_off = sourcing_lnum;
18681
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018682 if (line_arg != NULL)
18683 {
18684 /* Use eap->arg, split up in parts by line breaks. */
18685 theline = line_arg;
18686 p = vim_strchr(theline, '\n');
18687 if (p == NULL)
18688 line_arg += STRLEN(line_arg);
18689 else
18690 {
18691 *p = NUL;
18692 line_arg = p + 1;
18693 }
18694 }
18695 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 theline = getcmdline(':', 0L, indent);
18697 else
18698 theline = eap->getline(':', eap->cookie, indent);
18699 if (KeyTyped)
18700 lines_left = Rows - 1;
18701 if (theline == NULL)
18702 {
18703 EMSG(_("E126: Missing :endfunction"));
18704 goto erret;
18705 }
18706
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018707 /* Detect line continuation: sourcing_lnum increased more than one. */
18708 if (sourcing_lnum > sourcing_lnum_off + 1)
18709 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18710 else
18711 sourcing_lnum_off = 0;
18712
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 if (skip_until != NULL)
18714 {
18715 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18716 * don't check for ":endfunc". */
18717 if (STRCMP(theline, skip_until) == 0)
18718 {
18719 vim_free(skip_until);
18720 skip_until = NULL;
18721 }
18722 }
18723 else
18724 {
18725 /* skip ':' and blanks*/
18726 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18727 ;
18728
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018729 /* Check for "endfunction". */
18730 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018732 if (line_arg == NULL)
18733 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 break;
18735 }
18736
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018737 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 * at "end". */
18739 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18740 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018741 else if (STRNCMP(p, "if", 2) == 0
18742 || STRNCMP(p, "wh", 2) == 0
18743 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 || STRNCMP(p, "try", 3) == 0)
18745 indent += 2;
18746
18747 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018748 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018750 if (*p == '!')
18751 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 p += eval_fname_script(p);
18753 if (ASCII_ISALPHA(*p))
18754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018755 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 if (*skipwhite(p) == '(')
18757 {
18758 ++nesting;
18759 indent += 2;
18760 }
18761 }
18762 }
18763
18764 /* Check for ":append" or ":insert". */
18765 p = skip_range(p, NULL);
18766 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18767 || (p[0] == 'i'
18768 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18769 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18770 skip_until = vim_strsave((char_u *)".");
18771
18772 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18773 arg = skipwhite(skiptowhite(p));
18774 if (arg[0] == '<' && arg[1] =='<'
18775 && ((p[0] == 'p' && p[1] == 'y'
18776 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18777 || (p[0] == 'p' && p[1] == 'e'
18778 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18779 || (p[0] == 't' && p[1] == 'c'
18780 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18781 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18782 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018783 || (p[0] == 'm' && p[1] == 'z'
18784 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 ))
18786 {
18787 /* ":python <<" continues until a dot, like ":append" */
18788 p = skipwhite(arg + 2);
18789 if (*p == NUL)
18790 skip_until = vim_strsave((char_u *)".");
18791 else
18792 skip_until = vim_strsave(p);
18793 }
18794 }
18795
18796 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018797 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018798 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018799 if (line_arg == NULL)
18800 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018802 }
18803
18804 /* Copy the line to newly allocated memory. get_one_sourceline()
18805 * allocates 250 bytes per line, this saves 80% on average. The cost
18806 * is an extra alloc/free. */
18807 p = vim_strsave(theline);
18808 if (p != NULL)
18809 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018810 if (line_arg == NULL)
18811 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018812 theline = p;
18813 }
18814
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018815 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18816
18817 /* Add NULL lines for continuation lines, so that the line count is
18818 * equal to the index in the growarray. */
18819 while (sourcing_lnum_off-- > 0)
18820 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018821
18822 /* Check for end of eap->arg. */
18823 if (line_arg != NULL && *line_arg == NUL)
18824 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 }
18826
18827 /* Don't define the function when skipping commands or when an error was
18828 * detected. */
18829 if (eap->skip || did_emsg)
18830 goto erret;
18831
18832 /*
18833 * If there are no errors, add the function
18834 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018835 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018836 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018837 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018839 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018840 emsg_funcname("E707: Function name conflicts with variable: %s",
18841 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018842 goto erret;
18843 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018844
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018845 fp = find_func(name);
18846 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018848 if (!eap->forceit)
18849 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018850 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018851 goto erret;
18852 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018853 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018854 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018855 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018856 name);
18857 goto erret;
18858 }
18859 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018860 ga_clear_strings(&(fp->uf_args));
18861 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018862 vim_free(name);
18863 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 }
18866 else
18867 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018868 char numbuf[20];
18869
18870 fp = NULL;
18871 if (fudi.fd_newkey == NULL && !eap->forceit)
18872 {
18873 EMSG(_(e_funcdict));
18874 goto erret;
18875 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018876 if (fudi.fd_di == NULL)
18877 {
18878 /* Can't add a function to a locked dictionary */
18879 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18880 goto erret;
18881 }
18882 /* Can't change an existing function if it is locked */
18883 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18884 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018885
18886 /* Give the function a sequential number. Can only be used with a
18887 * Funcref! */
18888 vim_free(name);
18889 sprintf(numbuf, "%d", ++func_nr);
18890 name = vim_strsave((char_u *)numbuf);
18891 if (name == NULL)
18892 goto erret;
18893 }
18894
18895 if (fp == NULL)
18896 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018897 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018898 {
18899 int slen, plen;
18900 char_u *scriptname;
18901
18902 /* Check that the autoload name matches the script name. */
18903 j = FAIL;
18904 if (sourcing_name != NULL)
18905 {
18906 scriptname = autoload_name(name);
18907 if (scriptname != NULL)
18908 {
18909 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018910 plen = (int)STRLEN(p);
18911 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018912 if (slen > plen && fnamecmp(p,
18913 sourcing_name + slen - plen) == 0)
18914 j = OK;
18915 vim_free(scriptname);
18916 }
18917 }
18918 if (j == FAIL)
18919 {
18920 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18921 goto erret;
18922 }
18923 }
18924
18925 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 if (fp == NULL)
18927 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018928
18929 if (fudi.fd_dict != NULL)
18930 {
18931 if (fudi.fd_di == NULL)
18932 {
18933 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018934 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018935 if (fudi.fd_di == NULL)
18936 {
18937 vim_free(fp);
18938 goto erret;
18939 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018940 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18941 {
18942 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018943 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018944 goto erret;
18945 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018946 }
18947 else
18948 /* overwrite existing dict entry */
18949 clear_tv(&fudi.fd_di->di_tv);
18950 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018951 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018952 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018953 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018954
18955 /* behave like "dict" was used */
18956 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018957 }
18958
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018960 STRCPY(fp->uf_name, name);
18961 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018963 fp->uf_args = newargs;
18964 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018965#ifdef FEAT_PROFILE
18966 fp->uf_tml_count = NULL;
18967 fp->uf_tml_total = NULL;
18968 fp->uf_tml_self = NULL;
18969 fp->uf_profiling = FALSE;
18970 if (prof_def_func())
18971 func_do_profile(fp);
18972#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018973 fp->uf_varargs = varargs;
18974 fp->uf_flags = flags;
18975 fp->uf_calls = 0;
18976 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018977 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978
18979erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 ga_clear_strings(&newargs);
18981 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018982ret_free:
18983 vim_free(skip_until);
18984 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987}
18988
18989/*
18990 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018991 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018993 * flags:
18994 * TFN_INT: internal function name OK
18995 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 * Advances "pp" to just after the function name (if no error).
18997 */
18998 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018999trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 char_u **pp;
19001 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019002 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019005 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 char_u *start;
19007 char_u *end;
19008 int lead;
19009 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019011 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019012
19013 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019014 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019016
19017 /* Check for hard coded <SNR>: already translated function ID (from a user
19018 * command). */
19019 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19020 && (*pp)[2] == (int)KE_SNR)
19021 {
19022 *pp += 3;
19023 len = get_id_len(pp) + 3;
19024 return vim_strnsave(start, len);
19025 }
19026
19027 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19028 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019030 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019032
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019033 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19034 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019035 if (end == start)
19036 {
19037 if (!skip)
19038 EMSG(_("E129: Function name required"));
19039 goto theend;
19040 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019041 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019042 {
19043 /*
19044 * Report an invalid expression in braces, unless the expression
19045 * evaluation has been cancelled due to an aborting error, an
19046 * interrupt, or an exception.
19047 */
19048 if (!aborting())
19049 {
19050 if (end != NULL)
19051 EMSG2(_(e_invarg2), start);
19052 }
19053 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019054 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019055 goto theend;
19056 }
19057
19058 if (lv.ll_tv != NULL)
19059 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019060 if (fdp != NULL)
19061 {
19062 fdp->fd_dict = lv.ll_dict;
19063 fdp->fd_newkey = lv.ll_newkey;
19064 lv.ll_newkey = NULL;
19065 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019066 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019067 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19068 {
19069 name = vim_strsave(lv.ll_tv->vval.v_string);
19070 *pp = end;
19071 }
19072 else
19073 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019074 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19075 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019076 EMSG(_(e_funcref));
19077 else
19078 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019079 name = NULL;
19080 }
19081 goto theend;
19082 }
19083
19084 if (lv.ll_name == NULL)
19085 {
19086 /* Error found, but continue after the function name. */
19087 *pp = end;
19088 goto theend;
19089 }
19090
19091 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019092 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019093 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019094 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19095 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19096 {
19097 /* When there was "s:" already or the name expanded to get a
19098 * leading "s:" then remove it. */
19099 lv.ll_name += 2;
19100 len -= 2;
19101 lead = 2;
19102 }
19103 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019104 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019105 {
19106 if (lead == 2) /* skip over "s:" */
19107 lv.ll_name += 2;
19108 len = (int)(end - lv.ll_name);
19109 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019110
19111 /*
19112 * Copy the function name to allocated memory.
19113 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19114 * Accept <SNR>123_name() outside a script.
19115 */
19116 if (skip)
19117 lead = 0; /* do nothing */
19118 else if (lead > 0)
19119 {
19120 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019121 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19122 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019123 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019124 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019125 if (current_SID <= 0)
19126 {
19127 EMSG(_(e_usingsid));
19128 goto theend;
19129 }
19130 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19131 lead += (int)STRLEN(sid_buf);
19132 }
19133 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019134 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019135 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019136 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019137 goto theend;
19138 }
19139 name = alloc((unsigned)(len + lead + 1));
19140 if (name != NULL)
19141 {
19142 if (lead > 0)
19143 {
19144 name[0] = K_SPECIAL;
19145 name[1] = KS_EXTRA;
19146 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019147 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019148 STRCPY(name + 3, sid_buf);
19149 }
19150 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19151 name[len + lead] = NUL;
19152 }
19153 *pp = end;
19154
19155theend:
19156 clear_lval(&lv);
19157 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158}
19159
19160/*
19161 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19162 * Return 2 if "p" starts with "s:".
19163 * Return 0 otherwise.
19164 */
19165 static int
19166eval_fname_script(p)
19167 char_u *p;
19168{
19169 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19170 || STRNICMP(p + 1, "SNR>", 4) == 0))
19171 return 5;
19172 if (p[0] == 's' && p[1] == ':')
19173 return 2;
19174 return 0;
19175}
19176
19177/*
19178 * Return TRUE if "p" starts with "<SID>" or "s:".
19179 * Only works if eval_fname_script() returned non-zero for "p"!
19180 */
19181 static int
19182eval_fname_sid(p)
19183 char_u *p;
19184{
19185 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19186}
19187
19188/*
19189 * List the head of the function: "name(arg1, arg2)".
19190 */
19191 static void
19192list_func_head(fp, indent)
19193 ufunc_T *fp;
19194 int indent;
19195{
19196 int j;
19197
19198 msg_start();
19199 if (indent)
19200 MSG_PUTS(" ");
19201 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019202 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 {
19204 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019205 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 }
19207 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019208 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019210 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211 {
19212 if (j)
19213 MSG_PUTS(", ");
19214 msg_puts(FUNCARG(fp, j));
19215 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019216 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 {
19218 if (j)
19219 MSG_PUTS(", ");
19220 MSG_PUTS("...");
19221 }
19222 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019223 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019224 if (p_verbose > 0)
19225 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226}
19227
19228/*
19229 * Find a function by name, return pointer to it in ufuncs.
19230 * Return NULL for unknown function.
19231 */
19232 static ufunc_T *
19233find_func(name)
19234 char_u *name;
19235{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019236 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019238 hi = hash_find(&func_hashtab, name);
19239 if (!HASHITEM_EMPTY(hi))
19240 return HI2UF(hi);
19241 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242}
19243
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019244#if defined(EXITFREE) || defined(PROTO)
19245 void
19246free_all_functions()
19247{
19248 hashitem_T *hi;
19249
19250 /* Need to start all over every time, because func_free() may change the
19251 * hash table. */
19252 while (func_hashtab.ht_used > 0)
19253 for (hi = func_hashtab.ht_array; ; ++hi)
19254 if (!HASHITEM_EMPTY(hi))
19255 {
19256 func_free(HI2UF(hi));
19257 break;
19258 }
19259}
19260#endif
19261
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019262/*
19263 * Return TRUE if a function "name" exists.
19264 */
19265 static int
19266function_exists(name)
19267 char_u *name;
19268{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019269 char_u *nm = name;
19270 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019271 int n = FALSE;
19272
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019273 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019274 nm = skipwhite(nm);
19275
19276 /* Only accept "funcname", "funcname ", "funcname (..." and
19277 * "funcname(...", not "funcname!...". */
19278 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019279 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019280 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019281 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019282 else
19283 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019284 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019285 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019286 return n;
19287}
19288
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019289/*
19290 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019291 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019292 */
19293 static int
19294builtin_function(name)
19295 char_u *name;
19296{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019297 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19298 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019299}
19300
Bram Moolenaar05159a02005-02-26 23:04:13 +000019301#if defined(FEAT_PROFILE) || defined(PROTO)
19302/*
19303 * Start profiling function "fp".
19304 */
19305 static void
19306func_do_profile(fp)
19307 ufunc_T *fp;
19308{
19309 fp->uf_tm_count = 0;
19310 profile_zero(&fp->uf_tm_self);
19311 profile_zero(&fp->uf_tm_total);
19312 if (fp->uf_tml_count == NULL)
19313 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19314 (sizeof(int) * fp->uf_lines.ga_len));
19315 if (fp->uf_tml_total == NULL)
19316 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19317 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19318 if (fp->uf_tml_self == NULL)
19319 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19320 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19321 fp->uf_tml_idx = -1;
19322 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19323 || fp->uf_tml_self == NULL)
19324 return; /* out of memory */
19325
19326 fp->uf_profiling = TRUE;
19327}
19328
19329/*
19330 * Dump the profiling results for all functions in file "fd".
19331 */
19332 void
19333func_dump_profile(fd)
19334 FILE *fd;
19335{
19336 hashitem_T *hi;
19337 int todo;
19338 ufunc_T *fp;
19339 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019340 ufunc_T **sorttab;
19341 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019342
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019343 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019344 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19345
Bram Moolenaar05159a02005-02-26 23:04:13 +000019346 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19347 {
19348 if (!HASHITEM_EMPTY(hi))
19349 {
19350 --todo;
19351 fp = HI2UF(hi);
19352 if (fp->uf_profiling)
19353 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019354 if (sorttab != NULL)
19355 sorttab[st_len++] = fp;
19356
Bram Moolenaar05159a02005-02-26 23:04:13 +000019357 if (fp->uf_name[0] == K_SPECIAL)
19358 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19359 else
19360 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19361 if (fp->uf_tm_count == 1)
19362 fprintf(fd, "Called 1 time\n");
19363 else
19364 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19365 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19366 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19367 fprintf(fd, "\n");
19368 fprintf(fd, "count total (s) self (s)\n");
19369
19370 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19371 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019372 if (FUNCLINE(fp, i) == NULL)
19373 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019374 prof_func_line(fd, fp->uf_tml_count[i],
19375 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019376 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19377 }
19378 fprintf(fd, "\n");
19379 }
19380 }
19381 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019382
19383 if (sorttab != NULL && st_len > 0)
19384 {
19385 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19386 prof_total_cmp);
19387 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19388 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19389 prof_self_cmp);
19390 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19391 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019392}
Bram Moolenaar73830342005-02-28 22:48:19 +000019393
19394 static void
19395prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19396 FILE *fd;
19397 ufunc_T **sorttab;
19398 int st_len;
19399 char *title;
19400 int prefer_self; /* when equal print only self time */
19401{
19402 int i;
19403 ufunc_T *fp;
19404
19405 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19406 fprintf(fd, "count total (s) self (s) function\n");
19407 for (i = 0; i < 20 && i < st_len; ++i)
19408 {
19409 fp = sorttab[i];
19410 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19411 prefer_self);
19412 if (fp->uf_name[0] == K_SPECIAL)
19413 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19414 else
19415 fprintf(fd, " %s()\n", fp->uf_name);
19416 }
19417 fprintf(fd, "\n");
19418}
19419
19420/*
19421 * Print the count and times for one function or function line.
19422 */
19423 static void
19424prof_func_line(fd, count, total, self, prefer_self)
19425 FILE *fd;
19426 int count;
19427 proftime_T *total;
19428 proftime_T *self;
19429 int prefer_self; /* when equal print only self time */
19430{
19431 if (count > 0)
19432 {
19433 fprintf(fd, "%5d ", count);
19434 if (prefer_self && profile_equal(total, self))
19435 fprintf(fd, " ");
19436 else
19437 fprintf(fd, "%s ", profile_msg(total));
19438 if (!prefer_self && profile_equal(total, self))
19439 fprintf(fd, " ");
19440 else
19441 fprintf(fd, "%s ", profile_msg(self));
19442 }
19443 else
19444 fprintf(fd, " ");
19445}
19446
19447/*
19448 * Compare function for total time sorting.
19449 */
19450 static int
19451#ifdef __BORLANDC__
19452_RTLENTRYF
19453#endif
19454prof_total_cmp(s1, s2)
19455 const void *s1;
19456 const void *s2;
19457{
19458 ufunc_T *p1, *p2;
19459
19460 p1 = *(ufunc_T **)s1;
19461 p2 = *(ufunc_T **)s2;
19462 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19463}
19464
19465/*
19466 * Compare function for self time sorting.
19467 */
19468 static int
19469#ifdef __BORLANDC__
19470_RTLENTRYF
19471#endif
19472prof_self_cmp(s1, s2)
19473 const void *s1;
19474 const void *s2;
19475{
19476 ufunc_T *p1, *p2;
19477
19478 p1 = *(ufunc_T **)s1;
19479 p2 = *(ufunc_T **)s2;
19480 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19481}
19482
Bram Moolenaar05159a02005-02-26 23:04:13 +000019483#endif
19484
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019485/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019486 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019487 * Return TRUE if a package was loaded.
19488 */
19489 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019490script_autoload(name, reload)
19491 char_u *name;
19492 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019493{
19494 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019495 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019496 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019497 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019498
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019499 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019500 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019501 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019502 return FALSE;
19503
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019504 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019505
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019506 /* Find the name in the list of previously loaded package names. Skip
19507 * "autoload/", it's always the same. */
19508 for (i = 0; i < ga_loaded.ga_len; ++i)
19509 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19510 break;
19511 if (!reload && i < ga_loaded.ga_len)
19512 ret = FALSE; /* was loaded already */
19513 else
19514 {
19515 /* Remember the name if it wasn't loaded already. */
19516 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19517 {
19518 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19519 tofree = NULL;
19520 }
19521
19522 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019523 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019524 ret = TRUE;
19525 }
19526
19527 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019528 return ret;
19529}
19530
19531/*
19532 * Return the autoload script name for a function or variable name.
19533 * Returns NULL when out of memory.
19534 */
19535 static char_u *
19536autoload_name(name)
19537 char_u *name;
19538{
19539 char_u *p;
19540 char_u *scriptname;
19541
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019542 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019543 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19544 if (scriptname == NULL)
19545 return FALSE;
19546 STRCPY(scriptname, "autoload/");
19547 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019548 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019549 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019550 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019551 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019552 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019553}
19554
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19556
19557/*
19558 * Function given to ExpandGeneric() to obtain the list of user defined
19559 * function names.
19560 */
19561 char_u *
19562get_user_func_name(xp, idx)
19563 expand_T *xp;
19564 int idx;
19565{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019566 static long_u done;
19567 static hashitem_T *hi;
19568 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569
19570 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019572 done = 0;
19573 hi = func_hashtab.ht_array;
19574 }
19575 if (done < func_hashtab.ht_used)
19576 {
19577 if (done++ > 0)
19578 ++hi;
19579 while (HASHITEM_EMPTY(hi))
19580 ++hi;
19581 fp = HI2UF(hi);
19582
19583 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19584 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585
19586 cat_func_name(IObuff, fp);
19587 if (xp->xp_context != EXPAND_USER_FUNC)
19588 {
19589 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019590 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591 STRCAT(IObuff, ")");
19592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 return IObuff;
19594 }
19595 return NULL;
19596}
19597
19598#endif /* FEAT_CMDL_COMPL */
19599
19600/*
19601 * Copy the function name of "fp" to buffer "buf".
19602 * "buf" must be able to hold the function name plus three bytes.
19603 * Takes care of script-local function names.
19604 */
19605 static void
19606cat_func_name(buf, fp)
19607 char_u *buf;
19608 ufunc_T *fp;
19609{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019610 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 {
19612 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019613 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 }
19615 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019616 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617}
19618
19619/*
19620 * ":delfunction {name}"
19621 */
19622 void
19623ex_delfunction(eap)
19624 exarg_T *eap;
19625{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019626 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 char_u *p;
19628 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019629 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630
19631 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019632 name = trans_function_name(&p, eap->skip, 0, &fudi);
19633 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019635 {
19636 if (fudi.fd_dict != NULL && !eap->skip)
19637 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 if (!ends_excmd(*skipwhite(p)))
19641 {
19642 vim_free(name);
19643 EMSG(_(e_trailing));
19644 return;
19645 }
19646 eap->nextcmd = check_nextcmd(p);
19647 if (eap->nextcmd != NULL)
19648 *p = NUL;
19649
19650 if (!eap->skip)
19651 fp = find_func(name);
19652 vim_free(name);
19653
19654 if (!eap->skip)
19655 {
19656 if (fp == NULL)
19657 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019658 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 return;
19660 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019661 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 {
19663 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19664 return;
19665 }
19666
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019667 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019669 /* Delete the dict item that refers to the function, it will
19670 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019671 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019673 else
19674 func_free(fp);
19675 }
19676}
19677
19678/*
19679 * Free a function and remove it from the list of functions.
19680 */
19681 static void
19682func_free(fp)
19683 ufunc_T *fp;
19684{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019685 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019686
19687 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019688 ga_clear_strings(&(fp->uf_args));
19689 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019690#ifdef FEAT_PROFILE
19691 vim_free(fp->uf_tml_count);
19692 vim_free(fp->uf_tml_total);
19693 vim_free(fp->uf_tml_self);
19694#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019695
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019696 /* remove the function from the function hashtable */
19697 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19698 if (HASHITEM_EMPTY(hi))
19699 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019700 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019701 hash_remove(&func_hashtab, hi);
19702
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019703 vim_free(fp);
19704}
19705
19706/*
19707 * Unreference a Function: decrement the reference count and free it when it
19708 * becomes zero. Only for numbered functions.
19709 */
19710 static void
19711func_unref(name)
19712 char_u *name;
19713{
19714 ufunc_T *fp;
19715
19716 if (name != NULL && isdigit(*name))
19717 {
19718 fp = find_func(name);
19719 if (fp == NULL)
19720 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019721 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019722 {
19723 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019724 * when "uf_calls" becomes zero. */
19725 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019726 func_free(fp);
19727 }
19728 }
19729}
19730
19731/*
19732 * Count a reference to a Function.
19733 */
19734 static void
19735func_ref(name)
19736 char_u *name;
19737{
19738 ufunc_T *fp;
19739
19740 if (name != NULL && isdigit(*name))
19741 {
19742 fp = find_func(name);
19743 if (fp == NULL)
19744 EMSG2(_(e_intern2), "func_ref()");
19745 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019746 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 }
19748}
19749
19750/*
19751 * Call a user function.
19752 */
19753 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019754call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 ufunc_T *fp; /* pointer to function */
19756 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 typval_T *argvars; /* arguments */
19758 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 linenr_T firstline; /* first line of range */
19760 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019761 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762{
Bram Moolenaar33570922005-01-25 22:26:29 +000019763 char_u *save_sourcing_name;
19764 linenr_T save_sourcing_lnum;
19765 scid_T save_current_SID;
19766 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019767 int save_did_emsg;
19768 static int depth = 0;
19769 dictitem_T *v;
19770 int fixvar_idx = 0; /* index in fixvar[] */
19771 int i;
19772 int ai;
19773 char_u numbuf[NUMBUFLEN];
19774 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019775#ifdef FEAT_PROFILE
19776 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019777 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779
19780 /* If depth of calling is getting too high, don't execute the function */
19781 if (depth >= p_mfd)
19782 {
19783 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784 rettv->v_type = VAR_NUMBER;
19785 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 return;
19787 }
19788 ++depth;
19789
19790 line_breakcheck(); /* check for CTRL-C hit */
19791
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019792 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019793 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019795 fc.rettv = rettv;
19796 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 fc.linenr = 0;
19798 fc.returned = FALSE;
19799 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019801 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 fc.dbg_tick = debug_tick;
19803
Bram Moolenaar33570922005-01-25 22:26:29 +000019804 /*
19805 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19806 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19807 * each argument variable and saves a lot of time.
19808 */
19809 /*
19810 * Init l: variables.
19811 */
19812 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019813 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019814 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019815 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19816 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019817 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019818 name = v->di_key;
19819 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019820 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19821 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19822 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019823 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 v->di_tv.vval.v_dict = selfdict;
19825 ++selfdict->dv_refcount;
19826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019827
Bram Moolenaar33570922005-01-25 22:26:29 +000019828 /*
19829 * Init a: variables.
19830 * Set a:0 to "argcount".
19831 * Set a:000 to a list with room for the "..." arguments.
19832 */
19833 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19834 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019835 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019836 v = &fc.fixvar[fixvar_idx++].var;
19837 STRCPY(v->di_key, "000");
19838 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19839 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19840 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019841 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019842 v->di_tv.vval.v_list = &fc.l_varlist;
19843 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19844 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019845 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019846
19847 /*
19848 * Set a:firstline to "firstline" and a:lastline to "lastline".
19849 * Set a:name to named arguments.
19850 * Set a:N to the "..." arguments.
19851 */
19852 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19853 (varnumber_T)firstline);
19854 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19855 (varnumber_T)lastline);
19856 for (i = 0; i < argcount; ++i)
19857 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019858 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019859 if (ai < 0)
19860 /* named argument a:name */
19861 name = FUNCARG(fp, i);
19862 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019863 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 /* "..." argument a:1, a:2, etc. */
19865 sprintf((char *)numbuf, "%d", ai + 1);
19866 name = numbuf;
19867 }
19868 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19869 {
19870 v = &fc.fixvar[fixvar_idx++].var;
19871 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19872 }
19873 else
19874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019875 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19876 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 if (v == NULL)
19878 break;
19879 v->di_flags = DI_FLAGS_RO;
19880 }
19881 STRCPY(v->di_key, name);
19882 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19883
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019884 /* Note: the values are copied directly to avoid alloc/free.
19885 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019887 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019888
19889 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19890 {
19891 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19892 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019893 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019894 }
19895 }
19896
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 /* Don't redraw while executing the function. */
19898 ++RedrawingDisabled;
19899 save_sourcing_name = sourcing_name;
19900 save_sourcing_lnum = sourcing_lnum;
19901 sourcing_lnum = 1;
19902 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019903 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 if (sourcing_name != NULL)
19905 {
19906 if (save_sourcing_name != NULL
19907 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19908 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19909 else
19910 STRCPY(sourcing_name, "function ");
19911 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19912
19913 if (p_verbose >= 12)
19914 {
19915 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019916 verbose_enter_scroll();
19917
Bram Moolenaar555b2802005-05-19 21:08:39 +000019918 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 if (p_verbose >= 14)
19920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019922 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019923 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924
19925 msg_puts((char_u *)"(");
19926 for (i = 0; i < argcount; ++i)
19927 {
19928 if (i > 0)
19929 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019930 if (argvars[i].v_type == VAR_NUMBER)
19931 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 else
19933 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019934 trunc_string(tv2string(&argvars[i], &tofree,
19935 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019937 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 }
19939 }
19940 msg_puts((char_u *)")");
19941 }
19942 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019943
19944 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 --no_wait_return;
19946 }
19947 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019948#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019949 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019950 {
19951 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19952 func_do_profile(fp);
19953 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019954 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019955 {
19956 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019957 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019958 profile_zero(&fp->uf_tm_children);
19959 }
19960 script_prof_save(&wait_start);
19961 }
19962#endif
19963
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019965 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 save_did_emsg = did_emsg;
19967 did_emsg = FALSE;
19968
19969 /* call do_cmdline() to execute the lines */
19970 do_cmdline(NULL, get_func_line, (void *)&fc,
19971 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19972
19973 --RedrawingDisabled;
19974
19975 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019976 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978 clear_tv(rettv);
19979 rettv->v_type = VAR_NUMBER;
19980 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 }
19982
Bram Moolenaar05159a02005-02-26 23:04:13 +000019983#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019984 if (do_profiling == PROF_YES && (fp->uf_profiling
19985 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019986 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019987 profile_end(&call_start);
19988 profile_sub_wait(&wait_start, &call_start);
19989 profile_add(&fp->uf_tm_total, &call_start);
19990 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019991 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019992 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019993 profile_add(&fc.caller->func->uf_tm_children, &call_start);
19994 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019995 }
19996 }
19997#endif
19998
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 /* when being verbose, mention the return value */
20000 if (p_verbose >= 12)
20001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020003 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020006 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020007 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020008 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20009 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020010 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020012 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020013 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020014 char_u *tofree;
20015
Bram Moolenaar555b2802005-05-19 21:08:39 +000020016 /* The value may be very long. Skip the middle part, so that we
20017 * have some idea how it starts and ends. smsg() would always
20018 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000020019 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020020 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000020021 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020022 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 }
20024 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020025
20026 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 --no_wait_return;
20028 }
20029
20030 vim_free(sourcing_name);
20031 sourcing_name = save_sourcing_name;
20032 sourcing_lnum = save_sourcing_lnum;
20033 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020034#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020035 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020036 script_prof_restore(&wait_start);
20037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038
20039 if (p_verbose >= 12 && sourcing_name != NULL)
20040 {
20041 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020042 verbose_enter_scroll();
20043
Bram Moolenaar555b2802005-05-19 21:08:39 +000020044 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020046
20047 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 --no_wait_return;
20049 }
20050
20051 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020052 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 /* The a: variables typevals were not alloced, only free the allocated
20055 * variables. */
20056 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20057
20058 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 --depth;
20060}
20061
20062/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 * Add a number variable "name" to dict "dp" with value "nr".
20064 */
20065 static void
20066add_nr_var(dp, v, name, nr)
20067 dict_T *dp;
20068 dictitem_T *v;
20069 char *name;
20070 varnumber_T nr;
20071{
20072 STRCPY(v->di_key, name);
20073 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20074 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20075 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020076 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 v->di_tv.vval.v_number = nr;
20078}
20079
20080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 * ":return [expr]"
20082 */
20083 void
20084ex_return(eap)
20085 exarg_T *eap;
20086{
20087 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 int returning = FALSE;
20090
20091 if (current_funccal == NULL)
20092 {
20093 EMSG(_("E133: :return not inside a function"));
20094 return;
20095 }
20096
20097 if (eap->skip)
20098 ++emsg_skip;
20099
20100 eap->nextcmd = NULL;
20101 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020102 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 {
20104 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020105 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 }
20109 /* It's safer to return also on error. */
20110 else if (!eap->skip)
20111 {
20112 /*
20113 * Return unless the expression evaluation has been cancelled due to an
20114 * aborting error, an interrupt, or an exception.
20115 */
20116 if (!aborting())
20117 returning = do_return(eap, FALSE, TRUE, NULL);
20118 }
20119
20120 /* When skipping or the return gets pending, advance to the next command
20121 * in this line (!returning). Otherwise, ignore the rest of the line.
20122 * Following lines will be ignored by get_func_line(). */
20123 if (returning)
20124 eap->nextcmd = NULL;
20125 else if (eap->nextcmd == NULL) /* no argument */
20126 eap->nextcmd = check_nextcmd(arg);
20127
20128 if (eap->skip)
20129 --emsg_skip;
20130}
20131
20132/*
20133 * Return from a function. Possibly makes the return pending. Also called
20134 * for a pending return at the ":endtry" or after returning from an extra
20135 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020136 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020137 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 * FALSE when the return gets pending.
20139 */
20140 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 exarg_T *eap;
20143 int reanimate;
20144 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020145 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146{
20147 int idx;
20148 struct condstack *cstack = eap->cstack;
20149
20150 if (reanimate)
20151 /* Undo the return. */
20152 current_funccal->returned = FALSE;
20153
20154 /*
20155 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20156 * not in its finally clause (which then is to be executed next) is found.
20157 * In this case, make the ":return" pending for execution at the ":endtry".
20158 * Otherwise, return normally.
20159 */
20160 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20161 if (idx >= 0)
20162 {
20163 cstack->cs_pending[idx] = CSTP_RETURN;
20164
20165 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020166 /* A pending return again gets pending. "rettv" points to an
20167 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020169 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 else
20171 {
20172 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020173 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020177 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 {
20179 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020180 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020181 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 else
20183 EMSG(_(e_outofmem));
20184 }
20185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020186 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187
20188 if (reanimate)
20189 {
20190 /* The pending return value could be overwritten by a ":return"
20191 * without argument in a finally clause; reset the default
20192 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020193 current_funccal->rettv->v_type = VAR_NUMBER;
20194 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 }
20196 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 }
20199 else
20200 {
20201 current_funccal->returned = TRUE;
20202
20203 /* If the return is carried out now, store the return value. For
20204 * a return immediately after reanimation, the value is already
20205 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020206 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020208 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020211 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 }
20213 }
20214
20215 return idx < 0;
20216}
20217
20218/*
20219 * Free the variable with a pending return value.
20220 */
20221 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020222discard_pending_return(rettv)
20223 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224{
Bram Moolenaar33570922005-01-25 22:26:29 +000020225 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226}
20227
20228/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020229 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 * is an allocated string. Used by report_pending() for verbose messages.
20231 */
20232 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020233get_return_cmd(rettv)
20234 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020236 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020237 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020238 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020240 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020241 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020242 if (s == NULL)
20243 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020244
20245 STRCPY(IObuff, ":return ");
20246 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20247 if (STRLEN(s) + 8 >= IOSIZE)
20248 STRCPY(IObuff + IOSIZE - 4, "...");
20249 vim_free(tofree);
20250 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251}
20252
20253/*
20254 * Get next function line.
20255 * Called by do_cmdline() to get the next line.
20256 * Returns allocated string, or NULL for end of function.
20257 */
20258/* ARGSUSED */
20259 char_u *
20260get_func_line(c, cookie, indent)
20261 int c; /* not used */
20262 void *cookie;
20263 int indent; /* not used */
20264{
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020266 ufunc_T *fp = fcp->func;
20267 char_u *retval;
20268 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269
20270 /* If breakpoints have been added/deleted need to check for it. */
20271 if (fcp->dbg_tick != debug_tick)
20272 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020273 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 sourcing_lnum);
20275 fcp->dbg_tick = debug_tick;
20276 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020277#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020278 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020279 func_line_end(cookie);
20280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281
Bram Moolenaar05159a02005-02-26 23:04:13 +000020282 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020283 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20284 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 retval = NULL;
20286 else
20287 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020288 /* Skip NULL lines (continuation lines). */
20289 while (fcp->linenr < gap->ga_len
20290 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20291 ++fcp->linenr;
20292 if (fcp->linenr >= gap->ga_len)
20293 retval = NULL;
20294 else
20295 {
20296 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20297 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020298#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020299 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020300 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020301#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 }
20304
20305 /* Did we encounter a breakpoint? */
20306 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20307 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020308 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020310 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 sourcing_lnum);
20312 fcp->dbg_tick = debug_tick;
20313 }
20314
20315 return retval;
20316}
20317
Bram Moolenaar05159a02005-02-26 23:04:13 +000020318#if defined(FEAT_PROFILE) || defined(PROTO)
20319/*
20320 * Called when starting to read a function line.
20321 * "sourcing_lnum" must be correct!
20322 * When skipping lines it may not actually be executed, but we won't find out
20323 * until later and we need to store the time now.
20324 */
20325 void
20326func_line_start(cookie)
20327 void *cookie;
20328{
20329 funccall_T *fcp = (funccall_T *)cookie;
20330 ufunc_T *fp = fcp->func;
20331
20332 if (fp->uf_profiling && sourcing_lnum >= 1
20333 && sourcing_lnum <= fp->uf_lines.ga_len)
20334 {
20335 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020336 /* Skip continuation lines. */
20337 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20338 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020339 fp->uf_tml_execed = FALSE;
20340 profile_start(&fp->uf_tml_start);
20341 profile_zero(&fp->uf_tml_children);
20342 profile_get_wait(&fp->uf_tml_wait);
20343 }
20344}
20345
20346/*
20347 * Called when actually executing a function line.
20348 */
20349 void
20350func_line_exec(cookie)
20351 void *cookie;
20352{
20353 funccall_T *fcp = (funccall_T *)cookie;
20354 ufunc_T *fp = fcp->func;
20355
20356 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20357 fp->uf_tml_execed = TRUE;
20358}
20359
20360/*
20361 * Called when done with a function line.
20362 */
20363 void
20364func_line_end(cookie)
20365 void *cookie;
20366{
20367 funccall_T *fcp = (funccall_T *)cookie;
20368 ufunc_T *fp = fcp->func;
20369
20370 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20371 {
20372 if (fp->uf_tml_execed)
20373 {
20374 ++fp->uf_tml_count[fp->uf_tml_idx];
20375 profile_end(&fp->uf_tml_start);
20376 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020377 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020378 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20379 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020380 }
20381 fp->uf_tml_idx = -1;
20382 }
20383}
20384#endif
20385
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386/*
20387 * Return TRUE if the currently active function should be ended, because a
20388 * return was encountered or an error occured. Used inside a ":while".
20389 */
20390 int
20391func_has_ended(cookie)
20392 void *cookie;
20393{
Bram Moolenaar33570922005-01-25 22:26:29 +000020394 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395
20396 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20397 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020398 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 || fcp->returned);
20400}
20401
20402/*
20403 * return TRUE if cookie indicates a function which "abort"s on errors.
20404 */
20405 int
20406func_has_abort(cookie)
20407 void *cookie;
20408{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020409 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410}
20411
20412#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20413typedef enum
20414{
20415 VAR_FLAVOUR_DEFAULT,
20416 VAR_FLAVOUR_SESSION,
20417 VAR_FLAVOUR_VIMINFO
20418} var_flavour_T;
20419
20420static var_flavour_T var_flavour __ARGS((char_u *varname));
20421
20422 static var_flavour_T
20423var_flavour(varname)
20424 char_u *varname;
20425{
20426 char_u *p = varname;
20427
20428 if (ASCII_ISUPPER(*p))
20429 {
20430 while (*(++p))
20431 if (ASCII_ISLOWER(*p))
20432 return VAR_FLAVOUR_SESSION;
20433 return VAR_FLAVOUR_VIMINFO;
20434 }
20435 else
20436 return VAR_FLAVOUR_DEFAULT;
20437}
20438#endif
20439
20440#if defined(FEAT_VIMINFO) || defined(PROTO)
20441/*
20442 * Restore global vars that start with a capital from the viminfo file
20443 */
20444 int
20445read_viminfo_varlist(virp, writing)
20446 vir_T *virp;
20447 int writing;
20448{
20449 char_u *tab;
20450 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020451 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452
20453 if (!writing && (find_viminfo_parameter('!') != NULL))
20454 {
20455 tab = vim_strchr(virp->vir_line + 1, '\t');
20456 if (tab != NULL)
20457 {
20458 *tab++ = '\0'; /* isolate the variable name */
20459 if (*tab == 'S') /* string var */
20460 is_string = TRUE;
20461
20462 tab = vim_strchr(tab, '\t');
20463 if (tab != NULL)
20464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 if (is_string)
20466 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020467 tv.v_type = VAR_STRING;
20468 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 }
20471 else
20472 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020473 tv.v_type = VAR_NUMBER;
20474 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020476 set_var(virp->vir_line + 1, &tv, FALSE);
20477 if (is_string)
20478 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 }
20480 }
20481 }
20482
20483 return viminfo_readline(virp);
20484}
20485
20486/*
20487 * Write global vars that start with a capital to the viminfo file
20488 */
20489 void
20490write_viminfo_varlist(fp)
20491 FILE *fp;
20492{
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 hashitem_T *hi;
20494 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020495 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020496 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020497 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020498 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020499 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500
20501 if (find_viminfo_parameter('!') == NULL)
20502 return;
20503
20504 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020505
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020506 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020507 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020509 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020511 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020512 this_var = HI2DI(hi);
20513 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020514 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020515 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020516 {
20517 case VAR_STRING: s = "STR"; break;
20518 case VAR_NUMBER: s = "NUM"; break;
20519 default: continue;
20520 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020521 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020522 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020523 if (p != NULL)
20524 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020525 vim_free(tofree);
20526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 }
20528 }
20529}
20530#endif
20531
20532#if defined(FEAT_SESSION) || defined(PROTO)
20533 int
20534store_session_globals(fd)
20535 FILE *fd;
20536{
Bram Moolenaar33570922005-01-25 22:26:29 +000020537 hashitem_T *hi;
20538 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020539 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 char_u *p, *t;
20541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020542 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020545 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020547 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020548 this_var = HI2DI(hi);
20549 if ((this_var->di_tv.v_type == VAR_NUMBER
20550 || this_var->di_tv.v_type == VAR_STRING)
20551 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020552 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020553 /* Escape special characters with a backslash. Turn a LF and
20554 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020555 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020556 (char_u *)"\\\"\n\r");
20557 if (p == NULL) /* out of memory */
20558 break;
20559 for (t = p; *t != NUL; ++t)
20560 if (*t == '\n')
20561 *t = 'n';
20562 else if (*t == '\r')
20563 *t = 'r';
20564 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020565 this_var->di_key,
20566 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20567 : ' ',
20568 p,
20569 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20570 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020571 || put_eol(fd) == FAIL)
20572 {
20573 vim_free(p);
20574 return FAIL;
20575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 vim_free(p);
20577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 }
20579 }
20580 return OK;
20581}
20582#endif
20583
Bram Moolenaar661b1822005-07-28 22:36:45 +000020584/*
20585 * Display script name where an item was last set.
20586 * Should only be invoked when 'verbose' is non-zero.
20587 */
20588 void
20589last_set_msg(scriptID)
20590 scid_T scriptID;
20591{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020592 char_u *p;
20593
Bram Moolenaar661b1822005-07-28 22:36:45 +000020594 if (scriptID != 0)
20595 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020596 p = home_replace_save(NULL, get_scriptname(scriptID));
20597 if (p != NULL)
20598 {
20599 verbose_enter();
20600 MSG_PUTS(_("\n\tLast set from "));
20601 MSG_PUTS(p);
20602 vim_free(p);
20603 verbose_leave();
20604 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020605 }
20606}
20607
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608#endif /* FEAT_EVAL */
20609
20610#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20611
20612
20613#ifdef WIN3264
20614/*
20615 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20616 */
20617static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20618static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20619static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20620
20621/*
20622 * Get the short pathname of a file.
20623 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20624 */
20625 static int
20626get_short_pathname(fnamep, bufp, fnamelen)
20627 char_u **fnamep;
20628 char_u **bufp;
20629 int *fnamelen;
20630{
20631 int l,len;
20632 char_u *newbuf;
20633
20634 len = *fnamelen;
20635
20636 l = GetShortPathName(*fnamep, *fnamep, len);
20637 if (l > len - 1)
20638 {
20639 /* If that doesn't work (not enough space), then save the string
20640 * and try again with a new buffer big enough
20641 */
20642 newbuf = vim_strnsave(*fnamep, l);
20643 if (newbuf == NULL)
20644 return 0;
20645
20646 vim_free(*bufp);
20647 *fnamep = *bufp = newbuf;
20648
20649 l = GetShortPathName(*fnamep,*fnamep,l+1);
20650
20651 /* Really should always succeed, as the buffer is big enough */
20652 }
20653
20654 *fnamelen = l;
20655 return 1;
20656}
20657
20658/*
20659 * Create a short path name. Returns the length of the buffer it needs.
20660 * Doesn't copy over the end of the buffer passed in.
20661 */
20662 static int
20663shortpath_for_invalid_fname(fname, bufp, fnamelen)
20664 char_u **fname;
20665 char_u **bufp;
20666 int *fnamelen;
20667{
20668 char_u *s, *p, *pbuf2, *pbuf3;
20669 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020670 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671
20672 /* Make a copy */
20673 len2 = *fnamelen;
20674 pbuf2 = vim_strnsave(*fname, len2);
20675 pbuf3 = NULL;
20676
20677 s = pbuf2 + len2 - 1; /* Find the end */
20678 slen = 1;
20679 plen = len2;
20680
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020681 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 {
20683 --s;
20684 ++slen;
20685 --plen;
20686 }
20687
20688 do
20689 {
20690 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020691 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 {
20693 --s;
20694 ++slen;
20695 --plen;
20696 }
20697 if (s <= pbuf2)
20698 break;
20699
20700 /* Remeber the character that is about to be blatted */
20701 ch = *s;
20702 *s = 0; /* get_short_pathname requires a null-terminated string */
20703
20704 /* Try it in situ */
20705 p = pbuf2;
20706 if (!get_short_pathname(&p, &pbuf3, &plen))
20707 {
20708 vim_free(pbuf2);
20709 return -1;
20710 }
20711 *s = ch; /* Preserve the string */
20712 } while (plen == 0);
20713
20714 if (plen > 0)
20715 {
20716 /* Remeber the length of the new string. */
20717 *fnamelen = len = plen + slen;
20718 vim_free(*bufp);
20719 if (len > len2)
20720 {
20721 /* If there's not enough space in the currently allocated string,
20722 * then copy it to a buffer big enough.
20723 */
20724 *fname= *bufp = vim_strnsave(p, len);
20725 if (*fname == NULL)
20726 return -1;
20727 }
20728 else
20729 {
20730 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20731 *fname = *bufp = pbuf2;
20732 if (p != pbuf2)
20733 strncpy(*fname, p, plen);
20734 pbuf2 = NULL;
20735 }
20736 /* Concat the next bit */
20737 strncpy(*fname + plen, s, slen);
20738 (*fname)[len] = '\0';
20739 }
20740 vim_free(pbuf3);
20741 vim_free(pbuf2);
20742 return 0;
20743}
20744
20745/*
20746 * Get a pathname for a partial path.
20747 */
20748 static int
20749shortpath_for_partial(fnamep, bufp, fnamelen)
20750 char_u **fnamep;
20751 char_u **bufp;
20752 int *fnamelen;
20753{
20754 int sepcount, len, tflen;
20755 char_u *p;
20756 char_u *pbuf, *tfname;
20757 int hasTilde;
20758
20759 /* Count up the path seperators from the RHS.. so we know which part
20760 * of the path to return.
20761 */
20762 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020763 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 if (vim_ispathsep(*p))
20765 ++sepcount;
20766
20767 /* Need full path first (use expand_env() to remove a "~/") */
20768 hasTilde = (**fnamep == '~');
20769 if (hasTilde)
20770 pbuf = tfname = expand_env_save(*fnamep);
20771 else
20772 pbuf = tfname = FullName_save(*fnamep, FALSE);
20773
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020774 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775
20776 if (!get_short_pathname(&tfname, &pbuf, &len))
20777 return -1;
20778
20779 if (len == 0)
20780 {
20781 /* Don't have a valid filename, so shorten the rest of the
20782 * path if we can. This CAN give us invalid 8.3 filenames, but
20783 * there's not a lot of point in guessing what it might be.
20784 */
20785 len = tflen;
20786 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20787 return -1;
20788 }
20789
20790 /* Count the paths backward to find the beginning of the desired string. */
20791 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020792 {
20793#ifdef FEAT_MBYTE
20794 if (has_mbyte)
20795 p -= mb_head_off(tfname, p);
20796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 if (vim_ispathsep(*p))
20798 {
20799 if (sepcount == 0 || (hasTilde && sepcount == 1))
20800 break;
20801 else
20802 sepcount --;
20803 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 if (hasTilde)
20806 {
20807 --p;
20808 if (p >= tfname)
20809 *p = '~';
20810 else
20811 return -1;
20812 }
20813 else
20814 ++p;
20815
20816 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20817 vim_free(*bufp);
20818 *fnamelen = (int)STRLEN(p);
20819 *bufp = pbuf;
20820 *fnamep = p;
20821
20822 return 0;
20823}
20824#endif /* WIN3264 */
20825
20826/*
20827 * Adjust a filename, according to a string of modifiers.
20828 * *fnamep must be NUL terminated when called. When returning, the length is
20829 * determined by *fnamelen.
20830 * Returns valid flags.
20831 * When there is an error, *fnamep is set to NULL.
20832 */
20833 int
20834modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20835 char_u *src; /* string with modifiers */
20836 int *usedlen; /* characters after src that are used */
20837 char_u **fnamep; /* file name so far */
20838 char_u **bufp; /* buffer for allocated file name or NULL */
20839 int *fnamelen; /* length of fnamep */
20840{
20841 int valid = 0;
20842 char_u *tail;
20843 char_u *s, *p, *pbuf;
20844 char_u dirname[MAXPATHL];
20845 int c;
20846 int has_fullname = 0;
20847#ifdef WIN3264
20848 int has_shortname = 0;
20849#endif
20850
20851repeat:
20852 /* ":p" - full path/file_name */
20853 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20854 {
20855 has_fullname = 1;
20856
20857 valid |= VALID_PATH;
20858 *usedlen += 2;
20859
20860 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20861 if ((*fnamep)[0] == '~'
20862#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20863 && ((*fnamep)[1] == '/'
20864# ifdef BACKSLASH_IN_FILENAME
20865 || (*fnamep)[1] == '\\'
20866# endif
20867 || (*fnamep)[1] == NUL)
20868
20869#endif
20870 )
20871 {
20872 *fnamep = expand_env_save(*fnamep);
20873 vim_free(*bufp); /* free any allocated file name */
20874 *bufp = *fnamep;
20875 if (*fnamep == NULL)
20876 return -1;
20877 }
20878
20879 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020880 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 {
20882 if (vim_ispathsep(*p)
20883 && p[1] == '.'
20884 && (p[2] == NUL
20885 || vim_ispathsep(p[2])
20886 || (p[2] == '.'
20887 && (p[3] == NUL || vim_ispathsep(p[3])))))
20888 break;
20889 }
20890
20891 /* FullName_save() is slow, don't use it when not needed. */
20892 if (*p != NUL || !vim_isAbsName(*fnamep))
20893 {
20894 *fnamep = FullName_save(*fnamep, *p != NUL);
20895 vim_free(*bufp); /* free any allocated file name */
20896 *bufp = *fnamep;
20897 if (*fnamep == NULL)
20898 return -1;
20899 }
20900
20901 /* Append a path separator to a directory. */
20902 if (mch_isdir(*fnamep))
20903 {
20904 /* Make room for one or two extra characters. */
20905 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20906 vim_free(*bufp); /* free any allocated file name */
20907 *bufp = *fnamep;
20908 if (*fnamep == NULL)
20909 return -1;
20910 add_pathsep(*fnamep);
20911 }
20912 }
20913
20914 /* ":." - path relative to the current directory */
20915 /* ":~" - path relative to the home directory */
20916 /* ":8" - shortname path - postponed till after */
20917 while (src[*usedlen] == ':'
20918 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20919 {
20920 *usedlen += 2;
20921 if (c == '8')
20922 {
20923#ifdef WIN3264
20924 has_shortname = 1; /* Postpone this. */
20925#endif
20926 continue;
20927 }
20928 pbuf = NULL;
20929 /* Need full path first (use expand_env() to remove a "~/") */
20930 if (!has_fullname)
20931 {
20932 if (c == '.' && **fnamep == '~')
20933 p = pbuf = expand_env_save(*fnamep);
20934 else
20935 p = pbuf = FullName_save(*fnamep, FALSE);
20936 }
20937 else
20938 p = *fnamep;
20939
20940 has_fullname = 0;
20941
20942 if (p != NULL)
20943 {
20944 if (c == '.')
20945 {
20946 mch_dirname(dirname, MAXPATHL);
20947 s = shorten_fname(p, dirname);
20948 if (s != NULL)
20949 {
20950 *fnamep = s;
20951 if (pbuf != NULL)
20952 {
20953 vim_free(*bufp); /* free any allocated file name */
20954 *bufp = pbuf;
20955 pbuf = NULL;
20956 }
20957 }
20958 }
20959 else
20960 {
20961 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20962 /* Only replace it when it starts with '~' */
20963 if (*dirname == '~')
20964 {
20965 s = vim_strsave(dirname);
20966 if (s != NULL)
20967 {
20968 *fnamep = s;
20969 vim_free(*bufp);
20970 *bufp = s;
20971 }
20972 }
20973 }
20974 vim_free(pbuf);
20975 }
20976 }
20977
20978 tail = gettail(*fnamep);
20979 *fnamelen = (int)STRLEN(*fnamep);
20980
20981 /* ":h" - head, remove "/file_name", can be repeated */
20982 /* Don't remove the first "/" or "c:\" */
20983 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20984 {
20985 valid |= VALID_HEAD;
20986 *usedlen += 2;
20987 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020988 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 --tail;
20990 *fnamelen = (int)(tail - *fnamep);
20991#ifdef VMS
20992 if (*fnamelen > 0)
20993 *fnamelen += 1; /* the path separator is part of the path */
20994#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020995 while (tail > s && !after_pathsep(s, tail))
20996 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 }
20998
20999 /* ":8" - shortname */
21000 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21001 {
21002 *usedlen += 2;
21003#ifdef WIN3264
21004 has_shortname = 1;
21005#endif
21006 }
21007
21008#ifdef WIN3264
21009 /* Check shortname after we have done 'heads' and before we do 'tails'
21010 */
21011 if (has_shortname)
21012 {
21013 pbuf = NULL;
21014 /* Copy the string if it is shortened by :h */
21015 if (*fnamelen < (int)STRLEN(*fnamep))
21016 {
21017 p = vim_strnsave(*fnamep, *fnamelen);
21018 if (p == 0)
21019 return -1;
21020 vim_free(*bufp);
21021 *bufp = *fnamep = p;
21022 }
21023
21024 /* Split into two implementations - makes it easier. First is where
21025 * there isn't a full name already, second is where there is.
21026 */
21027 if (!has_fullname && !vim_isAbsName(*fnamep))
21028 {
21029 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21030 return -1;
21031 }
21032 else
21033 {
21034 int l;
21035
21036 /* Simple case, already have the full-name
21037 * Nearly always shorter, so try first time. */
21038 l = *fnamelen;
21039 if (!get_short_pathname(fnamep, bufp, &l))
21040 return -1;
21041
21042 if (l == 0)
21043 {
21044 /* Couldn't find the filename.. search the paths.
21045 */
21046 l = *fnamelen;
21047 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21048 return -1;
21049 }
21050 *fnamelen = l;
21051 }
21052 }
21053#endif /* WIN3264 */
21054
21055 /* ":t" - tail, just the basename */
21056 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21057 {
21058 *usedlen += 2;
21059 *fnamelen -= (int)(tail - *fnamep);
21060 *fnamep = tail;
21061 }
21062
21063 /* ":e" - extension, can be repeated */
21064 /* ":r" - root, without extension, can be repeated */
21065 while (src[*usedlen] == ':'
21066 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21067 {
21068 /* find a '.' in the tail:
21069 * - for second :e: before the current fname
21070 * - otherwise: The last '.'
21071 */
21072 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21073 s = *fnamep - 2;
21074 else
21075 s = *fnamep + *fnamelen - 1;
21076 for ( ; s > tail; --s)
21077 if (s[0] == '.')
21078 break;
21079 if (src[*usedlen + 1] == 'e') /* :e */
21080 {
21081 if (s > tail)
21082 {
21083 *fnamelen += (int)(*fnamep - (s + 1));
21084 *fnamep = s + 1;
21085#ifdef VMS
21086 /* cut version from the extension */
21087 s = *fnamep + *fnamelen - 1;
21088 for ( ; s > *fnamep; --s)
21089 if (s[0] == ';')
21090 break;
21091 if (s > *fnamep)
21092 *fnamelen = s - *fnamep;
21093#endif
21094 }
21095 else if (*fnamep <= tail)
21096 *fnamelen = 0;
21097 }
21098 else /* :r */
21099 {
21100 if (s > tail) /* remove one extension */
21101 *fnamelen = (int)(s - *fnamep);
21102 }
21103 *usedlen += 2;
21104 }
21105
21106 /* ":s?pat?foo?" - substitute */
21107 /* ":gs?pat?foo?" - global substitute */
21108 if (src[*usedlen] == ':'
21109 && (src[*usedlen + 1] == 's'
21110 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21111 {
21112 char_u *str;
21113 char_u *pat;
21114 char_u *sub;
21115 int sep;
21116 char_u *flags;
21117 int didit = FALSE;
21118
21119 flags = (char_u *)"";
21120 s = src + *usedlen + 2;
21121 if (src[*usedlen + 1] == 'g')
21122 {
21123 flags = (char_u *)"g";
21124 ++s;
21125 }
21126
21127 sep = *s++;
21128 if (sep)
21129 {
21130 /* find end of pattern */
21131 p = vim_strchr(s, sep);
21132 if (p != NULL)
21133 {
21134 pat = vim_strnsave(s, (int)(p - s));
21135 if (pat != NULL)
21136 {
21137 s = p + 1;
21138 /* find end of substitution */
21139 p = vim_strchr(s, sep);
21140 if (p != NULL)
21141 {
21142 sub = vim_strnsave(s, (int)(p - s));
21143 str = vim_strnsave(*fnamep, *fnamelen);
21144 if (sub != NULL && str != NULL)
21145 {
21146 *usedlen = (int)(p + 1 - src);
21147 s = do_string_sub(str, pat, sub, flags);
21148 if (s != NULL)
21149 {
21150 *fnamep = s;
21151 *fnamelen = (int)STRLEN(s);
21152 vim_free(*bufp);
21153 *bufp = s;
21154 didit = TRUE;
21155 }
21156 }
21157 vim_free(sub);
21158 vim_free(str);
21159 }
21160 vim_free(pat);
21161 }
21162 }
21163 /* after using ":s", repeat all the modifiers */
21164 if (didit)
21165 goto repeat;
21166 }
21167 }
21168
21169 return valid;
21170}
21171
21172/*
21173 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21174 * "flags" can be "g" to do a global substitute.
21175 * Returns an allocated string, NULL for error.
21176 */
21177 char_u *
21178do_string_sub(str, pat, sub, flags)
21179 char_u *str;
21180 char_u *pat;
21181 char_u *sub;
21182 char_u *flags;
21183{
21184 int sublen;
21185 regmatch_T regmatch;
21186 int i;
21187 int do_all;
21188 char_u *tail;
21189 garray_T ga;
21190 char_u *ret;
21191 char_u *save_cpo;
21192
21193 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21194 save_cpo = p_cpo;
21195 p_cpo = (char_u *)"";
21196
21197 ga_init2(&ga, 1, 200);
21198
21199 do_all = (flags[0] == 'g');
21200
21201 regmatch.rm_ic = p_ic;
21202 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21203 if (regmatch.regprog != NULL)
21204 {
21205 tail = str;
21206 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21207 {
21208 /*
21209 * Get some space for a temporary buffer to do the substitution
21210 * into. It will contain:
21211 * - The text up to where the match is.
21212 * - The substituted text.
21213 * - The text after the match.
21214 */
21215 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21216 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21217 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21218 {
21219 ga_clear(&ga);
21220 break;
21221 }
21222
21223 /* copy the text up to where the match is */
21224 i = (int)(regmatch.startp[0] - tail);
21225 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21226 /* add the substituted text */
21227 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21228 + ga.ga_len + i, TRUE, TRUE, FALSE);
21229 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 /* avoid getting stuck on a match with an empty string */
21231 if (tail == regmatch.endp[0])
21232 {
21233 if (*tail == NUL)
21234 break;
21235 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21236 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 }
21238 else
21239 {
21240 tail = regmatch.endp[0];
21241 if (*tail == NUL)
21242 break;
21243 }
21244 if (!do_all)
21245 break;
21246 }
21247
21248 if (ga.ga_data != NULL)
21249 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21250
21251 vim_free(regmatch.regprog);
21252 }
21253
21254 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21255 ga_clear(&ga);
21256 p_cpo = save_cpo;
21257
21258 return ret;
21259}
21260
21261#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */