blob: 30f16359921b7d6e36741b07b14daf6b1b4e2bf9 [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;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000901static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000902static char_u *redir_endp = NULL;
903static char_u *redir_varname = NULL;
904
905/*
906 * Start recording command output to a variable
907 * Returns OK if successfully completed the setup. FAIL otherwise.
908 */
909 int
910var_redir_start(name, append)
911 char_u *name;
912 int append; /* append to an existing variable */
913{
914 int save_emsg;
915 int err;
916 typval_T tv;
917
918 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000919 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000920 {
921 EMSG(_(e_invarg));
922 return FAIL;
923 }
924
925 redir_varname = vim_strsave(name);
926 if (redir_varname == NULL)
927 return FAIL;
928
929 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
930 if (redir_lval == NULL)
931 {
932 var_redir_stop();
933 return FAIL;
934 }
935
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000936 /* The output is stored in growarray "redir_ga" until redirection ends. */
937 ga_init2(&redir_ga, (int)sizeof(char), 500);
938
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000939 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000940 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
941 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
943 {
944 if (redir_endp != NULL && *redir_endp != NUL)
945 /* Trailing characters are present after the variable name */
946 EMSG(_(e_trailing));
947 else
948 EMSG(_(e_invarg));
949 var_redir_stop();
950 return FAIL;
951 }
952
953 /* check if we can write to the variable: set it to or append an empty
954 * string */
955 save_emsg = did_emsg;
956 did_emsg = FALSE;
957 tv.v_type = VAR_STRING;
958 tv.vval.v_string = (char_u *)"";
959 if (append)
960 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
961 else
962 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
963 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000964 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000965 if (err)
966 {
967 var_redir_stop();
968 return FAIL;
969 }
970 if (redir_lval->ll_newkey != NULL)
971 {
972 /* Dictionary item was created, don't do it again. */
973 vim_free(redir_lval->ll_newkey);
974 redir_lval->ll_newkey = NULL;
975 }
976
977 return OK;
978}
979
980/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000981 * Append "value[value_len]" to the variable set by var_redir_start().
982 * The actual appending is postponed until redirection ends, because the value
983 * appended may in fact be the string we write to, changing it may cause freed
984 * memory to be used:
985 * :redir => foo
986 * :let foo
987 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000988 */
989 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000990var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000991 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000992 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000993{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000994 size_t len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000995
996 if (redir_lval == NULL)
997 return;
998
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000999 if (value_len == -1)
1000 len = STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001 else
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001002 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001003
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001004 if (ga_grow(&redir_ga, (int)len) == OK)
1005 {
1006 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
1007 redir_ga.ga_len += len;
1008 }
1009 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011}
1012
1013/*
1014 * Stop redirecting command output to a variable.
1015 */
1016 void
1017var_redir_stop()
1018{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001019 typval_T tv;
1020
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021 if (redir_lval != NULL)
1022 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001023 /* Append the trailing NUL. */
1024 ga_append(&redir_ga, NUL);
1025
1026 /* Assign the text to the variable. */
1027 tv.v_type = VAR_STRING;
1028 tv.vval.v_string = redir_ga.ga_data;
1029 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1030 vim_free(tv.vval.v_string);
1031
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 clear_lval(redir_lval);
1033 vim_free(redir_lval);
1034 redir_lval = NULL;
1035 }
1036 vim_free(redir_varname);
1037 redir_varname = NULL;
1038}
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040# if defined(FEAT_MBYTE) || defined(PROTO)
1041 int
1042eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1043 char_u *enc_from;
1044 char_u *enc_to;
1045 char_u *fname_from;
1046 char_u *fname_to;
1047{
1048 int err = FALSE;
1049
1050 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1051 set_vim_var_string(VV_CC_TO, enc_to, -1);
1052 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1053 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1054 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1055 err = TRUE;
1056 set_vim_var_string(VV_CC_FROM, NULL, -1);
1057 set_vim_var_string(VV_CC_TO, NULL, -1);
1058 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1059 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1060
1061 if (err)
1062 return FAIL;
1063 return OK;
1064}
1065# endif
1066
1067# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1068 int
1069eval_printexpr(fname, args)
1070 char_u *fname;
1071 char_u *args;
1072{
1073 int err = FALSE;
1074
1075 set_vim_var_string(VV_FNAME_IN, fname, -1);
1076 set_vim_var_string(VV_CMDARG, args, -1);
1077 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1078 err = TRUE;
1079 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1080 set_vim_var_string(VV_CMDARG, NULL, -1);
1081
1082 if (err)
1083 {
1084 mch_remove(fname);
1085 return FAIL;
1086 }
1087 return OK;
1088}
1089# endif
1090
1091# if defined(FEAT_DIFF) || defined(PROTO)
1092 void
1093eval_diff(origfile, newfile, outfile)
1094 char_u *origfile;
1095 char_u *newfile;
1096 char_u *outfile;
1097{
1098 int err = FALSE;
1099
1100 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1101 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1102 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1103 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1104 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1105 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1106 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1107}
1108
1109 void
1110eval_patch(origfile, difffile, outfile)
1111 char_u *origfile;
1112 char_u *difffile;
1113 char_u *outfile;
1114{
1115 int err;
1116
1117 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1118 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1119 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1120 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1121 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1122 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1123 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1124}
1125# endif
1126
1127/*
1128 * Top level evaluation function, returning a boolean.
1129 * Sets "error" to TRUE if there was an error.
1130 * Return TRUE or FALSE.
1131 */
1132 int
1133eval_to_bool(arg, error, nextcmd, skip)
1134 char_u *arg;
1135 int *error;
1136 char_u **nextcmd;
1137 int skip; /* only parse, don't execute */
1138{
Bram Moolenaar33570922005-01-25 22:26:29 +00001139 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 int retval = FALSE;
1141
1142 if (skip)
1143 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001144 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 else
1147 {
1148 *error = FALSE;
1149 if (!skip)
1150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001151 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
1154 }
1155 if (skip)
1156 --emsg_skip;
1157
1158 return retval;
1159}
1160
1161/*
1162 * Top level evaluation function, returning a string. If "skip" is TRUE,
1163 * only parsing to "nextcmd" is done, without reporting errors. Return
1164 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1165 */
1166 char_u *
1167eval_to_string_skip(arg, nextcmd, skip)
1168 char_u *arg;
1169 char_u **nextcmd;
1170 int skip; /* only parse, don't execute */
1171{
Bram Moolenaar33570922005-01-25 22:26:29 +00001172 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 char_u *retval;
1174
1175 if (skip)
1176 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 retval = NULL;
1179 else
1180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001181 retval = vim_strsave(get_tv_string(&tv));
1182 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 if (skip)
1185 --emsg_skip;
1186
1187 return retval;
1188}
1189
1190/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001191 * Skip over an expression at "*pp".
1192 * Return FAIL for an error, OK otherwise.
1193 */
1194 int
1195skip_expr(pp)
1196 char_u **pp;
1197{
Bram Moolenaar33570922005-01-25 22:26:29 +00001198 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001199
1200 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001202}
1203
1204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 * Top level evaluation function, returning a string.
1206 * Return pointer to allocated memory, or NULL for failure.
1207 */
1208 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001209eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 char_u *arg;
1211 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001212 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213{
Bram Moolenaar33570922005-01-25 22:26:29 +00001214 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001216 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001218 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 retval = NULL;
1220 else
1221 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001222 if (dolist && tv.v_type == VAR_LIST)
1223 {
1224 ga_init2(&ga, (int)sizeof(char), 80);
1225 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1226 ga_append(&ga, NUL);
1227 retval = (char_u *)ga.ga_data;
1228 }
1229 else
1230 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233
1234 return retval;
1235}
1236
1237/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001238 * Call eval_to_string() without using current local variables and using
1239 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 */
1241 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001242eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 char_u *arg;
1244 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001245 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246{
1247 char_u *retval;
1248 void *save_funccalp;
1249
1250 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001251 if (use_sandbox)
1252 ++sandbox;
1253 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001254 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001255 if (use_sandbox)
1256 --sandbox;
1257 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 restore_funccal(save_funccalp);
1259 return retval;
1260}
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262/*
1263 * Top level evaluation function, returning a number.
1264 * Evaluates "expr" silently.
1265 * Returns -1 for an error.
1266 */
1267 int
1268eval_to_number(expr)
1269 char_u *expr;
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001273 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275 ++emsg_off;
1276
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 retval = -1;
1279 else
1280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001281 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 --emsg_off;
1285
1286 return retval;
1287}
1288
Bram Moolenaara40058a2005-07-11 22:42:07 +00001289/*
1290 * Prepare v: variable "idx" to be used.
1291 * Save the current typeval in "save_tv".
1292 * When not used yet add the variable to the v: hashtable.
1293 */
1294 static void
1295prepare_vimvar(idx, save_tv)
1296 int idx;
1297 typval_T *save_tv;
1298{
1299 *save_tv = vimvars[idx].vv_tv;
1300 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1301 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1302}
1303
1304/*
1305 * Restore v: variable "idx" to typeval "save_tv".
1306 * When no longer defined, remove the variable from the v: hashtable.
1307 */
1308 static void
1309restore_vimvar(idx, save_tv)
1310 int idx;
1311 typval_T *save_tv;
1312{
1313 hashitem_T *hi;
1314
1315 clear_tv(&vimvars[idx].vv_tv);
1316 vimvars[idx].vv_tv = *save_tv;
1317 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1318 {
1319 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1320 if (HASHITEM_EMPTY(hi))
1321 EMSG2(_(e_intern2), "restore_vimvar()");
1322 else
1323 hash_remove(&vimvarht, hi);
1324 }
1325}
1326
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001327#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001328/*
1329 * Evaluate an expression to a list with suggestions.
1330 * For the "expr:" part of 'spellsuggest'.
1331 */
1332 list_T *
1333eval_spell_expr(badword, expr)
1334 char_u *badword;
1335 char_u *expr;
1336{
1337 typval_T save_val;
1338 typval_T rettv;
1339 list_T *list = NULL;
1340 char_u *p = skipwhite(expr);
1341
1342 /* Set "v:val" to the bad word. */
1343 prepare_vimvar(VV_VAL, &save_val);
1344 vimvars[VV_VAL].vv_type = VAR_STRING;
1345 vimvars[VV_VAL].vv_str = badword;
1346 if (p_verbose == 0)
1347 ++emsg_off;
1348
1349 if (eval1(&p, &rettv, TRUE) == OK)
1350 {
1351 if (rettv.v_type != VAR_LIST)
1352 clear_tv(&rettv);
1353 else
1354 list = rettv.vval.v_list;
1355 }
1356
1357 if (p_verbose == 0)
1358 --emsg_off;
1359 vimvars[VV_VAL].vv_str = NULL;
1360 restore_vimvar(VV_VAL, &save_val);
1361
1362 return list;
1363}
1364
1365/*
1366 * "list" is supposed to contain two items: a word and a number. Return the
1367 * word in "pp" and the number as the return value.
1368 * Return -1 if anything isn't right.
1369 * Used to get the good word and score from the eval_spell_expr() result.
1370 */
1371 int
1372get_spellword(list, pp)
1373 list_T *list;
1374 char_u **pp;
1375{
1376 listitem_T *li;
1377
1378 li = list->lv_first;
1379 if (li == NULL)
1380 return -1;
1381 *pp = get_tv_string(&li->li_tv);
1382
1383 li = li->li_next;
1384 if (li == NULL)
1385 return -1;
1386 return get_tv_number(&li->li_tv);
1387}
1388#endif
1389
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001390/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001391 * Top level evaluation function.
1392 * Returns an allocated typval_T with the result.
1393 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001394 */
1395 typval_T *
1396eval_expr(arg, nextcmd)
1397 char_u *arg;
1398 char_u **nextcmd;
1399{
1400 typval_T *tv;
1401
1402 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001403 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001404 {
1405 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001406 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001407 }
1408
1409 return tv;
1410}
1411
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1414/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001415 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001417 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001419 static int
1420call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u *func;
1422 int argc;
1423 char_u **argv;
1424 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
Bram Moolenaar33570922005-01-25 22:26:29 +00001427 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 long n;
1429 int len;
1430 int i;
1431 int doesrange;
1432 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001433 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001435 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001437 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 for (i = 0; i < argc; i++)
1440 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001441 /* Pass a NULL or empty argument as an empty string */
1442 if (argv[i] == NULL || *argv[i] == NUL)
1443 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001444 argvars[i].v_type = VAR_STRING;
1445 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001446 continue;
1447 }
1448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 /* Recognize a number argument, the others must be strings. */
1450 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1451 if (len != 0 && len == (int)STRLEN(argv[i]))
1452 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001453 argvars[i].v_type = VAR_NUMBER;
1454 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
1456 else
1457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001458 argvars[i].v_type = VAR_STRING;
1459 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461 }
1462
1463 if (safe)
1464 {
1465 save_funccalp = save_funccal();
1466 ++sandbox;
1467 }
1468
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1470 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (safe)
1474 {
1475 --sandbox;
1476 restore_funccal(save_funccalp);
1477 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 vim_free(argvars);
1479
1480 if (ret == FAIL)
1481 clear_tv(rettv);
1482
1483 return ret;
1484}
1485
1486/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001487 * Call vimL function "func" and return the result as a string.
1488 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001489 * Uses argv[argc] for the function arguments.
1490 */
1491 void *
1492call_func_retstr(func, argc, argv, safe)
1493 char_u *func;
1494 int argc;
1495 char_u **argv;
1496 int safe; /* use the sandbox */
1497{
1498 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001499 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001500
1501 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1502 return NULL;
1503
1504 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001505 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 return retval;
1507}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001509#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001510/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001511 * Call vimL function "func" and return the result as a number.
1512 * Returns -1 when calling the function fails.
1513 * Uses argv[argc] for the function arguments.
1514 */
1515 long
1516call_func_retnr(func, argc, argv, safe)
1517 char_u *func;
1518 int argc;
1519 char_u **argv;
1520 int safe; /* use the sandbox */
1521{
1522 typval_T rettv;
1523 long retval;
1524
1525 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1526 return -1;
1527
1528 retval = get_tv_number_chk(&rettv, NULL);
1529 clear_tv(&rettv);
1530 return retval;
1531}
1532#endif
1533
1534/*
1535 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001536 * Uses argv[argc] for the function arguments.
1537 */
1538 void *
1539call_func_retlist(func, argc, argv, safe)
1540 char_u *func;
1541 int argc;
1542 char_u **argv;
1543 int safe; /* use the sandbox */
1544{
1545 typval_T rettv;
1546
1547 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1548 return NULL;
1549
1550 if (rettv.v_type != VAR_LIST)
1551 {
1552 clear_tv(&rettv);
1553 return NULL;
1554 }
1555
1556 return rettv.vval.v_list;
1557}
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559#endif
1560
1561/*
1562 * Save the current function call pointer, and set it to NULL.
1563 * Used when executing autocommands and for ":source".
1564 */
1565 void *
1566save_funccal()
1567{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001568 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 current_funccal = NULL;
1571 return (void *)fc;
1572}
1573
1574 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001575restore_funccal(vfc)
1576 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001578 funccall_T *fc = (funccall_T *)vfc;
1579
1580 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581}
1582
Bram Moolenaar05159a02005-02-26 23:04:13 +00001583#if defined(FEAT_PROFILE) || defined(PROTO)
1584/*
1585 * Prepare profiling for entering a child or something else that is not
1586 * counted for the script/function itself.
1587 * Should always be called in pair with prof_child_exit().
1588 */
1589 void
1590prof_child_enter(tm)
1591 proftime_T *tm; /* place to store waittime */
1592{
1593 funccall_T *fc = current_funccal;
1594
1595 if (fc != NULL && fc->func->uf_profiling)
1596 profile_start(&fc->prof_child);
1597 script_prof_save(tm);
1598}
1599
1600/*
1601 * Take care of time spent in a child.
1602 * Should always be called after prof_child_enter().
1603 */
1604 void
1605prof_child_exit(tm)
1606 proftime_T *tm; /* where waittime was stored */
1607{
1608 funccall_T *fc = current_funccal;
1609
1610 if (fc != NULL && fc->func->uf_profiling)
1611 {
1612 profile_end(&fc->prof_child);
1613 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1614 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1615 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1616 }
1617 script_prof_restore(tm);
1618}
1619#endif
1620
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622#ifdef FEAT_FOLDING
1623/*
1624 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1625 * it in "*cp". Doesn't give error messages.
1626 */
1627 int
1628eval_foldexpr(arg, cp)
1629 char_u *arg;
1630 int *cp;
1631{
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 int retval;
1634 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001635 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1636 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001639 if (use_sandbox)
1640 ++sandbox;
1641 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001643 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 retval = 0;
1645 else
1646 {
1647 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001648 if (tv.v_type == VAR_NUMBER)
1649 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001650 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 retval = 0;
1652 else
1653 {
1654 /* If the result is a string, check if there is a non-digit before
1655 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (!VIM_ISDIGIT(*s) && *s != '-')
1658 *cp = *s++;
1659 retval = atol((char *)s);
1660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001664 if (use_sandbox)
1665 --sandbox;
1666 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668 return retval;
1669}
1670#endif
1671
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 * ":let" list all variable values
1674 * ":let var1 var2" list variable values
1675 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 * ":let var += expr" assignment command.
1677 * ":let var -= expr" assignment command.
1678 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 */
1681 void
1682ex_let(eap)
1683 exarg_T *eap;
1684{
1685 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001686 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 int var_count = 0;
1690 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001692 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaardb552d602006-03-23 22:59:57 +00001694 argend = skip_var_list(arg, &var_count, &semicolon);
1695 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001697 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1698 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001699 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001702 /*
1703 * ":let" without "=": list variables
1704 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 if (*arg == '[')
1706 EMSG(_(e_invarg));
1707 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001708 /* ":let var1 var2" */
1709 arg = list_arg_vars(eap, arg);
1710 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001713 list_glob_vars();
1714 list_buf_vars();
1715 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001716#ifdef FEAT_WINDOWS
1717 list_tab_vars();
1718#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001719 list_script_vars();
1720 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001721 list_vim_vars();
1722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 eap->nextcmd = check_nextcmd(arg);
1724 }
1725 else
1726 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 op[0] = '=';
1728 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001729 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 {
1731 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1732 op[0] = expr[-1]; /* +=, -= or .= */
1733 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001735
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if (eap->skip)
1737 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (eap->skip)
1740 {
1741 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 --emsg_skip;
1744 }
1745 else if (i != FAIL)
1746 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
1751 }
1752}
1753
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754/*
1755 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1756 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 * When "nextchars" is not NULL it points to a string with characters that
1758 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1759 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 * Returns OK or FAIL;
1761 */
1762 static int
1763ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1764 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001765 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766 int copy; /* copy values from "tv", don't move */
1767 int semicolon; /* from skip_var_list() */
1768 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770{
1771 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001772 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001774 listitem_T *item;
1775 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776
1777 if (*arg != '[')
1778 {
1779 /*
1780 * ":let var = expr" or ":for var in list"
1781 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001782 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 return FAIL;
1784 return OK;
1785 }
1786
1787 /*
1788 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1789 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001790 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 {
1792 EMSG(_(e_listreq));
1793 return FAIL;
1794 }
1795
1796 i = list_len(l);
1797 if (semicolon == 0 && var_count < i)
1798 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001799 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 return FAIL;
1801 }
1802 if (var_count - semicolon > i)
1803 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001804 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805 return FAIL;
1806 }
1807
1808 item = l->lv_first;
1809 while (*arg != ']')
1810 {
1811 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 item = item->li_next;
1814 if (arg == NULL)
1815 return FAIL;
1816
1817 arg = skipwhite(arg);
1818 if (*arg == ';')
1819 {
1820 /* Put the rest of the list (may be empty) in the var after ';'.
1821 * Create a new list for this. */
1822 l = list_alloc();
1823 if (l == NULL)
1824 return FAIL;
1825 while (item != NULL)
1826 {
1827 list_append_tv(l, &item->li_tv);
1828 item = item->li_next;
1829 }
1830
1831 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001832 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833 ltv.vval.v_list = l;
1834 l->lv_refcount = 1;
1835
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1837 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 clear_tv(&ltv);
1839 if (arg == NULL)
1840 return FAIL;
1841 break;
1842 }
1843 else if (*arg != ',' && *arg != ']')
1844 {
1845 EMSG2(_(e_intern2), "ex_let_vars()");
1846 return FAIL;
1847 }
1848 }
1849
1850 return OK;
1851}
1852
1853/*
1854 * Skip over assignable variable "var" or list of variables "[var, var]".
1855 * Used for ":let varvar = expr" and ":for varvar in expr".
1856 * For "[var, var]" increment "*var_count" for each variable.
1857 * for "[var, var; var]" set "semicolon".
1858 * Return NULL for an error.
1859 */
1860 static char_u *
1861skip_var_list(arg, var_count, semicolon)
1862 char_u *arg;
1863 int *var_count;
1864 int *semicolon;
1865{
1866 char_u *p, *s;
1867
1868 if (*arg == '[')
1869 {
1870 /* "[var, var]": find the matching ']'. */
1871 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001872 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873 {
1874 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1875 s = skip_var_one(p);
1876 if (s == p)
1877 {
1878 EMSG2(_(e_invarg2), p);
1879 return NULL;
1880 }
1881 ++*var_count;
1882
1883 p = skipwhite(s);
1884 if (*p == ']')
1885 break;
1886 else if (*p == ';')
1887 {
1888 if (*semicolon == 1)
1889 {
1890 EMSG(_("Double ; in list of variables"));
1891 return NULL;
1892 }
1893 *semicolon = 1;
1894 }
1895 else if (*p != ',')
1896 {
1897 EMSG2(_(e_invarg2), p);
1898 return NULL;
1899 }
1900 }
1901 return p + 1;
1902 }
1903 else
1904 return skip_var_one(arg);
1905}
1906
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001907/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001908 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1909 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001910 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 static char_u *
1912skip_var_one(arg)
1913 char_u *arg;
1914{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001915 if (*arg == '@' && arg[1] != NUL)
1916 return arg + 2;
1917 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1918 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919}
1920
Bram Moolenaara7043832005-01-21 11:56:39 +00001921/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 * List variables for hashtab "ht" with prefix "prefix".
1923 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001924 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001926list_hashtable_vars(ht, prefix, empty)
1927 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001930{
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 hashitem_T *hi;
1932 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 int todo;
1934
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001935 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1937 {
1938 if (!HASHITEM_EMPTY(hi))
1939 {
1940 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 di = HI2DI(hi);
1942 if (empty || di->di_tv.v_type != VAR_STRING
1943 || di->di_tv.vval.v_string != NULL)
1944 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001945 }
1946 }
1947}
1948
1949/*
1950 * List global variables.
1951 */
1952 static void
1953list_glob_vars()
1954{
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001956}
1957
1958/*
1959 * List buffer variables.
1960 */
1961 static void
1962list_buf_vars()
1963{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001964 char_u numbuf[NUMBUFLEN];
1965
Bram Moolenaar33570922005-01-25 22:26:29 +00001966 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001967
1968 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1969 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001970}
1971
1972/*
1973 * List window variables.
1974 */
1975 static void
1976list_win_vars()
1977{
Bram Moolenaar33570922005-01-25 22:26:29 +00001978 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001979}
1980
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001981#ifdef FEAT_WINDOWS
1982/*
1983 * List tab page variables.
1984 */
1985 static void
1986list_tab_vars()
1987{
1988 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1989}
1990#endif
1991
Bram Moolenaara7043832005-01-21 11:56:39 +00001992/*
1993 * List Vim variables.
1994 */
1995 static void
1996list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997{
Bram Moolenaar33570922005-01-25 22:26:29 +00001998 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999}
2000
2001/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002002 * List script-local variables, if there is a script.
2003 */
2004 static void
2005list_script_vars()
2006{
2007 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
2008 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
2009}
2010
2011/*
2012 * List function variables, if there is a function.
2013 */
2014 static void
2015list_func_vars()
2016{
2017 if (current_funccal != NULL)
2018 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2019 (char_u *)"l:", FALSE);
2020}
2021
2022/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 * List variables in "arg".
2024 */
2025 static char_u *
2026list_arg_vars(eap, arg)
2027 exarg_T *eap;
2028 char_u *arg;
2029{
2030 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002031 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 char_u *name_start;
2034 char_u *arg_subsc;
2035 char_u *tofree;
2036 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037
2038 while (!ends_excmd(*arg) && !got_int)
2039 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002040 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002042 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2044 {
2045 emsg_severe = TRUE;
2046 EMSG(_(e_trailing));
2047 break;
2048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052 /* get_name_len() takes care of expanding curly braces */
2053 name_start = name = arg;
2054 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2055 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 /* This is mainly to keep test 49 working: when expanding
2058 * curly braces fails overrule the exception error message. */
2059 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 emsg_severe = TRUE;
2062 EMSG2(_(e_invarg2), arg);
2063 break;
2064 }
2065 error = TRUE;
2066 }
2067 else
2068 {
2069 if (tofree != NULL)
2070 name = tofree;
2071 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 else
2074 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 /* handle d.key, l[idx], f(expr) */
2076 arg_subsc = arg;
2077 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002079 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002082 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 case 'g': list_glob_vars(); break;
2086 case 'b': list_buf_vars(); break;
2087 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002088#ifdef FEAT_WINDOWS
2089 case 't': list_tab_vars(); break;
2090#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002091 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002092 case 's': list_script_vars(); break;
2093 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002094 default:
2095 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
2098 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 {
2100 char_u numbuf[NUMBUFLEN];
2101 char_u *tf;
2102 int c;
2103 char_u *s;
2104
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002105 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106 c = *arg;
2107 *arg = NUL;
2108 list_one_var_a((char_u *)"",
2109 arg == arg_subsc ? name : name_start,
2110 tv.v_type, s == NULL ? (char_u *)"" : s);
2111 *arg = c;
2112 vim_free(tf);
2113 }
2114 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 }
2117 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118
2119 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121
2122 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 }
2124
2125 return arg;
2126}
2127
2128/*
2129 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2130 * Returns a pointer to the char just after the var name.
2131 * Returns NULL if there is an error.
2132 */
2133 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002134ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002135 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002136 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002138 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002139 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140{
2141 int c1;
2142 char_u *name;
2143 char_u *p;
2144 char_u *arg_end = NULL;
2145 int len;
2146 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002147 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148
2149 /*
2150 * ":let $VAR = expr": Set environment variable.
2151 */
2152 if (*arg == '$')
2153 {
2154 /* Find the end of the name. */
2155 ++arg;
2156 name = arg;
2157 len = get_env_len(&arg);
2158 if (len == 0)
2159 EMSG2(_(e_invarg2), name - 1);
2160 else
2161 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 if (op != NULL && (*op == '+' || *op == '-'))
2163 EMSG2(_(e_letwrong), op);
2164 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002165 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166 EMSG(_(e_letunexp));
2167 else
2168 {
2169 c1 = name[len];
2170 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 p = get_tv_string_chk(tv);
2172 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 {
2174 int mustfree = FALSE;
2175 char_u *s = vim_getenv(name, &mustfree);
2176
2177 if (s != NULL)
2178 {
2179 p = tofree = concat_str(s, p);
2180 if (mustfree)
2181 vim_free(s);
2182 }
2183 }
2184 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002185 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002187 if (STRICMP(name, "HOME") == 0)
2188 init_homedir();
2189 else if (didset_vim && STRICMP(name, "VIM") == 0)
2190 didset_vim = FALSE;
2191 else if (didset_vimruntime
2192 && STRICMP(name, "VIMRUNTIME") == 0)
2193 didset_vimruntime = FALSE;
2194 arg_end = arg;
2195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002197 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 }
2199 }
2200 }
2201
2202 /*
2203 * ":let &option = expr": Set option value.
2204 * ":let &l:option = expr": Set local option value.
2205 * ":let &g:option = expr": Set global option value.
2206 */
2207 else if (*arg == '&')
2208 {
2209 /* Find the end of the name. */
2210 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002211 if (p == NULL || (endchars != NULL
2212 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 EMSG(_(e_letunexp));
2214 else
2215 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002216 long n;
2217 int opt_type;
2218 long numval;
2219 char_u *stringval = NULL;
2220 char_u *s;
2221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 c1 = *p;
2223 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002224
2225 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226 s = get_tv_string_chk(tv); /* != NULL if number or string */
2227 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002228 {
2229 opt_type = get_option_value(arg, &numval,
2230 &stringval, opt_flags);
2231 if ((opt_type == 1 && *op == '.')
2232 || (opt_type == 0 && *op != '.'))
2233 EMSG2(_(e_letwrong), op);
2234 else
2235 {
2236 if (opt_type == 1) /* number */
2237 {
2238 if (*op == '+')
2239 n = numval + n;
2240 else
2241 n = numval - n;
2242 }
2243 else if (opt_type == 0 && stringval != NULL) /* string */
2244 {
2245 s = concat_str(stringval, s);
2246 vim_free(stringval);
2247 stringval = s;
2248 }
2249 }
2250 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002251 if (s != NULL)
2252 {
2253 set_option_value(arg, n, s, opt_flags);
2254 arg_end = p;
2255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 }
2259 }
2260
2261 /*
2262 * ":let @r = expr": Set register contents.
2263 */
2264 else if (*arg == '@')
2265 {
2266 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267 if (op != NULL && (*op == '+' || *op == '-'))
2268 EMSG2(_(e_letwrong), op);
2269 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002270 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 EMSG(_(e_letunexp));
2272 else
2273 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002274 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002275 char_u *s;
2276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 p = get_tv_string_chk(tv);
2278 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002279 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002280 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002281 if (s != NULL)
2282 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002283 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002284 vim_free(s);
2285 }
2286 }
2287 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002288 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002289 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002290 arg_end = arg + 1;
2291 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002292 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 }
2294 }
2295
2296 /*
2297 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002300 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002302 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002304 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2308 EMSG(_(e_letunexp));
2309 else
2310 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 arg_end = p;
2313 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 }
2317
2318 else
2319 EMSG2(_(e_invarg2), arg);
2320
2321 return arg_end;
2322}
2323
2324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002325 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2326 */
2327 static int
2328check_changedtick(arg)
2329 char_u *arg;
2330{
2331 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2332 {
2333 EMSG2(_(e_readonlyvar), arg);
2334 return TRUE;
2335 }
2336 return FALSE;
2337}
2338
2339/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 * Get an lval: variable, Dict item or List item that can be assigned a value
2341 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2342 * "name.key", "name.key[expr]" etc.
2343 * Indexing only works if "name" is an existing List or Dictionary.
2344 * "name" points to the start of the name.
2345 * If "rettv" is not NULL it points to the value to be assigned.
2346 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2347 * wrong; must end in space or cmd separator.
2348 *
2349 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002350 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 */
2353 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002354get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002356 typval_T *rettv;
2357 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 int unlet;
2359 int skip;
2360 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002361 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 char_u *expr_start, *expr_end;
2365 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002366 dictitem_T *v;
2367 typval_T var1;
2368 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002370 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002372 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002373 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002376 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377
2378 if (skip)
2379 {
2380 /* When skipping just find the end of the name. */
2381 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002382 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 }
2384
2385 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002386 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (expr_start != NULL)
2388 {
2389 /* Don't expand the name when we already know there is an error. */
2390 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2391 && *p != '[' && *p != '.')
2392 {
2393 EMSG(_(e_trailing));
2394 return NULL;
2395 }
2396
2397 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2398 if (lp->ll_exp_name == NULL)
2399 {
2400 /* Report an invalid expression in braces, unless the
2401 * expression evaluation has been cancelled due to an
2402 * aborting error, an interrupt, or an exception. */
2403 if (!aborting() && !quiet)
2404 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002405 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 EMSG2(_(e_invarg2), name);
2407 return NULL;
2408 }
2409 }
2410 lp->ll_name = lp->ll_exp_name;
2411 }
2412 else
2413 lp->ll_name = name;
2414
2415 /* Without [idx] or .key we are done. */
2416 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2417 return p;
2418
2419 cc = *p;
2420 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002421 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 if (v == NULL && !quiet)
2423 EMSG2(_(e_undefvar), lp->ll_name);
2424 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 if (v == NULL)
2426 return NULL;
2427
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 /*
2429 * Loop until no more [idx] or .key is following.
2430 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002431 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2435 && !(lp->ll_tv->v_type == VAR_DICT
2436 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (!quiet)
2439 EMSG(_("E689: Can only index a List or Dictionary"));
2440 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002443 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 if (!quiet)
2445 EMSG(_("E708: [:] must come last"));
2446 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002448
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 len = -1;
2450 if (*p == '.')
2451 {
2452 key = p + 1;
2453 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2454 ;
2455 if (len == 0)
2456 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 if (!quiet)
2458 EMSG(_(e_emptykey));
2459 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 }
2461 p = key + len;
2462 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002463 else
2464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002466 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 if (*p == ':')
2468 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002469 else
2470 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 empty1 = FALSE;
2472 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 if (get_tv_string_chk(&var1) == NULL)
2475 {
2476 /* not a number or string */
2477 clear_tv(&var1);
2478 return NULL;
2479 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 }
2481
2482 /* Optionally get the second index [ :expr]. */
2483 if (*p == ':')
2484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002489 if (!empty1)
2490 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002492 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 if (rettv != NULL && (rettv->v_type != VAR_LIST
2494 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (!quiet)
2497 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 if (!empty1)
2499 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 }
2502 p = skipwhite(p + 1);
2503 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 else
2506 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2509 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 if (!empty1)
2511 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 if (get_tv_string_chk(&var2) == NULL)
2515 {
2516 /* not a number or string */
2517 if (!empty1)
2518 clear_tv(&var1);
2519 clear_tv(&var2);
2520 return NULL;
2521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002524 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002527
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002529 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 if (!quiet)
2531 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 if (!empty1)
2533 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 }
2538
2539 /* Skip to past ']'. */
2540 ++p;
2541 }
2542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 {
2545 if (len == -1)
2546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002548 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 if (*key == NUL)
2550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (!quiet)
2552 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 }
2556 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 lp->ll_list = NULL;
2558 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002559 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002562 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 if (len == -1)
2568 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 }
2571 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 if (len == -1)
2576 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 p = NULL;
2579 break;
2580 }
2581 if (len == -1)
2582 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 }
2585 else
2586 {
2587 /*
2588 * Get the number and item for the only or first index of the List.
2589 */
2590 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 else
2593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002594 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 clear_tv(&var1);
2596 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002597 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_list = lp->ll_tv->vval.v_list;
2599 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2600 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002602 if (lp->ll_n1 < 0)
2603 {
2604 lp->ll_n1 = 0;
2605 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2606 }
2607 }
2608 if (lp->ll_li == NULL)
2609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 }
2614
2615 /*
2616 * May need to find the item or absolute index for the second
2617 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 * When no index given: "lp->ll_empty2" is TRUE.
2619 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002623 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 }
2632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2634 if (lp->ll_n1 < 0)
2635 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2636 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638 }
2639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 }
2643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 return p;
2645}
2646
2647/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 */
2650 static void
2651clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002652 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653{
2654 vim_free(lp->ll_exp_name);
2655 vim_free(lp->ll_newkey);
2656}
2657
2658/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002659 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002661 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 */
2663 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002665 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002667 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670{
2671 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002672 listitem_T *ri;
2673 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674
2675 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 cc = *endp;
2680 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002681 if (op != NULL && *op != '=')
2682 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002683 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002685 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002686 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002687 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 {
2689 if (tv_op(&tv, rettv, op) == OK)
2690 set_var(lp->ll_name, &tv, FALSE);
2691 clear_tv(&tv);
2692 }
2693 }
2694 else
2695 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002699 else if (tv_check_lock(lp->ll_newkey == NULL
2700 ? lp->ll_tv->v_lock
2701 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2702 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 else if (lp->ll_range)
2704 {
2705 /*
2706 * Assign the List values to the list items.
2707 */
2708 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710 if (op != NULL && *op != '=')
2711 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2712 else
2713 {
2714 clear_tv(&lp->ll_li->li_tv);
2715 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2716 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 ri = ri->li_next;
2718 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2719 break;
2720 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002723 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 ri = NULL;
2726 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_li = lp->ll_li->li_next;
2730 ++lp->ll_n1;
2731 }
2732 if (ri != NULL)
2733 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 else if (lp->ll_empty2
2735 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 : lp->ll_n1 != lp->ll_n2)
2737 EMSG(_("E711: List value has not enough items"));
2738 }
2739 else
2740 {
2741 /*
2742 * Assign to a List or Dictionary item.
2743 */
2744 if (lp->ll_newkey != NULL)
2745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 if (op != NULL && *op != '=')
2747 {
2748 EMSG2(_(e_letwrong), op);
2749 return;
2750 }
2751
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002753 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (di == NULL)
2755 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002756 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2757 {
2758 vim_free(di);
2759 return;
2760 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002763 else if (op != NULL && *op != '=')
2764 {
2765 tv_op(lp->ll_tv, rettv, op);
2766 return;
2767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002768 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 /*
2772 * Assign the value to the variable or list item.
2773 */
2774 if (copy)
2775 copy_tv(rettv, lp->ll_tv);
2776 else
2777 {
2778 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002779 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002781 }
2782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002783}
2784
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002785/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002786 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2787 * Returns OK or FAIL.
2788 */
2789 static int
2790tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002791 typval_T *tv1;
2792 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 char_u *op;
2794{
2795 long n;
2796 char_u numbuf[NUMBUFLEN];
2797 char_u *s;
2798
2799 /* Can't do anything with a Funcref or a Dict on the right. */
2800 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2801 {
2802 switch (tv1->v_type)
2803 {
2804 case VAR_DICT:
2805 case VAR_FUNC:
2806 break;
2807
2808 case VAR_LIST:
2809 if (*op != '+' || tv2->v_type != VAR_LIST)
2810 break;
2811 /* List += List */
2812 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2813 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2814 return OK;
2815
2816 case VAR_NUMBER:
2817 case VAR_STRING:
2818 if (tv2->v_type == VAR_LIST)
2819 break;
2820 if (*op == '+' || *op == '-')
2821 {
2822 /* nr += nr or nr -= nr*/
2823 n = get_tv_number(tv1);
2824 if (*op == '+')
2825 n += get_tv_number(tv2);
2826 else
2827 n -= get_tv_number(tv2);
2828 clear_tv(tv1);
2829 tv1->v_type = VAR_NUMBER;
2830 tv1->vval.v_number = n;
2831 }
2832 else
2833 {
2834 /* str .= str */
2835 s = get_tv_string(tv1);
2836 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2837 clear_tv(tv1);
2838 tv1->v_type = VAR_STRING;
2839 tv1->vval.v_string = s;
2840 }
2841 return OK;
2842 }
2843 }
2844
2845 EMSG2(_(e_letwrong), op);
2846 return FAIL;
2847}
2848
2849/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002850 * Add a watcher to a list.
2851 */
2852 static void
2853list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002854 list_T *l;
2855 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856{
2857 lw->lw_next = l->lv_watch;
2858 l->lv_watch = lw;
2859}
2860
2861/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002862 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863 * No warning when it isn't found...
2864 */
2865 static void
2866list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 list_T *l;
2868 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002869{
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871
2872 lwp = &l->lv_watch;
2873 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2874 {
2875 if (lw == lwrem)
2876 {
2877 *lwp = lw->lw_next;
2878 break;
2879 }
2880 lwp = &lw->lw_next;
2881 }
2882}
2883
2884/*
2885 * Just before removing an item from a list: advance watchers to the next
2886 * item.
2887 */
2888 static void
2889list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 list_T *l;
2891 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002892{
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002894
2895 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2896 if (lw->lw_item == item)
2897 lw->lw_item = item->li_next;
2898}
2899
2900/*
2901 * Evaluate the expression used in a ":for var in expr" command.
2902 * "arg" points to "var".
2903 * Set "*errp" to TRUE for an error, FALSE otherwise;
2904 * Return a pointer that holds the info. Null when there is an error.
2905 */
2906 void *
2907eval_for_line(arg, errp, nextcmdp, skip)
2908 char_u *arg;
2909 int *errp;
2910 char_u **nextcmdp;
2911 int skip;
2912{
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 typval_T tv;
2916 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002917
2918 *errp = TRUE; /* default: there is an error */
2919
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002921 if (fi == NULL)
2922 return NULL;
2923
2924 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2925 if (expr == NULL)
2926 return fi;
2927
2928 expr = skipwhite(expr);
2929 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2930 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002931 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002932 return fi;
2933 }
2934
2935 if (skip)
2936 ++emsg_skip;
2937 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2938 {
2939 *errp = FALSE;
2940 if (!skip)
2941 {
2942 l = tv.vval.v_list;
2943 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002944 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002946 clear_tv(&tv);
2947 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002948 else
2949 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002950 /* No need to increment the refcount, it's already set for the
2951 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002952 fi->fi_list = l;
2953 list_add_watch(l, &fi->fi_lw);
2954 fi->fi_lw.lw_item = l->lv_first;
2955 }
2956 }
2957 }
2958 if (skip)
2959 --emsg_skip;
2960
2961 return fi;
2962}
2963
2964/*
2965 * Use the first item in a ":for" list. Advance to the next.
2966 * Assign the values to the variable (list). "arg" points to the first one.
2967 * Return TRUE when a valid item was found, FALSE when at end of list or
2968 * something wrong.
2969 */
2970 int
2971next_for_item(fi_void, arg)
2972 void *fi_void;
2973 char_u *arg;
2974{
Bram Moolenaar33570922005-01-25 22:26:29 +00002975 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002977 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978
2979 item = fi->fi_lw.lw_item;
2980 if (item == NULL)
2981 result = FALSE;
2982 else
2983 {
2984 fi->fi_lw.lw_item = item->li_next;
2985 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2986 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2987 }
2988 return result;
2989}
2990
2991/*
2992 * Free the structure used to store info used by ":for".
2993 */
2994 void
2995free_for_info(fi_void)
2996 void *fi_void;
2997{
Bram Moolenaar33570922005-01-25 22:26:29 +00002998 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002999
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003000 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003001 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003003 list_unref(fi->fi_list);
3004 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005 vim_free(fi);
3006}
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3009
3010 void
3011set_context_for_expression(xp, arg, cmdidx)
3012 expand_T *xp;
3013 char_u *arg;
3014 cmdidx_T cmdidx;
3015{
3016 int got_eq = FALSE;
3017 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003018 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003020 if (cmdidx == CMD_let)
3021 {
3022 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003023 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003024 {
3025 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003026 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003027 {
3028 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003029 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030 if (vim_iswhite(*p))
3031 break;
3032 }
3033 return;
3034 }
3035 }
3036 else
3037 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3038 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 while ((xp->xp_pattern = vim_strpbrk(arg,
3040 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3041 {
3042 c = *xp->xp_pattern;
3043 if (c == '&')
3044 {
3045 c = xp->xp_pattern[1];
3046 if (c == '&')
3047 {
3048 ++xp->xp_pattern;
3049 xp->xp_context = cmdidx != CMD_let || got_eq
3050 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3051 }
3052 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003055 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3056 xp->xp_pattern += 2;
3057
3058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 }
3060 else if (c == '$')
3061 {
3062 /* environment variable */
3063 xp->xp_context = EXPAND_ENV_VARS;
3064 }
3065 else if (c == '=')
3066 {
3067 got_eq = TRUE;
3068 xp->xp_context = EXPAND_EXPRESSION;
3069 }
3070 else if (c == '<'
3071 && xp->xp_context == EXPAND_FUNCTIONS
3072 && vim_strchr(xp->xp_pattern, '(') == NULL)
3073 {
3074 /* Function name can start with "<SNR>" */
3075 break;
3076 }
3077 else if (cmdidx != CMD_let || got_eq)
3078 {
3079 if (c == '"') /* string */
3080 {
3081 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3082 if (c == '\\' && xp->xp_pattern[1] != NUL)
3083 ++xp->xp_pattern;
3084 xp->xp_context = EXPAND_NOTHING;
3085 }
3086 else if (c == '\'') /* literal string */
3087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003088 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3090 /* skip */ ;
3091 xp->xp_context = EXPAND_NOTHING;
3092 }
3093 else if (c == '|')
3094 {
3095 if (xp->xp_pattern[1] == '|')
3096 {
3097 ++xp->xp_pattern;
3098 xp->xp_context = EXPAND_EXPRESSION;
3099 }
3100 else
3101 xp->xp_context = EXPAND_COMMANDS;
3102 }
3103 else
3104 xp->xp_context = EXPAND_EXPRESSION;
3105 }
3106 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107 /* Doesn't look like something valid, expand as an expression
3108 * anyway. */
3109 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 arg = xp->xp_pattern;
3111 if (*arg != NUL)
3112 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3113 /* skip */ ;
3114 }
3115 xp->xp_pattern = arg;
3116}
3117
3118#endif /* FEAT_CMDL_COMPL */
3119
3120/*
3121 * ":1,25call func(arg1, arg2)" function call.
3122 */
3123 void
3124ex_call(eap)
3125 exarg_T *eap;
3126{
3127 char_u *arg = eap->arg;
3128 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003132 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 linenr_T lnum;
3134 int doesrange;
3135 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003136 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003138 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003139 if (fudi.fd_newkey != NULL)
3140 {
3141 /* Still need to give an error message for missing key. */
3142 EMSG2(_(e_dictkey), fudi.fd_newkey);
3143 vim_free(fudi.fd_newkey);
3144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 if (tofree == NULL)
3146 return;
3147
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003148 /* Increase refcount on dictionary, it could get deleted when evaluating
3149 * the arguments. */
3150 if (fudi.fd_dict != NULL)
3151 ++fudi.fd_dict->dv_refcount;
3152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003153 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003154 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003155 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaar532c7802005-01-27 14:44:31 +00003157 /* Skip white space to allow ":call func ()". Not good, but required for
3158 * backward compatibility. */
3159 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003160 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 if (*startarg != '(')
3163 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003164 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 goto end;
3166 }
3167
3168 /*
3169 * When skipping, evaluate the function once, to find the end of the
3170 * arguments.
3171 * When the function takes a range, this is discovered after the first
3172 * call, and the loop is broken.
3173 */
3174 if (eap->skip)
3175 {
3176 ++emsg_skip;
3177 lnum = eap->line2; /* do it once, also with an invalid range */
3178 }
3179 else
3180 lnum = eap->line1;
3181 for ( ; lnum <= eap->line2; ++lnum)
3182 {
3183 if (!eap->skip && eap->addr_count > 0)
3184 {
3185 curwin->w_cursor.lnum = lnum;
3186 curwin->w_cursor.col = 0;
3187 }
3188 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003189 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003190 eap->line1, eap->line2, &doesrange,
3191 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 failed = TRUE;
3194 break;
3195 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003196
3197 /* Handle a function returning a Funcref, Dictionary or List. */
3198 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3199 {
3200 failed = TRUE;
3201 break;
3202 }
3203
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003204 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (doesrange || eap->skip)
3206 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003209 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003210 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003211 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (aborting())
3213 break;
3214 }
3215 if (eap->skip)
3216 --emsg_skip;
3217
3218 if (!failed)
3219 {
3220 /* Check for trailing illegal characters and a following command. */
3221 if (!ends_excmd(*arg))
3222 {
3223 emsg_severe = TRUE;
3224 EMSG(_(e_trailing));
3225 }
3226 else
3227 eap->nextcmd = check_nextcmd(arg);
3228 }
3229
3230end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003231 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003232 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233}
3234
3235/*
3236 * ":unlet[!] var1 ... " command.
3237 */
3238 void
3239ex_unlet(eap)
3240 exarg_T *eap;
3241{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003242 ex_unletlock(eap, eap->arg, 0);
3243}
3244
3245/*
3246 * ":lockvar" and ":unlockvar" commands
3247 */
3248 void
3249ex_lockvar(eap)
3250 exarg_T *eap;
3251{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003253 int deep = 2;
3254
3255 if (eap->forceit)
3256 deep = -1;
3257 else if (vim_isdigit(*arg))
3258 {
3259 deep = getdigits(&arg);
3260 arg = skipwhite(arg);
3261 }
3262
3263 ex_unletlock(eap, arg, deep);
3264}
3265
3266/*
3267 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3268 */
3269 static void
3270ex_unletlock(eap, argstart, deep)
3271 exarg_T *eap;
3272 char_u *argstart;
3273 int deep;
3274{
3275 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003278 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
3280 do
3281 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003282 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003283 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3284 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003285 if (lv.ll_name == NULL)
3286 error = TRUE; /* error but continue parsing */
3287 if (name_end == NULL || (!vim_iswhite(*name_end)
3288 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003290 if (name_end != NULL)
3291 {
3292 emsg_severe = TRUE;
3293 EMSG(_(e_trailing));
3294 }
3295 if (!(eap->skip || error))
3296 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 break;
3298 }
3299
3300 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003301 {
3302 if (eap->cmdidx == CMD_unlet)
3303 {
3304 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3305 error = TRUE;
3306 }
3307 else
3308 {
3309 if (do_lock_var(&lv, name_end, deep,
3310 eap->cmdidx == CMD_lockvar) == FAIL)
3311 error = TRUE;
3312 }
3313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003315 if (!eap->skip)
3316 clear_lval(&lv);
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 arg = skipwhite(name_end);
3319 } while (!ends_excmd(*arg));
3320
3321 eap->nextcmd = check_nextcmd(arg);
3322}
3323
Bram Moolenaar8c711452005-01-14 21:53:12 +00003324 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003325do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003327 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003328 int forceit;
3329{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003330 int ret = OK;
3331 int cc;
3332
3333 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003334 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003335 cc = *name_end;
3336 *name_end = NUL;
3337
3338 /* Normal name or expanded name. */
3339 if (check_changedtick(lp->ll_name))
3340 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003341 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003342 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003343 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003344 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003345 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3346 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003347 else if (lp->ll_range)
3348 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003349 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003350
3351 /* Delete a range of List items. */
3352 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3353 {
3354 li = lp->ll_li->li_next;
3355 listitem_remove(lp->ll_list, lp->ll_li);
3356 lp->ll_li = li;
3357 ++lp->ll_n1;
3358 }
3359 }
3360 else
3361 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003362 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003363 /* unlet a List item. */
3364 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003365 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003366 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003367 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003368 }
3369
3370 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003371}
3372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373/*
3374 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003375 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 */
3377 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003378do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003380 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
Bram Moolenaar33570922005-01-25 22:26:29 +00003382 hashtab_T *ht;
3383 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003384 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
Bram Moolenaar33570922005-01-25 22:26:29 +00003386 ht = find_var_ht(name, &varname);
3387 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 hi = hash_find(ht, varname);
3390 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003391 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003392 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3393 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003394 if (var_check_ro(HI2DI(hi)->di_flags, name))
3395 return FAIL;
3396 delete_var(ht, hi);
3397 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003400 if (forceit)
3401 return OK;
3402 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 return FAIL;
3404}
3405
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003406/*
3407 * Lock or unlock variable indicated by "lp".
3408 * "deep" is the levels to go (-1 for unlimited);
3409 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3410 */
3411 static int
3412do_lock_var(lp, name_end, deep, lock)
3413 lval_T *lp;
3414 char_u *name_end;
3415 int deep;
3416 int lock;
3417{
3418 int ret = OK;
3419 int cc;
3420 dictitem_T *di;
3421
3422 if (deep == 0) /* nothing to do */
3423 return OK;
3424
3425 if (lp->ll_tv == NULL)
3426 {
3427 cc = *name_end;
3428 *name_end = NUL;
3429
3430 /* Normal name or expanded name. */
3431 if (check_changedtick(lp->ll_name))
3432 ret = FAIL;
3433 else
3434 {
3435 di = find_var(lp->ll_name, NULL);
3436 if (di == NULL)
3437 ret = FAIL;
3438 else
3439 {
3440 if (lock)
3441 di->di_flags |= DI_FLAGS_LOCK;
3442 else
3443 di->di_flags &= ~DI_FLAGS_LOCK;
3444 item_lock(&di->di_tv, deep, lock);
3445 }
3446 }
3447 *name_end = cc;
3448 }
3449 else if (lp->ll_range)
3450 {
3451 listitem_T *li = lp->ll_li;
3452
3453 /* (un)lock a range of List items. */
3454 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3455 {
3456 item_lock(&li->li_tv, deep, lock);
3457 li = li->li_next;
3458 ++lp->ll_n1;
3459 }
3460 }
3461 else if (lp->ll_list != NULL)
3462 /* (un)lock a List item. */
3463 item_lock(&lp->ll_li->li_tv, deep, lock);
3464 else
3465 /* un(lock) a Dictionary item. */
3466 item_lock(&lp->ll_di->di_tv, deep, lock);
3467
3468 return ret;
3469}
3470
3471/*
3472 * Lock or unlock an item. "deep" is nr of levels to go.
3473 */
3474 static void
3475item_lock(tv, deep, lock)
3476 typval_T *tv;
3477 int deep;
3478 int lock;
3479{
3480 static int recurse = 0;
3481 list_T *l;
3482 listitem_T *li;
3483 dict_T *d;
3484 hashitem_T *hi;
3485 int todo;
3486
3487 if (recurse >= DICT_MAXNEST)
3488 {
3489 EMSG(_("E743: variable nested too deep for (un)lock"));
3490 return;
3491 }
3492 if (deep == 0)
3493 return;
3494 ++recurse;
3495
3496 /* lock/unlock the item itself */
3497 if (lock)
3498 tv->v_lock |= VAR_LOCKED;
3499 else
3500 tv->v_lock &= ~VAR_LOCKED;
3501
3502 switch (tv->v_type)
3503 {
3504 case VAR_LIST:
3505 if ((l = tv->vval.v_list) != NULL)
3506 {
3507 if (lock)
3508 l->lv_lock |= VAR_LOCKED;
3509 else
3510 l->lv_lock &= ~VAR_LOCKED;
3511 if (deep < 0 || deep > 1)
3512 /* recursive: lock/unlock the items the List contains */
3513 for (li = l->lv_first; li != NULL; li = li->li_next)
3514 item_lock(&li->li_tv, deep - 1, lock);
3515 }
3516 break;
3517 case VAR_DICT:
3518 if ((d = tv->vval.v_dict) != NULL)
3519 {
3520 if (lock)
3521 d->dv_lock |= VAR_LOCKED;
3522 else
3523 d->dv_lock &= ~VAR_LOCKED;
3524 if (deep < 0 || deep > 1)
3525 {
3526 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003527 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003528 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3529 {
3530 if (!HASHITEM_EMPTY(hi))
3531 {
3532 --todo;
3533 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3534 }
3535 }
3536 }
3537 }
3538 }
3539 --recurse;
3540}
3541
Bram Moolenaara40058a2005-07-11 22:42:07 +00003542/*
3543 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3544 * it refers to a List or Dictionary that is locked.
3545 */
3546 static int
3547tv_islocked(tv)
3548 typval_T *tv;
3549{
3550 return (tv->v_lock & VAR_LOCKED)
3551 || (tv->v_type == VAR_LIST
3552 && tv->vval.v_list != NULL
3553 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3554 || (tv->v_type == VAR_DICT
3555 && tv->vval.v_dict != NULL
3556 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3557}
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3560/*
3561 * Delete all "menutrans_" variables.
3562 */
3563 void
3564del_menutrans_vars()
3565{
Bram Moolenaar33570922005-01-25 22:26:29 +00003566 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003567 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
Bram Moolenaar33570922005-01-25 22:26:29 +00003569 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003570 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003572 {
3573 if (!HASHITEM_EMPTY(hi))
3574 {
3575 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003576 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3577 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003578 }
3579 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003580 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582#endif
3583
3584#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3585
3586/*
3587 * Local string buffer for the next two functions to store a variable name
3588 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3589 * get_user_var_name().
3590 */
3591
3592static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3593
3594static char_u *varnamebuf = NULL;
3595static int varnamebuflen = 0;
3596
3597/*
3598 * Function to concatenate a prefix and a variable name.
3599 */
3600 static char_u *
3601cat_prefix_varname(prefix, name)
3602 int prefix;
3603 char_u *name;
3604{
3605 int len;
3606
3607 len = (int)STRLEN(name) + 3;
3608 if (len > varnamebuflen)
3609 {
3610 vim_free(varnamebuf);
3611 len += 10; /* some additional space */
3612 varnamebuf = alloc(len);
3613 if (varnamebuf == NULL)
3614 {
3615 varnamebuflen = 0;
3616 return NULL;
3617 }
3618 varnamebuflen = len;
3619 }
3620 *varnamebuf = prefix;
3621 varnamebuf[1] = ':';
3622 STRCPY(varnamebuf + 2, name);
3623 return varnamebuf;
3624}
3625
3626/*
3627 * Function given to ExpandGeneric() to obtain the list of user defined
3628 * (global/buffer/window/built-in) variable names.
3629 */
3630/*ARGSUSED*/
3631 char_u *
3632get_user_var_name(xp, idx)
3633 expand_T *xp;
3634 int idx;
3635{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003636 static long_u gdone;
3637 static long_u bdone;
3638 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003639#ifdef FEAT_WINDOWS
3640 static long_u tdone;
3641#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003642 static int vidx;
3643 static hashitem_T *hi;
3644 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003647 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003649#ifdef FEAT_WINDOWS
3650 tdone = 0;
3651#endif
3652 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003653
3654 /* Global variables */
3655 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003657 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003659 else
3660 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003661 while (HASHITEM_EMPTY(hi))
3662 ++hi;
3663 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3664 return cat_prefix_varname('g', hi->hi_key);
3665 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003667
3668 /* b: variables */
3669 ht = &curbuf->b_vars.dv_hashtab;
3670 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003672 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003674 else
3675 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 while (HASHITEM_EMPTY(hi))
3677 ++hi;
3678 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003680 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 return (char_u *)"b:changedtick";
3684 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003685
3686 /* w: variables */
3687 ht = &curwin->w_vars.dv_hashtab;
3688 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003690 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003691 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003692 else
3693 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003694 while (HASHITEM_EMPTY(hi))
3695 ++hi;
3696 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003698
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003699#ifdef FEAT_WINDOWS
3700 /* t: variables */
3701 ht = &curtab->tp_vars.dv_hashtab;
3702 if (tdone < ht->ht_used)
3703 {
3704 if (tdone++ == 0)
3705 hi = ht->ht_array;
3706 else
3707 ++hi;
3708 while (HASHITEM_EMPTY(hi))
3709 ++hi;
3710 return cat_prefix_varname('t', hi->hi_key);
3711 }
3712#endif
3713
Bram Moolenaar33570922005-01-25 22:26:29 +00003714 /* v: variables */
3715 if (vidx < VV_LEN)
3716 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718 vim_free(varnamebuf);
3719 varnamebuf = NULL;
3720 varnamebuflen = 0;
3721 return NULL;
3722}
3723
3724#endif /* FEAT_CMDL_COMPL */
3725
3726/*
3727 * types for expressions.
3728 */
3729typedef enum
3730{
3731 TYPE_UNKNOWN = 0
3732 , TYPE_EQUAL /* == */
3733 , TYPE_NEQUAL /* != */
3734 , TYPE_GREATER /* > */
3735 , TYPE_GEQUAL /* >= */
3736 , TYPE_SMALLER /* < */
3737 , TYPE_SEQUAL /* <= */
3738 , TYPE_MATCH /* =~ */
3739 , TYPE_NOMATCH /* !~ */
3740} exptype_T;
3741
3742/*
3743 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003744 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3746 */
3747
3748/*
3749 * Handle zero level expression.
3750 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003751 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003752 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 * Return OK or FAIL.
3754 */
3755 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003756eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 char_u **nextcmd;
3760 int evaluate;
3761{
3762 int ret;
3763 char_u *p;
3764
3765 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003766 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 if (ret == FAIL || !ends_excmd(*p))
3768 {
3769 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 /*
3772 * Report the invalid expression unless the expression evaluation has
3773 * been cancelled due to an aborting error, an interrupt, or an
3774 * exception.
3775 */
3776 if (!aborting())
3777 EMSG2(_(e_invexpr2), arg);
3778 ret = FAIL;
3779 }
3780 if (nextcmd != NULL)
3781 *nextcmd = check_nextcmd(p);
3782
3783 return ret;
3784}
3785
3786/*
3787 * Handle top level expression:
3788 * expr1 ? expr0 : expr0
3789 *
3790 * "arg" must point to the first non-white of the expression.
3791 * "arg" is advanced to the next non-white after the recognized expression.
3792 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003793 * Note: "rettv.v_lock" is not set.
3794 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 * Return OK or FAIL.
3796 */
3797 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003798eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 int evaluate;
3802{
3803 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805
3806 /*
3807 * Get the first variable.
3808 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003809 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 return FAIL;
3811
3812 if ((*arg)[0] == '?')
3813 {
3814 result = FALSE;
3815 if (evaluate)
3816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003817 int error = FALSE;
3818
3819 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003821 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003822 if (error)
3823 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825
3826 /*
3827 * Get the second variable.
3828 */
3829 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 return FAIL;
3832
3833 /*
3834 * Check for the ":".
3835 */
3836 if ((*arg)[0] != ':')
3837 {
3838 EMSG(_("E109: Missing ':' after '?'"));
3839 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003840 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 return FAIL;
3842 }
3843
3844 /*
3845 * Get the third variable.
3846 */
3847 *arg = skipwhite(*arg + 1);
3848 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3849 {
3850 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return FAIL;
3853 }
3854 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857
3858 return OK;
3859}
3860
3861/*
3862 * Handle first level expression:
3863 * expr2 || expr2 || expr2 logical OR
3864 *
3865 * "arg" must point to the first non-white of the expression.
3866 * "arg" is advanced to the next non-white after the recognized expression.
3867 *
3868 * Return OK or FAIL.
3869 */
3870 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003871eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 int evaluate;
3875{
Bram Moolenaar33570922005-01-25 22:26:29 +00003876 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 long result;
3878 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003879 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
3881 /*
3882 * Get the first variable.
3883 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003884 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return FAIL;
3886
3887 /*
3888 * Repeat until there is no following "||".
3889 */
3890 first = TRUE;
3891 result = FALSE;
3892 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3893 {
3894 if (evaluate && first)
3895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003896 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003898 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003899 if (error)
3900 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 first = FALSE;
3902 }
3903
3904 /*
3905 * Get the second variable.
3906 */
3907 *arg = skipwhite(*arg + 2);
3908 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3909 return FAIL;
3910
3911 /*
3912 * Compute the result.
3913 */
3914 if (evaluate && !result)
3915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003916 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003919 if (error)
3920 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 if (evaluate)
3923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003924 rettv->v_type = VAR_NUMBER;
3925 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 }
3928
3929 return OK;
3930}
3931
3932/*
3933 * Handle second level expression:
3934 * expr3 && expr3 && expr3 logical AND
3935 *
3936 * "arg" must point to the first non-white of the expression.
3937 * "arg" is advanced to the next non-white after the recognized expression.
3938 *
3939 * Return OK or FAIL.
3940 */
3941 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 int evaluate;
3946{
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 long result;
3949 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003950 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952 /*
3953 * Get the first variable.
3954 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003955 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 return FAIL;
3957
3958 /*
3959 * Repeat until there is no following "&&".
3960 */
3961 first = TRUE;
3962 result = TRUE;
3963 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3964 {
3965 if (evaluate && first)
3966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003967 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003970 if (error)
3971 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 first = FALSE;
3973 }
3974
3975 /*
3976 * Get the second variable.
3977 */
3978 *arg = skipwhite(*arg + 2);
3979 if (eval4(arg, &var2, evaluate && result) == FAIL)
3980 return FAIL;
3981
3982 /*
3983 * Compute the result.
3984 */
3985 if (evaluate && result)
3986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003987 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003990 if (error)
3991 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 if (evaluate)
3994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995 rettv->v_type = VAR_NUMBER;
3996 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998 }
3999
4000 return OK;
4001}
4002
4003/*
4004 * Handle third level expression:
4005 * var1 == var2
4006 * var1 =~ var2
4007 * var1 != var2
4008 * var1 !~ var2
4009 * var1 > var2
4010 * var1 >= var2
4011 * var1 < var2
4012 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004013 * var1 is var2
4014 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 *
4016 * "arg" must point to the first non-white of the expression.
4017 * "arg" is advanced to the next non-white after the recognized expression.
4018 *
4019 * Return OK or FAIL.
4020 */
4021 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 int evaluate;
4026{
Bram Moolenaar33570922005-01-25 22:26:29 +00004027 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *p;
4029 int i;
4030 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004031 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 int len = 2;
4033 long n1, n2;
4034 char_u *s1, *s2;
4035 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4036 regmatch_T regmatch;
4037 int ic;
4038 char_u *save_cpo;
4039
4040 /*
4041 * Get the first variable.
4042 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045
4046 p = *arg;
4047 switch (p[0])
4048 {
4049 case '=': if (p[1] == '=')
4050 type = TYPE_EQUAL;
4051 else if (p[1] == '~')
4052 type = TYPE_MATCH;
4053 break;
4054 case '!': if (p[1] == '=')
4055 type = TYPE_NEQUAL;
4056 else if (p[1] == '~')
4057 type = TYPE_NOMATCH;
4058 break;
4059 case '>': if (p[1] != '=')
4060 {
4061 type = TYPE_GREATER;
4062 len = 1;
4063 }
4064 else
4065 type = TYPE_GEQUAL;
4066 break;
4067 case '<': if (p[1] != '=')
4068 {
4069 type = TYPE_SMALLER;
4070 len = 1;
4071 }
4072 else
4073 type = TYPE_SEQUAL;
4074 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004075 case 'i': if (p[1] == 's')
4076 {
4077 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4078 len = 5;
4079 if (!vim_isIDc(p[len]))
4080 {
4081 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4082 type_is = TRUE;
4083 }
4084 }
4085 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087
4088 /*
4089 * If there is a comparitive operator, use it.
4090 */
4091 if (type != TYPE_UNKNOWN)
4092 {
4093 /* extra question mark appended: ignore case */
4094 if (p[len] == '?')
4095 {
4096 ic = TRUE;
4097 ++len;
4098 }
4099 /* extra '#' appended: match case */
4100 else if (p[len] == '#')
4101 {
4102 ic = FALSE;
4103 ++len;
4104 }
4105 /* nothing appened: use 'ignorecase' */
4106 else
4107 ic = p_ic;
4108
4109 /*
4110 * Get the second variable.
4111 */
4112 *arg = skipwhite(p + len);
4113 if (eval5(arg, &var2, evaluate) == FAIL)
4114 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 return FAIL;
4117 }
4118
4119 if (evaluate)
4120 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004121 if (type_is && rettv->v_type != var2.v_type)
4122 {
4123 /* For "is" a different type always means FALSE, for "notis"
4124 * it means TRUE. */
4125 n1 = (type == TYPE_NEQUAL);
4126 }
4127 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4128 {
4129 if (type_is)
4130 {
4131 n1 = (rettv->v_type == var2.v_type
4132 && rettv->vval.v_list == var2.vval.v_list);
4133 if (type == TYPE_NEQUAL)
4134 n1 = !n1;
4135 }
4136 else if (rettv->v_type != var2.v_type
4137 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4138 {
4139 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004140 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004141 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004142 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004143 clear_tv(rettv);
4144 clear_tv(&var2);
4145 return FAIL;
4146 }
4147 else
4148 {
4149 /* Compare two Lists for being equal or unequal. */
4150 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4151 if (type == TYPE_NEQUAL)
4152 n1 = !n1;
4153 }
4154 }
4155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004156 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4157 {
4158 if (type_is)
4159 {
4160 n1 = (rettv->v_type == var2.v_type
4161 && rettv->vval.v_dict == var2.vval.v_dict);
4162 if (type == TYPE_NEQUAL)
4163 n1 = !n1;
4164 }
4165 else if (rettv->v_type != var2.v_type
4166 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4167 {
4168 if (rettv->v_type != var2.v_type)
4169 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4170 else
4171 EMSG(_("E736: Invalid operation for Dictionary"));
4172 clear_tv(rettv);
4173 clear_tv(&var2);
4174 return FAIL;
4175 }
4176 else
4177 {
4178 /* Compare two Dictionaries for being equal or unequal. */
4179 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4180 if (type == TYPE_NEQUAL)
4181 n1 = !n1;
4182 }
4183 }
4184
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004185 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4186 {
4187 if (rettv->v_type != var2.v_type
4188 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4189 {
4190 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004191 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004192 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004193 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004194 clear_tv(rettv);
4195 clear_tv(&var2);
4196 return FAIL;
4197 }
4198 else
4199 {
4200 /* Compare two Funcrefs for being equal or unequal. */
4201 if (rettv->vval.v_string == NULL
4202 || var2.vval.v_string == NULL)
4203 n1 = FALSE;
4204 else
4205 n1 = STRCMP(rettv->vval.v_string,
4206 var2.vval.v_string) == 0;
4207 if (type == TYPE_NEQUAL)
4208 n1 = !n1;
4209 }
4210 }
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 /*
4213 * If one of the two variables is a number, compare as a number.
4214 * When using "=~" or "!~", always compare as string.
4215 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 n1 = get_tv_number(rettv);
4220 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 switch (type)
4222 {
4223 case TYPE_EQUAL: n1 = (n1 == n2); break;
4224 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4225 case TYPE_GREATER: n1 = (n1 > n2); break;
4226 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4227 case TYPE_SMALLER: n1 = (n1 < n2); break;
4228 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4229 case TYPE_UNKNOWN:
4230 case TYPE_MATCH:
4231 case TYPE_NOMATCH: break; /* avoid gcc warning */
4232 }
4233 }
4234 else
4235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 s1 = get_tv_string_buf(rettv, buf1);
4237 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4239 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4240 else
4241 i = 0;
4242 n1 = FALSE;
4243 switch (type)
4244 {
4245 case TYPE_EQUAL: n1 = (i == 0); break;
4246 case TYPE_NEQUAL: n1 = (i != 0); break;
4247 case TYPE_GREATER: n1 = (i > 0); break;
4248 case TYPE_GEQUAL: n1 = (i >= 0); break;
4249 case TYPE_SMALLER: n1 = (i < 0); break;
4250 case TYPE_SEQUAL: n1 = (i <= 0); break;
4251
4252 case TYPE_MATCH:
4253 case TYPE_NOMATCH:
4254 /* avoid 'l' flag in 'cpoptions' */
4255 save_cpo = p_cpo;
4256 p_cpo = (char_u *)"";
4257 regmatch.regprog = vim_regcomp(s2,
4258 RE_MAGIC + RE_STRING);
4259 regmatch.rm_ic = ic;
4260 if (regmatch.regprog != NULL)
4261 {
4262 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4263 vim_free(regmatch.regprog);
4264 if (type == TYPE_NOMATCH)
4265 n1 = !n1;
4266 }
4267 p_cpo = save_cpo;
4268 break;
4269
4270 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4271 }
4272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(rettv);
4274 clear_tv(&var2);
4275 rettv->v_type = VAR_NUMBER;
4276 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 }
4279
4280 return OK;
4281}
4282
4283/*
4284 * Handle fourth level expression:
4285 * + number addition
4286 * - number subtraction
4287 * . string concatenation
4288 *
4289 * "arg" must point to the first non-white of the expression.
4290 * "arg" is advanced to the next non-white after the recognized expression.
4291 *
4292 * Return OK or FAIL.
4293 */
4294 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int evaluate;
4299{
Bram Moolenaar33570922005-01-25 22:26:29 +00004300 typval_T var2;
4301 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 int op;
4303 long n1, n2;
4304 char_u *s1, *s2;
4305 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4306 char_u *p;
4307
4308 /*
4309 * Get the first variable.
4310 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313
4314 /*
4315 * Repeat computing, until no '+', '-' or '.' is following.
4316 */
4317 for (;;)
4318 {
4319 op = **arg;
4320 if (op != '+' && op != '-' && op != '.')
4321 break;
4322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (op != '+' || rettv->v_type != VAR_LIST)
4324 {
4325 /* For "list + ...", an illegal use of the first operand as
4326 * a number cannot be determined before evaluating the 2nd
4327 * operand: if this is also a list, all is ok.
4328 * For "something . ...", "something - ..." or "non-list + ...",
4329 * we know that the first operand needs to be a string or number
4330 * without evaluating the 2nd operand. So check before to avoid
4331 * side effects after an error. */
4332 if (evaluate && get_tv_string_chk(rettv) == NULL)
4333 {
4334 clear_tv(rettv);
4335 return FAIL;
4336 }
4337 }
4338
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 /*
4340 * Get the second variable.
4341 */
4342 *arg = skipwhite(*arg + 1);
4343 if (eval6(arg, &var2, evaluate) == FAIL)
4344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 return FAIL;
4347 }
4348
4349 if (evaluate)
4350 {
4351 /*
4352 * Compute the result.
4353 */
4354 if (op == '.')
4355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4357 s2 = get_tv_string_buf_chk(&var2, buf2);
4358 if (s2 == NULL) /* type error ? */
4359 {
4360 clear_tv(rettv);
4361 clear_tv(&var2);
4362 return FAIL;
4363 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004364 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 clear_tv(rettv);
4366 rettv->v_type = VAR_STRING;
4367 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004369 else if (op == '+' && rettv->v_type == VAR_LIST
4370 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004371 {
4372 /* concatenate Lists */
4373 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4374 &var3) == FAIL)
4375 {
4376 clear_tv(rettv);
4377 clear_tv(&var2);
4378 return FAIL;
4379 }
4380 clear_tv(rettv);
4381 *rettv = var3;
4382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 else
4384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004385 int error = FALSE;
4386
4387 n1 = get_tv_number_chk(rettv, &error);
4388 if (error)
4389 {
4390 /* This can only happen for "list + non-list".
4391 * For "non-list + ..." or "something - ...", we returned
4392 * before evaluating the 2nd operand. */
4393 clear_tv(rettv);
4394 return FAIL;
4395 }
4396 n2 = get_tv_number_chk(&var2, &error);
4397 if (error)
4398 {
4399 clear_tv(rettv);
4400 clear_tv(&var2);
4401 return FAIL;
4402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (op == '+')
4405 n1 = n1 + n2;
4406 else
4407 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 rettv->v_type = VAR_NUMBER;
4409 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413 }
4414 return OK;
4415}
4416
4417/*
4418 * Handle fifth level expression:
4419 * * number multiplication
4420 * / number division
4421 * % number modulo
4422 *
4423 * "arg" must point to the first non-white of the expression.
4424 * "arg" is advanced to the next non-white after the recognized expression.
4425 *
4426 * Return OK or FAIL.
4427 */
4428 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004429eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 int evaluate;
4433{
Bram Moolenaar33570922005-01-25 22:26:29 +00004434 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 int op;
4436 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004437 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439 /*
4440 * Get the first variable.
4441 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 return FAIL;
4444
4445 /*
4446 * Repeat computing, until no '*', '/' or '%' is following.
4447 */
4448 for (;;)
4449 {
4450 op = **arg;
4451 if (op != '*' && op != '/' && op != '%')
4452 break;
4453
4454 if (evaluate)
4455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004456 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004458 if (error)
4459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461 else
4462 n1 = 0;
4463
4464 /*
4465 * Get the second variable.
4466 */
4467 *arg = skipwhite(*arg + 1);
4468 if (eval7(arg, &var2, evaluate) == FAIL)
4469 return FAIL;
4470
4471 if (evaluate)
4472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004473 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004475 if (error)
4476 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
4478 /*
4479 * Compute the result.
4480 */
4481 if (op == '*')
4482 n1 = n1 * n2;
4483 else if (op == '/')
4484 {
4485 if (n2 == 0) /* give an error message? */
4486 n1 = 0x7fffffffL;
4487 else
4488 n1 = n1 / n2;
4489 }
4490 else
4491 {
4492 if (n2 == 0) /* give an error message? */
4493 n1 = 0;
4494 else
4495 n1 = n1 % n2;
4496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004497 rettv->v_type = VAR_NUMBER;
4498 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 }
4501
4502 return OK;
4503}
4504
4505/*
4506 * Handle sixth level expression:
4507 * number number constant
4508 * "string" string contstant
4509 * 'string' literal string contstant
4510 * &option-name option value
4511 * @r register contents
4512 * identifier variable value
4513 * function() function call
4514 * $VAR environment variable
4515 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004516 * [expr, expr] List
4517 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 *
4519 * Also handle:
4520 * ! in front logical NOT
4521 * - in front unary minus
4522 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523 * trailing [] subscript in String or List
4524 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 *
4526 * "arg" must point to the first non-white of the expression.
4527 * "arg" is advanced to the next non-white after the recognized expression.
4528 *
4529 * Return OK or FAIL.
4530 */
4531 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004532eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 int evaluate;
4536{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 long n;
4538 int len;
4539 char_u *s;
4540 int val;
4541 char_u *start_leader, *end_leader;
4542 int ret = OK;
4543 char_u *alias;
4544
4545 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004546 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004547 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004549 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 /*
4552 * Skip '!' and '-' characters. They are handled later.
4553 */
4554 start_leader = *arg;
4555 while (**arg == '!' || **arg == '-' || **arg == '+')
4556 *arg = skipwhite(*arg + 1);
4557 end_leader = *arg;
4558
4559 switch (**arg)
4560 {
4561 /*
4562 * Number constant.
4563 */
4564 case '0':
4565 case '1':
4566 case '2':
4567 case '3':
4568 case '4':
4569 case '5':
4570 case '6':
4571 case '7':
4572 case '8':
4573 case '9':
4574 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4575 *arg += len;
4576 if (evaluate)
4577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 rettv->v_type = VAR_NUMBER;
4579 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 break;
4582
4583 /*
4584 * String constant: "string".
4585 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004586 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 break;
4588
4589 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004590 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 break;
4594
4595 /*
4596 * List: [expr, expr]
4597 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 break;
4600
4601 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 * Dictionary: {key: val, key: val}
4603 */
4604 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4605 break;
4606
4607 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004608 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004610 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
4613 /*
4614 * Environment variable: $VAR.
4615 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618
4619 /*
4620 * Register contents: @r.
4621 */
4622 case '@': ++*arg;
4623 if (evaluate)
4624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004626 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 if (**arg != NUL)
4629 ++*arg;
4630 break;
4631
4632 /*
4633 * nested expression: (expression).
4634 */
4635 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (**arg == ')')
4638 ++*arg;
4639 else if (ret == OK)
4640 {
4641 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004642 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 ret = FAIL;
4644 }
4645 break;
4646
Bram Moolenaar8c711452005-01-14 21:53:12 +00004647 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 break;
4649 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004650
4651 if (ret == NOTDONE)
4652 {
4653 /*
4654 * Must be a variable or function name.
4655 * Can also be a curly-braces kind of name: {expr}.
4656 */
4657 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004658 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 if (alias != NULL)
4660 s = alias;
4661
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004662 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 ret = FAIL;
4664 else
4665 {
4666 if (**arg == '(') /* recursive! */
4667 {
4668 /* If "s" is the name of a variable of type VAR_FUNC
4669 * use its contents. */
4670 s = deref_func_name(s, &len);
4671
4672 /* Invoke the function. */
4673 ret = get_func_tv(s, len, rettv, arg,
4674 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004675 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676 /* Stop the expression evaluation when immediately
4677 * aborting on error, or when an interrupt occurred or
4678 * an exception was thrown but not caught. */
4679 if (aborting())
4680 {
4681 if (ret == OK)
4682 clear_tv(rettv);
4683 ret = FAIL;
4684 }
4685 }
4686 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004687 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004688 else
4689 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 }
4691
4692 if (alias != NULL)
4693 vim_free(alias);
4694 }
4695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 *arg = skipwhite(*arg);
4697
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004698 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4699 * expr(expr). */
4700 if (ret == OK)
4701 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 /*
4704 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4705 */
4706 if (ret == OK && evaluate && end_leader > start_leader)
4707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004708 int error = FALSE;
4709
4710 val = get_tv_number_chk(rettv, &error);
4711 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 clear_tv(rettv);
4714 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 else
4717 {
4718 while (end_leader > start_leader)
4719 {
4720 --end_leader;
4721 if (*end_leader == '!')
4722 val = !val;
4723 else if (*end_leader == '-')
4724 val = -val;
4725 }
4726 clear_tv(rettv);
4727 rettv->v_type = VAR_NUMBER;
4728 rettv->vval.v_number = val;
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731
4732 return ret;
4733}
4734
4735/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004736 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4737 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4739 */
4740 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004741eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004743 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004745 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746{
4747 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004748 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 long len = -1;
4751 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004757 if (verbose)
4758 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 return FAIL;
4760 }
4761
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 /*
4765 * dict.name
4766 */
4767 key = *arg + 1;
4768 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4769 ;
4770 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004772 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 }
4774 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776 /*
4777 * something[idx]
4778 *
4779 * Get the (first) variable from inside the [].
4780 */
4781 *arg = skipwhite(*arg + 1);
4782 if (**arg == ':')
4783 empty1 = TRUE;
4784 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4785 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4787 {
4788 /* not a number or string */
4789 clear_tv(&var1);
4790 return FAIL;
4791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792
4793 /*
4794 * Get the second variable from inside the [:].
4795 */
4796 if (**arg == ':')
4797 {
4798 range = TRUE;
4799 *arg = skipwhite(*arg + 1);
4800 if (**arg == ']')
4801 empty2 = TRUE;
4802 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 if (!empty1)
4805 clear_tv(&var1);
4806 return FAIL;
4807 }
4808 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4809 {
4810 /* not a number or string */
4811 if (!empty1)
4812 clear_tv(&var1);
4813 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 return FAIL;
4815 }
4816 }
4817
4818 /* Check for the ']'. */
4819 if (**arg != ']')
4820 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004821 if (verbose)
4822 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004823 clear_tv(&var1);
4824 if (range)
4825 clear_tv(&var2);
4826 return FAIL;
4827 }
4828 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 }
4830
4831 if (evaluate)
4832 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004833 n1 = 0;
4834 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 n1 = get_tv_number(&var1);
4837 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 }
4839 if (range)
4840 {
4841 if (empty2)
4842 n2 = -1;
4843 else
4844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 n2 = get_tv_number(&var2);
4846 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 }
4848 }
4849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004851 {
4852 case VAR_NUMBER:
4853 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 len = (long)STRLEN(s);
4856 if (range)
4857 {
4858 /* The resulting variable is a substring. If the indexes
4859 * are out of range the result is empty. */
4860 if (n1 < 0)
4861 {
4862 n1 = len + n1;
4863 if (n1 < 0)
4864 n1 = 0;
4865 }
4866 if (n2 < 0)
4867 n2 = len + n2;
4868 else if (n2 >= len)
4869 n2 = len;
4870 if (n1 >= len || n2 < 0 || n1 > n2)
4871 s = NULL;
4872 else
4873 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4874 }
4875 else
4876 {
4877 /* The resulting variable is a string of a single
4878 * character. If the index is too big or negative the
4879 * result is empty. */
4880 if (n1 >= len || n1 < 0)
4881 s = NULL;
4882 else
4883 s = vim_strnsave(s + n1, 1);
4884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 clear_tv(rettv);
4886 rettv->v_type = VAR_STRING;
4887 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004888 break;
4889
4890 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 if (n1 < 0)
4893 n1 = len + n1;
4894 if (!empty1 && (n1 < 0 || n1 >= len))
4895 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004896 /* For a range we allow invalid values and return an empty
4897 * list. A list index out of range is an error. */
4898 if (!range)
4899 {
4900 if (verbose)
4901 EMSGN(_(e_listidx), n1);
4902 return FAIL;
4903 }
4904 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004905 }
4906 if (range)
4907 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004908 list_T *l;
4909 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910
4911 if (n2 < 0)
4912 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004913 else if (n2 >= len)
4914 n2 = len - 1;
4915 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004916 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 l = list_alloc();
4918 if (l == NULL)
4919 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004921 n1 <= n2; ++n1)
4922 {
4923 if (list_append_tv(l, &item->li_tv) == FAIL)
4924 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004925 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004926 return FAIL;
4927 }
4928 item = item->li_next;
4929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(rettv);
4931 rettv->v_type = VAR_LIST;
4932 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004933 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934 }
4935 else
4936 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004937 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 clear_tv(rettv);
4939 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 }
4941 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942
4943 case VAR_DICT:
4944 if (range)
4945 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004946 if (verbose)
4947 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948 if (len == -1)
4949 clear_tv(&var1);
4950 return FAIL;
4951 }
4952 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004953 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004954
4955 if (len == -1)
4956 {
4957 key = get_tv_string(&var1);
4958 if (*key == NUL)
4959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004960 if (verbose)
4961 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 clear_tv(&var1);
4963 return FAIL;
4964 }
4965 }
4966
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004967 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004969 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004970 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 if (len == -1)
4972 clear_tv(&var1);
4973 if (item == NULL)
4974 return FAIL;
4975
4976 copy_tv(&item->di_tv, &var1);
4977 clear_tv(rettv);
4978 *rettv = var1;
4979 }
4980 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 }
4982 }
4983
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 return OK;
4985}
4986
4987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 * Get an option value.
4989 * "arg" points to the '&' or '+' before the option name.
4990 * "arg" is advanced to character after the option name.
4991 * Return OK or FAIL.
4992 */
4993 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004996 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 int evaluate;
4998{
4999 char_u *option_end;
5000 long numval;
5001 char_u *stringval;
5002 int opt_type;
5003 int c;
5004 int working = (**arg == '+'); /* has("+option") */
5005 int ret = OK;
5006 int opt_flags;
5007
5008 /*
5009 * Isolate the option name and find its value.
5010 */
5011 option_end = find_option_end(arg, &opt_flags);
5012 if (option_end == NULL)
5013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005014 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 EMSG2(_("E112: Option name missing: %s"), *arg);
5016 return FAIL;
5017 }
5018
5019 if (!evaluate)
5020 {
5021 *arg = option_end;
5022 return OK;
5023 }
5024
5025 c = *option_end;
5026 *option_end = NUL;
5027 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
5030 if (opt_type == -3) /* invalid name */
5031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005032 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 EMSG2(_("E113: Unknown option: %s"), *arg);
5034 ret = FAIL;
5035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005036 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
5038 if (opt_type == -2) /* hidden string option */
5039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040 rettv->v_type = VAR_STRING;
5041 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043 else if (opt_type == -1) /* hidden number option */
5044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005045 rettv->v_type = VAR_NUMBER;
5046 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048 else if (opt_type == 1) /* number option */
5049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005050 rettv->v_type = VAR_NUMBER;
5051 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053 else /* string option */
5054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055 rettv->v_type = VAR_STRING;
5056 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 }
5059 else if (working && (opt_type == -2 || opt_type == -1))
5060 ret = FAIL;
5061
5062 *option_end = c; /* put back for error messages */
5063 *arg = option_end;
5064
5065 return ret;
5066}
5067
5068/*
5069 * Allocate a variable for a string constant.
5070 * Return OK or FAIL.
5071 */
5072 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 int evaluate;
5077{
5078 char_u *p;
5079 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 int extra = 0;
5081
5082 /*
5083 * Find the end of the string, skipping backslashed characters.
5084 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
5087 if (*p == '\\' && p[1] != NUL)
5088 {
5089 ++p;
5090 /* A "\<x>" form occupies at least 4 characters, and produces up
5091 * to 6 characters: reserve space for 2 extra */
5092 if (*p == '<')
5093 extra += 2;
5094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096
5097 if (*p != '"')
5098 {
5099 EMSG2(_("E114: Missing quote: %s"), *arg);
5100 return FAIL;
5101 }
5102
5103 /* If only parsing, set *arg and return here */
5104 if (!evaluate)
5105 {
5106 *arg = p + 1;
5107 return OK;
5108 }
5109
5110 /*
5111 * Copy the string into allocated memory, handling backslashed
5112 * characters.
5113 */
5114 name = alloc((unsigned)(p - *arg + extra));
5115 if (name == NULL)
5116 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 rettv->v_type = VAR_STRING;
5118 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 if (*p == '\\')
5123 {
5124 switch (*++p)
5125 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 case 'b': *name++ = BS; ++p; break;
5127 case 'e': *name++ = ESC; ++p; break;
5128 case 'f': *name++ = FF; ++p; break;
5129 case 'n': *name++ = NL; ++p; break;
5130 case 'r': *name++ = CAR; ++p; break;
5131 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 case 'X': /* hex: "\x1", "\x12" */
5134 case 'x':
5135 case 'u': /* Unicode: "\u0023" */
5136 case 'U':
5137 if (vim_isxdigit(p[1]))
5138 {
5139 int n, nr;
5140 int c = toupper(*p);
5141
5142 if (c == 'X')
5143 n = 2;
5144 else
5145 n = 4;
5146 nr = 0;
5147 while (--n >= 0 && vim_isxdigit(p[1]))
5148 {
5149 ++p;
5150 nr = (nr << 4) + hex2nr(*p);
5151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153#ifdef FEAT_MBYTE
5154 /* For "\u" store the number according to
5155 * 'encoding'. */
5156 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 else
5159#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163
5164 /* octal: "\1", "\12", "\123" */
5165 case '0':
5166 case '1':
5167 case '2':
5168 case '3':
5169 case '4':
5170 case '5':
5171 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 case '7': *name = *p++ - '0';
5173 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 *name = (*name << 3) + *p++ - '0';
5176 if (*p >= '0' && *p <= '7')
5177 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (extra != 0)
5185 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 break;
5188 }
5189 /* FALLTHROUGH */
5190
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193 }
5194 }
5195 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 *arg = p + 1;
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 return OK;
5203}
5204
5205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 * Return OK or FAIL.
5208 */
5209 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005210get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 int evaluate;
5214{
5215 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005216 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005217 int reduce = 0;
5218
5219 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005221 */
5222 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
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 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005227 break;
5228 ++reduce;
5229 ++p;
5230 }
5231 }
5232
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005234 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005236 return FAIL;
5237 }
5238
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005240 if (!evaluate)
5241 {
5242 *arg = p + 1;
5243 return OK;
5244 }
5245
5246 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005248 */
5249 str = alloc((unsigned)((p - *arg) - reduce));
5250 if (str == NULL)
5251 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 rettv->v_type = VAR_STRING;
5253 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005254
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005256 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005258 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005260 break;
5261 ++p;
5262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005266 *arg = p + 1;
5267
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005268 return OK;
5269}
5270
5271/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 * Allocate a variable for a List and fill it from "*arg".
5273 * Return OK or FAIL.
5274 */
5275 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005276get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005278 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 int evaluate;
5280{
Bram Moolenaar33570922005-01-25 22:26:29 +00005281 list_T *l = NULL;
5282 typval_T tv;
5283 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284
5285 if (evaluate)
5286 {
5287 l = list_alloc();
5288 if (l == NULL)
5289 return FAIL;
5290 }
5291
5292 *arg = skipwhite(*arg + 1);
5293 while (**arg != ']' && **arg != NUL)
5294 {
5295 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5296 goto failret;
5297 if (evaluate)
5298 {
5299 item = listitem_alloc();
5300 if (item != NULL)
5301 {
5302 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005303 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 list_append(l, item);
5305 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005306 else
5307 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 }
5309
5310 if (**arg == ']')
5311 break;
5312 if (**arg != ',')
5313 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 goto failret;
5316 }
5317 *arg = skipwhite(*arg + 1);
5318 }
5319
5320 if (**arg != ']')
5321 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323failret:
5324 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005325 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 return FAIL;
5327 }
5328
5329 *arg = skipwhite(*arg + 1);
5330 if (evaluate)
5331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005332 rettv->v_type = VAR_LIST;
5333 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 ++l->lv_refcount;
5335 }
5336
5337 return OK;
5338}
5339
5340/*
5341 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005342 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005344 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345list_alloc()
5346{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005347 list_T *l;
5348
5349 l = (list_T *)alloc_clear(sizeof(list_T));
5350 if (l != NULL)
5351 {
5352 /* Prepend the list to the list of lists for garbage collection. */
5353 if (first_list != NULL)
5354 first_list->lv_used_prev = l;
5355 l->lv_used_prev = NULL;
5356 l->lv_used_next = first_list;
5357 first_list = l;
5358 }
5359 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360}
5361
5362/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005363 * Allocate an empty list for a return value.
5364 * Returns OK or FAIL.
5365 */
5366 static int
5367rettv_list_alloc(rettv)
5368 typval_T *rettv;
5369{
5370 list_T *l = list_alloc();
5371
5372 if (l == NULL)
5373 return FAIL;
5374
5375 rettv->vval.v_list = l;
5376 rettv->v_type = VAR_LIST;
5377 ++l->lv_refcount;
5378 return OK;
5379}
5380
5381/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 * Unreference a list: decrement the reference count and free it when it
5383 * becomes zero.
5384 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005385 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005389 if (l != NULL && --l->lv_refcount <= 0)
5390 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391}
5392
5393/*
5394 * Free a list, including all items it points to.
5395 * Ignores the reference count.
5396 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005397 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005398list_free(l, recurse)
5399 list_T *l;
5400 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401{
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005404 /* Remove the list from the list of lists for garbage collection. */
5405 if (l->lv_used_prev == NULL)
5406 first_list = l->lv_used_next;
5407 else
5408 l->lv_used_prev->lv_used_next = l->lv_used_next;
5409 if (l->lv_used_next != NULL)
5410 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5411
Bram Moolenaard9fba312005-06-26 22:34:35 +00005412 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005414 /* Remove the item before deleting it. */
5415 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005416 if (recurse || (item->li_tv.v_type != VAR_LIST
5417 && item->li_tv.v_type != VAR_DICT))
5418 clear_tv(&item->li_tv);
5419 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 }
5421 vim_free(l);
5422}
5423
5424/*
5425 * Allocate a list item.
5426 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428listitem_alloc()
5429{
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431}
5432
5433/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005434 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 */
5436 static void
5437listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005438 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 vim_free(item);
5442}
5443
5444/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005445 * Remove a list item from a List and free it. Also clears the value.
5446 */
5447 static void
5448listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 list_T *l;
5450 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005451{
5452 list_remove(l, item, item);
5453 listitem_free(item);
5454}
5455
5456/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 * Get the number of items in a list.
5458 */
5459 static long
5460list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005461 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 if (l == NULL)
5464 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005465 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466}
5467
5468/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005469 * Return TRUE when two lists have exactly the same values.
5470 */
5471 static int
5472list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 list_T *l1;
5474 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005475 int ic; /* ignore case for strings */
5476{
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005478
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005479 if (l1 == l2)
5480 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005481 if (list_len(l1) != list_len(l2))
5482 return FALSE;
5483
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005484 for (item1 = l1->lv_first, item2 = l2->lv_first;
5485 item1 != NULL && item2 != NULL;
5486 item1 = item1->li_next, item2 = item2->li_next)
5487 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5488 return FALSE;
5489 return item1 == NULL && item2 == NULL;
5490}
5491
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005492#if defined(FEAT_PYTHON) || defined(PROTO)
5493/*
5494 * Return the dictitem that an entry in a hashtable points to.
5495 */
5496 dictitem_T *
5497dict_lookup(hi)
5498 hashitem_T *hi;
5499{
5500 return HI2DI(hi);
5501}
5502#endif
5503
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005505 * Return TRUE when two dictionaries have exactly the same key/values.
5506 */
5507 static int
5508dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 dict_T *d1;
5510 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005511 int ic; /* ignore case for strings */
5512{
Bram Moolenaar33570922005-01-25 22:26:29 +00005513 hashitem_T *hi;
5514 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005515 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005516
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005517 if (d1 == d2)
5518 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005519 if (dict_len(d1) != dict_len(d2))
5520 return FALSE;
5521
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005522 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005524 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005525 if (!HASHITEM_EMPTY(hi))
5526 {
5527 item2 = dict_find(d2, hi->hi_key, -1);
5528 if (item2 == NULL)
5529 return FALSE;
5530 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5531 return FALSE;
5532 --todo;
5533 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005534 }
5535 return TRUE;
5536}
5537
5538/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005539 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005540 * Compares the items just like "==" would compare them, but strings and
5541 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005542 */
5543 static int
5544tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 typval_T *tv1;
5546 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005547 int ic; /* ignore case */
5548{
5549 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005550 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005551 static int recursive = 0; /* cach recursive loops */
5552 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005553
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005554 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005555 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005556 /* Catch lists and dicts that have an endless loop by limiting
5557 * recursiveness to 1000. We guess they are equal then. */
5558 if (recursive >= 1000)
5559 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005560
5561 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005562 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005563 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005564 ++recursive;
5565 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5566 --recursive;
5567 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005568
5569 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005570 ++recursive;
5571 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5572 --recursive;
5573 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005574
5575 case VAR_FUNC:
5576 return (tv1->vval.v_string != NULL
5577 && tv2->vval.v_string != NULL
5578 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5579
5580 case VAR_NUMBER:
5581 return tv1->vval.v_number == tv2->vval.v_number;
5582
5583 case VAR_STRING:
5584 s1 = get_tv_string_buf(tv1, buf1);
5585 s2 = get_tv_string_buf(tv2, buf2);
5586 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005587 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005588
5589 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005590 return TRUE;
5591}
5592
5593/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 * Locate item with index "n" in list "l" and return it.
5595 * A negative index is counted from the end; -1 is the last item.
5596 * Returns NULL when "n" is out of range.
5597 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005598 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 long n;
5602{
Bram Moolenaar33570922005-01-25 22:26:29 +00005603 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 long idx;
5605
5606 if (l == NULL)
5607 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005608
5609 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005611 n = l->lv_len + n;
5612
5613 /* Check for index out of range. */
5614 if (n < 0 || n >= l->lv_len)
5615 return NULL;
5616
5617 /* When there is a cached index may start search from there. */
5618 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005620 if (n < l->lv_idx / 2)
5621 {
5622 /* closest to the start of the list */
5623 item = l->lv_first;
5624 idx = 0;
5625 }
5626 else if (n > (l->lv_idx + l->lv_len) / 2)
5627 {
5628 /* closest to the end of the list */
5629 item = l->lv_last;
5630 idx = l->lv_len - 1;
5631 }
5632 else
5633 {
5634 /* closest to the cached index */
5635 item = l->lv_idx_item;
5636 idx = l->lv_idx;
5637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638 }
5639 else
5640 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005641 if (n < l->lv_len / 2)
5642 {
5643 /* closest to the start of the list */
5644 item = l->lv_first;
5645 idx = 0;
5646 }
5647 else
5648 {
5649 /* closest to the end of the list */
5650 item = l->lv_last;
5651 idx = l->lv_len - 1;
5652 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005654
5655 while (n > idx)
5656 {
5657 /* search forward */
5658 item = item->li_next;
5659 ++idx;
5660 }
5661 while (n < idx)
5662 {
5663 /* search backward */
5664 item = item->li_prev;
5665 --idx;
5666 }
5667
5668 /* cache the used index */
5669 l->lv_idx = idx;
5670 l->lv_idx_item = item;
5671
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672 return item;
5673}
5674
5675/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005676 * Get list item "l[idx]" as a number.
5677 */
5678 static long
5679list_find_nr(l, idx, errorp)
5680 list_T *l;
5681 long idx;
5682 int *errorp; /* set to TRUE when something wrong */
5683{
5684 listitem_T *li;
5685
5686 li = list_find(l, idx);
5687 if (li == NULL)
5688 {
5689 if (errorp != NULL)
5690 *errorp = TRUE;
5691 return -1L;
5692 }
5693 return get_tv_number_chk(&li->li_tv, errorp);
5694}
5695
5696/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005697 * Locate "item" list "l" and return its index.
5698 * Returns -1 when "item" is not in the list.
5699 */
5700 static long
5701list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 list_T *l;
5703 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005704{
5705 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005707
5708 if (l == NULL)
5709 return -1;
5710 idx = 0;
5711 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5712 ++idx;
5713 if (li == NULL)
5714 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005715 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005716}
5717
5718/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 * Append item "item" to the end of list "l".
5720 */
5721 static void
5722list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 list_T *l;
5724 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725{
5726 if (l->lv_last == NULL)
5727 {
5728 /* empty list */
5729 l->lv_first = item;
5730 l->lv_last = item;
5731 item->li_prev = NULL;
5732 }
5733 else
5734 {
5735 l->lv_last->li_next = item;
5736 item->li_prev = l->lv_last;
5737 l->lv_last = item;
5738 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005739 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 item->li_next = NULL;
5741}
5742
5743/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005744 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005745 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 */
5747 static int
5748list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *l;
5750 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005752 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753
Bram Moolenaar05159a02005-02-26 23:04:13 +00005754 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005756 copy_tv(tv, &li->li_tv);
5757 list_append(l, li);
5758 return OK;
5759}
5760
5761/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005762 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005763 * Return FAIL when out of memory.
5764 */
5765 int
5766list_append_dict(list, dict)
5767 list_T *list;
5768 dict_T *dict;
5769{
5770 listitem_T *li = listitem_alloc();
5771
5772 if (li == NULL)
5773 return FAIL;
5774 li->li_tv.v_type = VAR_DICT;
5775 li->li_tv.v_lock = 0;
5776 li->li_tv.vval.v_dict = dict;
5777 list_append(list, li);
5778 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 return OK;
5780}
5781
5782/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005783 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005784 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005785 * Returns FAIL when out of memory.
5786 */
5787 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005788list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005789 list_T *l;
5790 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005791 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005792{
5793 listitem_T *li = listitem_alloc();
5794
5795 if (li == NULL)
5796 return FAIL;
5797 list_append(l, li);
5798 li->li_tv.v_type = VAR_STRING;
5799 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005800 if (str == NULL)
5801 li->li_tv.vval.v_string = NULL;
5802 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005803 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005804 return FAIL;
5805 return OK;
5806}
5807
5808/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005809 * Append "n" to list "l".
5810 * Returns FAIL when out of memory.
5811 */
5812 static int
5813list_append_number(l, n)
5814 list_T *l;
5815 varnumber_T n;
5816{
5817 listitem_T *li;
5818
5819 li = listitem_alloc();
5820 if (li == NULL)
5821 return FAIL;
5822 li->li_tv.v_type = VAR_NUMBER;
5823 li->li_tv.v_lock = 0;
5824 li->li_tv.vval.v_number = n;
5825 list_append(l, li);
5826 return OK;
5827}
5828
5829/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005830 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005831 * If "item" is NULL append at the end.
5832 * Return FAIL when out of memory.
5833 */
5834 static int
5835list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005836 list_T *l;
5837 typval_T *tv;
5838 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005839{
Bram Moolenaar33570922005-01-25 22:26:29 +00005840 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005841
5842 if (ni == NULL)
5843 return FAIL;
5844 copy_tv(tv, &ni->li_tv);
5845 if (item == NULL)
5846 /* Append new item at end of list. */
5847 list_append(l, ni);
5848 else
5849 {
5850 /* Insert new item before existing item. */
5851 ni->li_prev = item->li_prev;
5852 ni->li_next = item;
5853 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005854 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005855 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005856 ++l->lv_idx;
5857 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005858 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005859 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005860 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005861 l->lv_idx_item = NULL;
5862 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005864 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865 }
5866 return OK;
5867}
5868
5869/*
5870 * Extend "l1" with "l2".
5871 * If "bef" is NULL append at the end, otherwise insert before this item.
5872 * Returns FAIL when out of memory.
5873 */
5874 static int
5875list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005876 list_T *l1;
5877 list_T *l2;
5878 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879{
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005881
5882 for (item = l2->lv_first; item != NULL; item = item->li_next)
5883 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5884 return FAIL;
5885 return OK;
5886}
5887
5888/*
5889 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5890 * Return FAIL when out of memory.
5891 */
5892 static int
5893list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 list_T *l1;
5895 list_T *l2;
5896 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005897{
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005899
5900 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005901 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005902 if (l == NULL)
5903 return FAIL;
5904 tv->v_type = VAR_LIST;
5905 tv->vval.v_list = l;
5906
5907 /* append all items from the second list */
5908 return list_extend(l, l2, NULL);
5909}
5910
5911/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005914 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 * Returns NULL when out of memory.
5916 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005918list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005919 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005921 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922{
Bram Moolenaar33570922005-01-25 22:26:29 +00005923 list_T *copy;
5924 listitem_T *item;
5925 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926
5927 if (orig == NULL)
5928 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929
5930 copy = list_alloc();
5931 if (copy != NULL)
5932 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005933 if (copyID != 0)
5934 {
5935 /* Do this before adding the items, because one of the items may
5936 * refer back to this list. */
5937 orig->lv_copyID = copyID;
5938 orig->lv_copylist = copy;
5939 }
5940 for (item = orig->lv_first; item != NULL && !got_int;
5941 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 {
5943 ni = listitem_alloc();
5944 if (ni == NULL)
5945 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005946 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005947 {
5948 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5949 {
5950 vim_free(ni);
5951 break;
5952 }
5953 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005955 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 list_append(copy, ni);
5957 }
5958 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005959 if (item != NULL)
5960 {
5961 list_unref(copy);
5962 copy = NULL;
5963 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 }
5965
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 return copy;
5967}
5968
5969/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005970 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005971 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005973 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005974list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 list_T *l;
5976 listitem_T *item;
5977 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978{
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005981 /* notify watchers */
5982 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005984 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005985 list_fix_watch(l, ip);
5986 if (ip == item2)
5987 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989
5990 if (item2->li_next == NULL)
5991 l->lv_last = item->li_prev;
5992 else
5993 item2->li_next->li_prev = item->li_prev;
5994 if (item->li_prev == NULL)
5995 l->lv_first = item2->li_next;
5996 else
5997 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005998 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999}
6000
6001/*
6002 * Return an allocated string with the string representation of a list.
6003 * May return NULL.
6004 */
6005 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006006list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006008 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009{
6010 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011
6012 if (tv->vval.v_list == NULL)
6013 return NULL;
6014 ga_init2(&ga, (int)sizeof(char), 80);
6015 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006016 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006017 {
6018 vim_free(ga.ga_data);
6019 return NULL;
6020 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 ga_append(&ga, ']');
6022 ga_append(&ga, NUL);
6023 return (char_u *)ga.ga_data;
6024}
6025
6026/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006027 * Join list "l" into a string in "*gap", using separator "sep".
6028 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006029 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006030 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006031 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006033 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006035 char_u *sep;
6036 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006037 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006038{
6039 int first = TRUE;
6040 char_u *tofree;
6041 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006043 char_u *s;
6044
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006045 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006046 {
6047 if (first)
6048 first = FALSE;
6049 else
6050 ga_concat(gap, sep);
6051
6052 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006053 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006055 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056 if (s != NULL)
6057 ga_concat(gap, s);
6058 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006059 if (s == NULL)
6060 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006061 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006062 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006063}
6064
6065/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006066 * Garbage collection for lists and dictionaries.
6067 *
6068 * We use reference counts to be able to free most items right away when they
6069 * are no longer used. But for composite items it's possible that it becomes
6070 * unused while the reference count is > 0: When there is a recursive
6071 * reference. Example:
6072 * :let l = [1, 2, 3]
6073 * :let d = {9: l}
6074 * :let l[1] = d
6075 *
6076 * Since this is quite unusual we handle this with garbage collection: every
6077 * once in a while find out which lists and dicts are not referenced from any
6078 * variable.
6079 *
6080 * Here is a good reference text about garbage collection (refers to Python
6081 * but it applies to all reference-counting mechanisms):
6082 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006083 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006084
6085/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006086 * Do garbage collection for lists and dicts.
6087 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006088 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006089 int
6090garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006091{
6092 dict_T *dd;
6093 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006094 int copyID = ++current_copyID;
6095 buf_T *buf;
6096 win_T *wp;
6097 int i;
6098 funccall_T *fc;
6099 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006100#ifdef FEAT_WINDOWS
6101 tabpage_T *tp;
6102#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006103
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006104 /* Only do this once. */
6105 want_garbage_collect = FALSE;
6106 may_garbage_collect = FALSE;
6107
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006108 /*
6109 * 1. Go through all accessible variables and mark all lists and dicts
6110 * with copyID.
6111 */
6112 /* script-local variables */
6113 for (i = 1; i <= ga_scripts.ga_len; ++i)
6114 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6115
6116 /* buffer-local variables */
6117 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6118 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6119
6120 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006121 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006122 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6123
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006124#ifdef FEAT_WINDOWS
6125 /* tabpage-local variables */
6126 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6127 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6128#endif
6129
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006130 /* global variables */
6131 set_ref_in_ht(&globvarht, copyID);
6132
6133 /* function-local variables */
6134 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6135 {
6136 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6137 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6138 }
6139
6140 /*
6141 * 2. Go through the list of dicts and free items without the copyID.
6142 */
6143 for (dd = first_dict; dd != NULL; )
6144 if (dd->dv_copyID != copyID)
6145 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006146 /* Free the Dictionary and ordinary items it contains, but don't
6147 * recurse into Lists and Dictionaries, they will be in the list
6148 * of dicts or list of lists. */
6149 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006150 did_free = TRUE;
6151
6152 /* restart, next dict may also have been freed */
6153 dd = first_dict;
6154 }
6155 else
6156 dd = dd->dv_used_next;
6157
6158 /*
6159 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006160 * But don't free a list that has a watcher (used in a for loop), these
6161 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006162 */
6163 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006164 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006165 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006166 /* Free the List and ordinary items it contains, but don't recurse
6167 * into Lists and Dictionaries, they will be in the list of dicts
6168 * or list of lists. */
6169 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006170 did_free = TRUE;
6171
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006172 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006173 ll = first_list;
6174 }
6175 else
6176 ll = ll->lv_used_next;
6177
6178 return did_free;
6179}
6180
6181/*
6182 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6183 */
6184 static void
6185set_ref_in_ht(ht, copyID)
6186 hashtab_T *ht;
6187 int copyID;
6188{
6189 int todo;
6190 hashitem_T *hi;
6191
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006192 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006193 for (hi = ht->ht_array; todo > 0; ++hi)
6194 if (!HASHITEM_EMPTY(hi))
6195 {
6196 --todo;
6197 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6198 }
6199}
6200
6201/*
6202 * Mark all lists and dicts referenced through list "l" with "copyID".
6203 */
6204 static void
6205set_ref_in_list(l, copyID)
6206 list_T *l;
6207 int copyID;
6208{
6209 listitem_T *li;
6210
6211 for (li = l->lv_first; li != NULL; li = li->li_next)
6212 set_ref_in_item(&li->li_tv, copyID);
6213}
6214
6215/*
6216 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6217 */
6218 static void
6219set_ref_in_item(tv, copyID)
6220 typval_T *tv;
6221 int copyID;
6222{
6223 dict_T *dd;
6224 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006225
6226 switch (tv->v_type)
6227 {
6228 case VAR_DICT:
6229 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006230 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006231 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006232 /* Didn't see this dict yet. */
6233 dd->dv_copyID = copyID;
6234 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006235 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006236 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006237
6238 case VAR_LIST:
6239 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006240 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006241 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006242 /* Didn't see this list yet. */
6243 ll->lv_copyID = copyID;
6244 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006245 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006246 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006247 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006248 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006249}
6250
6251/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006252 * Allocate an empty header for a dictionary.
6253 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006254 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255dict_alloc()
6256{
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006260 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006261 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006262 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006263 if (first_dict != NULL)
6264 first_dict->dv_used_prev = d;
6265 d->dv_used_next = first_dict;
6266 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006267 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006268
Bram Moolenaar33570922005-01-25 22:26:29 +00006269 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006270 d->dv_lock = 0;
6271 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006272 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006273 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006275}
6276
6277/*
6278 * Unreference a Dictionary: decrement the reference count and free it when it
6279 * becomes zero.
6280 */
6281 static void
6282dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006285 if (d != NULL && --d->dv_refcount <= 0)
6286 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006287}
6288
6289/*
6290 * Free a Dictionary, including all items it contains.
6291 * Ignores the reference count.
6292 */
6293 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006294dict_free(d, recurse)
6295 dict_T *d;
6296 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006297{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006300 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006301
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006302 /* Remove the dict from the list of dicts for garbage collection. */
6303 if (d->dv_used_prev == NULL)
6304 first_dict = d->dv_used_next;
6305 else
6306 d->dv_used_prev->dv_used_next = d->dv_used_next;
6307 if (d->dv_used_next != NULL)
6308 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6309
6310 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006311 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006312 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006315 if (!HASHITEM_EMPTY(hi))
6316 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006317 /* Remove the item before deleting it, just in case there is
6318 * something recursive causing trouble. */
6319 di = HI2DI(hi);
6320 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006321 if (recurse || (di->di_tv.v_type != VAR_LIST
6322 && di->di_tv.v_type != VAR_DICT))
6323 clear_tv(&di->di_tv);
6324 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 --todo;
6326 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006327 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006328 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006329 vim_free(d);
6330}
6331
6332/*
6333 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334 * The "key" is copied to the new item.
6335 * Note that the value of the item "di_tv" still needs to be initialized!
6336 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006339dictitem_alloc(key)
6340 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006341{
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006344 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006346 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006347 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006348 di->di_flags = 0;
6349 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006350 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351}
6352
6353/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006354 * Make a copy of a Dictionary item.
6355 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359{
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006361
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006362 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6363 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006364 if (di != NULL)
6365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006366 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006367 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006368 copy_tv(&org->di_tv, &di->di_tv);
6369 }
6370 return di;
6371}
6372
6373/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 * Remove item "item" from Dictionary "dict" and free it.
6375 */
6376 static void
6377dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 dict_T *dict;
6379 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006380{
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006382
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006384 if (HASHITEM_EMPTY(hi))
6385 EMSG2(_(e_intern2), "dictitem_remove()");
6386 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006388 dictitem_free(item);
6389}
6390
6391/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006392 * Free a dict item. Also clears the value.
6393 */
6394 static void
6395dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006397{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398 clear_tv(&item->di_tv);
6399 vim_free(item);
6400}
6401
6402/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006403 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6404 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006405 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006406 * Returns NULL when out of memory.
6407 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006409dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006411 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006412 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006413{
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 dict_T *copy;
6415 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006416 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006418
6419 if (orig == NULL)
6420 return NULL;
6421
6422 copy = dict_alloc();
6423 if (copy != NULL)
6424 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006425 if (copyID != 0)
6426 {
6427 orig->dv_copyID = copyID;
6428 orig->dv_copydict = copy;
6429 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006430 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006431 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006433 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006435 --todo;
6436
6437 di = dictitem_alloc(hi->hi_key);
6438 if (di == NULL)
6439 break;
6440 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006441 {
6442 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6443 copyID) == FAIL)
6444 {
6445 vim_free(di);
6446 break;
6447 }
6448 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006449 else
6450 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6451 if (dict_add(copy, di) == FAIL)
6452 {
6453 dictitem_free(di);
6454 break;
6455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006458
Bram Moolenaare9a41262005-01-15 22:18:47 +00006459 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006460 if (todo > 0)
6461 {
6462 dict_unref(copy);
6463 copy = NULL;
6464 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006465 }
6466
6467 return copy;
6468}
6469
6470/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006471 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006472 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006473 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006474 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006475dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 dict_T *d;
6477 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006478{
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006480}
6481
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006483 * Add a number or string entry to dictionary "d".
6484 * When "str" is NULL use number "nr", otherwise use "str".
6485 * Returns FAIL when out of memory and when key already exists.
6486 */
6487 int
6488dict_add_nr_str(d, key, nr, str)
6489 dict_T *d;
6490 char *key;
6491 long nr;
6492 char_u *str;
6493{
6494 dictitem_T *item;
6495
6496 item = dictitem_alloc((char_u *)key);
6497 if (item == NULL)
6498 return FAIL;
6499 item->di_tv.v_lock = 0;
6500 if (str == NULL)
6501 {
6502 item->di_tv.v_type = VAR_NUMBER;
6503 item->di_tv.vval.v_number = nr;
6504 }
6505 else
6506 {
6507 item->di_tv.v_type = VAR_STRING;
6508 item->di_tv.vval.v_string = vim_strsave(str);
6509 }
6510 if (dict_add(d, item) == FAIL)
6511 {
6512 dictitem_free(item);
6513 return FAIL;
6514 }
6515 return OK;
6516}
6517
6518/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006519 * Get the number of items in a Dictionary.
6520 */
6521 static long
6522dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525 if (d == NULL)
6526 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006527 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006528}
6529
6530/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006531 * Find item "key[len]" in Dictionary "d".
6532 * If "len" is negative use strlen(key).
6533 * Returns NULL when not found.
6534 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006535 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006536dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006537 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006538 char_u *key;
6539 int len;
6540{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006541#define AKEYLEN 200
6542 char_u buf[AKEYLEN];
6543 char_u *akey;
6544 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006546
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006547 if (len < 0)
6548 akey = key;
6549 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006551 tofree = akey = vim_strnsave(key, len);
6552 if (akey == NULL)
6553 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006554 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006555 else
6556 {
6557 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006558 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006559 akey = buf;
6560 }
6561
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006563 vim_free(tofree);
6564 if (HASHITEM_EMPTY(hi))
6565 return NULL;
6566 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006567}
6568
6569/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006570 * Get a string item from a dictionary.
6571 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006572 * Returns NULL if the entry doesn't exist or out of memory.
6573 */
6574 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006575get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006576 dict_T *d;
6577 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006578 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006579{
6580 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006581 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006582
6583 di = dict_find(d, key, -1);
6584 if (di == NULL)
6585 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006586 s = get_tv_string(&di->di_tv);
6587 if (save && s != NULL)
6588 s = vim_strsave(s);
6589 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006590}
6591
6592/*
6593 * Get a number item from a dictionary.
6594 * Returns 0 if the entry doesn't exist or out of memory.
6595 */
6596 long
6597get_dict_number(d, key)
6598 dict_T *d;
6599 char_u *key;
6600{
6601 dictitem_T *di;
6602
6603 di = dict_find(d, key, -1);
6604 if (di == NULL)
6605 return 0;
6606 return get_tv_number(&di->di_tv);
6607}
6608
6609/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006610 * Return an allocated string with the string representation of a Dictionary.
6611 * May return NULL.
6612 */
6613 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617{
6618 garray_T ga;
6619 int first = TRUE;
6620 char_u *tofree;
6621 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006622 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006623 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006625 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006626
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006627 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006628 return NULL;
6629 ga_init2(&ga, (int)sizeof(char), 80);
6630 ga_append(&ga, '{');
6631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006632 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006633 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006634 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006635 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006637 --todo;
6638
6639 if (first)
6640 first = FALSE;
6641 else
6642 ga_concat(&ga, (char_u *)", ");
6643
6644 tofree = string_quote(hi->hi_key, FALSE);
6645 if (tofree != NULL)
6646 {
6647 ga_concat(&ga, tofree);
6648 vim_free(tofree);
6649 }
6650 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006651 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006652 if (s != NULL)
6653 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006655 if (s == NULL)
6656 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006658 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 if (todo > 0)
6660 {
6661 vim_free(ga.ga_data);
6662 return NULL;
6663 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664
6665 ga_append(&ga, '}');
6666 ga_append(&ga, NUL);
6667 return (char_u *)ga.ga_data;
6668}
6669
6670/*
6671 * Allocate a variable for a Dictionary and fill it from "*arg".
6672 * Return OK or FAIL. Returns NOTDONE for {expr}.
6673 */
6674 static int
6675get_dict_tv(arg, rettv, evaluate)
6676 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006677 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678 int evaluate;
6679{
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 dict_T *d = NULL;
6681 typval_T tvkey;
6682 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006686 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687
6688 /*
6689 * First check if it's not a curly-braces thing: {expr}.
6690 * Must do this without evaluating, otherwise a function may be called
6691 * twice. Unfortunately this means we need to call eval1() twice for the
6692 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006693 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006694 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006695 if (*start != '}')
6696 {
6697 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6698 return FAIL;
6699 if (*start == '}')
6700 return NOTDONE;
6701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006702
6703 if (evaluate)
6704 {
6705 d = dict_alloc();
6706 if (d == NULL)
6707 return FAIL;
6708 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006709 tvkey.v_type = VAR_UNKNOWN;
6710 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711
6712 *arg = skipwhite(*arg + 1);
6713 while (**arg != '}' && **arg != NUL)
6714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006715 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006716 goto failret;
6717 if (**arg != ':')
6718 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006719 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006720 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006721 goto failret;
6722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006723 key = get_tv_string_buf_chk(&tvkey, buf);
6724 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006726 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6727 if (key != NULL)
6728 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006729 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006730 goto failret;
6731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006732
6733 *arg = skipwhite(*arg + 1);
6734 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6735 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006736 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006737 goto failret;
6738 }
6739 if (evaluate)
6740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006741 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006742 if (item != NULL)
6743 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006744 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006745 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006746 clear_tv(&tv);
6747 goto failret;
6748 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006749 item = dictitem_alloc(key);
6750 clear_tv(&tvkey);
6751 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006753 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006754 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006755 if (dict_add(d, item) == FAIL)
6756 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006757 }
6758 }
6759
6760 if (**arg == '}')
6761 break;
6762 if (**arg != ',')
6763 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006764 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765 goto failret;
6766 }
6767 *arg = skipwhite(*arg + 1);
6768 }
6769
6770 if (**arg != '}')
6771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006772 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006773failret:
6774 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006775 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006776 return FAIL;
6777 }
6778
6779 *arg = skipwhite(*arg + 1);
6780 if (evaluate)
6781 {
6782 rettv->v_type = VAR_DICT;
6783 rettv->vval.v_dict = d;
6784 ++d->dv_refcount;
6785 }
6786
6787 return OK;
6788}
6789
Bram Moolenaar8c711452005-01-14 21:53:12 +00006790/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 * Return a string with the string representation of a variable.
6792 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006793 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006795 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006796 * May return NULL;
6797 */
6798 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006799echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006800 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006802 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006803 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006804{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805 static int recurse = 0;
6806 char_u *r = NULL;
6807
Bram Moolenaar33570922005-01-25 22:26:29 +00006808 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006810 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006811 *tofree = NULL;
6812 return NULL;
6813 }
6814 ++recurse;
6815
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006816 switch (tv->v_type)
6817 {
6818 case VAR_FUNC:
6819 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006820 r = tv->vval.v_string;
6821 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006823 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006824 if (tv->vval.v_list == NULL)
6825 {
6826 *tofree = NULL;
6827 r = NULL;
6828 }
6829 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6830 {
6831 *tofree = NULL;
6832 r = (char_u *)"[...]";
6833 }
6834 else
6835 {
6836 tv->vval.v_list->lv_copyID = copyID;
6837 *tofree = list2string(tv, copyID);
6838 r = *tofree;
6839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006840 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006841
Bram Moolenaar8c711452005-01-14 21:53:12 +00006842 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006843 if (tv->vval.v_dict == NULL)
6844 {
6845 *tofree = NULL;
6846 r = NULL;
6847 }
6848 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6849 {
6850 *tofree = NULL;
6851 r = (char_u *)"{...}";
6852 }
6853 else
6854 {
6855 tv->vval.v_dict->dv_copyID = copyID;
6856 *tofree = dict2string(tv, copyID);
6857 r = *tofree;
6858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006861 case VAR_STRING:
6862 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006863 *tofree = NULL;
6864 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006865 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006866
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006867 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006868 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006869 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871
6872 --recurse;
6873 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874}
6875
6876/*
6877 * Return a string with the string representation of a variable.
6878 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6879 * "numbuf" is used for a number.
6880 * Puts quotes around strings, so that they can be parsed back by eval().
6881 * May return NULL;
6882 */
6883 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006884tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006885 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006886 char_u **tofree;
6887 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006888 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006889{
6890 switch (tv->v_type)
6891 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006892 case VAR_FUNC:
6893 *tofree = string_quote(tv->vval.v_string, TRUE);
6894 return *tofree;
6895 case VAR_STRING:
6896 *tofree = string_quote(tv->vval.v_string, FALSE);
6897 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006899 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006900 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006903 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006904 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006905 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006906}
6907
6908/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006909 * Return string "str" in ' quotes, doubling ' characters.
6910 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006911 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006912 */
6913 static char_u *
6914string_quote(str, function)
6915 char_u *str;
6916 int function;
6917{
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006919 char_u *p, *r, *s;
6920
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 len = (function ? 13 : 3);
6922 if (str != NULL)
6923 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006924 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 for (p = str; *p != NUL; mb_ptr_adv(p))
6926 if (*p == '\'')
6927 ++len;
6928 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006929 s = r = alloc(len);
6930 if (r != NULL)
6931 {
6932 if (function)
6933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006935 r += 10;
6936 }
6937 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006938 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 if (str != NULL)
6940 for (p = str; *p != NUL; )
6941 {
6942 if (*p == '\'')
6943 *r++ = '\'';
6944 MB_COPY_CHAR(p, r);
6945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006946 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006947 if (function)
6948 *r++ = ')';
6949 *r++ = NUL;
6950 }
6951 return s;
6952}
6953
6954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 * Get the value of an environment variable.
6956 * "arg" is pointing to the '$'. It is advanced to after the name.
6957 * If the environment variable was not set, silently assume it is empty.
6958 * Always return OK.
6959 */
6960 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006961get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 int evaluate;
6965{
6966 char_u *string = NULL;
6967 int len;
6968 int cc;
6969 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006970 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
6972 ++*arg;
6973 name = *arg;
6974 len = get_env_len(arg);
6975 if (evaluate)
6976 {
6977 if (len != 0)
6978 {
6979 cc = name[len];
6980 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006981 /* first try vim_getenv(), fast for normal environment vars */
6982 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006984 {
6985 if (!mustfree)
6986 string = vim_strsave(string);
6987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 else
6989 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006990 if (mustfree)
6991 vim_free(string);
6992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 /* next try expanding things like $VIM and ${HOME} */
6994 string = expand_env_save(name - 1);
6995 if (string != NULL && *string == '$')
6996 {
6997 vim_free(string);
6998 string = NULL;
6999 }
7000 }
7001 name[len] = cc;
7002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007003 rettv->v_type = VAR_STRING;
7004 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 }
7006
7007 return OK;
7008}
7009
7010/*
7011 * Array with names and number of arguments of all internal functions
7012 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7013 */
7014static struct fst
7015{
7016 char *f_name; /* function name */
7017 char f_min_argc; /* minimal number of arguments */
7018 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007019 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007020 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021} functions[] =
7022{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007023 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {"append", 2, 2, f_append},
7025 {"argc", 0, 0, f_argc},
7026 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007027 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007029 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 {"bufexists", 1, 1, f_bufexists},
7031 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7032 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7033 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7034 {"buflisted", 1, 1, f_buflisted},
7035 {"bufloaded", 1, 1, f_bufloaded},
7036 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007037 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 {"bufwinnr", 1, 1, f_bufwinnr},
7039 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007040 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007041 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007042 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {"char2nr", 1, 1, f_char2nr},
7044 {"cindent", 1, 1, f_cindent},
7045 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007046#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007047 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007048 {"complete_add", 1, 1, f_complete_add},
7049 {"complete_check", 0, 0, f_complete_check},
7050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007052 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007053 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007055 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007056 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"delete", 1, 1, f_delete},
7058 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007059 {"diff_filler", 1, 1, f_diff_filler},
7060 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007061 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007063 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {"eventhandler", 0, 0, f_eventhandler},
7065 {"executable", 1, 1, f_executable},
7066 {"exists", 1, 1, f_exists},
7067 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007068 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007069 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7071 {"filereadable", 1, 1, f_filereadable},
7072 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007073 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007074 {"finddir", 1, 3, f_finddir},
7075 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"fnamemodify", 2, 2, f_fnamemodify},
7077 {"foldclosed", 1, 1, f_foldclosed},
7078 {"foldclosedend", 1, 1, f_foldclosedend},
7079 {"foldlevel", 1, 1, f_foldlevel},
7080 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007081 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007083 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007085 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007086 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"getbufvar", 2, 2, f_getbufvar},
7088 {"getchar", 0, 1, f_getchar},
7089 {"getcharmod", 0, 0, f_getcharmod},
7090 {"getcmdline", 0, 0, f_getcmdline},
7091 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007092 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007094 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007095 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {"getfsize", 1, 1, f_getfsize},
7097 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007098 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007099 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007100 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007101 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007102 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007103 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007105 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 {"getwinposx", 0, 0, f_getwinposx},
7107 {"getwinposy", 0, 0, f_getwinposy},
7108 {"getwinvar", 2, 2, f_getwinvar},
7109 {"glob", 1, 1, f_glob},
7110 {"globpath", 2, 2, f_globpath},
7111 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007113 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7115 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7116 {"histadd", 2, 2, f_histadd},
7117 {"histdel", 1, 2, f_histdel},
7118 {"histget", 1, 2, f_histget},
7119 {"histnr", 1, 1, f_histnr},
7120 {"hlID", 1, 1, f_hlID},
7121 {"hlexists", 1, 1, f_hlexists},
7122 {"hostname", 0, 0, f_hostname},
7123 {"iconv", 3, 3, f_iconv},
7124 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007125 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007126 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007128 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {"inputrestore", 0, 0, f_inputrestore},
7130 {"inputsave", 0, 0, f_inputsave},
7131 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007132 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007134 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007136 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007139 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {"libcall", 3, 3, f_libcall},
7141 {"libcallnr", 3, 3, f_libcallnr},
7142 {"line", 1, 1, f_line},
7143 {"line2byte", 1, 1, f_line2byte},
7144 {"lispindent", 1, 1, f_lispindent},
7145 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007146 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007147 {"maparg", 1, 3, f_maparg},
7148 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007149 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007150 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007151 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007152 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007153 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007154 {"max", 1, 1, f_max},
7155 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007156#ifdef vim_mkdir
7157 {"mkdir", 1, 3, f_mkdir},
7158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {"mode", 0, 0, f_mode},
7160 {"nextnonblank", 1, 1, f_nextnonblank},
7161 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007162 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007164 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007165 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007167 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007168 {"reltime", 0, 2, f_reltime},
7169 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {"remote_expr", 2, 3, f_remote_expr},
7171 {"remote_foreground", 1, 1, f_remote_foreground},
7172 {"remote_peek", 1, 2, f_remote_peek},
7173 {"remote_read", 1, 1, f_remote_read},
7174 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007175 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007177 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007179 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007180 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007181 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007182 {"searchpair", 3, 6, f_searchpair},
7183 {"searchpairpos", 3, 6, f_searchpairpos},
7184 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 {"server2client", 2, 2, f_server2client},
7186 {"serverlist", 0, 0, f_serverlist},
7187 {"setbufvar", 3, 3, f_setbufvar},
7188 {"setcmdpos", 1, 1, f_setcmdpos},
7189 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007190 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007191 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007192 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007194 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007196 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007198 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007199 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007200 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007201 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007202 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007203 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204#ifdef HAVE_STRFTIME
7205 {"strftime", 1, 2, f_strftime},
7206#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007208 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {"strlen", 1, 1, f_strlen},
7210 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007211 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {"strtrans", 1, 1, f_strtrans},
7213 {"submatch", 1, 1, f_submatch},
7214 {"substitute", 4, 4, f_substitute},
7215 {"synID", 3, 3, f_synID},
7216 {"synIDattr", 2, 3, f_synIDattr},
7217 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007218 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007219 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007220 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007221 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007222 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007223 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007225 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {"tolower", 1, 1, f_tolower},
7227 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007228 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"virtcol", 1, 1, f_virtcol},
7232 {"visualmode", 0, 1, f_visualmode},
7233 {"winbufnr", 1, 1, f_winbufnr},
7234 {"wincol", 0, 0, f_wincol},
7235 {"winheight", 1, 1, f_winheight},
7236 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007237 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007239 {"winrestview", 1, 1, f_winrestview},
7240 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007242 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243};
7244
7245#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7246
7247/*
7248 * Function given to ExpandGeneric() to obtain the list of internal
7249 * or user defined function names.
7250 */
7251 char_u *
7252get_function_name(xp, idx)
7253 expand_T *xp;
7254 int idx;
7255{
7256 static int intidx = -1;
7257 char_u *name;
7258
7259 if (idx == 0)
7260 intidx = -1;
7261 if (intidx < 0)
7262 {
7263 name = get_user_func_name(xp, idx);
7264 if (name != NULL)
7265 return name;
7266 }
7267 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7268 {
7269 STRCPY(IObuff, functions[intidx].f_name);
7270 STRCAT(IObuff, "(");
7271 if (functions[intidx].f_max_argc == 0)
7272 STRCAT(IObuff, ")");
7273 return IObuff;
7274 }
7275
7276 return NULL;
7277}
7278
7279/*
7280 * Function given to ExpandGeneric() to obtain the list of internal or
7281 * user defined variable or function names.
7282 */
7283/*ARGSUSED*/
7284 char_u *
7285get_expr_name(xp, idx)
7286 expand_T *xp;
7287 int idx;
7288{
7289 static int intidx = -1;
7290 char_u *name;
7291
7292 if (idx == 0)
7293 intidx = -1;
7294 if (intidx < 0)
7295 {
7296 name = get_function_name(xp, idx);
7297 if (name != NULL)
7298 return name;
7299 }
7300 return get_user_var_name(xp, ++intidx);
7301}
7302
7303#endif /* FEAT_CMDL_COMPL */
7304
7305/*
7306 * Find internal function in table above.
7307 * Return index, or -1 if not found
7308 */
7309 static int
7310find_internal_func(name)
7311 char_u *name; /* name of the function */
7312{
7313 int first = 0;
7314 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7315 int cmp;
7316 int x;
7317
7318 /*
7319 * Find the function name in the table. Binary search.
7320 */
7321 while (first <= last)
7322 {
7323 x = first + ((unsigned)(last - first) >> 1);
7324 cmp = STRCMP(name, functions[x].f_name);
7325 if (cmp < 0)
7326 last = x - 1;
7327 else if (cmp > 0)
7328 first = x + 1;
7329 else
7330 return x;
7331 }
7332 return -1;
7333}
7334
7335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007336 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7337 * name it contains, otherwise return "name".
7338 */
7339 static char_u *
7340deref_func_name(name, lenp)
7341 char_u *name;
7342 int *lenp;
7343{
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 int cc;
7346
7347 cc = name[*lenp];
7348 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007349 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007350 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007352 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007354 {
7355 *lenp = 0;
7356 return (char_u *)""; /* just in case */
7357 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007358 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007360 }
7361
7362 return name;
7363}
7364
7365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 * Allocate a variable for the result of a function.
7367 * Return OK or FAIL.
7368 */
7369 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7371 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 char_u *name; /* name of the function */
7373 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 char_u **arg; /* argument, pointing to the '(' */
7376 linenr_T firstline; /* first line of range */
7377 linenr_T lastline; /* last line of range */
7378 int *doesrange; /* return: function handled range */
7379 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
7382 char_u *argp;
7383 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007384 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 int argcount = 0; /* number of arguments found */
7386
7387 /*
7388 * Get the arguments.
7389 */
7390 argp = *arg;
7391 while (argcount < MAX_FUNC_ARGS)
7392 {
7393 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7394 if (*argp == ')' || *argp == ',' || *argp == NUL)
7395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7397 {
7398 ret = FAIL;
7399 break;
7400 }
7401 ++argcount;
7402 if (*argp != ',')
7403 break;
7404 }
7405 if (*argp == ')')
7406 ++argp;
7407 else
7408 ret = FAIL;
7409
7410 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007411 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 {
7415 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007416 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007418 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
7421 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007422 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
7424 *arg = skipwhite(argp);
7425 return ret;
7426}
7427
7428
7429/*
7430 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007431 * Return OK when the function can't be called, FAIL otherwise.
7432 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 */
7434 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007435call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007436 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 char_u *name; /* name of the function */
7438 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007441 typval_T *argvars; /* vars for arguments, must have "argcount"
7442 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 linenr_T firstline; /* first line of range */
7444 linenr_T lastline; /* last line of range */
7445 int *doesrange; /* return: function handled range */
7446 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450#define ERROR_UNKNOWN 0
7451#define ERROR_TOOMANY 1
7452#define ERROR_TOOFEW 2
7453#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007454#define ERROR_DICT 4
7455#define ERROR_NONE 5
7456#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 int error = ERROR_NONE;
7458 int i;
7459 int llen;
7460 ufunc_T *fp;
7461 int cc;
7462#define FLEN_FIXED 40
7463 char_u fname_buf[FLEN_FIXED + 1];
7464 char_u *fname;
7465
7466 /*
7467 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7468 * Change <SNR>123_name() to K_SNR 123_name().
7469 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7470 */
7471 cc = name[len];
7472 name[len] = NUL;
7473 llen = eval_fname_script(name);
7474 if (llen > 0)
7475 {
7476 fname_buf[0] = K_SPECIAL;
7477 fname_buf[1] = KS_EXTRA;
7478 fname_buf[2] = (int)KE_SNR;
7479 i = 3;
7480 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7481 {
7482 if (current_SID <= 0)
7483 error = ERROR_SCRIPT;
7484 else
7485 {
7486 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7487 i = (int)STRLEN(fname_buf);
7488 }
7489 }
7490 if (i + STRLEN(name + llen) < FLEN_FIXED)
7491 {
7492 STRCPY(fname_buf + i, name + llen);
7493 fname = fname_buf;
7494 }
7495 else
7496 {
7497 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7498 if (fname == NULL)
7499 error = ERROR_OTHER;
7500 else
7501 {
7502 mch_memmove(fname, fname_buf, (size_t)i);
7503 STRCPY(fname + i, name + llen);
7504 }
7505 }
7506 }
7507 else
7508 fname = name;
7509
7510 *doesrange = FALSE;
7511
7512
7513 /* execute the function if no errors detected and executing */
7514 if (evaluate && error == ERROR_NONE)
7515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 error = ERROR_UNKNOWN;
7518
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007519 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 {
7521 /*
7522 * User defined function.
7523 */
7524 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007527 /* Trigger FuncUndefined event, may load the function. */
7528 if (fp == NULL
7529 && apply_autocmds(EVENT_FUNCUNDEFINED,
7530 fname, fname, TRUE, NULL)
7531 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007533 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 fp = find_func(fname);
7535 }
7536#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007537 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007538 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007539 {
7540 /* loaded a package, search for the function again */
7541 fp = find_func(fname);
7542 }
7543
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (fp != NULL)
7545 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007546 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007548 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007550 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007552 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007553 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 else
7555 {
7556 /*
7557 * Call the user function.
7558 * Save and restore search patterns, script variables and
7559 * redo buffer.
7560 */
7561 save_search_patterns();
7562 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007563 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007566 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7567 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7568 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007569 /* Function was unreferenced while being used, free it
7570 * now. */
7571 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 restoreRedobuff();
7573 restore_search_patterns();
7574 error = ERROR_NONE;
7575 }
7576 }
7577 }
7578 else
7579 {
7580 /*
7581 * Find the function name in the table, call its implementation.
7582 */
7583 i = find_internal_func(fname);
7584 if (i >= 0)
7585 {
7586 if (argcount < functions[i].f_min_argc)
7587 error = ERROR_TOOFEW;
7588 else if (argcount > functions[i].f_max_argc)
7589 error = ERROR_TOOMANY;
7590 else
7591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007592 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 error = ERROR_NONE;
7595 }
7596 }
7597 }
7598 /*
7599 * The function call (or "FuncUndefined" autocommand sequence) might
7600 * have been aborted by an error, an interrupt, or an explicitly thrown
7601 * exception that has not been caught so far. This situation can be
7602 * tested for by calling aborting(). For an error in an internal
7603 * function or for the "E132" error in call_user_func(), however, the
7604 * throw point at which the "force_abort" flag (temporarily reset by
7605 * emsg()) is normally updated has not been reached yet. We need to
7606 * update that flag first to make aborting() reliable.
7607 */
7608 update_force_abort();
7609 }
7610 if (error == ERROR_NONE)
7611 ret = OK;
7612
7613 /*
7614 * Report an error unless the argument evaluation or function call has been
7615 * cancelled due to an aborting error, an interrupt, or an exception.
7616 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007617 if (!aborting())
7618 {
7619 switch (error)
7620 {
7621 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007622 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 break;
7624 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 break;
7627 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007628 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 name);
7630 break;
7631 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007632 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 name);
7634 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007636 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 name);
7638 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 }
7640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641
7642 name[len] = cc;
7643 if (fname != name && fname != fname_buf)
7644 vim_free(fname);
7645
7646 return ret;
7647}
7648
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007649/*
7650 * Give an error message with a function name. Handle <SNR> things.
7651 */
7652 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007653emsg_funcname(ermsg, name)
7654 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007655 char_u *name;
7656{
7657 char_u *p;
7658
7659 if (*name == K_SPECIAL)
7660 p = concat_str((char_u *)"<SNR>", name + 3);
7661 else
7662 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007663 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 if (p != name)
7665 vim_free(p);
7666}
7667
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668/*********************************************
7669 * Implementation of the built-in functions
7670 */
7671
7672/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007673 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 */
7675 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007676f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007677 typval_T *argvars;
7678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007685 if ((l = argvars[0].vval.v_list) != NULL
7686 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7687 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007689 }
7690 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007691 EMSG(_(e_listreq));
7692}
7693
7694/*
7695 * "append(lnum, string/list)" function
7696 */
7697 static void
7698f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 typval_T *argvars;
7700 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007701{
7702 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007703 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 list_T *l = NULL;
7705 listitem_T *li = NULL;
7706 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007707 long added = 0;
7708
Bram Moolenaar0d660222005-01-07 21:51:51 +00007709 lnum = get_tv_lnum(argvars);
7710 if (lnum >= 0
7711 && lnum <= curbuf->b_ml.ml_line_count
7712 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007714 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007715 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007716 l = argvars[1].vval.v_list;
7717 if (l == NULL)
7718 return;
7719 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007720 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007721 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007722 for (;;)
7723 {
7724 if (l == NULL)
7725 tv = &argvars[1]; /* append a string */
7726 else if (li == NULL)
7727 break; /* end of list */
7728 else
7729 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007730 line = get_tv_string_chk(tv);
7731 if (line == NULL) /* type error */
7732 {
7733 rettv->vval.v_number = 1; /* Failed */
7734 break;
7735 }
7736 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007737 ++added;
7738 if (l == NULL)
7739 break;
7740 li = li->li_next;
7741 }
7742
7743 appended_lines_mark(lnum, added);
7744 if (curwin->w_cursor.lnum > lnum)
7745 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007747 else
7748 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749}
7750
7751/*
7752 * "argc()" function
7753 */
7754/* ARGSUSED */
7755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007756f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 typval_T *argvars;
7758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761}
7762
7763/*
7764 * "argidx()" function
7765 */
7766/* ARGSUSED */
7767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007769 typval_T *argvars;
7770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007772 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
7775/*
7776 * "argv(nr)" function
7777 */
7778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007779f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 typval_T *argvars;
7781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 int idx;
7784
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007785 if (argvars[0].v_type != VAR_UNKNOWN)
7786 {
7787 idx = get_tv_number_chk(&argvars[0], NULL);
7788 if (idx >= 0 && idx < ARGCOUNT)
7789 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7790 else
7791 rettv->vval.v_string = NULL;
7792 rettv->v_type = VAR_STRING;
7793 }
7794 else if (rettv_list_alloc(rettv) == OK)
7795 for (idx = 0; idx < ARGCOUNT; ++idx)
7796 list_append_string(rettv->vval.v_list,
7797 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
7800/*
7801 * "browse(save, title, initdir, default)" function
7802 */
7803/* ARGSUSED */
7804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 typval_T *argvars;
7807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
7809#ifdef FEAT_BROWSE
7810 int save;
7811 char_u *title;
7812 char_u *initdir;
7813 char_u *defname;
7814 char_u buf[NUMBUFLEN];
7815 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007816 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007818 save = get_tv_number_chk(&argvars[0], &error);
7819 title = get_tv_string_chk(&argvars[1]);
7820 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7821 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007823 if (error || title == NULL || initdir == NULL || defname == NULL)
7824 rettv->vval.v_string = NULL;
7825 else
7826 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007827 do_browse(save ? BROWSE_SAVE : 0,
7828 title, defname, NULL, initdir, NULL, curbuf);
7829#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007831#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007832 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007833}
7834
7835/*
7836 * "browsedir(title, initdir)" function
7837 */
7838/* ARGSUSED */
7839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007841 typval_T *argvars;
7842 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007843{
7844#ifdef FEAT_BROWSE
7845 char_u *title;
7846 char_u *initdir;
7847 char_u buf[NUMBUFLEN];
7848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007849 title = get_tv_string_chk(&argvars[0]);
7850 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 if (title == NULL || initdir == NULL)
7853 rettv->vval.v_string = NULL;
7854 else
7855 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007856 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861}
7862
Bram Moolenaar33570922005-01-25 22:26:29 +00007863static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007864
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865/*
7866 * Find a buffer by number or exact name.
7867 */
7868 static buf_T *
7869find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
7872 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007874 if (avar->v_type == VAR_NUMBER)
7875 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007876 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007879 if (buf == NULL)
7880 {
7881 /* No full path name match, try a match with a URL or a "nofile"
7882 * buffer, these don't use the full path. */
7883 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7884 if (buf->b_fname != NULL
7885 && (path_with_url(buf->b_fname)
7886#ifdef FEAT_QUICKFIX
7887 || bt_nofile(buf)
7888#endif
7889 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007890 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007891 break;
7892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894 return buf;
7895}
7896
7897/*
7898 * "bufexists(expr)" function
7899 */
7900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 typval_T *argvars;
7903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007905 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906}
7907
7908/*
7909 * "buflisted(expr)" function
7910 */
7911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007912f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 typval_T *argvars;
7914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 buf_T *buf;
7917
7918 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007919 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920}
7921
7922/*
7923 * "bufloaded(expr)" function
7924 */
7925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007927 typval_T *argvars;
7928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 buf_T *buf;
7931
7932 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934}
7935
Bram Moolenaar33570922005-01-25 22:26:29 +00007936static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007937
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938/*
7939 * Get buffer by number or pattern.
7940 */
7941 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007945 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 int save_magic;
7947 char_u *save_cpo;
7948 buf_T *buf;
7949
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007950 if (tv->v_type == VAR_NUMBER)
7951 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007952 if (tv->v_type != VAR_STRING)
7953 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 if (name == NULL || *name == NUL)
7955 return curbuf;
7956 if (name[0] == '$' && name[1] == NUL)
7957 return lastbuf;
7958
7959 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7960 save_magic = p_magic;
7961 p_magic = TRUE;
7962 save_cpo = p_cpo;
7963 p_cpo = (char_u *)"";
7964
7965 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7966 TRUE, FALSE));
7967
7968 p_magic = save_magic;
7969 p_cpo = save_cpo;
7970
7971 /* If not found, try expanding the name, like done for bufexists(). */
7972 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974
7975 return buf;
7976}
7977
7978/*
7979 * "bufname(expr)" function
7980 */
7981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007982f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007983 typval_T *argvars;
7984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 buf_T *buf;
7987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007988 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007990 buf = get_buf_tv(&argvars[0]);
7991 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 --emsg_off;
7997}
7998
7999/*
8000 * "bufnr(expr)" function
8001 */
8002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 typval_T *argvars;
8005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
8007 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008008 int error = FALSE;
8009 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008011 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008014 --emsg_off;
8015
8016 /* If the buffer isn't found and the second argument is not zero create a
8017 * new buffer. */
8018 if (buf == NULL
8019 && argvars[1].v_type != VAR_UNKNOWN
8020 && get_tv_number_chk(&argvars[1], &error) != 0
8021 && !error
8022 && (name = get_tv_string_chk(&argvars[0])) != NULL
8023 && !error)
8024 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8025
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
8031
8032/*
8033 * "bufwinnr(nr)" function
8034 */
8035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008037 typval_T *argvars;
8038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039{
8040#ifdef FEAT_WINDOWS
8041 win_T *wp;
8042 int winnr = 0;
8043#endif
8044 buf_T *buf;
8045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049#ifdef FEAT_WINDOWS
8050 for (wp = firstwin; wp; wp = wp->w_next)
8051 {
8052 ++winnr;
8053 if (wp->w_buffer == buf)
8054 break;
8055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008058 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059#endif
8060 --emsg_off;
8061}
8062
8063/*
8064 * "byte2line(byte)" function
8065 */
8066/*ARGSUSED*/
8067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008069 typval_T *argvars;
8070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074#else
8075 long boff = 0;
8076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008077 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 (linenr_T)0, &boff);
8083#endif
8084}
8085
8086/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008087 * "byteidx()" function
8088 */
8089/*ARGSUSED*/
8090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008091f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008092 typval_T *argvars;
8093 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008094{
8095#ifdef FEAT_MBYTE
8096 char_u *t;
8097#endif
8098 char_u *str;
8099 long idx;
8100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008101 str = get_tv_string_chk(&argvars[0]);
8102 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008103 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008104 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008105 return;
8106
8107#ifdef FEAT_MBYTE
8108 t = str;
8109 for ( ; idx > 0; idx--)
8110 {
8111 if (*t == NUL) /* EOL reached */
8112 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008113 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008114 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008115 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008116#else
8117 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008119#endif
8120}
8121
8122/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008123 * "call(func, arglist)" function
8124 */
8125 static void
8126f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 typval_T *argvars;
8128 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008129{
8130 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008131 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008132 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008134 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008136
8137 rettv->vval.v_number = 0;
8138 if (argvars[1].v_type != VAR_LIST)
8139 {
8140 EMSG(_(e_listreq));
8141 return;
8142 }
8143 if (argvars[1].vval.v_list == NULL)
8144 return;
8145
8146 if (argvars[0].v_type == VAR_FUNC)
8147 func = argvars[0].vval.v_string;
8148 else
8149 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008150 if (*func == NUL)
8151 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008152
Bram Moolenaare9a41262005-01-15 22:18:47 +00008153 if (argvars[2].v_type != VAR_UNKNOWN)
8154 {
8155 if (argvars[2].v_type != VAR_DICT)
8156 {
8157 EMSG(_(e_dictreq));
8158 return;
8159 }
8160 selfdict = argvars[2].vval.v_dict;
8161 }
8162
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008163 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8164 item = item->li_next)
8165 {
8166 if (argc == MAX_FUNC_ARGS)
8167 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008168 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008169 break;
8170 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008171 /* Make a copy of each argument. This is needed to be able to set
8172 * v_lock to VAR_FIXED in the copy without changing the original list.
8173 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008174 copy_tv(&item->li_tv, &argv[argc++]);
8175 }
8176
8177 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008178 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008179 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8180 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008181
8182 /* Free the arguments. */
8183 while (argc > 0)
8184 clear_tv(&argv[--argc]);
8185}
8186
8187/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008188 * "changenr()" function
8189 */
8190/*ARGSUSED*/
8191 static void
8192f_changenr(argvars, rettv)
8193 typval_T *argvars;
8194 typval_T *rettv;
8195{
8196 rettv->vval.v_number = curbuf->b_u_seq_cur;
8197}
8198
8199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 * "char2nr(string)" function
8201 */
8202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 typval_T *argvars;
8205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207#ifdef FEAT_MBYTE
8208 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008209 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 else
8211#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008212 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213}
8214
8215/*
8216 * "cindent(lnum)" function
8217 */
8218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008220 typval_T *argvars;
8221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223#ifdef FEAT_CINDENT
8224 pos_T pos;
8225 linenr_T lnum;
8226
8227 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008228 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8230 {
8231 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 curwin->w_cursor = pos;
8234 }
8235 else
8236#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238}
8239
8240/*
8241 * "col(string)" function
8242 */
8243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008245 typval_T *argvars;
8246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
8248 colnr_T col = 0;
8249 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008250 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008252 fp = var2fpos(&argvars[0], FALSE, &fnum);
8253 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {
8255 if (fp->col == MAXCOL)
8256 {
8257 /* '> can be MAXCOL, get the length of the line then */
8258 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008259 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 else
8261 col = MAXCOL;
8262 }
8263 else
8264 {
8265 col = fp->col + 1;
8266#ifdef FEAT_VIRTUALEDIT
8267 /* col(".") when the cursor is on the NUL at the end of the line
8268 * because of "coladd" can be seen as an extra column. */
8269 if (virtual_active() && fp == &curwin->w_cursor)
8270 {
8271 char_u *p = ml_get_cursor();
8272
8273 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8274 curwin->w_virtcol - curwin->w_cursor.coladd))
8275 {
8276# ifdef FEAT_MBYTE
8277 int l;
8278
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008279 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 col += l;
8281# else
8282 if (*p != NUL && p[1] == NUL)
8283 ++col;
8284# endif
8285 }
8286 }
8287#endif
8288 }
8289 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008290 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291}
8292
Bram Moolenaar572cb562005-08-05 21:35:02 +00008293#if defined(FEAT_INS_EXPAND)
8294/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008295 * "complete()" function
8296 */
8297/*ARGSUSED*/
8298 static void
8299f_complete(argvars, rettv)
8300 typval_T *argvars;
8301 typval_T *rettv;
8302{
8303 int startcol;
8304
8305 if ((State & INSERT) == 0)
8306 {
8307 EMSG(_("E785: complete() can only be used in Insert mode"));
8308 return;
8309 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008310
8311 /* Check for undo allowed here, because if something was already inserted
8312 * the line was already saved for undo and this check isn't done. */
8313 if (!undo_allowed())
8314 return;
8315
Bram Moolenaarade00832006-03-10 21:46:58 +00008316 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8317 {
8318 EMSG(_(e_invarg));
8319 return;
8320 }
8321
8322 startcol = get_tv_number_chk(&argvars[0], NULL);
8323 if (startcol <= 0)
8324 return;
8325
8326 set_completion(startcol - 1, argvars[1].vval.v_list);
8327}
8328
8329/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008330 * "complete_add()" function
8331 */
8332/*ARGSUSED*/
8333 static void
8334f_complete_add(argvars, rettv)
8335 typval_T *argvars;
8336 typval_T *rettv;
8337{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008338 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008339}
8340
8341/*
8342 * "complete_check()" function
8343 */
8344/*ARGSUSED*/
8345 static void
8346f_complete_check(argvars, rettv)
8347 typval_T *argvars;
8348 typval_T *rettv;
8349{
8350 int saved = RedrawingDisabled;
8351
8352 RedrawingDisabled = 0;
8353 ins_compl_check_keys(0);
8354 rettv->vval.v_number = compl_interrupted;
8355 RedrawingDisabled = saved;
8356}
8357#endif
8358
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359/*
8360 * "confirm(message, buttons[, default [, type]])" function
8361 */
8362/*ARGSUSED*/
8363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008365 typval_T *argvars;
8366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8369 char_u *message;
8370 char_u *buttons = NULL;
8371 char_u buf[NUMBUFLEN];
8372 char_u buf2[NUMBUFLEN];
8373 int def = 1;
8374 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008375 char_u *typestr;
8376 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008378 message = get_tv_string_chk(&argvars[0]);
8379 if (message == NULL)
8380 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008381 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008383 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8384 if (buttons == NULL)
8385 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008386 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008388 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008389 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008391 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8392 if (typestr == NULL)
8393 error = TRUE;
8394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008396 switch (TOUPPER_ASC(*typestr))
8397 {
8398 case 'E': type = VIM_ERROR; break;
8399 case 'Q': type = VIM_QUESTION; break;
8400 case 'I': type = VIM_INFO; break;
8401 case 'W': type = VIM_WARNING; break;
8402 case 'G': type = VIM_GENERIC; break;
8403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
8405 }
8406 }
8407 }
8408
8409 if (buttons == NULL || *buttons == NUL)
8410 buttons = (char_u *)_("&Ok");
8411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008412 if (error)
8413 rettv->vval.v_number = 0;
8414 else
8415 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 def, NULL);
8417#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419#endif
8420}
8421
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008422/*
8423 * "copy()" function
8424 */
8425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008426f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008427 typval_T *argvars;
8428 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008429{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008430 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008431}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
8433/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008434 * "count()" function
8435 */
8436 static void
8437f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008438 typval_T *argvars;
8439 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008440{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008441 long n = 0;
8442 int ic = FALSE;
8443
Bram Moolenaare9a41262005-01-15 22:18:47 +00008444 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008445 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008446 listitem_T *li;
8447 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008448 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008449
Bram Moolenaare9a41262005-01-15 22:18:47 +00008450 if ((l = argvars[0].vval.v_list) != NULL)
8451 {
8452 li = l->lv_first;
8453 if (argvars[2].v_type != VAR_UNKNOWN)
8454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008455 int error = FALSE;
8456
8457 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008458 if (argvars[3].v_type != VAR_UNKNOWN)
8459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460 idx = get_tv_number_chk(&argvars[3], &error);
8461 if (!error)
8462 {
8463 li = list_find(l, idx);
8464 if (li == NULL)
8465 EMSGN(_(e_listidx), idx);
8466 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468 if (error)
8469 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008470 }
8471
8472 for ( ; li != NULL; li = li->li_next)
8473 if (tv_equal(&li->li_tv, &argvars[1], ic))
8474 ++n;
8475 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008476 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008477 else if (argvars[0].v_type == VAR_DICT)
8478 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008479 int todo;
8480 dict_T *d;
8481 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008482
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008483 if ((d = argvars[0].vval.v_dict) != NULL)
8484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008485 int error = FALSE;
8486
Bram Moolenaare9a41262005-01-15 22:18:47 +00008487 if (argvars[2].v_type != VAR_UNKNOWN)
8488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008489 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 if (argvars[3].v_type != VAR_UNKNOWN)
8491 EMSG(_(e_invarg));
8492 }
8493
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008494 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008496 {
8497 if (!HASHITEM_EMPTY(hi))
8498 {
8499 --todo;
8500 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8501 ++n;
8502 }
8503 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008504 }
8505 }
8506 else
8507 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008508 rettv->vval.v_number = n;
8509}
8510
8511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8513 *
8514 * Checks the existence of a cscope connection.
8515 */
8516/*ARGSUSED*/
8517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008518f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 typval_T *argvars;
8520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521{
8522#ifdef FEAT_CSCOPE
8523 int num = 0;
8524 char_u *dbpath = NULL;
8525 char_u *prepend = NULL;
8526 char_u buf[NUMBUFLEN];
8527
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 if (argvars[0].v_type != VAR_UNKNOWN
8529 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 num = (int)get_tv_number(&argvars[0]);
8532 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008534 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
8536
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008537 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540#endif
8541}
8542
8543/*
8544 * "cursor(lnum, col)" function
8545 *
8546 * Moves the cursor to the specified line and column
8547 */
8548/*ARGSUSED*/
8549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008550f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008551 typval_T *argvars;
8552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008555#ifdef FEAT_VIRTUALEDIT
8556 long coladd = 0;
8557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
Bram Moolenaara5525202006-03-02 22:52:09 +00008559 if (argvars[1].v_type == VAR_UNKNOWN)
8560 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008561 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008562
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008563 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008564 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008565 line = pos.lnum;
8566 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008567#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008568 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008569#endif
8570 }
8571 else
8572 {
8573 line = get_tv_lnum(argvars);
8574 col = get_tv_number_chk(&argvars[1], NULL);
8575#ifdef FEAT_VIRTUALEDIT
8576 if (argvars[2].v_type != VAR_UNKNOWN)
8577 coladd = get_tv_number_chk(&argvars[2], NULL);
8578#endif
8579 }
8580 if (line < 0 || col < 0
8581#ifdef FEAT_VIRTUALEDIT
8582 || coladd < 0
8583#endif
8584 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008585 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (line > 0)
8587 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (col > 0)
8589 curwin->w_cursor.col = col - 1;
8590#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008591 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592#endif
8593
8594 /* Make sure the cursor is in a valid position. */
8595 check_cursor();
8596#ifdef FEAT_MBYTE
8597 /* Correct cursor for multi-byte character. */
8598 if (has_mbyte)
8599 mb_adjust_cursor();
8600#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008601
8602 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603}
8604
8605/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008606 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 */
8608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 typval_T *argvars;
8611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008613 int noref = 0;
8614
8615 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008616 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008617 if (noref < 0 || noref > 1)
8618 EMSG(_(e_invarg));
8619 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008620 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621}
8622
8623/*
8624 * "delete()" function
8625 */
8626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008628 typval_T *argvars;
8629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635}
8636
8637/*
8638 * "did_filetype()" function
8639 */
8640/*ARGSUSED*/
8641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008643 typval_T *argvars;
8644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#endif
8651}
8652
8653/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008654 * "diff_filler()" function
8655 */
8656/*ARGSUSED*/
8657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 typval_T *argvars;
8660 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008661{
8662#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008664#endif
8665}
8666
8667/*
8668 * "diff_hlID()" function
8669 */
8670/*ARGSUSED*/
8671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008673 typval_T *argvars;
8674 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008675{
8676#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008678 static linenr_T prev_lnum = 0;
8679 static int changedtick = 0;
8680 static int fnum = 0;
8681 static int change_start = 0;
8682 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008683 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008684 int filler_lines;
8685 int col;
8686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 if (lnum < 0) /* ignore type error in {lnum} arg */
8688 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008689 if (lnum != prev_lnum
8690 || changedtick != curbuf->b_changedtick
8691 || fnum != curbuf->b_fnum)
8692 {
8693 /* New line, buffer, change: need to get the values. */
8694 filler_lines = diff_check(curwin, lnum);
8695 if (filler_lines < 0)
8696 {
8697 if (filler_lines == -1)
8698 {
8699 change_start = MAXCOL;
8700 change_end = -1;
8701 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8702 hlID = HLF_ADD; /* added line */
8703 else
8704 hlID = HLF_CHD; /* changed line */
8705 }
8706 else
8707 hlID = HLF_ADD; /* added line */
8708 }
8709 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008710 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008711 prev_lnum = lnum;
8712 changedtick = curbuf->b_changedtick;
8713 fnum = curbuf->b_fnum;
8714 }
8715
8716 if (hlID == HLF_CHD || hlID == HLF_TXD)
8717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008718 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008719 if (col >= change_start && col <= change_end)
8720 hlID = HLF_TXD; /* changed text */
8721 else
8722 hlID = HLF_CHD; /* changed line */
8723 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008724 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008725#endif
8726}
8727
8728/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008729 * "empty({expr})" function
8730 */
8731 static void
8732f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 typval_T *argvars;
8734 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008735{
8736 int n;
8737
8738 switch (argvars[0].v_type)
8739 {
8740 case VAR_STRING:
8741 case VAR_FUNC:
8742 n = argvars[0].vval.v_string == NULL
8743 || *argvars[0].vval.v_string == NUL;
8744 break;
8745 case VAR_NUMBER:
8746 n = argvars[0].vval.v_number == 0;
8747 break;
8748 case VAR_LIST:
8749 n = argvars[0].vval.v_list == NULL
8750 || argvars[0].vval.v_list->lv_first == NULL;
8751 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008752 case VAR_DICT:
8753 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008755 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008756 default:
8757 EMSG2(_(e_intern2), "f_empty()");
8758 n = 0;
8759 }
8760
8761 rettv->vval.v_number = n;
8762}
8763
8764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 * "escape({string}, {chars})" function
8766 */
8767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 typval_T *argvars;
8770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
8772 char_u buf[NUMBUFLEN];
8773
Bram Moolenaar758711c2005-02-02 23:11:38 +00008774 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8775 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777}
8778
8779/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008780 * "eval()" function
8781 */
8782/*ARGSUSED*/
8783 static void
8784f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 typval_T *argvars;
8786 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008787{
8788 char_u *s;
8789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008790 s = get_tv_string_chk(&argvars[0]);
8791 if (s != NULL)
8792 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8795 {
8796 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008797 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008798 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008799 else if (*s != NUL)
8800 EMSG(_(e_trailing));
8801}
8802
8803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 * "eventhandler()" function
8805 */
8806/*ARGSUSED*/
8807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 typval_T *argvars;
8810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813}
8814
8815/*
8816 * "executable()" function
8817 */
8818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008820 typval_T *argvars;
8821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
8825
8826/*
8827 * "exists()" function
8828 */
8829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *argvars;
8832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
8834 char_u *p;
8835 char_u *name;
8836 int n = FALSE;
8837 int len = 0;
8838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 if (*p == '$') /* environment variable */
8841 {
8842 /* first try "normal" environment variables (fast) */
8843 if (mch_getenv(p + 1) != NULL)
8844 n = TRUE;
8845 else
8846 {
8847 /* try expanding things like $VIM and ${HOME} */
8848 p = expand_env_save(p);
8849 if (p != NULL && *p != '$')
8850 n = TRUE;
8851 vim_free(p);
8852 }
8853 }
8854 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008857 if (*skipwhite(p) != NUL)
8858 n = FALSE; /* trailing garbage */
8859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 else if (*p == '*') /* internal or user defined function */
8861 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008862 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 }
8864 else if (*p == ':')
8865 {
8866 n = cmd_exists(p + 1);
8867 }
8868 else if (*p == '#')
8869 {
8870#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008871 if (p[1] == '#')
8872 n = autocmd_supported(p + 2);
8873 else
8874 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875#endif
8876 }
8877 else /* internal variable */
8878 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008879 char_u *tofree;
8880 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008882 /* get_name_len() takes care of expanding curly braces */
8883 name = p;
8884 len = get_name_len(&p, &tofree, TRUE, FALSE);
8885 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008887 if (tofree != NULL)
8888 name = tofree;
8889 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8890 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008892 /* handle d.key, l[idx], f(expr) */
8893 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8894 if (n)
8895 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 }
8897 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008898 if (*p != NUL)
8899 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008901 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 }
8903
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905}
8906
8907/*
8908 * "expand()" function
8909 */
8910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 typval_T *argvars;
8913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
8915 char_u *s;
8916 int len;
8917 char_u *errormsg;
8918 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8919 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->v_type = VAR_STRING;
8923 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 if (*s == '%' || *s == '#' || *s == '<')
8925 {
8926 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 --emsg_off;
8929 }
8930 else
8931 {
8932 /* When the optional second argument is non-zero, don't remove matches
8933 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 if (argvars[1].v_type != VAR_UNKNOWN
8935 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 if (!error)
8938 {
8939 ExpandInit(&xpc);
8940 xpc.xp_context = EXPAND_FILES;
8941 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 }
8943 else
8944 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 }
8946}
8947
8948/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008949 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008950 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008951 */
8952 static void
8953f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008954 typval_T *argvars;
8955 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008956{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008957 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008958 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008959 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 list_T *l1, *l2;
8961 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008964
Bram Moolenaare9a41262005-01-15 22:18:47 +00008965 l1 = argvars[0].vval.v_list;
8966 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008967 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8968 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008969 {
8970 if (argvars[2].v_type != VAR_UNKNOWN)
8971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 before = get_tv_number_chk(&argvars[2], &error);
8973 if (error)
8974 return; /* type error; errmsg already given */
8975
Bram Moolenaar758711c2005-02-02 23:11:38 +00008976 if (before == l1->lv_len)
8977 item = NULL;
8978 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008979 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008980 item = list_find(l1, before);
8981 if (item == NULL)
8982 {
8983 EMSGN(_(e_listidx), before);
8984 return;
8985 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008986 }
8987 }
8988 else
8989 item = NULL;
8990 list_extend(l1, l2, item);
8991
Bram Moolenaare9a41262005-01-15 22:18:47 +00008992 copy_tv(&argvars[0], rettv);
8993 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008995 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8996 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008997 dict_T *d1, *d2;
8998 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 char_u *action;
9000 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009002 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009003
9004 d1 = argvars[0].vval.v_dict;
9005 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009006 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9007 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 {
9009 /* Check the third argument. */
9010 if (argvars[2].v_type != VAR_UNKNOWN)
9011 {
9012 static char *(av[]) = {"keep", "force", "error"};
9013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009014 action = get_tv_string_chk(&argvars[2]);
9015 if (action == NULL)
9016 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 for (i = 0; i < 3; ++i)
9018 if (STRCMP(action, av[i]) == 0)
9019 break;
9020 if (i == 3)
9021 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009022 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009023 return;
9024 }
9025 }
9026 else
9027 action = (char_u *)"force";
9028
9029 /* Go over all entries in the second dict and add them to the
9030 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009031 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009032 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009033 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009034 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009036 --todo;
9037 di1 = dict_find(d1, hi2->hi_key, -1);
9038 if (di1 == NULL)
9039 {
9040 di1 = dictitem_copy(HI2DI(hi2));
9041 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9042 dictitem_free(di1);
9043 }
9044 else if (*action == 'e')
9045 {
9046 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9047 break;
9048 }
9049 else if (*action == 'f')
9050 {
9051 clear_tv(&di1->di_tv);
9052 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9053 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054 }
9055 }
9056
Bram Moolenaare9a41262005-01-15 22:18:47 +00009057 copy_tv(&argvars[0], rettv);
9058 }
9059 }
9060 else
9061 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009062}
9063
9064/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009065 * "feedkeys()" function
9066 */
9067/*ARGSUSED*/
9068 static void
9069f_feedkeys(argvars, rettv)
9070 typval_T *argvars;
9071 typval_T *rettv;
9072{
9073 int remap = TRUE;
9074 char_u *keys, *flags;
9075 char_u nbuf[NUMBUFLEN];
9076 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009077 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009078
9079 rettv->vval.v_number = 0;
9080 keys = get_tv_string(&argvars[0]);
9081 if (*keys != NUL)
9082 {
9083 if (argvars[1].v_type != VAR_UNKNOWN)
9084 {
9085 flags = get_tv_string_buf(&argvars[1], nbuf);
9086 for ( ; *flags != NUL; ++flags)
9087 {
9088 switch (*flags)
9089 {
9090 case 'n': remap = FALSE; break;
9091 case 'm': remap = TRUE; break;
9092 case 't': typed = TRUE; break;
9093 }
9094 }
9095 }
9096
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009097 /* Need to escape K_SPECIAL and CSI before putting the string in the
9098 * typeahead buffer. */
9099 keys_esc = vim_strsave_escape_csi(keys);
9100 if (keys_esc != NULL)
9101 {
9102 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009103 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009104 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009105 if (vgetc_busy)
9106 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009107 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009108 }
9109}
9110
9111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 * "filereadable()" function
9113 */
9114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 typval_T *argvars;
9117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 FILE *fd;
9120 char_u *p;
9121 int n;
9122
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9125 {
9126 n = TRUE;
9127 fclose(fd);
9128 }
9129 else
9130 n = FALSE;
9131
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133}
9134
9135/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009136 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 * rights to write into.
9138 */
9139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009141 typval_T *argvars;
9142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009144 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145}
9146
Bram Moolenaar33570922005-01-25 22:26:29 +00009147static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009148
9149 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009150findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009151 typval_T *argvars;
9152 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009153 int dir;
9154{
9155#ifdef FEAT_SEARCHPATH
9156 char_u *fname;
9157 char_u *fresult = NULL;
9158 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9159 char_u *p;
9160 char_u pathbuf[NUMBUFLEN];
9161 int count = 1;
9162 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009163 int error = FALSE;
9164#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009165
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009166 rettv->vval.v_string = NULL;
9167 rettv->v_type = VAR_STRING;
9168
9169#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009171
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009172 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9175 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009176 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009177 else
9178 {
9179 if (*p != NUL)
9180 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009183 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009184 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009185 }
9186
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009187 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9188 error = TRUE;
9189
9190 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 do
9193 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009194 if (rettv->v_type == VAR_STRING)
9195 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 fresult = find_file_in_path_option(first ? fname : NULL,
9197 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009198 0, first, path, dir, NULL,
9199 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009200 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009201
9202 if (fresult != NULL && rettv->v_type == VAR_LIST)
9203 list_append_string(rettv->vval.v_list, fresult, -1);
9204
9205 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009207
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009208 if (rettv->v_type == VAR_STRING)
9209 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009210#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009211}
9212
Bram Moolenaar33570922005-01-25 22:26:29 +00009213static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9214static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009215
9216/*
9217 * Implementation of map() and filter().
9218 */
9219 static void
9220filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009221 typval_T *argvars;
9222 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009223 int map;
9224{
9225 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009226 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009227 listitem_T *li, *nli;
9228 list_T *l = NULL;
9229 dictitem_T *di;
9230 hashtab_T *ht;
9231 hashitem_T *hi;
9232 dict_T *d = NULL;
9233 typval_T save_val;
9234 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009235 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009236 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009237 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009238 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009239
9240 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009241 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009242 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009243 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009244 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009245 return;
9246 }
9247 else if (argvars[0].v_type == VAR_DICT)
9248 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009249 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009250 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009251 return;
9252 }
9253 else
9254 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009255 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009256 return;
9257 }
9258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009259 expr = get_tv_string_buf_chk(&argvars[1], buf);
9260 /* On type errors, the preceding call has already displayed an error
9261 * message. Avoid a misleading error message for an empty string that
9262 * was not passed as argument. */
9263 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 prepare_vimvar(VV_VAL, &save_val);
9266 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009267
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009268 /* We reset "did_emsg" to be able to detect whether an error
9269 * occurred during evaluation of the expression. */
9270 save_did_emsg = did_emsg;
9271 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 prepare_vimvar(VV_KEY, &save_key);
9276 vimvars[VV_KEY].vv_type = VAR_STRING;
9277
9278 ht = &d->dv_hashtab;
9279 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009280 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009281 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009283 if (!HASHITEM_EMPTY(hi))
9284 {
9285 --todo;
9286 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009287 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009288 break;
9289 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009290 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009291 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009292 break;
9293 if (!map && rem)
9294 dictitem_remove(d, di);
9295 clear_tv(&vimvars[VV_KEY].vv_tv);
9296 }
9297 }
9298 hash_unlock(ht);
9299
9300 restore_vimvar(VV_KEY, &save_key);
9301 }
9302 else
9303 {
9304 for (li = l->lv_first; li != NULL; li = nli)
9305 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009306 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009307 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009309 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009310 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009311 break;
9312 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009313 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009314 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009315 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009317 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009318
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009319 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009320 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009321
9322 copy_tv(&argvars[0], rettv);
9323}
9324
9325 static int
9326filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009328 char_u *expr;
9329 int map;
9330 int *remp;
9331{
Bram Moolenaar33570922005-01-25 22:26:29 +00009332 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009333 char_u *s;
9334
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009336 s = expr;
9337 if (eval1(&s, &rettv, TRUE) == FAIL)
9338 return FAIL;
9339 if (*s != NUL) /* check for trailing chars after expr */
9340 {
9341 EMSG2(_(e_invexpr2), s);
9342 return FAIL;
9343 }
9344 if (map)
9345 {
9346 /* map(): replace the list item value */
9347 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009348 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009349 *tv = rettv;
9350 }
9351 else
9352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009353 int error = FALSE;
9354
Bram Moolenaare9a41262005-01-15 22:18:47 +00009355 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009357 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 /* On type error, nothing has been removed; return FAIL to stop the
9359 * loop. The error message was given by get_tv_number_chk(). */
9360 if (error)
9361 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009362 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009364 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009365}
9366
9367/*
9368 * "filter()" function
9369 */
9370 static void
9371f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 typval_T *argvars;
9373 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009374{
9375 filter_map(argvars, rettv, FALSE);
9376}
9377
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009378/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009379 * "finddir({fname}[, {path}[, {count}]])" function
9380 */
9381 static void
9382f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009385{
9386 findfilendir(argvars, rettv, TRUE);
9387}
9388
9389/*
9390 * "findfile({fname}[, {path}[, {count}]])" function
9391 */
9392 static void
9393f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009394 typval_T *argvars;
9395 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009396{
9397 findfilendir(argvars, rettv, FALSE);
9398}
9399
9400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 * "fnamemodify({fname}, {mods})" function
9402 */
9403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009405 typval_T *argvars;
9406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 char_u *fname;
9409 char_u *mods;
9410 int usedlen = 0;
9411 int len;
9412 char_u *fbuf = NULL;
9413 char_u buf[NUMBUFLEN];
9414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009415 fname = get_tv_string_chk(&argvars[0]);
9416 mods = get_tv_string_buf_chk(&argvars[1], buf);
9417 if (fname == NULL || mods == NULL)
9418 fname = NULL;
9419 else
9420 {
9421 len = (int)STRLEN(fname);
9422 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 vim_free(fbuf);
9431}
9432
Bram Moolenaar33570922005-01-25 22:26:29 +00009433static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434
9435/*
9436 * "foldclosed()" function
9437 */
9438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 typval_T *argvars;
9441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 int end;
9443{
9444#ifdef FEAT_FOLDING
9445 linenr_T lnum;
9446 linenr_T first, last;
9447
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9450 {
9451 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9452 {
9453 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 return;
9458 }
9459 }
9460#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462}
9463
9464/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465 * "foldclosed()" function
9466 */
9467 static void
9468f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009469 typval_T *argvars;
9470 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009471{
9472 foldclosed_both(argvars, rettv, FALSE);
9473}
9474
9475/*
9476 * "foldclosedend()" function
9477 */
9478 static void
9479f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009480 typval_T *argvars;
9481 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009482{
9483 foldclosed_both(argvars, rettv, TRUE);
9484}
9485
9486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 * "foldlevel()" function
9488 */
9489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009491 typval_T *argvars;
9492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494#ifdef FEAT_FOLDING
9495 linenr_T lnum;
9496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 else
9501#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503}
9504
9505/*
9506 * "foldtext()" function
9507 */
9508/*ARGSUSED*/
9509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009511 typval_T *argvars;
9512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
9514#ifdef FEAT_FOLDING
9515 linenr_T lnum;
9516 char_u *s;
9517 char_u *r;
9518 int len;
9519 char *txt;
9520#endif
9521
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522 rettv->v_type = VAR_STRING;
9523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009525 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9526 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9527 <= curbuf->b_ml.ml_line_count
9528 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
9530 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009531 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9532 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 {
9534 if (!linewhite(lnum))
9535 break;
9536 ++lnum;
9537 }
9538
9539 /* Find interesting text in this line. */
9540 s = skipwhite(ml_get(lnum));
9541 /* skip C comment-start */
9542 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009545 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009546 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009547 {
9548 s = skipwhite(ml_get(lnum + 1));
9549 if (*s == '*')
9550 s = skipwhite(s + 1);
9551 }
9552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 txt = _("+-%s%3ld lines: ");
9554 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009555 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 + 20 /* for %3ld */
9557 + STRLEN(s))); /* concatenated */
9558 if (r != NULL)
9559 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009560 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9561 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9562 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 len = (int)STRLEN(r);
9564 STRCAT(r, s);
9565 /* remove 'foldmarker' and 'commentstring' */
9566 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 }
9569 }
9570#endif
9571}
9572
9573/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574 * "foldtextresult(lnum)" function
9575 */
9576/*ARGSUSED*/
9577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009579 typval_T *argvars;
9580 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009581{
9582#ifdef FEAT_FOLDING
9583 linenr_T lnum;
9584 char_u *text;
9585 char_u buf[51];
9586 foldinfo_T foldinfo;
9587 int fold_count;
9588#endif
9589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590 rettv->v_type = VAR_STRING;
9591 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009592#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 /* treat illegal types and illegal string values for {lnum} the same */
9595 if (lnum < 0)
9596 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009597 fold_count = foldedCount(curwin, lnum, &foldinfo);
9598 if (fold_count > 0)
9599 {
9600 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9601 &foldinfo, buf);
9602 if (text == buf)
9603 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009605 }
9606#endif
9607}
9608
9609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 * "foreground()" function
9611 */
9612/*ARGSUSED*/
9613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009615 typval_T *argvars;
9616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619#ifdef FEAT_GUI
9620 if (gui.in_use)
9621 gui_mch_set_foreground();
9622#else
9623# ifdef WIN32
9624 win32_set_foreground();
9625# endif
9626#endif
9627}
9628
9629/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009630 * "function()" function
9631 */
9632/*ARGSUSED*/
9633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *argvars;
9636 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009637{
9638 char_u *s;
9639
Bram Moolenaara7043832005-01-21 11:56:39 +00009640 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009642 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009643 EMSG2(_(e_invarg2), s);
9644 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009645 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009646 else
9647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->vval.v_string = vim_strsave(s);
9649 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009650 }
9651}
9652
9653/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009654 * "garbagecollect()" function
9655 */
9656/*ARGSUSED*/
9657 static void
9658f_garbagecollect(argvars, rettv)
9659 typval_T *argvars;
9660 typval_T *rettv;
9661{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009662 /* This is postponed until we are back at the toplevel, because we may be
9663 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9664 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009665}
9666
9667/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009668 * "get()" function
9669 */
9670 static void
9671f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009672 typval_T *argvars;
9673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009674{
Bram Moolenaar33570922005-01-25 22:26:29 +00009675 listitem_T *li;
9676 list_T *l;
9677 dictitem_T *di;
9678 dict_T *d;
9679 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009680
Bram Moolenaare9a41262005-01-15 22:18:47 +00009681 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009682 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009685 int error = FALSE;
9686
9687 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9688 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009689 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009690 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009691 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009692 else if (argvars[0].v_type == VAR_DICT)
9693 {
9694 if ((d = argvars[0].vval.v_dict) != NULL)
9695 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009696 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009697 if (di != NULL)
9698 tv = &di->di_tv;
9699 }
9700 }
9701 else
9702 EMSG2(_(e_listdictarg), "get()");
9703
9704 if (tv == NULL)
9705 {
9706 if (argvars[2].v_type == VAR_UNKNOWN)
9707 rettv->vval.v_number = 0;
9708 else
9709 copy_tv(&argvars[2], rettv);
9710 }
9711 else
9712 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009713}
9714
Bram Moolenaar342337a2005-07-21 21:11:17 +00009715static 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 +00009716
9717/*
9718 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009719 * Return a range (from start to end) of lines in rettv from the specified
9720 * buffer.
9721 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009722 */
9723 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009724get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009725 buf_T *buf;
9726 linenr_T start;
9727 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009728 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009729 typval_T *rettv;
9730{
9731 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009732
Bram Moolenaar342337a2005-07-21 21:11:17 +00009733 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009734 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009735 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009736 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009737 }
9738 else
9739 rettv->vval.v_number = 0;
9740
9741 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9742 return;
9743
9744 if (!retlist)
9745 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009746 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9747 p = ml_get_buf(buf, start, FALSE);
9748 else
9749 p = (char_u *)"";
9750
9751 rettv->v_type = VAR_STRING;
9752 rettv->vval.v_string = vim_strsave(p);
9753 }
9754 else
9755 {
9756 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009757 return;
9758
9759 if (start < 1)
9760 start = 1;
9761 if (end > buf->b_ml.ml_line_count)
9762 end = buf->b_ml.ml_line_count;
9763 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009764 if (list_append_string(rettv->vval.v_list,
9765 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009766 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009767 }
9768}
9769
9770/*
9771 * "getbufline()" function
9772 */
9773 static void
9774f_getbufline(argvars, rettv)
9775 typval_T *argvars;
9776 typval_T *rettv;
9777{
9778 linenr_T lnum;
9779 linenr_T end;
9780 buf_T *buf;
9781
9782 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9783 ++emsg_off;
9784 buf = get_buf_tv(&argvars[0]);
9785 --emsg_off;
9786
Bram Moolenaar661b1822005-07-28 22:36:45 +00009787 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009788 if (argvars[2].v_type == VAR_UNKNOWN)
9789 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009790 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009791 end = get_tv_lnum_buf(&argvars[2], buf);
9792
Bram Moolenaar342337a2005-07-21 21:11:17 +00009793 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009794}
9795
Bram Moolenaar0d660222005-01-07 21:51:51 +00009796/*
9797 * "getbufvar()" function
9798 */
9799 static void
9800f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009801 typval_T *argvars;
9802 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009803{
9804 buf_T *buf;
9805 buf_T *save_curbuf;
9806 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009807 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9810 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009811 ++emsg_off;
9812 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009813
9814 rettv->v_type = VAR_STRING;
9815 rettv->vval.v_string = NULL;
9816
9817 if (buf != NULL && varname != NULL)
9818 {
9819 if (*varname == '&') /* buffer-local-option */
9820 {
9821 /* set curbuf to be our buf, temporarily */
9822 save_curbuf = curbuf;
9823 curbuf = buf;
9824
9825 get_option_tv(&varname, rettv, TRUE);
9826
9827 /* restore previous notion of curbuf */
9828 curbuf = save_curbuf;
9829 }
9830 else
9831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 if (*varname == NUL)
9833 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9834 * scope prefix before the NUL byte is required by
9835 * find_var_in_ht(). */
9836 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009837 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009838 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009839 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009840 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009841 }
9842 }
9843
9844 --emsg_off;
9845}
9846
9847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 * "getchar()" function
9849 */
9850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009852 typval_T *argvars;
9853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854{
9855 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009858 /* Position the cursor. Needed after a message that ends in a space. */
9859 windgoto(msg_row, msg_col);
9860
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 ++no_mapping;
9862 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009863 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 /* getchar(): blocking wait. */
9865 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009866 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 /* getchar(1): only check if char avail */
9868 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 else if (error || vpeekc() == NUL)
9870 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 n = 0;
9872 else
9873 /* getchar(0) and char avail: return char */
9874 n = safe_vgetc();
9875 --no_mapping;
9876 --allow_keys;
9877
Bram Moolenaar219b8702006-11-01 14:32:36 +00009878 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9879 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9880 vimvars[VV_MOUSE_COL].vv_nr = 0;
9881
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 if (IS_SPECIAL(n) || mod_mask != 0)
9884 {
9885 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9886 int i = 0;
9887
9888 /* Turn a special key into three bytes, plus modifier. */
9889 if (mod_mask != 0)
9890 {
9891 temp[i++] = K_SPECIAL;
9892 temp[i++] = KS_MODIFIER;
9893 temp[i++] = mod_mask;
9894 }
9895 if (IS_SPECIAL(n))
9896 {
9897 temp[i++] = K_SPECIAL;
9898 temp[i++] = K_SECOND(n);
9899 temp[i++] = K_THIRD(n);
9900 }
9901#ifdef FEAT_MBYTE
9902 else if (has_mbyte)
9903 i += (*mb_char2bytes)(n, temp + i);
9904#endif
9905 else
9906 temp[i++] = n;
9907 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908 rettv->v_type = VAR_STRING;
9909 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009910
9911#ifdef FEAT_MOUSE
9912 if (n == K_LEFTMOUSE
9913 || n == K_LEFTMOUSE_NM
9914 || n == K_LEFTDRAG
9915 || n == K_LEFTRELEASE
9916 || n == K_LEFTRELEASE_NM
9917 || n == K_MIDDLEMOUSE
9918 || n == K_MIDDLEDRAG
9919 || n == K_MIDDLERELEASE
9920 || n == K_RIGHTMOUSE
9921 || n == K_RIGHTDRAG
9922 || n == K_RIGHTRELEASE
9923 || n == K_X1MOUSE
9924 || n == K_X1DRAG
9925 || n == K_X1RELEASE
9926 || n == K_X2MOUSE
9927 || n == K_X2DRAG
9928 || n == K_X2RELEASE
9929 || n == K_MOUSEDOWN
9930 || n == K_MOUSEUP)
9931 {
9932 int row = mouse_row;
9933 int col = mouse_col;
9934 win_T *win;
9935 linenr_T lnum;
9936# ifdef FEAT_WINDOWS
9937 win_T *wp;
9938# endif
9939 int n = 1;
9940
9941 if (row >= 0 && col >= 0)
9942 {
9943 /* Find the window at the mouse coordinates and compute the
9944 * text position. */
9945 win = mouse_find_win(&row, &col);
9946 (void)mouse_comp_pos(win, &row, &col, &lnum);
9947# ifdef FEAT_WINDOWS
9948 for (wp = firstwin; wp != win; wp = wp->w_next)
9949 ++n;
9950# endif
9951 vimvars[VV_MOUSE_WIN].vv_nr = n;
9952 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
9953 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
9954 }
9955 }
9956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 }
9958}
9959
9960/*
9961 * "getcharmod()" function
9962 */
9963/*ARGSUSED*/
9964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009966 typval_T *argvars;
9967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970}
9971
9972/*
9973 * "getcmdline()" function
9974 */
9975/*ARGSUSED*/
9976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009978 typval_T *argvars;
9979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009981 rettv->v_type = VAR_STRING;
9982 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983}
9984
9985/*
9986 * "getcmdpos()" function
9987 */
9988/*ARGSUSED*/
9989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009991 typval_T *argvars;
9992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
9996
9997/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009998 * "getcmdtype()" function
9999 */
10000/*ARGSUSED*/
10001 static void
10002f_getcmdtype(argvars, rettv)
10003 typval_T *argvars;
10004 typval_T *rettv;
10005{
10006 rettv->v_type = VAR_STRING;
10007 rettv->vval.v_string = alloc(2);
10008 if (rettv->vval.v_string != NULL)
10009 {
10010 rettv->vval.v_string[0] = get_cmdline_type();
10011 rettv->vval.v_string[1] = NUL;
10012 }
10013}
10014
10015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 * "getcwd()" function
10017 */
10018/*ARGSUSED*/
10019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010021 typval_T *argvars;
10022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023{
10024 char_u cwd[MAXPATHL];
10025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 else
10030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010033 if (rettv->vval.v_string != NULL)
10034 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035#endif
10036 }
10037}
10038
10039/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010040 * "getfontname()" function
10041 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010042/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010045 typval_T *argvars;
10046 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_STRING;
10049 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010050#ifdef FEAT_GUI
10051 if (gui.in_use)
10052 {
10053 GuiFont font;
10054 char_u *name = NULL;
10055
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010056 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010057 {
10058 /* Get the "Normal" font. Either the name saved by
10059 * hl_set_font_name() or from the font ID. */
10060 font = gui.norm_font;
10061 name = hl_get_font_name();
10062 }
10063 else
10064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010066 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10067 return;
10068 font = gui_mch_get_font(name, FALSE);
10069 if (font == NOFONT)
10070 return; /* Invalid font name, return empty string. */
10071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010073 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010074 gui_mch_free_font(font);
10075 }
10076#endif
10077}
10078
10079/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010080 * "getfperm({fname})" function
10081 */
10082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010084 typval_T *argvars;
10085 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010086{
10087 char_u *fname;
10088 struct stat st;
10089 char_u *perm = NULL;
10090 char_u flags[] = "rwx";
10091 int i;
10092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010096 if (mch_stat((char *)fname, &st) >= 0)
10097 {
10098 perm = vim_strsave((char_u *)"---------");
10099 if (perm != NULL)
10100 {
10101 for (i = 0; i < 9; i++)
10102 {
10103 if (st.st_mode & (1 << (8 - i)))
10104 perm[i] = flags[i % 3];
10105 }
10106 }
10107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010109}
10110
10111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 * "getfsize({fname})" function
10113 */
10114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010116 typval_T *argvars;
10117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
10119 char_u *fname;
10120 struct stat st;
10121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
10126 if (mch_stat((char *)fname, &st) >= 0)
10127 {
10128 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 }
10133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135}
10136
10137/*
10138 * "getftime({fname})" function
10139 */
10140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010142 typval_T *argvars;
10143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144{
10145 char_u *fname;
10146 struct stat st;
10147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149
10150 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154}
10155
10156/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010157 * "getftype({fname})" function
10158 */
10159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010161 typval_T *argvars;
10162 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010163{
10164 char_u *fname;
10165 struct stat st;
10166 char_u *type = NULL;
10167 char *t;
10168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010172 if (mch_lstat((char *)fname, &st) >= 0)
10173 {
10174#ifdef S_ISREG
10175 if (S_ISREG(st.st_mode))
10176 t = "file";
10177 else if (S_ISDIR(st.st_mode))
10178 t = "dir";
10179# ifdef S_ISLNK
10180 else if (S_ISLNK(st.st_mode))
10181 t = "link";
10182# endif
10183# ifdef S_ISBLK
10184 else if (S_ISBLK(st.st_mode))
10185 t = "bdev";
10186# endif
10187# ifdef S_ISCHR
10188 else if (S_ISCHR(st.st_mode))
10189 t = "cdev";
10190# endif
10191# ifdef S_ISFIFO
10192 else if (S_ISFIFO(st.st_mode))
10193 t = "fifo";
10194# endif
10195# ifdef S_ISSOCK
10196 else if (S_ISSOCK(st.st_mode))
10197 t = "fifo";
10198# endif
10199 else
10200 t = "other";
10201#else
10202# ifdef S_IFMT
10203 switch (st.st_mode & S_IFMT)
10204 {
10205 case S_IFREG: t = "file"; break;
10206 case S_IFDIR: t = "dir"; break;
10207# ifdef S_IFLNK
10208 case S_IFLNK: t = "link"; break;
10209# endif
10210# ifdef S_IFBLK
10211 case S_IFBLK: t = "bdev"; break;
10212# endif
10213# ifdef S_IFCHR
10214 case S_IFCHR: t = "cdev"; break;
10215# endif
10216# ifdef S_IFIFO
10217 case S_IFIFO: t = "fifo"; break;
10218# endif
10219# ifdef S_IFSOCK
10220 case S_IFSOCK: t = "socket"; break;
10221# endif
10222 default: t = "other";
10223 }
10224# else
10225 if (mch_isdir(fname))
10226 t = "dir";
10227 else
10228 t = "file";
10229# endif
10230#endif
10231 type = vim_strsave((char_u *)t);
10232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010234}
10235
10236/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010237 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010238 */
10239 static void
10240f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010241 typval_T *argvars;
10242 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010243{
10244 linenr_T lnum;
10245 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010246 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010247
10248 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010249 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010250 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010251 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010252 retlist = FALSE;
10253 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010254 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010255 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010256 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010257 retlist = TRUE;
10258 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010259
Bram Moolenaar342337a2005-07-21 21:11:17 +000010260 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010261}
10262
10263/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010264 * "getpos(string)" function
10265 */
10266 static void
10267f_getpos(argvars, rettv)
10268 typval_T *argvars;
10269 typval_T *rettv;
10270{
10271 pos_T *fp;
10272 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010273 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010274
10275 if (rettv_list_alloc(rettv) == OK)
10276 {
10277 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010278 fp = var2fpos(&argvars[0], TRUE, &fnum);
10279 if (fnum != -1)
10280 list_append_number(l, (varnumber_T)fnum);
10281 else
10282 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010283 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10284 : (varnumber_T)0);
10285 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10286 : (varnumber_T)0);
10287 list_append_number(l,
10288#ifdef FEAT_VIRTUALEDIT
10289 (fp != NULL) ? (varnumber_T)fp->coladd :
10290#endif
10291 (varnumber_T)0);
10292 }
10293 else
10294 rettv->vval.v_number = FALSE;
10295}
10296
10297/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010298 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010299 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010300/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010301 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010302f_getqflist(argvars, rettv)
10303 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010304 typval_T *rettv;
10305{
10306#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010307 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010308#endif
10309
10310 rettv->vval.v_number = FALSE;
10311#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010312 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010313 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010314 wp = NULL;
10315 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10316 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010317 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010318 if (wp == NULL)
10319 return;
10320 }
10321
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010322 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010323 }
10324#endif
10325}
10326
10327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 * "getreg()" function
10329 */
10330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334{
10335 char_u *strregname;
10336 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010337 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010338 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010340 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010342 strregname = get_tv_string_chk(&argvars[0]);
10343 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010344 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 regname = (strregname == NULL ? '"' : *strregname);
10350 if (regname == 0)
10351 regname = '"';
10352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010353 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010354 rettv->vval.v_string = error ? NULL :
10355 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356}
10357
10358/*
10359 * "getregtype()" function
10360 */
10361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010363 typval_T *argvars;
10364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365{
10366 char_u *strregname;
10367 int regname;
10368 char_u buf[NUMBUFLEN + 2];
10369 long reglen = 0;
10370
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010372 {
10373 strregname = get_tv_string_chk(&argvars[0]);
10374 if (strregname == NULL) /* type error; errmsg already given */
10375 {
10376 rettv->v_type = VAR_STRING;
10377 rettv->vval.v_string = NULL;
10378 return;
10379 }
10380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 else
10382 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384
10385 regname = (strregname == NULL ? '"' : *strregname);
10386 if (regname == 0)
10387 regname = '"';
10388
10389 buf[0] = NUL;
10390 buf[1] = NUL;
10391 switch (get_reg_type(regname, &reglen))
10392 {
10393 case MLINE: buf[0] = 'V'; break;
10394 case MCHAR: buf[0] = 'v'; break;
10395#ifdef FEAT_VISUAL
10396 case MBLOCK:
10397 buf[0] = Ctrl_V;
10398 sprintf((char *)buf + 1, "%ld", reglen + 1);
10399 break;
10400#endif
10401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010402 rettv->v_type = VAR_STRING;
10403 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404}
10405
10406/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010407 * "gettabwinvar()" function
10408 */
10409 static void
10410f_gettabwinvar(argvars, rettv)
10411 typval_T *argvars;
10412 typval_T *rettv;
10413{
10414 getwinvar(argvars, rettv, 1);
10415}
10416
10417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 * "getwinposx()" function
10419 */
10420/*ARGSUSED*/
10421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010423 typval_T *argvars;
10424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010426 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427#ifdef FEAT_GUI
10428 if (gui.in_use)
10429 {
10430 int x, y;
10431
10432 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 }
10435#endif
10436}
10437
10438/*
10439 * "getwinposy()" function
10440 */
10441/*ARGSUSED*/
10442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010444 typval_T *argvars;
10445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448#ifdef FEAT_GUI
10449 if (gui.in_use)
10450 {
10451 int x, y;
10452
10453 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 }
10456#endif
10457}
10458
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010459/*
10460 * Find window specifed by "vp" in tabpage "tp".
10461 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010462 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010463find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010464 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010465 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010466{
10467#ifdef FEAT_WINDOWS
10468 win_T *wp;
10469#endif
10470 int nr;
10471
10472 nr = get_tv_number_chk(vp, NULL);
10473
10474#ifdef FEAT_WINDOWS
10475 if (nr < 0)
10476 return NULL;
10477 if (nr == 0)
10478 return curwin;
10479
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010480 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10481 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010482 if (--nr <= 0)
10483 break;
10484 return wp;
10485#else
10486 if (nr == 0 || nr == 1)
10487 return curwin;
10488 return NULL;
10489#endif
10490}
10491
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492/*
10493 * "getwinvar()" function
10494 */
10495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010497 typval_T *argvars;
10498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010500 getwinvar(argvars, rettv, 0);
10501}
10502
10503/*
10504 * getwinvar() and gettabwinvar()
10505 */
10506 static void
10507getwinvar(argvars, rettv, off)
10508 typval_T *argvars;
10509 typval_T *rettv;
10510 int off; /* 1 for gettabwinvar() */
10511{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 win_T *win, *oldcurwin;
10513 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010514 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010515 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010517#ifdef FEAT_WINDOWS
10518 if (off == 1)
10519 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10520 else
10521 tp = curtab;
10522#endif
10523 win = find_win_by_nr(&argvars[off], tp);
10524 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010527 rettv->v_type = VAR_STRING;
10528 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529
10530 if (win != NULL && varname != NULL)
10531 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010532 /* Set curwin to be our win, temporarily. Also set curbuf, so
10533 * that we can get buffer-local options. */
10534 oldcurwin = curwin;
10535 curwin = win;
10536 curbuf = win->w_buffer;
10537
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010539 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 else
10541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 if (*varname == NUL)
10543 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10544 * scope prefix before the NUL byte is required by
10545 * find_var_in_ht(). */
10546 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010548 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010550 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010552
10553 /* restore previous notion of curwin */
10554 curwin = oldcurwin;
10555 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 }
10557
10558 --emsg_off;
10559}
10560
10561/*
10562 * "glob()" function
10563 */
10564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569 expand_T xpc;
10570
10571 ExpandInit(&xpc);
10572 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 rettv->v_type = VAR_STRING;
10574 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576}
10577
10578/*
10579 * "globpath()" function
10580 */
10581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 typval_T *argvars;
10584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
10586 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590 if (file == NULL)
10591 rettv->vval.v_string = NULL;
10592 else
10593 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594}
10595
10596/*
10597 * "has()" function
10598 */
10599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *argvars;
10602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 int i;
10605 char_u *name;
10606 int n = FALSE;
10607 static char *(has_list[]) =
10608 {
10609#ifdef AMIGA
10610 "amiga",
10611# ifdef FEAT_ARP
10612 "arp",
10613# endif
10614#endif
10615#ifdef __BEOS__
10616 "beos",
10617#endif
10618#ifdef MSDOS
10619# ifdef DJGPP
10620 "dos32",
10621# else
10622 "dos16",
10623# endif
10624#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010625#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 "mac",
10627#endif
10628#if defined(MACOS_X_UNIX)
10629 "macunix",
10630#endif
10631#ifdef OS2
10632 "os2",
10633#endif
10634#ifdef __QNX__
10635 "qnx",
10636#endif
10637#ifdef RISCOS
10638 "riscos",
10639#endif
10640#ifdef UNIX
10641 "unix",
10642#endif
10643#ifdef VMS
10644 "vms",
10645#endif
10646#ifdef WIN16
10647 "win16",
10648#endif
10649#ifdef WIN32
10650 "win32",
10651#endif
10652#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10653 "win32unix",
10654#endif
10655#ifdef WIN64
10656 "win64",
10657#endif
10658#ifdef EBCDIC
10659 "ebcdic",
10660#endif
10661#ifndef CASE_INSENSITIVE_FILENAME
10662 "fname_case",
10663#endif
10664#ifdef FEAT_ARABIC
10665 "arabic",
10666#endif
10667#ifdef FEAT_AUTOCMD
10668 "autocmd",
10669#endif
10670#ifdef FEAT_BEVAL
10671 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010672# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10673 "balloon_multiline",
10674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675#endif
10676#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10677 "builtin_terms",
10678# ifdef ALL_BUILTIN_TCAPS
10679 "all_builtin_terms",
10680# endif
10681#endif
10682#ifdef FEAT_BYTEOFF
10683 "byte_offset",
10684#endif
10685#ifdef FEAT_CINDENT
10686 "cindent",
10687#endif
10688#ifdef FEAT_CLIENTSERVER
10689 "clientserver",
10690#endif
10691#ifdef FEAT_CLIPBOARD
10692 "clipboard",
10693#endif
10694#ifdef FEAT_CMDL_COMPL
10695 "cmdline_compl",
10696#endif
10697#ifdef FEAT_CMDHIST
10698 "cmdline_hist",
10699#endif
10700#ifdef FEAT_COMMENTS
10701 "comments",
10702#endif
10703#ifdef FEAT_CRYPT
10704 "cryptv",
10705#endif
10706#ifdef FEAT_CSCOPE
10707 "cscope",
10708#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010709#ifdef CURSOR_SHAPE
10710 "cursorshape",
10711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712#ifdef DEBUG
10713 "debug",
10714#endif
10715#ifdef FEAT_CON_DIALOG
10716 "dialog_con",
10717#endif
10718#ifdef FEAT_GUI_DIALOG
10719 "dialog_gui",
10720#endif
10721#ifdef FEAT_DIFF
10722 "diff",
10723#endif
10724#ifdef FEAT_DIGRAPHS
10725 "digraphs",
10726#endif
10727#ifdef FEAT_DND
10728 "dnd",
10729#endif
10730#ifdef FEAT_EMACS_TAGS
10731 "emacs_tags",
10732#endif
10733 "eval", /* always present, of course! */
10734#ifdef FEAT_EX_EXTRA
10735 "ex_extra",
10736#endif
10737#ifdef FEAT_SEARCH_EXTRA
10738 "extra_search",
10739#endif
10740#ifdef FEAT_FKMAP
10741 "farsi",
10742#endif
10743#ifdef FEAT_SEARCHPATH
10744 "file_in_path",
10745#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010746#if defined(UNIX) && !defined(USE_SYSTEM)
10747 "filterpipe",
10748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749#ifdef FEAT_FIND_ID
10750 "find_in_path",
10751#endif
10752#ifdef FEAT_FOLDING
10753 "folding",
10754#endif
10755#ifdef FEAT_FOOTER
10756 "footer",
10757#endif
10758#if !defined(USE_SYSTEM) && defined(UNIX)
10759 "fork",
10760#endif
10761#ifdef FEAT_GETTEXT
10762 "gettext",
10763#endif
10764#ifdef FEAT_GUI
10765 "gui",
10766#endif
10767#ifdef FEAT_GUI_ATHENA
10768# ifdef FEAT_GUI_NEXTAW
10769 "gui_neXtaw",
10770# else
10771 "gui_athena",
10772# endif
10773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774#ifdef FEAT_GUI_GTK
10775 "gui_gtk",
10776# ifdef HAVE_GTK2
10777 "gui_gtk2",
10778# endif
10779#endif
10780#ifdef FEAT_GUI_MAC
10781 "gui_mac",
10782#endif
10783#ifdef FEAT_GUI_MOTIF
10784 "gui_motif",
10785#endif
10786#ifdef FEAT_GUI_PHOTON
10787 "gui_photon",
10788#endif
10789#ifdef FEAT_GUI_W16
10790 "gui_win16",
10791#endif
10792#ifdef FEAT_GUI_W32
10793 "gui_win32",
10794#endif
10795#ifdef FEAT_HANGULIN
10796 "hangul_input",
10797#endif
10798#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10799 "iconv",
10800#endif
10801#ifdef FEAT_INS_EXPAND
10802 "insert_expand",
10803#endif
10804#ifdef FEAT_JUMPLIST
10805 "jumplist",
10806#endif
10807#ifdef FEAT_KEYMAP
10808 "keymap",
10809#endif
10810#ifdef FEAT_LANGMAP
10811 "langmap",
10812#endif
10813#ifdef FEAT_LIBCALL
10814 "libcall",
10815#endif
10816#ifdef FEAT_LINEBREAK
10817 "linebreak",
10818#endif
10819#ifdef FEAT_LISP
10820 "lispindent",
10821#endif
10822#ifdef FEAT_LISTCMDS
10823 "listcmds",
10824#endif
10825#ifdef FEAT_LOCALMAP
10826 "localmap",
10827#endif
10828#ifdef FEAT_MENU
10829 "menu",
10830#endif
10831#ifdef FEAT_SESSION
10832 "mksession",
10833#endif
10834#ifdef FEAT_MODIFY_FNAME
10835 "modify_fname",
10836#endif
10837#ifdef FEAT_MOUSE
10838 "mouse",
10839#endif
10840#ifdef FEAT_MOUSESHAPE
10841 "mouseshape",
10842#endif
10843#if defined(UNIX) || defined(VMS)
10844# ifdef FEAT_MOUSE_DEC
10845 "mouse_dec",
10846# endif
10847# ifdef FEAT_MOUSE_GPM
10848 "mouse_gpm",
10849# endif
10850# ifdef FEAT_MOUSE_JSB
10851 "mouse_jsbterm",
10852# endif
10853# ifdef FEAT_MOUSE_NET
10854 "mouse_netterm",
10855# endif
10856# ifdef FEAT_MOUSE_PTERM
10857 "mouse_pterm",
10858# endif
10859# ifdef FEAT_MOUSE_XTERM
10860 "mouse_xterm",
10861# endif
10862#endif
10863#ifdef FEAT_MBYTE
10864 "multi_byte",
10865#endif
10866#ifdef FEAT_MBYTE_IME
10867 "multi_byte_ime",
10868#endif
10869#ifdef FEAT_MULTI_LANG
10870 "multi_lang",
10871#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010872#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010873#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010874 "mzscheme",
10875#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877#ifdef FEAT_OLE
10878 "ole",
10879#endif
10880#ifdef FEAT_OSFILETYPE
10881 "osfiletype",
10882#endif
10883#ifdef FEAT_PATH_EXTRA
10884 "path_extra",
10885#endif
10886#ifdef FEAT_PERL
10887#ifndef DYNAMIC_PERL
10888 "perl",
10889#endif
10890#endif
10891#ifdef FEAT_PYTHON
10892#ifndef DYNAMIC_PYTHON
10893 "python",
10894#endif
10895#endif
10896#ifdef FEAT_POSTSCRIPT
10897 "postscript",
10898#endif
10899#ifdef FEAT_PRINTER
10900 "printer",
10901#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010902#ifdef FEAT_PROFILE
10903 "profile",
10904#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010905#ifdef FEAT_RELTIME
10906 "reltime",
10907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908#ifdef FEAT_QUICKFIX
10909 "quickfix",
10910#endif
10911#ifdef FEAT_RIGHTLEFT
10912 "rightleft",
10913#endif
10914#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10915 "ruby",
10916#endif
10917#ifdef FEAT_SCROLLBIND
10918 "scrollbind",
10919#endif
10920#ifdef FEAT_CMDL_INFO
10921 "showcmd",
10922 "cmdline_info",
10923#endif
10924#ifdef FEAT_SIGNS
10925 "signs",
10926#endif
10927#ifdef FEAT_SMARTINDENT
10928 "smartindent",
10929#endif
10930#ifdef FEAT_SNIFF
10931 "sniff",
10932#endif
10933#ifdef FEAT_STL_OPT
10934 "statusline",
10935#endif
10936#ifdef FEAT_SUN_WORKSHOP
10937 "sun_workshop",
10938#endif
10939#ifdef FEAT_NETBEANS_INTG
10940 "netbeans_intg",
10941#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010942#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010943 "spell",
10944#endif
10945#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 "syntax",
10947#endif
10948#if defined(USE_SYSTEM) || !defined(UNIX)
10949 "system",
10950#endif
10951#ifdef FEAT_TAG_BINS
10952 "tag_binary",
10953#endif
10954#ifdef FEAT_TAG_OLDSTATIC
10955 "tag_old_static",
10956#endif
10957#ifdef FEAT_TAG_ANYWHITE
10958 "tag_any_white",
10959#endif
10960#ifdef FEAT_TCL
10961# ifndef DYNAMIC_TCL
10962 "tcl",
10963# endif
10964#endif
10965#ifdef TERMINFO
10966 "terminfo",
10967#endif
10968#ifdef FEAT_TERMRESPONSE
10969 "termresponse",
10970#endif
10971#ifdef FEAT_TEXTOBJ
10972 "textobjects",
10973#endif
10974#ifdef HAVE_TGETENT
10975 "tgetent",
10976#endif
10977#ifdef FEAT_TITLE
10978 "title",
10979#endif
10980#ifdef FEAT_TOOLBAR
10981 "toolbar",
10982#endif
10983#ifdef FEAT_USR_CMDS
10984 "user-commands", /* was accidentally included in 5.4 */
10985 "user_commands",
10986#endif
10987#ifdef FEAT_VIMINFO
10988 "viminfo",
10989#endif
10990#ifdef FEAT_VERTSPLIT
10991 "vertsplit",
10992#endif
10993#ifdef FEAT_VIRTUALEDIT
10994 "virtualedit",
10995#endif
10996#ifdef FEAT_VISUAL
10997 "visual",
10998#endif
10999#ifdef FEAT_VISUALEXTRA
11000 "visualextra",
11001#endif
11002#ifdef FEAT_VREPLACE
11003 "vreplace",
11004#endif
11005#ifdef FEAT_WILDIGN
11006 "wildignore",
11007#endif
11008#ifdef FEAT_WILDMENU
11009 "wildmenu",
11010#endif
11011#ifdef FEAT_WINDOWS
11012 "windows",
11013#endif
11014#ifdef FEAT_WAK
11015 "winaltkeys",
11016#endif
11017#ifdef FEAT_WRITEBACKUP
11018 "writebackup",
11019#endif
11020#ifdef FEAT_XIM
11021 "xim",
11022#endif
11023#ifdef FEAT_XFONTSET
11024 "xfontset",
11025#endif
11026#ifdef USE_XSMP
11027 "xsmp",
11028#endif
11029#ifdef USE_XSMP_INTERACT
11030 "xsmp_interact",
11031#endif
11032#ifdef FEAT_XCLIPBOARD
11033 "xterm_clipboard",
11034#endif
11035#ifdef FEAT_XTERM_SAVE
11036 "xterm_save",
11037#endif
11038#if defined(UNIX) && defined(FEAT_X11)
11039 "X11",
11040#endif
11041 NULL
11042 };
11043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 for (i = 0; has_list[i] != NULL; ++i)
11046 if (STRICMP(name, has_list[i]) == 0)
11047 {
11048 n = TRUE;
11049 break;
11050 }
11051
11052 if (n == FALSE)
11053 {
11054 if (STRNICMP(name, "patch", 5) == 0)
11055 n = has_patch(atoi((char *)name + 5));
11056 else if (STRICMP(name, "vim_starting") == 0)
11057 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011058#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11059 else if (STRICMP(name, "balloon_multiline") == 0)
11060 n = multiline_balloon_available();
11061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062#ifdef DYNAMIC_TCL
11063 else if (STRICMP(name, "tcl") == 0)
11064 n = tcl_enabled(FALSE);
11065#endif
11066#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11067 else if (STRICMP(name, "iconv") == 0)
11068 n = iconv_enabled(FALSE);
11069#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011070#ifdef DYNAMIC_MZSCHEME
11071 else if (STRICMP(name, "mzscheme") == 0)
11072 n = mzscheme_enabled(FALSE);
11073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074#ifdef DYNAMIC_RUBY
11075 else if (STRICMP(name, "ruby") == 0)
11076 n = ruby_enabled(FALSE);
11077#endif
11078#ifdef DYNAMIC_PYTHON
11079 else if (STRICMP(name, "python") == 0)
11080 n = python_enabled(FALSE);
11081#endif
11082#ifdef DYNAMIC_PERL
11083 else if (STRICMP(name, "perl") == 0)
11084 n = perl_enabled(FALSE);
11085#endif
11086#ifdef FEAT_GUI
11087 else if (STRICMP(name, "gui_running") == 0)
11088 n = (gui.in_use || gui.starting);
11089# ifdef FEAT_GUI_W32
11090 else if (STRICMP(name, "gui_win32s") == 0)
11091 n = gui_is_win32s();
11092# endif
11093# ifdef FEAT_BROWSE
11094 else if (STRICMP(name, "browse") == 0)
11095 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11096# endif
11097#endif
11098#ifdef FEAT_SYN_HL
11099 else if (STRICMP(name, "syntax_items") == 0)
11100 n = syntax_present(curbuf);
11101#endif
11102#if defined(WIN3264)
11103 else if (STRICMP(name, "win95") == 0)
11104 n = mch_windows95();
11105#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011106#ifdef FEAT_NETBEANS_INTG
11107 else if (STRICMP(name, "netbeans_enabled") == 0)
11108 n = usingNetbeans;
11109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 }
11111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113}
11114
11115/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011116 * "has_key()" function
11117 */
11118 static void
11119f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011120 typval_T *argvars;
11121 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122{
11123 rettv->vval.v_number = 0;
11124 if (argvars[0].v_type != VAR_DICT)
11125 {
11126 EMSG(_(e_dictreq));
11127 return;
11128 }
11129 if (argvars[0].vval.v_dict == NULL)
11130 return;
11131
11132 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011133 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134}
11135
11136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 * "hasmapto()" function
11138 */
11139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *argvars;
11142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
11144 char_u *name;
11145 char_u *mode;
11146 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011147 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011150 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 mode = (char_u *)"nvo";
11152 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011155 if (argvars[2].v_type != VAR_UNKNOWN)
11156 abbr = get_tv_number(&argvars[2]);
11157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158
Bram Moolenaar2c932302006-03-18 21:42:09 +000011159 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163}
11164
11165/*
11166 * "histadd()" function
11167 */
11168/*ARGSUSED*/
11169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *argvars;
11172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173{
11174#ifdef FEAT_CMDHIST
11175 int histype;
11176 char_u *str;
11177 char_u buf[NUMBUFLEN];
11178#endif
11179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 if (check_restricted() || check_secure())
11182 return;
11183#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11185 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 if (histype >= 0)
11187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 if (*str != NUL)
11190 {
11191 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 return;
11194 }
11195 }
11196#endif
11197}
11198
11199/*
11200 * "histdel()" function
11201 */
11202/*ARGSUSED*/
11203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011205 typval_T *argvars;
11206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
11208#ifdef FEAT_CMDHIST
11209 int n;
11210 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11214 if (str == NULL)
11215 n = 0;
11216 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011219 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011221 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 else
11224 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 get_tv_string_buf(&argvars[1], buf));
11227 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230#endif
11231}
11232
11233/*
11234 * "histget()" function
11235 */
11236/*ARGSUSED*/
11237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011238f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011239 typval_T *argvars;
11240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241{
11242#ifdef FEAT_CMDHIST
11243 int type;
11244 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011245 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11248 if (str == NULL)
11249 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 {
11252 type = get_histtype(str);
11253 if (argvars[1].v_type == VAR_UNKNOWN)
11254 idx = get_history_idx(type);
11255 else
11256 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11257 /* -1 on type error */
11258 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264}
11265
11266/*
11267 * "histnr()" function
11268 */
11269/*ARGSUSED*/
11270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011272 typval_T *argvars;
11273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
11275 int i;
11276
11277#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011278 char_u *history = get_tv_string_chk(&argvars[0]);
11279
11280 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 if (i >= HIST_CMD && i < HIST_COUNT)
11282 i = get_history_idx(i);
11283 else
11284#endif
11285 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287}
11288
11289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 * "highlightID(name)" function
11291 */
11292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011294 typval_T *argvars;
11295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298}
11299
11300/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011301 * "highlight_exists()" function
11302 */
11303 static void
11304f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011305 typval_T *argvars;
11306 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011307{
11308 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11309}
11310
11311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 * "hostname()" function
11313 */
11314/*ARGSUSED*/
11315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011317 typval_T *argvars;
11318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319{
11320 char_u hostname[256];
11321
11322 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->v_type = VAR_STRING;
11324 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325}
11326
11327/*
11328 * iconv() function
11329 */
11330/*ARGSUSED*/
11331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011333 typval_T *argvars;
11334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335{
11336#ifdef FEAT_MBYTE
11337 char_u buf1[NUMBUFLEN];
11338 char_u buf2[NUMBUFLEN];
11339 char_u *from, *to, *str;
11340 vimconv_T vimconv;
11341#endif
11342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 rettv->v_type = VAR_STRING;
11344 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345
11346#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 str = get_tv_string(&argvars[0]);
11348 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11349 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 vimconv.vc_type = CONV_NONE;
11351 convert_setup(&vimconv, from, to);
11352
11353 /* If the encodings are equal, no conversion needed. */
11354 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358
11359 convert_setup(&vimconv, NULL, NULL);
11360 vim_free(from);
11361 vim_free(to);
11362#endif
11363}
11364
11365/*
11366 * "indent()" function
11367 */
11368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *argvars;
11371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372{
11373 linenr_T lnum;
11374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380}
11381
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011382/*
11383 * "index()" function
11384 */
11385 static void
11386f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011387 typval_T *argvars;
11388 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011389{
Bram Moolenaar33570922005-01-25 22:26:29 +000011390 list_T *l;
11391 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011392 long idx = 0;
11393 int ic = FALSE;
11394
11395 rettv->vval.v_number = -1;
11396 if (argvars[0].v_type != VAR_LIST)
11397 {
11398 EMSG(_(e_listreq));
11399 return;
11400 }
11401 l = argvars[0].vval.v_list;
11402 if (l != NULL)
11403 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011404 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 int error = FALSE;
11408
Bram Moolenaar758711c2005-02-02 23:11:38 +000011409 /* Start at specified item. Use the cached index that list_find()
11410 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011412 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011413 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 ic = get_tv_number_chk(&argvars[3], &error);
11415 if (error)
11416 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011417 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011418
Bram Moolenaar758711c2005-02-02 23:11:38 +000011419 for ( ; item != NULL; item = item->li_next, ++idx)
11420 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011421 {
11422 rettv->vval.v_number = idx;
11423 break;
11424 }
11425 }
11426}
11427
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428static int inputsecret_flag = 0;
11429
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011430static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11431
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011433 * This function is used by f_input() and f_inputdialog() functions. The third
11434 * argument to f_input() specifies the type of completion to use at the
11435 * prompt. The third argument to f_inputdialog() specifies the value to return
11436 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 */
11438 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011439get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011442 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011444 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 char_u *p = NULL;
11446 int c;
11447 char_u buf[NUMBUFLEN];
11448 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011450 int xp_type = EXPAND_NOTHING;
11451 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454
11455#ifdef NO_CONSOLE_INPUT
11456 /* While starting up, there is no place to enter text. */
11457 if (no_console_input())
11458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 return;
11461 }
11462#endif
11463
11464 cmd_silent = FALSE; /* Want to see the prompt. */
11465 if (prompt != NULL)
11466 {
11467 /* Only the part of the message after the last NL is considered as
11468 * prompt for the command line */
11469 p = vim_strrchr(prompt, '\n');
11470 if (p == NULL)
11471 p = prompt;
11472 else
11473 {
11474 ++p;
11475 c = *p;
11476 *p = NUL;
11477 msg_start();
11478 msg_clr_eos();
11479 msg_puts_attr(prompt, echo_attr);
11480 msg_didout = FALSE;
11481 msg_starthere();
11482 *p = c;
11483 }
11484 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011486 if (argvars[1].v_type != VAR_UNKNOWN)
11487 {
11488 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11489 if (defstr != NULL)
11490 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011492 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011493 {
11494 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011495 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011496 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011497
Bram Moolenaar4463f292005-09-25 22:20:24 +000011498 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011499
Bram Moolenaar4463f292005-09-25 22:20:24 +000011500 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11501 if (xp_name == NULL)
11502 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011503
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011504 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011505
Bram Moolenaar4463f292005-09-25 22:20:24 +000011506 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11507 &xp_arg) == FAIL)
11508 return;
11509 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011510 }
11511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011512 if (defstr != NULL)
11513 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011514 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11515 xp_type, xp_arg);
11516
11517 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 /* since the user typed this, no need to wait for return */
11520 need_wait_return = FALSE;
11521 msg_didout = FALSE;
11522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 cmd_silent = cmd_silent_save;
11524}
11525
11526/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011527 * "input()" function
11528 * Also handles inputsecret() when inputsecret is set.
11529 */
11530 static void
11531f_input(argvars, rettv)
11532 typval_T *argvars;
11533 typval_T *rettv;
11534{
11535 get_user_input(argvars, rettv, FALSE);
11536}
11537
11538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 * "inputdialog()" function
11540 */
11541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011543 typval_T *argvars;
11544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545{
11546#if defined(FEAT_GUI_TEXTDIALOG)
11547 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11548 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11549 {
11550 char_u *message;
11551 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011552 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 message = get_tv_string_chk(&argvars[0]);
11555 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011556 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011557 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 else
11559 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011560 if (message != NULL && defstr != NULL
11561 && do_dialog(VIM_QUESTION, NULL, message,
11562 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 else
11565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011566 if (message != NULL && defstr != NULL
11567 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011568 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->vval.v_string = vim_strsave(
11570 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 }
11576 else
11577#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011578 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579}
11580
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011581/*
11582 * "inputlist()" function
11583 */
11584 static void
11585f_inputlist(argvars, rettv)
11586 typval_T *argvars;
11587 typval_T *rettv;
11588{
11589 listitem_T *li;
11590 int selected;
11591 int mouse_used;
11592
11593 rettv->vval.v_number = 0;
11594#ifdef NO_CONSOLE_INPUT
11595 /* While starting up, there is no place to enter text. */
11596 if (no_console_input())
11597 return;
11598#endif
11599 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11600 {
11601 EMSG2(_(e_listarg), "inputlist()");
11602 return;
11603 }
11604
11605 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011606 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011607 lines_left = Rows; /* avoid more prompt */
11608 msg_scroll = TRUE;
11609 msg_clr_eos();
11610
11611 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11612 {
11613 msg_puts(get_tv_string(&li->li_tv));
11614 msg_putchar('\n');
11615 }
11616
11617 /* Ask for choice. */
11618 selected = prompt_for_number(&mouse_used);
11619 if (mouse_used)
11620 selected -= lines_left;
11621
11622 rettv->vval.v_number = selected;
11623}
11624
11625
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11627
11628/*
11629 * "inputrestore()" function
11630 */
11631/*ARGSUSED*/
11632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011634 typval_T *argvars;
11635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636{
11637 if (ga_userinput.ga_len > 0)
11638 {
11639 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11641 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 }
11644 else if (p_verbose > 1)
11645 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011646 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 }
11649}
11650
11651/*
11652 * "inputsave()" function
11653 */
11654/*ARGSUSED*/
11655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011656f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011657 typval_T *argvars;
11658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659{
11660 /* Add an entry to the stack of typehead storage. */
11661 if (ga_grow(&ga_userinput, 1) == OK)
11662 {
11663 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11664 + ga_userinput.ga_len);
11665 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 }
11668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670}
11671
11672/*
11673 * "inputsecret()" function
11674 */
11675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 typval_T *argvars;
11678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679{
11680 ++cmdline_star;
11681 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 --cmdline_star;
11684 --inputsecret_flag;
11685}
11686
11687/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011688 * "insert()" function
11689 */
11690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 typval_T *argvars;
11693 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011694{
11695 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011696 listitem_T *item;
11697 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011698 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011699
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011700 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011701 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011702 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011703 else if ((l = argvars[0].vval.v_list) != NULL
11704 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011705 {
11706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 before = get_tv_number_chk(&argvars[2], &error);
11708 if (error)
11709 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011710
Bram Moolenaar758711c2005-02-02 23:11:38 +000011711 if (before == l->lv_len)
11712 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011713 else
11714 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011715 item = list_find(l, before);
11716 if (item == NULL)
11717 {
11718 EMSGN(_(e_listidx), before);
11719 l = NULL;
11720 }
11721 }
11722 if (l != NULL)
11723 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011724 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011725 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011726 }
11727 }
11728}
11729
11730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 * "isdirectory()" function
11732 */
11733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011735 typval_T *argvars;
11736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011738 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739}
11740
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011741/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011742 * "islocked()" function
11743 */
11744 static void
11745f_islocked(argvars, rettv)
11746 typval_T *argvars;
11747 typval_T *rettv;
11748{
11749 lval_T lv;
11750 char_u *end;
11751 dictitem_T *di;
11752
11753 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011754 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11755 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011756 if (end != NULL && lv.ll_name != NULL)
11757 {
11758 if (*end != NUL)
11759 EMSG(_(e_trailing));
11760 else
11761 {
11762 if (lv.ll_tv == NULL)
11763 {
11764 if (check_changedtick(lv.ll_name))
11765 rettv->vval.v_number = 1; /* always locked */
11766 else
11767 {
11768 di = find_var(lv.ll_name, NULL);
11769 if (di != NULL)
11770 {
11771 /* Consider a variable locked when:
11772 * 1. the variable itself is locked
11773 * 2. the value of the variable is locked.
11774 * 3. the List or Dict value is locked.
11775 */
11776 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11777 || tv_islocked(&di->di_tv));
11778 }
11779 }
11780 }
11781 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011782 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011783 else if (lv.ll_newkey != NULL)
11784 EMSG2(_(e_dictkey), lv.ll_newkey);
11785 else if (lv.ll_list != NULL)
11786 /* List item. */
11787 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11788 else
11789 /* Dictionary item. */
11790 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11791 }
11792 }
11793
11794 clear_lval(&lv);
11795}
11796
Bram Moolenaar33570922005-01-25 22:26:29 +000011797static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011798
11799/*
11800 * Turn a dict into a list:
11801 * "what" == 0: list of keys
11802 * "what" == 1: list of values
11803 * "what" == 2: list of items
11804 */
11805 static void
11806dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011807 typval_T *argvars;
11808 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011809 int what;
11810{
Bram Moolenaar33570922005-01-25 22:26:29 +000011811 list_T *l2;
11812 dictitem_T *di;
11813 hashitem_T *hi;
11814 listitem_T *li;
11815 listitem_T *li2;
11816 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011817 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011818
11819 rettv->vval.v_number = 0;
11820 if (argvars[0].v_type != VAR_DICT)
11821 {
11822 EMSG(_(e_dictreq));
11823 return;
11824 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011825 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011826 return;
11827
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011828 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011829 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011830
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011831 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011832 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011833 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011834 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011835 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011836 --todo;
11837 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011838
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011839 li = listitem_alloc();
11840 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011841 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011842 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011843
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011844 if (what == 0)
11845 {
11846 /* keys() */
11847 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011848 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011849 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11850 }
11851 else if (what == 1)
11852 {
11853 /* values() */
11854 copy_tv(&di->di_tv, &li->li_tv);
11855 }
11856 else
11857 {
11858 /* items() */
11859 l2 = list_alloc();
11860 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011861 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011862 li->li_tv.vval.v_list = l2;
11863 if (l2 == NULL)
11864 break;
11865 ++l2->lv_refcount;
11866
11867 li2 = listitem_alloc();
11868 if (li2 == NULL)
11869 break;
11870 list_append(l2, li2);
11871 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011872 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011873 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11874
11875 li2 = listitem_alloc();
11876 if (li2 == NULL)
11877 break;
11878 list_append(l2, li2);
11879 copy_tv(&di->di_tv, &li2->li_tv);
11880 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011881 }
11882 }
11883}
11884
11885/*
11886 * "items(dict)" function
11887 */
11888 static void
11889f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011890 typval_T *argvars;
11891 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011892{
11893 dict_list(argvars, rettv, 2);
11894}
11895
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011897 * "join()" function
11898 */
11899 static void
11900f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011901 typval_T *argvars;
11902 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011903{
11904 garray_T ga;
11905 char_u *sep;
11906
11907 rettv->vval.v_number = 0;
11908 if (argvars[0].v_type != VAR_LIST)
11909 {
11910 EMSG(_(e_listreq));
11911 return;
11912 }
11913 if (argvars[0].vval.v_list == NULL)
11914 return;
11915 if (argvars[1].v_type == VAR_UNKNOWN)
11916 sep = (char_u *)" ";
11917 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011919
11920 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011921
11922 if (sep != NULL)
11923 {
11924 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011925 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011926 ga_append(&ga, NUL);
11927 rettv->vval.v_string = (char_u *)ga.ga_data;
11928 }
11929 else
11930 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011931}
11932
11933/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011934 * "keys()" function
11935 */
11936 static void
11937f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 typval_T *argvars;
11939 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011940{
11941 dict_list(argvars, rettv, 0);
11942}
11943
11944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 * "last_buffer_nr()" function.
11946 */
11947/*ARGSUSED*/
11948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 typval_T *argvars;
11951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952{
11953 int n = 0;
11954 buf_T *buf;
11955
11956 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11957 if (n < buf->b_fnum)
11958 n = buf->b_fnum;
11959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011961}
11962
11963/*
11964 * "len()" function
11965 */
11966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 typval_T *argvars;
11969 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011970{
11971 switch (argvars[0].v_type)
11972 {
11973 case VAR_STRING:
11974 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975 rettv->vval.v_number = (varnumber_T)STRLEN(
11976 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011977 break;
11978 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011980 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011981 case VAR_DICT:
11982 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11983 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011984 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011985 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011986 break;
11987 }
11988}
11989
Bram Moolenaar33570922005-01-25 22:26:29 +000011990static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011991
11992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011994 typval_T *argvars;
11995 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011996 int type;
11997{
11998#ifdef FEAT_LIBCALL
11999 char_u *string_in;
12000 char_u **string_result;
12001 int nr_result;
12002#endif
12003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012004 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012005 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012006 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012007 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012009
12010 if (check_restricted() || check_secure())
12011 return;
12012
12013#ifdef FEAT_LIBCALL
12014 /* The first two args must be strings, otherwise its meaningless */
12015 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12016 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012017 string_in = NULL;
12018 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012019 string_in = argvars[2].vval.v_string;
12020 if (type == VAR_NUMBER)
12021 string_result = NULL;
12022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012024 if (mch_libcall(argvars[0].vval.v_string,
12025 argvars[1].vval.v_string,
12026 string_in,
12027 argvars[2].vval.v_number,
12028 string_result,
12029 &nr_result) == OK
12030 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012032 }
12033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034}
12035
12036/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012037 * "libcall()" function
12038 */
12039 static void
12040f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012041 typval_T *argvars;
12042 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012043{
12044 libcall_common(argvars, rettv, VAR_STRING);
12045}
12046
12047/*
12048 * "libcallnr()" function
12049 */
12050 static void
12051f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012054{
12055 libcall_common(argvars, rettv, VAR_NUMBER);
12056}
12057
12058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 * "line(string)" function
12060 */
12061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012063 typval_T *argvars;
12064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065{
12066 linenr_T lnum = 0;
12067 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012068 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012070 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 if (fp != NULL)
12072 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074}
12075
12076/*
12077 * "line2byte(lnum)" function
12078 */
12079/*ARGSUSED*/
12080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012082 typval_T *argvars;
12083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084{
12085#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087#else
12088 linenr_T lnum;
12089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12095 if (rettv->vval.v_number >= 0)
12096 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097#endif
12098}
12099
12100/*
12101 * "lispindent(lnum)" function
12102 */
12103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012104f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012105 typval_T *argvars;
12106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107{
12108#ifdef FEAT_LISP
12109 pos_T pos;
12110 linenr_T lnum;
12111
12112 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12115 {
12116 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 curwin->w_cursor = pos;
12119 }
12120 else
12121#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123}
12124
12125/*
12126 * "localtime()" function
12127 */
12128/*ARGSUSED*/
12129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 typval_T *argvars;
12132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135}
12136
Bram Moolenaar33570922005-01-25 22:26:29 +000012137static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138
12139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012141 typval_T *argvars;
12142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 int exact;
12144{
12145 char_u *keys;
12146 char_u *which;
12147 char_u buf[NUMBUFLEN];
12148 char_u *keys_buf = NULL;
12149 char_u *rhs;
12150 int mode;
12151 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012152 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153
12154 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->v_type = VAR_STRING;
12156 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 if (*keys == NUL)
12160 return;
12161
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012162 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012164 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012165 if (argvars[2].v_type != VAR_UNKNOWN)
12166 abbr = get_tv_number(&argvars[2]);
12167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 else
12169 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 if (which == NULL)
12171 return;
12172
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 mode = get_map_mode(&which, 0);
12174
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012175 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012176 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 vim_free(keys_buf);
12178 if (rhs != NULL)
12179 {
12180 ga_init(&ga);
12181 ga.ga_itemsize = 1;
12182 ga.ga_growsize = 40;
12183
12184 while (*rhs != NUL)
12185 ga_concat(&ga, str2special(&rhs, FALSE));
12186
12187 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 }
12190}
12191
12192/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012193 * "map()" function
12194 */
12195 static void
12196f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012197 typval_T *argvars;
12198 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012199{
12200 filter_map(argvars, rettv, TRUE);
12201}
12202
12203/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012204 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 */
12206 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012207f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012208 typval_T *argvars;
12209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012211 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212}
12213
12214/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012215 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 */
12217 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012218f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012219 typval_T *argvars;
12220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012222 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223}
12224
Bram Moolenaar33570922005-01-25 22:26:29 +000012225static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226
12227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012229 typval_T *argvars;
12230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 int type;
12232{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012233 char_u *str = NULL;
12234 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 char_u *pat;
12236 regmatch_T regmatch;
12237 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012238 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 char_u *save_cpo;
12240 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012241 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012242 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012243 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012244 list_T *l = NULL;
12245 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012246 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012247 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248
12249 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12250 save_cpo = p_cpo;
12251 p_cpo = (char_u *)"";
12252
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012253 rettv->vval.v_number = -1;
12254 if (type == 3)
12255 {
12256 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012257 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012258 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012259 }
12260 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262 rettv->v_type = VAR_STRING;
12263 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012266 if (argvars[0].v_type == VAR_LIST)
12267 {
12268 if ((l = argvars[0].vval.v_list) == NULL)
12269 goto theend;
12270 li = l->lv_first;
12271 }
12272 else
12273 expr = str = get_tv_string(&argvars[0]);
12274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12276 if (pat == NULL)
12277 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012278
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012279 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012281 int error = FALSE;
12282
12283 start = get_tv_number_chk(&argvars[2], &error);
12284 if (error)
12285 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012286 if (l != NULL)
12287 {
12288 li = list_find(l, start);
12289 if (li == NULL)
12290 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012291 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012292 }
12293 else
12294 {
12295 if (start < 0)
12296 start = 0;
12297 if (start > (long)STRLEN(str))
12298 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012299 /* When "count" argument is there ignore matches before "start",
12300 * otherwise skip part of the string. Differs when pattern is "^"
12301 * or "\<". */
12302 if (argvars[3].v_type != VAR_UNKNOWN)
12303 startcol = start;
12304 else
12305 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012306 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012308 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012309 nth = get_tv_number_chk(&argvars[3], &error);
12310 if (error)
12311 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 }
12313
12314 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12315 if (regmatch.regprog != NULL)
12316 {
12317 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012318
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012319 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012320 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012321 if (l != NULL)
12322 {
12323 if (li == NULL)
12324 {
12325 match = FALSE;
12326 break;
12327 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012328 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012329 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012330 if (str == NULL)
12331 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012332 }
12333
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012334 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012335
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012336 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012337 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012338 if (l == NULL && !match)
12339 break;
12340
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012341 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012342 if (l != NULL)
12343 {
12344 li = li->li_next;
12345 ++idx;
12346 }
12347 else
12348 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012349#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012350 startcol = (colnr_T)(regmatch.startp[0]
12351 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012352#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012353 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012354#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012355 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012356 }
12357
12358 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012360 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012361 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012362 int i;
12363
12364 /* return list with matched string and submatches */
12365 for (i = 0; i < NSUBEXP; ++i)
12366 {
12367 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012368 {
12369 if (list_append_string(rettv->vval.v_list,
12370 (char_u *)"", 0) == FAIL)
12371 break;
12372 }
12373 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012374 regmatch.startp[i],
12375 (int)(regmatch.endp[i] - regmatch.startp[i]))
12376 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012377 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012378 }
12379 }
12380 else if (type == 2)
12381 {
12382 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012383 if (l != NULL)
12384 copy_tv(&li->li_tv, rettv);
12385 else
12386 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012388 }
12389 else if (l != NULL)
12390 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 else
12392 {
12393 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 (varnumber_T)(regmatch.startp[0] - str);
12396 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012399 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 }
12401 }
12402 vim_free(regmatch.regprog);
12403 }
12404
12405theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012406 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 p_cpo = save_cpo;
12408}
12409
12410/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012411 * "match()" function
12412 */
12413 static void
12414f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012415 typval_T *argvars;
12416 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012417{
12418 find_some_match(argvars, rettv, 1);
12419}
12420
12421/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012422 * "matcharg()" function
12423 */
12424 static void
12425f_matcharg(argvars, rettv)
12426 typval_T *argvars;
12427 typval_T *rettv;
12428{
12429 if (rettv_list_alloc(rettv) == OK)
12430 {
12431#ifdef FEAT_SEARCH_EXTRA
12432 int mi = get_tv_number(&argvars[0]);
12433
12434 if (mi >= 1 && mi <= 3)
12435 {
12436 list_append_string(rettv->vval.v_list,
12437 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12438 list_append_string(rettv->vval.v_list,
12439 curwin->w_match_pat[mi - 1], -1);
12440 }
12441#endif
12442 }
12443}
12444
12445/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012446 * "matchend()" function
12447 */
12448 static void
12449f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012450 typval_T *argvars;
12451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012452{
12453 find_some_match(argvars, rettv, 0);
12454}
12455
12456/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012457 * "matchlist()" function
12458 */
12459 static void
12460f_matchlist(argvars, rettv)
12461 typval_T *argvars;
12462 typval_T *rettv;
12463{
12464 find_some_match(argvars, rettv, 3);
12465}
12466
12467/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012468 * "matchstr()" function
12469 */
12470 static void
12471f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012472 typval_T *argvars;
12473 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012474{
12475 find_some_match(argvars, rettv, 2);
12476}
12477
Bram Moolenaar33570922005-01-25 22:26:29 +000012478static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012479
12480 static void
12481max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012482 typval_T *argvars;
12483 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012484 int domax;
12485{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012486 long n = 0;
12487 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012488 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012489
12490 if (argvars[0].v_type == VAR_LIST)
12491 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012492 list_T *l;
12493 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012494
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012495 l = argvars[0].vval.v_list;
12496 if (l != NULL)
12497 {
12498 li = l->lv_first;
12499 if (li != NULL)
12500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012501 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012502 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012503 {
12504 li = li->li_next;
12505 if (li == NULL)
12506 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012507 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012508 if (domax ? i > n : i < n)
12509 n = i;
12510 }
12511 }
12512 }
12513 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012514 else if (argvars[0].v_type == VAR_DICT)
12515 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012516 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012517 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012518 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012519 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012520
12521 d = argvars[0].vval.v_dict;
12522 if (d != NULL)
12523 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012524 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012526 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012527 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012528 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012529 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012530 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012531 if (first)
12532 {
12533 n = i;
12534 first = FALSE;
12535 }
12536 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012537 n = i;
12538 }
12539 }
12540 }
12541 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012542 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012543 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012545}
12546
12547/*
12548 * "max()" function
12549 */
12550 static void
12551f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012552 typval_T *argvars;
12553 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012554{
12555 max_min(argvars, rettv, TRUE);
12556}
12557
12558/*
12559 * "min()" function
12560 */
12561 static void
12562f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012563 typval_T *argvars;
12564 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012565{
12566 max_min(argvars, rettv, FALSE);
12567}
12568
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012569static int mkdir_recurse __ARGS((char_u *dir, int prot));
12570
12571/*
12572 * Create the directory in which "dir" is located, and higher levels when
12573 * needed.
12574 */
12575 static int
12576mkdir_recurse(dir, prot)
12577 char_u *dir;
12578 int prot;
12579{
12580 char_u *p;
12581 char_u *updir;
12582 int r = FAIL;
12583
12584 /* Get end of directory name in "dir".
12585 * We're done when it's "/" or "c:/". */
12586 p = gettail_sep(dir);
12587 if (p <= get_past_head(dir))
12588 return OK;
12589
12590 /* If the directory exists we're done. Otherwise: create it.*/
12591 updir = vim_strnsave(dir, (int)(p - dir));
12592 if (updir == NULL)
12593 return FAIL;
12594 if (mch_isdir(updir))
12595 r = OK;
12596 else if (mkdir_recurse(updir, prot) == OK)
12597 r = vim_mkdir_emsg(updir, prot);
12598 vim_free(updir);
12599 return r;
12600}
12601
12602#ifdef vim_mkdir
12603/*
12604 * "mkdir()" function
12605 */
12606 static void
12607f_mkdir(argvars, rettv)
12608 typval_T *argvars;
12609 typval_T *rettv;
12610{
12611 char_u *dir;
12612 char_u buf[NUMBUFLEN];
12613 int prot = 0755;
12614
12615 rettv->vval.v_number = FAIL;
12616 if (check_restricted() || check_secure())
12617 return;
12618
12619 dir = get_tv_string_buf(&argvars[0], buf);
12620 if (argvars[1].v_type != VAR_UNKNOWN)
12621 {
12622 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 prot = get_tv_number_chk(&argvars[2], NULL);
12624 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012625 mkdir_recurse(dir, prot);
12626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012627 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012628}
12629#endif
12630
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 * "mode()" function
12633 */
12634/*ARGSUSED*/
12635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012636f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012637 typval_T *argvars;
12638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639{
12640 char_u buf[2];
12641
12642#ifdef FEAT_VISUAL
12643 if (VIsual_active)
12644 {
12645 if (VIsual_select)
12646 buf[0] = VIsual_mode + 's' - 'v';
12647 else
12648 buf[0] = VIsual_mode;
12649 }
12650 else
12651#endif
12652 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12653 buf[0] = 'r';
12654 else if (State & INSERT)
12655 {
12656 if (State & REPLACE_FLAG)
12657 buf[0] = 'R';
12658 else
12659 buf[0] = 'i';
12660 }
12661 else if (State & CMDLINE)
12662 buf[0] = 'c';
12663 else
12664 buf[0] = 'n';
12665
12666 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_string = vim_strsave(buf);
12668 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669}
12670
12671/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012672 * "nextnonblank()" function
12673 */
12674 static void
12675f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012678{
12679 linenr_T lnum;
12680
12681 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012684 {
12685 lnum = 0;
12686 break;
12687 }
12688 if (*skipwhite(ml_get(lnum)) != NUL)
12689 break;
12690 }
12691 rettv->vval.v_number = lnum;
12692}
12693
12694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 * "nr2char()" function
12696 */
12697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012699 typval_T *argvars;
12700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701{
12702 char_u buf[NUMBUFLEN];
12703
12704#ifdef FEAT_MBYTE
12705 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 else
12708#endif
12709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 buf[1] = NUL;
12712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012713 rettv->v_type = VAR_STRING;
12714 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012715}
12716
12717/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012718 * "pathshorten()" function
12719 */
12720 static void
12721f_pathshorten(argvars, rettv)
12722 typval_T *argvars;
12723 typval_T *rettv;
12724{
12725 char_u *p;
12726
12727 rettv->v_type = VAR_STRING;
12728 p = get_tv_string_chk(&argvars[0]);
12729 if (p == NULL)
12730 rettv->vval.v_string = NULL;
12731 else
12732 {
12733 p = vim_strsave(p);
12734 rettv->vval.v_string = p;
12735 if (p != NULL)
12736 shorten_dir(p);
12737 }
12738}
12739
12740/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012741 * "prevnonblank()" function
12742 */
12743 static void
12744f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012745 typval_T *argvars;
12746 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012747{
12748 linenr_T lnum;
12749
12750 lnum = get_tv_lnum(argvars);
12751 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12752 lnum = 0;
12753 else
12754 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12755 --lnum;
12756 rettv->vval.v_number = lnum;
12757}
12758
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012759#ifdef HAVE_STDARG_H
12760/* This dummy va_list is here because:
12761 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12762 * - locally in the function results in a "used before set" warning
12763 * - using va_start() to initialize it gives "function with fixed args" error */
12764static va_list ap;
12765#endif
12766
Bram Moolenaar8c711452005-01-14 21:53:12 +000012767/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012768 * "printf()" function
12769 */
12770 static void
12771f_printf(argvars, rettv)
12772 typval_T *argvars;
12773 typval_T *rettv;
12774{
12775 rettv->v_type = VAR_STRING;
12776 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012777#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012778 {
12779 char_u buf[NUMBUFLEN];
12780 int len;
12781 char_u *s;
12782 int saved_did_emsg = did_emsg;
12783 char *fmt;
12784
12785 /* Get the required length, allocate the buffer and do it for real. */
12786 did_emsg = FALSE;
12787 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012788 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012789 if (!did_emsg)
12790 {
12791 s = alloc(len + 1);
12792 if (s != NULL)
12793 {
12794 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012795 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012796 }
12797 }
12798 did_emsg |= saved_did_emsg;
12799 }
12800#endif
12801}
12802
12803/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012804 * "pumvisible()" function
12805 */
12806/*ARGSUSED*/
12807 static void
12808f_pumvisible(argvars, rettv)
12809 typval_T *argvars;
12810 typval_T *rettv;
12811{
12812 rettv->vval.v_number = 0;
12813#ifdef FEAT_INS_EXPAND
12814 if (pum_visible())
12815 rettv->vval.v_number = 1;
12816#endif
12817}
12818
12819/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012820 * "range()" function
12821 */
12822 static void
12823f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *argvars;
12825 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012826{
12827 long start;
12828 long end;
12829 long stride = 1;
12830 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012831 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012833 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012834 if (argvars[1].v_type == VAR_UNKNOWN)
12835 {
12836 end = start - 1;
12837 start = 0;
12838 }
12839 else
12840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012841 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012843 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012844 }
12845
12846 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012847 if (error)
12848 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012849 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012850 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012851 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012852 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012853 else
12854 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012855 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012856 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012857 if (list_append_number(rettv->vval.v_list,
12858 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012859 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012860 }
12861}
12862
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012863/*
12864 * "readfile()" function
12865 */
12866 static void
12867f_readfile(argvars, rettv)
12868 typval_T *argvars;
12869 typval_T *rettv;
12870{
12871 int binary = FALSE;
12872 char_u *fname;
12873 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012874 listitem_T *li;
12875#define FREAD_SIZE 200 /* optimized for text lines */
12876 char_u buf[FREAD_SIZE];
12877 int readlen; /* size of last fread() */
12878 int buflen; /* nr of valid chars in buf[] */
12879 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12880 int tolist; /* first byte in buf[] still to be put in list */
12881 int chop; /* how many CR to chop off */
12882 char_u *prev = NULL; /* previously read bytes, if any */
12883 int prevlen = 0; /* length of "prev" if not NULL */
12884 char_u *s;
12885 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012886 long maxline = MAXLNUM;
12887 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012888
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012889 if (argvars[1].v_type != VAR_UNKNOWN)
12890 {
12891 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12892 binary = TRUE;
12893 if (argvars[2].v_type != VAR_UNKNOWN)
12894 maxline = get_tv_number(&argvars[2]);
12895 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012896
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012897 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012898 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012899
12900 /* Always open the file in binary mode, library functions have a mind of
12901 * their own about CR-LF conversion. */
12902 fname = get_tv_string(&argvars[0]);
12903 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12904 {
12905 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12906 return;
12907 }
12908
12909 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012910 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012911 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012912 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012913 buflen = filtd + readlen;
12914 tolist = 0;
12915 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12916 {
12917 if (buf[filtd] == '\n' || readlen <= 0)
12918 {
12919 /* Only when in binary mode add an empty list item when the
12920 * last line ends in a '\n'. */
12921 if (!binary && readlen == 0 && filtd == 0)
12922 break;
12923
12924 /* Found end-of-line or end-of-file: add a text line to the
12925 * list. */
12926 chop = 0;
12927 if (!binary)
12928 while (filtd - chop - 1 >= tolist
12929 && buf[filtd - chop - 1] == '\r')
12930 ++chop;
12931 len = filtd - tolist - chop;
12932 if (prev == NULL)
12933 s = vim_strnsave(buf + tolist, len);
12934 else
12935 {
12936 s = alloc((unsigned)(prevlen + len + 1));
12937 if (s != NULL)
12938 {
12939 mch_memmove(s, prev, prevlen);
12940 vim_free(prev);
12941 prev = NULL;
12942 mch_memmove(s + prevlen, buf + tolist, len);
12943 s[prevlen + len] = NUL;
12944 }
12945 }
12946 tolist = filtd + 1;
12947
12948 li = listitem_alloc();
12949 if (li == NULL)
12950 {
12951 vim_free(s);
12952 break;
12953 }
12954 li->li_tv.v_type = VAR_STRING;
12955 li->li_tv.v_lock = 0;
12956 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012957 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012958
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012959 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012960 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012961 if (readlen <= 0)
12962 break;
12963 }
12964 else if (buf[filtd] == NUL)
12965 buf[filtd] = '\n';
12966 }
12967 if (readlen <= 0)
12968 break;
12969
12970 if (tolist == 0)
12971 {
12972 /* "buf" is full, need to move text to an allocated buffer */
12973 if (prev == NULL)
12974 {
12975 prev = vim_strnsave(buf, buflen);
12976 prevlen = buflen;
12977 }
12978 else
12979 {
12980 s = alloc((unsigned)(prevlen + buflen));
12981 if (s != NULL)
12982 {
12983 mch_memmove(s, prev, prevlen);
12984 mch_memmove(s + prevlen, buf, buflen);
12985 vim_free(prev);
12986 prev = s;
12987 prevlen += buflen;
12988 }
12989 }
12990 filtd = 0;
12991 }
12992 else
12993 {
12994 mch_memmove(buf, buf + tolist, buflen - tolist);
12995 filtd -= tolist;
12996 }
12997 }
12998
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012999 /*
13000 * For a negative line count use only the lines at the end of the file,
13001 * free the rest.
13002 */
13003 if (maxline < 0)
13004 while (cnt > -maxline)
13005 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013006 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013007 --cnt;
13008 }
13009
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013010 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013011 fclose(fd);
13012}
13013
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013014#if defined(FEAT_RELTIME)
13015static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13016
13017/*
13018 * Convert a List to proftime_T.
13019 * Return FAIL when there is something wrong.
13020 */
13021 static int
13022list2proftime(arg, tm)
13023 typval_T *arg;
13024 proftime_T *tm;
13025{
13026 long n1, n2;
13027 int error = FALSE;
13028
13029 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13030 || arg->vval.v_list->lv_len != 2)
13031 return FAIL;
13032 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13033 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13034# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013035 tm->HighPart = n1;
13036 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013037# else
13038 tm->tv_sec = n1;
13039 tm->tv_usec = n2;
13040# endif
13041 return error ? FAIL : OK;
13042}
13043#endif /* FEAT_RELTIME */
13044
13045/*
13046 * "reltime()" function
13047 */
13048 static void
13049f_reltime(argvars, rettv)
13050 typval_T *argvars;
13051 typval_T *rettv;
13052{
13053#ifdef FEAT_RELTIME
13054 proftime_T res;
13055 proftime_T start;
13056
13057 if (argvars[0].v_type == VAR_UNKNOWN)
13058 {
13059 /* No arguments: get current time. */
13060 profile_start(&res);
13061 }
13062 else if (argvars[1].v_type == VAR_UNKNOWN)
13063 {
13064 if (list2proftime(&argvars[0], &res) == FAIL)
13065 return;
13066 profile_end(&res);
13067 }
13068 else
13069 {
13070 /* Two arguments: compute the difference. */
13071 if (list2proftime(&argvars[0], &start) == FAIL
13072 || list2proftime(&argvars[1], &res) == FAIL)
13073 return;
13074 profile_sub(&res, &start);
13075 }
13076
13077 if (rettv_list_alloc(rettv) == OK)
13078 {
13079 long n1, n2;
13080
13081# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013082 n1 = res.HighPart;
13083 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013084# else
13085 n1 = res.tv_sec;
13086 n2 = res.tv_usec;
13087# endif
13088 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13089 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13090 }
13091#endif
13092}
13093
13094/*
13095 * "reltimestr()" function
13096 */
13097 static void
13098f_reltimestr(argvars, rettv)
13099 typval_T *argvars;
13100 typval_T *rettv;
13101{
13102#ifdef FEAT_RELTIME
13103 proftime_T tm;
13104#endif
13105
13106 rettv->v_type = VAR_STRING;
13107 rettv->vval.v_string = NULL;
13108#ifdef FEAT_RELTIME
13109 if (list2proftime(&argvars[0], &tm) == OK)
13110 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13111#endif
13112}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013113
Bram Moolenaar0d660222005-01-07 21:51:51 +000013114#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13115static void make_connection __ARGS((void));
13116static int check_connection __ARGS((void));
13117
13118 static void
13119make_connection()
13120{
13121 if (X_DISPLAY == NULL
13122# ifdef FEAT_GUI
13123 && !gui.in_use
13124# endif
13125 )
13126 {
13127 x_force_connect = TRUE;
13128 setup_term_clip();
13129 x_force_connect = FALSE;
13130 }
13131}
13132
13133 static int
13134check_connection()
13135{
13136 make_connection();
13137 if (X_DISPLAY == NULL)
13138 {
13139 EMSG(_("E240: No connection to Vim server"));
13140 return FAIL;
13141 }
13142 return OK;
13143}
13144#endif
13145
13146#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013147static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013148
13149 static void
13150remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013151 typval_T *argvars;
13152 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013153 int expr;
13154{
13155 char_u *server_name;
13156 char_u *keys;
13157 char_u *r = NULL;
13158 char_u buf[NUMBUFLEN];
13159# ifdef WIN32
13160 HWND w;
13161# else
13162 Window w;
13163# endif
13164
13165 if (check_restricted() || check_secure())
13166 return;
13167
13168# ifdef FEAT_X11
13169 if (check_connection() == FAIL)
13170 return;
13171# endif
13172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013173 server_name = get_tv_string_chk(&argvars[0]);
13174 if (server_name == NULL)
13175 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013176 keys = get_tv_string_buf(&argvars[1], buf);
13177# ifdef WIN32
13178 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13179# else
13180 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13181 < 0)
13182# endif
13183 {
13184 if (r != NULL)
13185 EMSG(r); /* sending worked but evaluation failed */
13186 else
13187 EMSG2(_("E241: Unable to send to %s"), server_name);
13188 return;
13189 }
13190
13191 rettv->vval.v_string = r;
13192
13193 if (argvars[2].v_type != VAR_UNKNOWN)
13194 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013195 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013196 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013198
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013199 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 v.di_tv.v_type = VAR_STRING;
13201 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013202 idvar = get_tv_string_chk(&argvars[2]);
13203 if (idvar != NULL)
13204 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013205 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013206 }
13207}
13208#endif
13209
13210/*
13211 * "remote_expr()" function
13212 */
13213/*ARGSUSED*/
13214 static void
13215f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013216 typval_T *argvars;
13217 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013218{
13219 rettv->v_type = VAR_STRING;
13220 rettv->vval.v_string = NULL;
13221#ifdef FEAT_CLIENTSERVER
13222 remote_common(argvars, rettv, TRUE);
13223#endif
13224}
13225
13226/*
13227 * "remote_foreground()" function
13228 */
13229/*ARGSUSED*/
13230 static void
13231f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 typval_T *argvars;
13233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013234{
13235 rettv->vval.v_number = 0;
13236#ifdef FEAT_CLIENTSERVER
13237# ifdef WIN32
13238 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013239 {
13240 char_u *server_name = get_tv_string_chk(&argvars[0]);
13241
13242 if (server_name != NULL)
13243 serverForeground(server_name);
13244 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013245# else
13246 /* Send a foreground() expression to the server. */
13247 argvars[1].v_type = VAR_STRING;
13248 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13249 argvars[2].v_type = VAR_UNKNOWN;
13250 remote_common(argvars, rettv, TRUE);
13251 vim_free(argvars[1].vval.v_string);
13252# endif
13253#endif
13254}
13255
13256/*ARGSUSED*/
13257 static void
13258f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013259 typval_T *argvars;
13260 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013261{
13262#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013263 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013264 char_u *s = NULL;
13265# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013266 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013267# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013269
13270 if (check_restricted() || check_secure())
13271 {
13272 rettv->vval.v_number = -1;
13273 return;
13274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 serverid = get_tv_string_chk(&argvars[0]);
13276 if (serverid == NULL)
13277 {
13278 rettv->vval.v_number = -1;
13279 return; /* type error; errmsg already given */
13280 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013281# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013282 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013283 if (n == 0)
13284 rettv->vval.v_number = -1;
13285 else
13286 {
13287 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13288 rettv->vval.v_number = (s != NULL);
13289 }
13290# else
13291 rettv->vval.v_number = 0;
13292 if (check_connection() == FAIL)
13293 return;
13294
13295 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013296 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013297# endif
13298
13299 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013301 char_u *retvar;
13302
Bram Moolenaar33570922005-01-25 22:26:29 +000013303 v.di_tv.v_type = VAR_STRING;
13304 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 retvar = get_tv_string_chk(&argvars[1]);
13306 if (retvar != NULL)
13307 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013308 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013309 }
13310#else
13311 rettv->vval.v_number = -1;
13312#endif
13313}
13314
13315/*ARGSUSED*/
13316 static void
13317f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013318 typval_T *argvars;
13319 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013320{
13321 char_u *r = NULL;
13322
13323#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 char_u *serverid = get_tv_string_chk(&argvars[0]);
13325
13326 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013327 {
13328# ifdef WIN32
13329 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013330 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013331
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013332 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013333 if (n != 0)
13334 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13335 if (r == NULL)
13336# else
13337 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013338 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013339# endif
13340 EMSG(_("E277: Unable to read a server reply"));
13341 }
13342#endif
13343 rettv->v_type = VAR_STRING;
13344 rettv->vval.v_string = r;
13345}
13346
13347/*
13348 * "remote_send()" function
13349 */
13350/*ARGSUSED*/
13351 static void
13352f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 typval_T *argvars;
13354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013355{
13356 rettv->v_type = VAR_STRING;
13357 rettv->vval.v_string = NULL;
13358#ifdef FEAT_CLIENTSERVER
13359 remote_common(argvars, rettv, FALSE);
13360#endif
13361}
13362
13363/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013364 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013365 */
13366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013367f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 typval_T *argvars;
13369 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013370{
Bram Moolenaar33570922005-01-25 22:26:29 +000013371 list_T *l;
13372 listitem_T *item, *item2;
13373 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013374 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013375 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013376 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 dict_T *d;
13378 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013379
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013380 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013381 if (argvars[0].v_type == VAR_DICT)
13382 {
13383 if (argvars[2].v_type != VAR_UNKNOWN)
13384 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013385 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013386 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013388 key = get_tv_string_chk(&argvars[1]);
13389 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013391 di = dict_find(d, key, -1);
13392 if (di == NULL)
13393 EMSG2(_(e_dictkey), key);
13394 else
13395 {
13396 *rettv = di->di_tv;
13397 init_tv(&di->di_tv);
13398 dictitem_remove(d, di);
13399 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013400 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013401 }
13402 }
13403 else if (argvars[0].v_type != VAR_LIST)
13404 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013405 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013406 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013408 int error = FALSE;
13409
13410 idx = get_tv_number_chk(&argvars[1], &error);
13411 if (error)
13412 ; /* type error: do nothing, errmsg already given */
13413 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013414 EMSGN(_(e_listidx), idx);
13415 else
13416 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013417 if (argvars[2].v_type == VAR_UNKNOWN)
13418 {
13419 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013420 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013421 *rettv = item->li_tv;
13422 vim_free(item);
13423 }
13424 else
13425 {
13426 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013427 end = get_tv_number_chk(&argvars[2], &error);
13428 if (error)
13429 ; /* type error: do nothing */
13430 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013431 EMSGN(_(e_listidx), end);
13432 else
13433 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013434 int cnt = 0;
13435
13436 for (li = item; li != NULL; li = li->li_next)
13437 {
13438 ++cnt;
13439 if (li == item2)
13440 break;
13441 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013442 if (li == NULL) /* didn't find "item2" after "item" */
13443 EMSG(_(e_invrange));
13444 else
13445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013446 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013447 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013448 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013449 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013450 l->lv_first = item;
13451 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013452 item->li_prev = NULL;
13453 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013454 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013455 }
13456 }
13457 }
13458 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013459 }
13460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461}
13462
13463/*
13464 * "rename({from}, {to})" function
13465 */
13466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013468 typval_T *argvars;
13469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470{
13471 char_u buf[NUMBUFLEN];
13472
13473 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13477 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478}
13479
13480/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013481 * "repeat()" function
13482 */
13483/*ARGSUSED*/
13484 static void
13485f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013486 typval_T *argvars;
13487 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013488{
13489 char_u *p;
13490 int n;
13491 int slen;
13492 int len;
13493 char_u *r;
13494 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013495
13496 n = get_tv_number(&argvars[1]);
13497 if (argvars[0].v_type == VAR_LIST)
13498 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013499 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013500 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013501 if (list_extend(rettv->vval.v_list,
13502 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013503 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013504 }
13505 else
13506 {
13507 p = get_tv_string(&argvars[0]);
13508 rettv->v_type = VAR_STRING;
13509 rettv->vval.v_string = NULL;
13510
13511 slen = (int)STRLEN(p);
13512 len = slen * n;
13513 if (len <= 0)
13514 return;
13515
13516 r = alloc(len + 1);
13517 if (r != NULL)
13518 {
13519 for (i = 0; i < n; i++)
13520 mch_memmove(r + i * slen, p, (size_t)slen);
13521 r[len] = NUL;
13522 }
13523
13524 rettv->vval.v_string = r;
13525 }
13526}
13527
13528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 * "resolve()" function
13530 */
13531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013533 typval_T *argvars;
13534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535{
13536 char_u *p;
13537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539#ifdef FEAT_SHORTCUT
13540 {
13541 char_u *v = NULL;
13542
13543 v = mch_resolve_shortcut(p);
13544 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 }
13549#else
13550# ifdef HAVE_READLINK
13551 {
13552 char_u buf[MAXPATHL + 1];
13553 char_u *cpy;
13554 int len;
13555 char_u *remain = NULL;
13556 char_u *q;
13557 int is_relative_to_current = FALSE;
13558 int has_trailing_pathsep = FALSE;
13559 int limit = 100;
13560
13561 p = vim_strsave(p);
13562
13563 if (p[0] == '.' && (vim_ispathsep(p[1])
13564 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13565 is_relative_to_current = TRUE;
13566
13567 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013568 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 has_trailing_pathsep = TRUE;
13570
13571 q = getnextcomp(p);
13572 if (*q != NUL)
13573 {
13574 /* Separate the first path component in "p", and keep the
13575 * remainder (beginning with the path separator). */
13576 remain = vim_strsave(q - 1);
13577 q[-1] = NUL;
13578 }
13579
13580 for (;;)
13581 {
13582 for (;;)
13583 {
13584 len = readlink((char *)p, (char *)buf, MAXPATHL);
13585 if (len <= 0)
13586 break;
13587 buf[len] = NUL;
13588
13589 if (limit-- == 0)
13590 {
13591 vim_free(p);
13592 vim_free(remain);
13593 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 goto fail;
13596 }
13597
13598 /* Ensure that the result will have a trailing path separator
13599 * if the argument has one. */
13600 if (remain == NULL && has_trailing_pathsep)
13601 add_pathsep(buf);
13602
13603 /* Separate the first path component in the link value and
13604 * concatenate the remainders. */
13605 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13606 if (*q != NUL)
13607 {
13608 if (remain == NULL)
13609 remain = vim_strsave(q - 1);
13610 else
13611 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013612 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 if (cpy != NULL)
13614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 vim_free(remain);
13616 remain = cpy;
13617 }
13618 }
13619 q[-1] = NUL;
13620 }
13621
13622 q = gettail(p);
13623 if (q > p && *q == NUL)
13624 {
13625 /* Ignore trailing path separator. */
13626 q[-1] = NUL;
13627 q = gettail(p);
13628 }
13629 if (q > p && !mch_isFullName(buf))
13630 {
13631 /* symlink is relative to directory of argument */
13632 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13633 if (cpy != NULL)
13634 {
13635 STRCPY(cpy, p);
13636 STRCPY(gettail(cpy), buf);
13637 vim_free(p);
13638 p = cpy;
13639 }
13640 }
13641 else
13642 {
13643 vim_free(p);
13644 p = vim_strsave(buf);
13645 }
13646 }
13647
13648 if (remain == NULL)
13649 break;
13650
13651 /* Append the first path component of "remain" to "p". */
13652 q = getnextcomp(remain + 1);
13653 len = q - remain - (*q != NUL);
13654 cpy = vim_strnsave(p, STRLEN(p) + len);
13655 if (cpy != NULL)
13656 {
13657 STRNCAT(cpy, remain, len);
13658 vim_free(p);
13659 p = cpy;
13660 }
13661 /* Shorten "remain". */
13662 if (*q != NUL)
13663 STRCPY(remain, q - 1);
13664 else
13665 {
13666 vim_free(remain);
13667 remain = NULL;
13668 }
13669 }
13670
13671 /* If the result is a relative path name, make it explicitly relative to
13672 * the current directory if and only if the argument had this form. */
13673 if (!vim_ispathsep(*p))
13674 {
13675 if (is_relative_to_current
13676 && *p != NUL
13677 && !(p[0] == '.'
13678 && (p[1] == NUL
13679 || vim_ispathsep(p[1])
13680 || (p[1] == '.'
13681 && (p[2] == NUL
13682 || vim_ispathsep(p[2]))))))
13683 {
13684 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013685 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 if (cpy != NULL)
13687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 vim_free(p);
13689 p = cpy;
13690 }
13691 }
13692 else if (!is_relative_to_current)
13693 {
13694 /* Strip leading "./". */
13695 q = p;
13696 while (q[0] == '.' && vim_ispathsep(q[1]))
13697 q += 2;
13698 if (q > p)
13699 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13700 }
13701 }
13702
13703 /* Ensure that the result will have no trailing path separator
13704 * if the argument had none. But keep "/" or "//". */
13705 if (!has_trailing_pathsep)
13706 {
13707 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013708 if (after_pathsep(p, q))
13709 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 }
13711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 }
13714# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716# endif
13717#endif
13718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720
13721#ifdef HAVE_READLINK
13722fail:
13723#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725}
13726
13727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013728 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 */
13730 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013731f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 typval_T *argvars;
13733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734{
Bram Moolenaar33570922005-01-25 22:26:29 +000013735 list_T *l;
13736 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
Bram Moolenaar0d660222005-01-07 21:51:51 +000013738 rettv->vval.v_number = 0;
13739 if (argvars[0].v_type != VAR_LIST)
13740 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013741 else if ((l = argvars[0].vval.v_list) != NULL
13742 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013743 {
13744 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013745 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013746 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013747 while (li != NULL)
13748 {
13749 ni = li->li_prev;
13750 list_append(l, li);
13751 li = ni;
13752 }
13753 rettv->vval.v_list = l;
13754 rettv->v_type = VAR_LIST;
13755 ++l->lv_refcount;
13756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757}
13758
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013759#define SP_NOMOVE 0x01 /* don't move cursor */
13760#define SP_REPEAT 0x02 /* repeat to find outer pair */
13761#define SP_RETCOUNT 0x04 /* return matchcount */
13762#define SP_SETPCMARK 0x08 /* set previous context mark */
13763#define SP_START 0x10 /* accept match at start position */
13764#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13765#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013766
Bram Moolenaar33570922005-01-25 22:26:29 +000013767static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013768
13769/*
13770 * Get flags for a search function.
13771 * Possibly sets "p_ws".
13772 * Returns BACKWARD, FORWARD or zero (for an error).
13773 */
13774 static int
13775get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013776 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013777 int *flagsp;
13778{
13779 int dir = FORWARD;
13780 char_u *flags;
13781 char_u nbuf[NUMBUFLEN];
13782 int mask;
13783
13784 if (varp->v_type != VAR_UNKNOWN)
13785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786 flags = get_tv_string_buf_chk(varp, nbuf);
13787 if (flags == NULL)
13788 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789 while (*flags != NUL)
13790 {
13791 switch (*flags)
13792 {
13793 case 'b': dir = BACKWARD; break;
13794 case 'w': p_ws = TRUE; break;
13795 case 'W': p_ws = FALSE; break;
13796 default: mask = 0;
13797 if (flagsp != NULL)
13798 switch (*flags)
13799 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013800 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013801 case 'e': mask = SP_END; break;
13802 case 'm': mask = SP_RETCOUNT; break;
13803 case 'n': mask = SP_NOMOVE; break;
13804 case 'p': mask = SP_SUBPAT; break;
13805 case 'r': mask = SP_REPEAT; break;
13806 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013807 }
13808 if (mask == 0)
13809 {
13810 EMSG2(_(e_invarg2), flags);
13811 dir = 0;
13812 }
13813 else
13814 *flagsp |= mask;
13815 }
13816 if (dir == 0)
13817 break;
13818 ++flags;
13819 }
13820 }
13821 return dir;
13822}
13823
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013825 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013827 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013828search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013829 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013830 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013831 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013833 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 char_u *pat;
13835 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013836 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 int save_p_ws = p_ws;
13838 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013839 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013840 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013841 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013842 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013844 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013845 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013846 if (dir == 0)
13847 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013848 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013849 if (flags & SP_START)
13850 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013851 if (flags & SP_END)
13852 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013853
13854 /* Optional extra argument: line number to stop searching. */
13855 if (argvars[1].v_type != VAR_UNKNOWN
13856 && argvars[2].v_type != VAR_UNKNOWN)
13857 {
13858 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13859 if (lnum_stop < 0)
13860 goto theend;
13861 }
13862
Bram Moolenaar231334e2005-07-25 20:46:57 +000013863 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013864 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013865 * Check to make sure only those flags are set.
13866 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13867 * flags cannot be set. Check for that condition also.
13868 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013869 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013870 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013872 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013873 goto theend;
13874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013876 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013877 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13878 options, RE_SEARCH, (linenr_T)lnum_stop);
13879 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013881 if (flags & SP_SUBPAT)
13882 retval = subpatnum;
13883 else
13884 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013885 if (flags & SP_SETPCMARK)
13886 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013888 if (match_pos != NULL)
13889 {
13890 /* Store the match cursor position */
13891 match_pos->lnum = pos.lnum;
13892 match_pos->col = pos.col + 1;
13893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 /* "/$" will put the cursor after the end of the line, may need to
13895 * correct that here */
13896 check_cursor();
13897 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013898
13899 /* If 'n' flag is used: restore cursor position. */
13900 if (flags & SP_NOMOVE)
13901 curwin->w_cursor = save_cursor;
13902theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013904
13905 return retval;
13906}
13907
13908/*
13909 * "search()" function
13910 */
13911 static void
13912f_search(argvars, rettv)
13913 typval_T *argvars;
13914 typval_T *rettv;
13915{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013916 int flags = 0;
13917
13918 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919}
13920
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013922 * "searchdecl()" function
13923 */
13924 static void
13925f_searchdecl(argvars, rettv)
13926 typval_T *argvars;
13927 typval_T *rettv;
13928{
13929 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013930 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013931 int error = FALSE;
13932 char_u *name;
13933
13934 rettv->vval.v_number = 1; /* default: FAIL */
13935
13936 name = get_tv_string_chk(&argvars[0]);
13937 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013938 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013939 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013940 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13941 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13942 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013943 if (!error && name != NULL)
13944 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013945 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013946}
13947
13948/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013949 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013951 static int
13952searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013953 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013954 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955{
13956 char_u *spat, *mpat, *epat;
13957 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 int dir;
13960 int flags = 0;
13961 char_u nbuf1[NUMBUFLEN];
13962 char_u nbuf2[NUMBUFLEN];
13963 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013964 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013965 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 spat = get_tv_string_chk(&argvars[0]);
13969 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13970 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13971 if (spat == NULL || mpat == NULL || epat == NULL)
13972 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 /* Handle the optional fourth argument: flags */
13975 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013976 if (dir == 0)
13977 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013978
13979 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013980 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13981 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013982 if ((flags & (SP_END | SP_SUBPAT)) != 0
13983 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013984 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013985 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013986 goto theend;
13987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013989 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013990 if (argvars[3].v_type == VAR_UNKNOWN
13991 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 skip = (char_u *)"";
13993 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013996 if (argvars[5].v_type != VAR_UNKNOWN)
13997 {
13998 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13999 if (lnum_stop < 0)
14000 goto theend;
14001 }
14002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003 if (skip == NULL)
14004 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014006 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14007 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014008
14009theend:
14010 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014011
14012 return retval;
14013}
14014
14015/*
14016 * "searchpair()" function
14017 */
14018 static void
14019f_searchpair(argvars, rettv)
14020 typval_T *argvars;
14021 typval_T *rettv;
14022{
14023 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14024}
14025
14026/*
14027 * "searchpairpos()" function
14028 */
14029 static void
14030f_searchpairpos(argvars, rettv)
14031 typval_T *argvars;
14032 typval_T *rettv;
14033{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014034 pos_T match_pos;
14035 int lnum = 0;
14036 int col = 0;
14037
14038 rettv->vval.v_number = 0;
14039
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014040 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014041 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014042
14043 if (searchpair_cmn(argvars, &match_pos) > 0)
14044 {
14045 lnum = match_pos.lnum;
14046 col = match_pos.col;
14047 }
14048
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014049 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14050 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014051}
14052
14053/*
14054 * Search for a start/middle/end thing.
14055 * Used by searchpair(), see its documentation for the details.
14056 * Returns 0 or -1 for no match,
14057 */
14058 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014059do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014060 char_u *spat; /* start pattern */
14061 char_u *mpat; /* middle pattern */
14062 char_u *epat; /* end pattern */
14063 int dir; /* BACKWARD or FORWARD */
14064 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014065 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014066 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014067 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014068{
14069 char_u *save_cpo;
14070 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14071 long retval = 0;
14072 pos_T pos;
14073 pos_T firstpos;
14074 pos_T foundpos;
14075 pos_T save_cursor;
14076 pos_T save_pos;
14077 int n;
14078 int r;
14079 int nest = 1;
14080 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014081 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014082
14083 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14084 save_cpo = p_cpo;
14085 p_cpo = (char_u *)"";
14086
14087 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14088 * start/middle/end (pat3, for the top pair). */
14089 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14090 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14091 if (pat2 == NULL || pat3 == NULL)
14092 goto theend;
14093 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14094 if (*mpat == NUL)
14095 STRCPY(pat3, pat2);
14096 else
14097 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14098 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014099 if (flags & SP_START)
14100 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014101
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 save_cursor = curwin->w_cursor;
14103 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014104 clearpos(&firstpos);
14105 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 pat = pat3;
14107 for (;;)
14108 {
14109 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014110 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14112 /* didn't find it or found the first match again: FAIL */
14113 break;
14114
14115 if (firstpos.lnum == 0)
14116 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014117 if (equalpos(pos, foundpos))
14118 {
14119 /* Found the same position again. Can happen with a pattern that
14120 * has "\zs" at the end and searching backwards. Advance one
14121 * character and try again. */
14122 if (dir == BACKWARD)
14123 decl(&pos);
14124 else
14125 incl(&pos);
14126 }
14127 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128
14129 /* If the skip pattern matches, ignore this match. */
14130 if (*skip != NUL)
14131 {
14132 save_pos = curwin->w_cursor;
14133 curwin->w_cursor = pos;
14134 r = eval_to_bool(skip, &err, NULL, FALSE);
14135 curwin->w_cursor = save_pos;
14136 if (err)
14137 {
14138 /* Evaluating {skip} caused an error, break here. */
14139 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014140 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 break;
14142 }
14143 if (r)
14144 continue;
14145 }
14146
14147 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14148 {
14149 /* Found end when searching backwards or start when searching
14150 * forward: nested pair. */
14151 ++nest;
14152 pat = pat2; /* nested, don't search for middle */
14153 }
14154 else
14155 {
14156 /* Found end when searching forward or start when searching
14157 * backward: end of (nested) pair; or found middle in outer pair. */
14158 if (--nest == 1)
14159 pat = pat3; /* outer level, search for middle */
14160 }
14161
14162 if (nest == 0)
14163 {
14164 /* Found the match: return matchcount or line number. */
14165 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014166 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014168 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014169 if (flags & SP_SETPCMARK)
14170 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 curwin->w_cursor = pos;
14172 if (!(flags & SP_REPEAT))
14173 break;
14174 nest = 1; /* search for next unmatched */
14175 }
14176 }
14177
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014178 if (match_pos != NULL)
14179 {
14180 /* Store the match cursor position */
14181 match_pos->lnum = curwin->w_cursor.lnum;
14182 match_pos->col = curwin->w_cursor.col + 1;
14183 }
14184
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014186 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 curwin->w_cursor = save_cursor;
14188
14189theend:
14190 vim_free(pat2);
14191 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014193
14194 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195}
14196
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014197/*
14198 * "searchpos()" function
14199 */
14200 static void
14201f_searchpos(argvars, rettv)
14202 typval_T *argvars;
14203 typval_T *rettv;
14204{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014205 pos_T match_pos;
14206 int lnum = 0;
14207 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014208 int n;
14209 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014210
14211 rettv->vval.v_number = 0;
14212
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014213 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014214 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014215
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014216 n = search_cmn(argvars, &match_pos, &flags);
14217 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014218 {
14219 lnum = match_pos.lnum;
14220 col = match_pos.col;
14221 }
14222
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014223 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14224 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014225 if (flags & SP_SUBPAT)
14226 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014227}
14228
14229
Bram Moolenaar0d660222005-01-07 21:51:51 +000014230/*ARGSUSED*/
14231 static void
14232f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014233 typval_T *argvars;
14234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014236#ifdef FEAT_CLIENTSERVER
14237 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 char_u *server = get_tv_string_chk(&argvars[0]);
14239 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240
Bram Moolenaar0d660222005-01-07 21:51:51 +000014241 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 if (server == NULL || reply == NULL)
14243 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244 if (check_restricted() || check_secure())
14245 return;
14246# ifdef FEAT_X11
14247 if (check_connection() == FAIL)
14248 return;
14249# endif
14250
14251 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014253 EMSG(_("E258: Unable to send to client"));
14254 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256 rettv->vval.v_number = 0;
14257#else
14258 rettv->vval.v_number = -1;
14259#endif
14260}
14261
14262/*ARGSUSED*/
14263 static void
14264f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014265 typval_T *argvars;
14266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267{
14268 char_u *r = NULL;
14269
14270#ifdef FEAT_CLIENTSERVER
14271# ifdef WIN32
14272 r = serverGetVimNames();
14273# else
14274 make_connection();
14275 if (X_DISPLAY != NULL)
14276 r = serverGetVimNames(X_DISPLAY);
14277# endif
14278#endif
14279 rettv->v_type = VAR_STRING;
14280 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281}
14282
14283/*
14284 * "setbufvar()" function
14285 */
14286/*ARGSUSED*/
14287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014289 typval_T *argvars;
14290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291{
14292 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014295 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 char_u nbuf[NUMBUFLEN];
14297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 rettv->vval.v_number = 0;
14299
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 if (check_restricted() || check_secure())
14301 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14303 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014304 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 varp = &argvars[2];
14306
14307 if (buf != NULL && varname != NULL && varp != NULL)
14308 {
14309 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311
14312 if (*varname == '&')
14313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014314 long numval;
14315 char_u *strval;
14316 int error = FALSE;
14317
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014319 numval = get_tv_number_chk(varp, &error);
14320 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 if (!error && strval != NULL)
14322 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 }
14324 else
14325 {
14326 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14327 if (bufvarname != NULL)
14328 {
14329 STRCPY(bufvarname, "b:");
14330 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014331 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 vim_free(bufvarname);
14333 }
14334 }
14335
14336 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339}
14340
14341/*
14342 * "setcmdpos()" function
14343 */
14344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014346 typval_T *argvars;
14347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 int pos = (int)get_tv_number(&argvars[0]) - 1;
14350
14351 if (pos >= 0)
14352 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353}
14354
14355/*
14356 * "setline()" function
14357 */
14358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014359f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 typval_T *argvars;
14361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362{
14363 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014364 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014365 list_T *l = NULL;
14366 listitem_T *li = NULL;
14367 long added = 0;
14368 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014370 lnum = get_tv_lnum(&argvars[0]);
14371 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014373 l = argvars[1].vval.v_list;
14374 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014376 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014378
14379 rettv->vval.v_number = 0; /* OK */
14380 for (;;)
14381 {
14382 if (l != NULL)
14383 {
14384 /* list argument, get next string */
14385 if (li == NULL)
14386 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014388 li = li->li_next;
14389 }
14390
14391 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014392 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014393 break;
14394 if (lnum <= curbuf->b_ml.ml_line_count)
14395 {
14396 /* existing line, replace it */
14397 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14398 {
14399 changed_bytes(lnum, 0);
14400 check_cursor_col();
14401 rettv->vval.v_number = 0; /* OK */
14402 }
14403 }
14404 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14405 {
14406 /* lnum is one past the last line, append the line */
14407 ++added;
14408 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14409 rettv->vval.v_number = 0; /* OK */
14410 }
14411
14412 if (l == NULL) /* only one string argument */
14413 break;
14414 ++lnum;
14415 }
14416
14417 if (added > 0)
14418 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419}
14420
14421/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014422 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014423 */
14424/*ARGSUSED*/
14425 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014426set_qf_ll_list(wp, list_arg, action_arg, rettv)
14427 win_T *wp;
14428 typval_T *list_arg;
14429 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014430 typval_T *rettv;
14431{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014432#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014433 char_u *act;
14434 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014435#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014436
Bram Moolenaar2641f772005-03-25 21:58:17 +000014437 rettv->vval.v_number = -1;
14438
14439#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014440 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014441 EMSG(_(e_listreq));
14442 else
14443 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014444 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014445
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014446 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014447 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014448 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 if (act == NULL)
14450 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014451 if (*act == 'a' || *act == 'r')
14452 action = *act;
14453 }
14454
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014455 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014456 rettv->vval.v_number = 0;
14457 }
14458#endif
14459}
14460
14461/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014462 * "setloclist()" function
14463 */
14464/*ARGSUSED*/
14465 static void
14466f_setloclist(argvars, rettv)
14467 typval_T *argvars;
14468 typval_T *rettv;
14469{
14470 win_T *win;
14471
14472 rettv->vval.v_number = -1;
14473
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014474 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014475 if (win != NULL)
14476 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14477}
14478
14479/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014480 * "setpos()" function
14481 */
14482/*ARGSUSED*/
14483 static void
14484f_setpos(argvars, rettv)
14485 typval_T *argvars;
14486 typval_T *rettv;
14487{
14488 pos_T pos;
14489 int fnum;
14490 char_u *name;
14491
14492 name = get_tv_string_chk(argvars);
14493 if (name != NULL)
14494 {
14495 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14496 {
14497 --pos.col;
14498 if (name[0] == '.') /* cursor */
14499 {
14500 if (fnum == curbuf->b_fnum)
14501 {
14502 curwin->w_cursor = pos;
14503 check_cursor();
14504 }
14505 else
14506 EMSG(_(e_invarg));
14507 }
14508 else if (name[0] == '\'') /* mark */
14509 (void)setmark_pos(name[1], &pos, fnum);
14510 else
14511 EMSG(_(e_invarg));
14512 }
14513 }
14514}
14515
14516/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014517 * "setqflist()" function
14518 */
14519/*ARGSUSED*/
14520 static void
14521f_setqflist(argvars, rettv)
14522 typval_T *argvars;
14523 typval_T *rettv;
14524{
14525 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14526}
14527
14528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 * "setreg()" function
14530 */
14531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014533 typval_T *argvars;
14534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535{
14536 int regname;
14537 char_u *strregname;
14538 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014539 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 int append;
14541 char_u yank_type;
14542 long block_len;
14543
14544 block_len = -1;
14545 yank_type = MAUTO;
14546 append = FALSE;
14547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014549 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551 if (strregname == NULL)
14552 return; /* type error; errmsg already given */
14553 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 if (regname == 0 || regname == '@')
14555 regname = '"';
14556 else if (regname == '=')
14557 return;
14558
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014561 stropt = get_tv_string_chk(&argvars[2]);
14562 if (stropt == NULL)
14563 return; /* type error */
14564 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 switch (*stropt)
14566 {
14567 case 'a': case 'A': /* append */
14568 append = TRUE;
14569 break;
14570 case 'v': case 'c': /* character-wise selection */
14571 yank_type = MCHAR;
14572 break;
14573 case 'V': case 'l': /* line-wise selection */
14574 yank_type = MLINE;
14575 break;
14576#ifdef FEAT_VISUAL
14577 case 'b': case Ctrl_V: /* block-wise selection */
14578 yank_type = MBLOCK;
14579 if (VIM_ISDIGIT(stropt[1]))
14580 {
14581 ++stropt;
14582 block_len = getdigits(&stropt) - 1;
14583 --stropt;
14584 }
14585 break;
14586#endif
14587 }
14588 }
14589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014590 strval = get_tv_string_chk(&argvars[1]);
14591 if (strval != NULL)
14592 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595}
14596
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014597/*
14598 * "settabwinvar()" function
14599 */
14600 static void
14601f_settabwinvar(argvars, rettv)
14602 typval_T *argvars;
14603 typval_T *rettv;
14604{
14605 setwinvar(argvars, rettv, 1);
14606}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607
14608/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014609 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014613 typval_T *argvars;
14614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014616 setwinvar(argvars, rettv, 0);
14617}
14618
14619/*
14620 * "setwinvar()" and "settabwinvar()" functions
14621 */
14622 static void
14623setwinvar(argvars, rettv, off)
14624 typval_T *argvars;
14625 typval_T *rettv;
14626 int off;
14627{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 win_T *win;
14629#ifdef FEAT_WINDOWS
14630 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014631 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632#endif
14633 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014634 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014636 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014638 rettv->vval.v_number = 0;
14639
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 if (check_restricted() || check_secure())
14641 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014642
14643#ifdef FEAT_WINDOWS
14644 if (off == 1)
14645 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14646 else
14647 tp = curtab;
14648#endif
14649 win = find_win_by_nr(&argvars[off], tp);
14650 varname = get_tv_string_chk(&argvars[off + 1]);
14651 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652
14653 if (win != NULL && varname != NULL && varp != NULL)
14654 {
14655#ifdef FEAT_WINDOWS
14656 /* set curwin to be our win, temporarily */
14657 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014658 save_curtab = curtab;
14659 goto_tabpage_tp(tp);
14660 if (!win_valid(win))
14661 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662 curwin = win;
14663 curbuf = curwin->w_buffer;
14664#endif
14665
14666 if (*varname == '&')
14667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 long numval;
14669 char_u *strval;
14670 int error = FALSE;
14671
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014673 numval = get_tv_number_chk(varp, &error);
14674 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 if (!error && strval != NULL)
14676 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 }
14678 else
14679 {
14680 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14681 if (winvarname != NULL)
14682 {
14683 STRCPY(winvarname, "w:");
14684 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014685 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 vim_free(winvarname);
14687 }
14688 }
14689
14690#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014691 /* Restore current tabpage and window, if still valid (autocomands can
14692 * make them invalid). */
14693 if (valid_tabpage(save_curtab))
14694 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 if (win_valid(save_curwin))
14696 {
14697 curwin = save_curwin;
14698 curbuf = curwin->w_buffer;
14699 }
14700#endif
14701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702}
14703
14704/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014705 * "shellescape({string})" function
14706 */
14707 static void
14708f_shellescape(argvars, rettv)
14709 typval_T *argvars;
14710 typval_T *rettv;
14711{
14712 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14713 rettv->v_type = VAR_STRING;
14714}
14715
14716/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014717 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718 */
14719 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 typval_T *argvars;
14722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726 p = get_tv_string(&argvars[0]);
14727 rettv->vval.v_string = vim_strsave(p);
14728 simplify_filename(rettv->vval.v_string); /* simplify in place */
14729 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730}
14731
Bram Moolenaar0d660222005-01-07 21:51:51 +000014732static int
14733#ifdef __BORLANDC__
14734 _RTLENTRYF
14735#endif
14736 item_compare __ARGS((const void *s1, const void *s2));
14737static int
14738#ifdef __BORLANDC__
14739 _RTLENTRYF
14740#endif
14741 item_compare2 __ARGS((const void *s1, const void *s2));
14742
14743static int item_compare_ic;
14744static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014745static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014746#define ITEM_COMPARE_FAIL 999
14747
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751 static int
14752#ifdef __BORLANDC__
14753_RTLENTRYF
14754#endif
14755item_compare(s1, s2)
14756 const void *s1;
14757 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014759 char_u *p1, *p2;
14760 char_u *tofree1, *tofree2;
14761 int res;
14762 char_u numbuf1[NUMBUFLEN];
14763 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014765 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14766 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014767 if (item_compare_ic)
14768 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014770 res = STRCMP(p1, p2);
14771 vim_free(tofree1);
14772 vim_free(tofree2);
14773 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774}
14775
14776 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014777#ifdef __BORLANDC__
14778_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014780item_compare2(s1, s2)
14781 const void *s1;
14782 const void *s2;
14783{
14784 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014786 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014787 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 /* shortcut after failure in previous call; compare all items equal */
14790 if (item_compare_func_err)
14791 return 0;
14792
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014793 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14794 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14796 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014797
14798 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014799 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014800 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014801 clear_tv(&argv[0]);
14802 clear_tv(&argv[1]);
14803
14804 if (res == FAIL)
14805 res = ITEM_COMPARE_FAIL;
14806 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014807 /* return value has wrong type */
14808 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14809 if (item_compare_func_err)
14810 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014811 clear_tv(&rettv);
14812 return res;
14813}
14814
14815/*
14816 * "sort({list})" function
14817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014819f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014820 typval_T *argvars;
14821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822{
Bram Moolenaar33570922005-01-25 22:26:29 +000014823 list_T *l;
14824 listitem_T *li;
14825 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014826 long len;
14827 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828
Bram Moolenaar0d660222005-01-07 21:51:51 +000014829 rettv->vval.v_number = 0;
14830 if (argvars[0].v_type != VAR_LIST)
14831 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 else
14833 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014834 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014835 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014836 return;
14837 rettv->vval.v_list = l;
14838 rettv->v_type = VAR_LIST;
14839 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841 len = list_len(l);
14842 if (len <= 1)
14843 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844
Bram Moolenaar0d660222005-01-07 21:51:51 +000014845 item_compare_ic = FALSE;
14846 item_compare_func = NULL;
14847 if (argvars[1].v_type != VAR_UNKNOWN)
14848 {
14849 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014850 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014851 else
14852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014853 int error = FALSE;
14854
14855 i = get_tv_number_chk(&argvars[1], &error);
14856 if (error)
14857 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014858 if (i == 1)
14859 item_compare_ic = TRUE;
14860 else
14861 item_compare_func = get_tv_string(&argvars[1]);
14862 }
14863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864
Bram Moolenaar0d660222005-01-07 21:51:51 +000014865 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014866 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014867 if (ptrs == NULL)
14868 return;
14869 i = 0;
14870 for (li = l->lv_first; li != NULL; li = li->li_next)
14871 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014873 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874 /* test the compare function */
14875 if (item_compare_func != NULL
14876 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14877 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014878 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 {
14881 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014882 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014883 item_compare_func == NULL ? item_compare : item_compare2);
14884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885 if (!item_compare_func_err)
14886 {
14887 /* Clear the List and append the items in the sorted order. */
14888 l->lv_first = l->lv_last = NULL;
14889 l->lv_len = 0;
14890 for (i = 0; i < len; ++i)
14891 list_append(l, ptrs[i]);
14892 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014893 }
14894
14895 vim_free(ptrs);
14896 }
14897}
14898
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014899/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014900 * "soundfold({word})" function
14901 */
14902 static void
14903f_soundfold(argvars, rettv)
14904 typval_T *argvars;
14905 typval_T *rettv;
14906{
14907 char_u *s;
14908
14909 rettv->v_type = VAR_STRING;
14910 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014911#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014912 rettv->vval.v_string = eval_soundfold(s);
14913#else
14914 rettv->vval.v_string = vim_strsave(s);
14915#endif
14916}
14917
14918/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014919 * "spellbadword()" function
14920 */
14921/* ARGSUSED */
14922 static void
14923f_spellbadword(argvars, rettv)
14924 typval_T *argvars;
14925 typval_T *rettv;
14926{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014927 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014928 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014929 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014930
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014931 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014932 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014933
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014934#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014935 if (argvars[0].v_type == VAR_UNKNOWN)
14936 {
14937 /* Find the start and length of the badly spelled word. */
14938 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14939 if (len != 0)
14940 word = ml_get_cursor();
14941 }
14942 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14943 {
14944 char_u *str = get_tv_string_chk(&argvars[0]);
14945 int capcol = -1;
14946
14947 if (str != NULL)
14948 {
14949 /* Check the argument for spelling. */
14950 while (*str != NUL)
14951 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014952 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014953 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014954 {
14955 word = str;
14956 break;
14957 }
14958 str += len;
14959 }
14960 }
14961 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014962#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014964 list_append_string(rettv->vval.v_list, word, len);
14965 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014966 attr == HLF_SPB ? "bad" :
14967 attr == HLF_SPR ? "rare" :
14968 attr == HLF_SPL ? "local" :
14969 attr == HLF_SPC ? "caps" :
14970 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014971}
14972
14973/*
14974 * "spellsuggest()" function
14975 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014976/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014977 static void
14978f_spellsuggest(argvars, rettv)
14979 typval_T *argvars;
14980 typval_T *rettv;
14981{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014982#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014983 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014984 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014985 int maxcount;
14986 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014987 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014988 listitem_T *li;
14989 int need_capital = FALSE;
14990#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014991
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014992 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014993 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014994
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014995#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014996 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14997 {
14998 str = get_tv_string(&argvars[0]);
14999 if (argvars[1].v_type != VAR_UNKNOWN)
15000 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015001 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015002 if (maxcount <= 0)
15003 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015004 if (argvars[2].v_type != VAR_UNKNOWN)
15005 {
15006 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15007 if (typeerr)
15008 return;
15009 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015010 }
15011 else
15012 maxcount = 25;
15013
Bram Moolenaar4770d092006-01-12 23:22:24 +000015014 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015015
15016 for (i = 0; i < ga.ga_len; ++i)
15017 {
15018 str = ((char_u **)ga.ga_data)[i];
15019
15020 li = listitem_alloc();
15021 if (li == NULL)
15022 vim_free(str);
15023 else
15024 {
15025 li->li_tv.v_type = VAR_STRING;
15026 li->li_tv.v_lock = 0;
15027 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015028 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015029 }
15030 }
15031 ga_clear(&ga);
15032 }
15033#endif
15034}
15035
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015037f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015038 typval_T *argvars;
15039 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040{
15041 char_u *str;
15042 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015043 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044 regmatch_T regmatch;
15045 char_u patbuf[NUMBUFLEN];
15046 char_u *save_cpo;
15047 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015049 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051
15052 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15053 save_cpo = p_cpo;
15054 p_cpo = (char_u *)"";
15055
15056 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015057 if (argvars[1].v_type != VAR_UNKNOWN)
15058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015059 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15060 if (pat == NULL)
15061 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015062 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015063 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015064 }
15065 if (pat == NULL || *pat == NUL)
15066 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015068 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 if (typeerr)
15071 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15074 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015077 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015079 if (*str == NUL)
15080 match = FALSE; /* empty item at the end */
15081 else
15082 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 if (match)
15084 end = regmatch.startp[0];
15085 else
15086 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015087 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15088 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015090 if (list_append_string(rettv->vval.v_list, str,
15091 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 }
15094 if (!match)
15095 break;
15096 /* Advance to just after the match. */
15097 if (regmatch.endp[0] > str)
15098 col = 0;
15099 else
15100 {
15101 /* Don't get stuck at the same match. */
15102#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015103 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104#else
15105 col = 1;
15106#endif
15107 }
15108 str = regmatch.endp[0];
15109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113
Bram Moolenaar0d660222005-01-07 21:51:51 +000015114 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115}
15116
Bram Moolenaar2c932302006-03-18 21:42:09 +000015117/*
15118 * "str2nr()" function
15119 */
15120 static void
15121f_str2nr(argvars, rettv)
15122 typval_T *argvars;
15123 typval_T *rettv;
15124{
15125 int base = 10;
15126 char_u *p;
15127 long n;
15128
15129 if (argvars[1].v_type != VAR_UNKNOWN)
15130 {
15131 base = get_tv_number(&argvars[1]);
15132 if (base != 8 && base != 10 && base != 16)
15133 {
15134 EMSG(_(e_invarg));
15135 return;
15136 }
15137 }
15138
15139 p = skipwhite(get_tv_string(&argvars[0]));
15140 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15141 rettv->vval.v_number = n;
15142}
15143
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144#ifdef HAVE_STRFTIME
15145/*
15146 * "strftime({format}[, {time}])" function
15147 */
15148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015150 typval_T *argvars;
15151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152{
15153 char_u result_buf[256];
15154 struct tm *curtime;
15155 time_t seconds;
15156 char_u *p;
15157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015158 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015160 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015161 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 seconds = time(NULL);
15163 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015164 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165 curtime = localtime(&seconds);
15166 /* MSVC returns NULL for an invalid value of seconds. */
15167 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015168 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169 else
15170 {
15171# ifdef FEAT_MBYTE
15172 vimconv_T conv;
15173 char_u *enc;
15174
15175 conv.vc_type = CONV_NONE;
15176 enc = enc_locale();
15177 convert_setup(&conv, p_enc, enc);
15178 if (conv.vc_type != CONV_NONE)
15179 p = string_convert(&conv, p, NULL);
15180# endif
15181 if (p != NULL)
15182 (void)strftime((char *)result_buf, sizeof(result_buf),
15183 (char *)p, curtime);
15184 else
15185 result_buf[0] = NUL;
15186
15187# ifdef FEAT_MBYTE
15188 if (conv.vc_type != CONV_NONE)
15189 vim_free(p);
15190 convert_setup(&conv, enc, p_enc);
15191 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015192 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193 else
15194# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015195 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196
15197# ifdef FEAT_MBYTE
15198 /* Release conversion descriptors */
15199 convert_setup(&conv, NULL, NULL);
15200 vim_free(enc);
15201# endif
15202 }
15203}
15204#endif
15205
15206/*
15207 * "stridx()" function
15208 */
15209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015210f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015211 typval_T *argvars;
15212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213{
15214 char_u buf[NUMBUFLEN];
15215 char_u *needle;
15216 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015217 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015221 needle = get_tv_string_chk(&argvars[1]);
15222 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015223 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 if (needle == NULL || haystack == NULL)
15225 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226
Bram Moolenaar33570922005-01-25 22:26:29 +000015227 if (argvars[2].v_type != VAR_UNKNOWN)
15228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 int error = FALSE;
15230
15231 start_idx = get_tv_number_chk(&argvars[2], &error);
15232 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015233 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015234 if (start_idx >= 0)
15235 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015236 }
15237
15238 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15239 if (pos != NULL)
15240 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241}
15242
15243/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015244 * "string()" function
15245 */
15246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 typval_T *argvars;
15249 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015250{
15251 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015252 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015254 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015255 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015256 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258}
15259
15260/*
15261 * "strlen()" function
15262 */
15263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015264f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015265 typval_T *argvars;
15266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268 rettv->vval.v_number = (varnumber_T)(STRLEN(
15269 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270}
15271
15272/*
15273 * "strpart()" function
15274 */
15275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015276f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015277 typval_T *argvars;
15278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279{
15280 char_u *p;
15281 int n;
15282 int len;
15283 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015284 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 slen = (int)STRLEN(p);
15288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015289 n = get_tv_number_chk(&argvars[1], &error);
15290 if (error)
15291 len = 0;
15292 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015293 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 else
15295 len = slen - n; /* default len: all bytes that are available. */
15296
15297 /*
15298 * Only return the overlap between the specified part and the actual
15299 * string.
15300 */
15301 if (n < 0)
15302 {
15303 len += n;
15304 n = 0;
15305 }
15306 else if (n > slen)
15307 n = slen;
15308 if (len < 0)
15309 len = 0;
15310 else if (n + len > slen)
15311 len = slen - n;
15312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015313 rettv->v_type = VAR_STRING;
15314 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315}
15316
15317/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015318 * "strridx()" function
15319 */
15320 static void
15321f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015322 typval_T *argvars;
15323 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324{
15325 char_u buf[NUMBUFLEN];
15326 char_u *needle;
15327 char_u *haystack;
15328 char_u *rest;
15329 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015330 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015332 needle = get_tv_string_chk(&argvars[1]);
15333 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015334
15335 rettv->vval.v_number = -1;
15336 if (needle == NULL || haystack == NULL)
15337 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015338
15339 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015340 if (argvars[2].v_type != VAR_UNKNOWN)
15341 {
15342 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015343 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015344 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015345 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015346 }
15347 else
15348 end_idx = haystack_len;
15349
Bram Moolenaar0d660222005-01-07 21:51:51 +000015350 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015351 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015352 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015353 lastmatch = haystack + end_idx;
15354 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015355 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015356 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015357 for (rest = haystack; *rest != '\0'; ++rest)
15358 {
15359 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015360 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015361 break;
15362 lastmatch = rest;
15363 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015364 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015365
15366 if (lastmatch == NULL)
15367 rettv->vval.v_number = -1;
15368 else
15369 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15370}
15371
15372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 * "strtrans()" function
15374 */
15375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015376f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015377 typval_T *argvars;
15378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015380 rettv->v_type = VAR_STRING;
15381 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382}
15383
15384/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015385 * "submatch()" function
15386 */
15387 static void
15388f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015389 typval_T *argvars;
15390 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015391{
15392 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015393 rettv->vval.v_string =
15394 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015395}
15396
15397/*
15398 * "substitute()" function
15399 */
15400 static void
15401f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015402 typval_T *argvars;
15403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015404{
15405 char_u patbuf[NUMBUFLEN];
15406 char_u subbuf[NUMBUFLEN];
15407 char_u flagsbuf[NUMBUFLEN];
15408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015409 char_u *str = get_tv_string_chk(&argvars[0]);
15410 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15411 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15412 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15413
Bram Moolenaar0d660222005-01-07 21:51:51 +000015414 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015415 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15416 rettv->vval.v_string = NULL;
15417 else
15418 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015419}
15420
15421/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015422 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 */
15424/*ARGSUSED*/
15425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015426f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015427 typval_T *argvars;
15428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429{
15430 int id = 0;
15431#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015432 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 long col;
15434 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015435 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015437 lnum = get_tv_lnum(argvars); /* -1 on type error */
15438 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15439 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015441 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015442 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015443 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444#endif
15445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015446 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447}
15448
15449/*
15450 * "synIDattr(id, what [, mode])" function
15451 */
15452/*ARGSUSED*/
15453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015454f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015455 typval_T *argvars;
15456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457{
15458 char_u *p = NULL;
15459#ifdef FEAT_SYN_HL
15460 int id;
15461 char_u *what;
15462 char_u *mode;
15463 char_u modebuf[NUMBUFLEN];
15464 int modec;
15465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466 id = get_tv_number(&argvars[0]);
15467 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015468 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015470 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471 modec = TOLOWER_ASC(mode[0]);
15472 if (modec != 't' && modec != 'c'
15473#ifdef FEAT_GUI
15474 && modec != 'g'
15475#endif
15476 )
15477 modec = 0; /* replace invalid with current */
15478 }
15479 else
15480 {
15481#ifdef FEAT_GUI
15482 if (gui.in_use)
15483 modec = 'g';
15484 else
15485#endif
15486 if (t_colors > 1)
15487 modec = 'c';
15488 else
15489 modec = 't';
15490 }
15491
15492
15493 switch (TOLOWER_ASC(what[0]))
15494 {
15495 case 'b':
15496 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15497 p = highlight_color(id, what, modec);
15498 else /* bold */
15499 p = highlight_has_attr(id, HL_BOLD, modec);
15500 break;
15501
15502 case 'f': /* fg[#] */
15503 p = highlight_color(id, what, modec);
15504 break;
15505
15506 case 'i':
15507 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15508 p = highlight_has_attr(id, HL_INVERSE, modec);
15509 else /* italic */
15510 p = highlight_has_attr(id, HL_ITALIC, modec);
15511 break;
15512
15513 case 'n': /* name */
15514 p = get_highlight_name(NULL, id - 1);
15515 break;
15516
15517 case 'r': /* reverse */
15518 p = highlight_has_attr(id, HL_INVERSE, modec);
15519 break;
15520
15521 case 's': /* standout */
15522 p = highlight_has_attr(id, HL_STANDOUT, modec);
15523 break;
15524
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015525 case 'u':
15526 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15527 /* underline */
15528 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15529 else
15530 /* undercurl */
15531 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 break;
15533 }
15534
15535 if (p != NULL)
15536 p = vim_strsave(p);
15537#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 rettv->v_type = VAR_STRING;
15539 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540}
15541
15542/*
15543 * "synIDtrans(id)" function
15544 */
15545/*ARGSUSED*/
15546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015548 typval_T *argvars;
15549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550{
15551 int id;
15552
15553#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555
15556 if (id > 0)
15557 id = syn_get_final_id(id);
15558 else
15559#endif
15560 id = 0;
15561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563}
15564
15565/*
15566 * "system()" function
15567 */
15568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015570 typval_T *argvars;
15571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015573 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015575 char_u *infile = NULL;
15576 char_u buf[NUMBUFLEN];
15577 int err = FALSE;
15578 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015580 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015581 {
15582 /*
15583 * Write the string to a temp file, to be used for input of the shell
15584 * command.
15585 */
15586 if ((infile = vim_tempname('i')) == NULL)
15587 {
15588 EMSG(_(e_notmp));
15589 return;
15590 }
15591
15592 fd = mch_fopen((char *)infile, WRITEBIN);
15593 if (fd == NULL)
15594 {
15595 EMSG2(_(e_notopen), infile);
15596 goto done;
15597 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 p = get_tv_string_buf_chk(&argvars[1], buf);
15599 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015600 {
15601 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015602 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015603 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015604 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15605 err = TRUE;
15606 if (fclose(fd) != 0)
15607 err = TRUE;
15608 if (err)
15609 {
15610 EMSG(_("E677: Error writing temp file"));
15611 goto done;
15612 }
15613 }
15614
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015615 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15616 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015617
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618#ifdef USE_CR
15619 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015620 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 {
15622 char_u *s;
15623
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015624 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 {
15626 if (*s == CAR)
15627 *s = NL;
15628 }
15629 }
15630#else
15631# ifdef USE_CRNL
15632 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015633 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 {
15635 char_u *s, *d;
15636
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015637 d = res;
15638 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 {
15640 if (s[0] == CAR && s[1] == NL)
15641 ++s;
15642 *d++ = *s;
15643 }
15644 *d = NUL;
15645 }
15646# endif
15647#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015648
15649done:
15650 if (infile != NULL)
15651 {
15652 mch_remove(infile);
15653 vim_free(infile);
15654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015655 rettv->v_type = VAR_STRING;
15656 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657}
15658
15659/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015660 * "tabpagebuflist()" function
15661 */
15662/* ARGSUSED */
15663 static void
15664f_tabpagebuflist(argvars, rettv)
15665 typval_T *argvars;
15666 typval_T *rettv;
15667{
15668#ifndef FEAT_WINDOWS
15669 rettv->vval.v_number = 0;
15670#else
15671 tabpage_T *tp;
15672 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015673
15674 if (argvars[0].v_type == VAR_UNKNOWN)
15675 wp = firstwin;
15676 else
15677 {
15678 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15679 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015680 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015681 }
15682 if (wp == NULL)
15683 rettv->vval.v_number = 0;
15684 else
15685 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015686 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015687 rettv->vval.v_number = 0;
15688 else
15689 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015690 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015691 if (list_append_number(rettv->vval.v_list,
15692 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015693 break;
15694 }
15695 }
15696#endif
15697}
15698
15699
15700/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015701 * "tabpagenr()" function
15702 */
15703/* ARGSUSED */
15704 static void
15705f_tabpagenr(argvars, rettv)
15706 typval_T *argvars;
15707 typval_T *rettv;
15708{
15709 int nr = 1;
15710#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015711 char_u *arg;
15712
15713 if (argvars[0].v_type != VAR_UNKNOWN)
15714 {
15715 arg = get_tv_string_chk(&argvars[0]);
15716 nr = 0;
15717 if (arg != NULL)
15718 {
15719 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015720 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015721 else
15722 EMSG2(_(e_invexpr2), arg);
15723 }
15724 }
15725 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015726 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015727#endif
15728 rettv->vval.v_number = nr;
15729}
15730
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015731
15732#ifdef FEAT_WINDOWS
15733static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15734
15735/*
15736 * Common code for tabpagewinnr() and winnr().
15737 */
15738 static int
15739get_winnr(tp, argvar)
15740 tabpage_T *tp;
15741 typval_T *argvar;
15742{
15743 win_T *twin;
15744 int nr = 1;
15745 win_T *wp;
15746 char_u *arg;
15747
15748 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15749 if (argvar->v_type != VAR_UNKNOWN)
15750 {
15751 arg = get_tv_string_chk(argvar);
15752 if (arg == NULL)
15753 nr = 0; /* type error; errmsg already given */
15754 else if (STRCMP(arg, "$") == 0)
15755 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15756 else if (STRCMP(arg, "#") == 0)
15757 {
15758 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15759 if (twin == NULL)
15760 nr = 0;
15761 }
15762 else
15763 {
15764 EMSG2(_(e_invexpr2), arg);
15765 nr = 0;
15766 }
15767 }
15768
15769 if (nr > 0)
15770 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15771 wp != twin; wp = wp->w_next)
15772 ++nr;
15773 return nr;
15774}
15775#endif
15776
15777/*
15778 * "tabpagewinnr()" function
15779 */
15780/* ARGSUSED */
15781 static void
15782f_tabpagewinnr(argvars, rettv)
15783 typval_T *argvars;
15784 typval_T *rettv;
15785{
15786 int nr = 1;
15787#ifdef FEAT_WINDOWS
15788 tabpage_T *tp;
15789
15790 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15791 if (tp == NULL)
15792 nr = 0;
15793 else
15794 nr = get_winnr(tp, &argvars[1]);
15795#endif
15796 rettv->vval.v_number = nr;
15797}
15798
15799
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015800/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015801 * "tagfiles()" function
15802 */
15803/*ARGSUSED*/
15804 static void
15805f_tagfiles(argvars, rettv)
15806 typval_T *argvars;
15807 typval_T *rettv;
15808{
15809 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015810 tagname_T tn;
15811 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015812
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015813 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015814 {
15815 rettv->vval.v_number = 0;
15816 return;
15817 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015818
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015819 for (first = TRUE; ; first = FALSE)
15820 if (get_tagfname(&tn, first, fname) == FAIL
15821 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015822 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015823 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015824}
15825
15826/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015827 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015828 */
15829 static void
15830f_taglist(argvars, rettv)
15831 typval_T *argvars;
15832 typval_T *rettv;
15833{
15834 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015835
15836 tag_pattern = get_tv_string(&argvars[0]);
15837
15838 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015839 if (*tag_pattern == NUL)
15840 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015841
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015842 if (rettv_list_alloc(rettv) == OK)
15843 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015844}
15845
15846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847 * "tempname()" function
15848 */
15849/*ARGSUSED*/
15850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015851f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015852 typval_T *argvars;
15853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854{
15855 static int x = 'A';
15856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015857 rettv->v_type = VAR_STRING;
15858 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859
15860 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15861 * names. Skip 'I' and 'O', they are used for shell redirection. */
15862 do
15863 {
15864 if (x == 'Z')
15865 x = '0';
15866 else if (x == '9')
15867 x = 'A';
15868 else
15869 {
15870#ifdef EBCDIC
15871 if (x == 'I')
15872 x = 'J';
15873 else if (x == 'R')
15874 x = 'S';
15875 else
15876#endif
15877 ++x;
15878 }
15879 } while (x == 'I' || x == 'O');
15880}
15881
15882/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015883 * "test(list)" function: Just checking the walls...
15884 */
15885/*ARGSUSED*/
15886 static void
15887f_test(argvars, rettv)
15888 typval_T *argvars;
15889 typval_T *rettv;
15890{
15891 /* Used for unit testing. Change the code below to your liking. */
15892#if 0
15893 listitem_T *li;
15894 list_T *l;
15895 char_u *bad, *good;
15896
15897 if (argvars[0].v_type != VAR_LIST)
15898 return;
15899 l = argvars[0].vval.v_list;
15900 if (l == NULL)
15901 return;
15902 li = l->lv_first;
15903 if (li == NULL)
15904 return;
15905 bad = get_tv_string(&li->li_tv);
15906 li = li->li_next;
15907 if (li == NULL)
15908 return;
15909 good = get_tv_string(&li->li_tv);
15910 rettv->vval.v_number = test_edit_score(bad, good);
15911#endif
15912}
15913
15914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915 * "tolower(string)" function
15916 */
15917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015918f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015919 typval_T *argvars;
15920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921{
15922 char_u *p;
15923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015924 p = vim_strsave(get_tv_string(&argvars[0]));
15925 rettv->v_type = VAR_STRING;
15926 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927
15928 if (p != NULL)
15929 while (*p != NUL)
15930 {
15931#ifdef FEAT_MBYTE
15932 int l;
15933
15934 if (enc_utf8)
15935 {
15936 int c, lc;
15937
15938 c = utf_ptr2char(p);
15939 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015940 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 /* TODO: reallocate string when byte count changes. */
15942 if (utf_char2len(lc) == l)
15943 utf_char2bytes(lc, p);
15944 p += l;
15945 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015946 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 p += l; /* skip multi-byte character */
15948 else
15949#endif
15950 {
15951 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15952 ++p;
15953 }
15954 }
15955}
15956
15957/*
15958 * "toupper(string)" function
15959 */
15960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015961f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015962 typval_T *argvars;
15963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015965 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015966 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967}
15968
15969/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015970 * "tr(string, fromstr, tostr)" function
15971 */
15972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015973f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015974 typval_T *argvars;
15975 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015976{
15977 char_u *instr;
15978 char_u *fromstr;
15979 char_u *tostr;
15980 char_u *p;
15981#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015982 int inlen;
15983 int fromlen;
15984 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015985 int idx;
15986 char_u *cpstr;
15987 int cplen;
15988 int first = TRUE;
15989#endif
15990 char_u buf[NUMBUFLEN];
15991 char_u buf2[NUMBUFLEN];
15992 garray_T ga;
15993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015994 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015995 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15996 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015997
15998 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015999 rettv->v_type = VAR_STRING;
16000 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016001 if (fromstr == NULL || tostr == NULL)
16002 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016003 ga_init2(&ga, (int)sizeof(char), 80);
16004
16005#ifdef FEAT_MBYTE
16006 if (!has_mbyte)
16007#endif
16008 /* not multi-byte: fromstr and tostr must be the same length */
16009 if (STRLEN(fromstr) != STRLEN(tostr))
16010 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016011#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016012error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016013#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016014 EMSG2(_(e_invarg2), fromstr);
16015 ga_clear(&ga);
16016 return;
16017 }
16018
16019 /* fromstr and tostr have to contain the same number of chars */
16020 while (*instr != NUL)
16021 {
16022#ifdef FEAT_MBYTE
16023 if (has_mbyte)
16024 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016025 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016026 cpstr = instr;
16027 cplen = inlen;
16028 idx = 0;
16029 for (p = fromstr; *p != NUL; p += fromlen)
16030 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016031 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016032 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16033 {
16034 for (p = tostr; *p != NUL; p += tolen)
16035 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016036 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016037 if (idx-- == 0)
16038 {
16039 cplen = tolen;
16040 cpstr = p;
16041 break;
16042 }
16043 }
16044 if (*p == NUL) /* tostr is shorter than fromstr */
16045 goto error;
16046 break;
16047 }
16048 ++idx;
16049 }
16050
16051 if (first && cpstr == instr)
16052 {
16053 /* Check that fromstr and tostr have the same number of
16054 * (multi-byte) characters. Done only once when a character
16055 * of instr doesn't appear in fromstr. */
16056 first = FALSE;
16057 for (p = tostr; *p != NUL; p += tolen)
16058 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016059 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016060 --idx;
16061 }
16062 if (idx != 0)
16063 goto error;
16064 }
16065
16066 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016067 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016068 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016069
16070 instr += inlen;
16071 }
16072 else
16073#endif
16074 {
16075 /* When not using multi-byte chars we can do it faster. */
16076 p = vim_strchr(fromstr, *instr);
16077 if (p != NULL)
16078 ga_append(&ga, tostr[p - fromstr]);
16079 else
16080 ga_append(&ga, *instr);
16081 ++instr;
16082 }
16083 }
16084
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016085 /* add a terminating NUL */
16086 ga_grow(&ga, 1);
16087 ga_append(&ga, NUL);
16088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016089 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016090}
16091
16092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 * "type(expr)" function
16094 */
16095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016096f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016097 typval_T *argvars;
16098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016100 int n;
16101
16102 switch (argvars[0].v_type)
16103 {
16104 case VAR_NUMBER: n = 0; break;
16105 case VAR_STRING: n = 1; break;
16106 case VAR_FUNC: n = 2; break;
16107 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016108 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016109 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16110 }
16111 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112}
16113
16114/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016115 * "values(dict)" function
16116 */
16117 static void
16118f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016119 typval_T *argvars;
16120 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016121{
16122 dict_list(argvars, rettv, 1);
16123}
16124
16125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 * "virtcol(string)" function
16127 */
16128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016129f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016130 typval_T *argvars;
16131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132{
16133 colnr_T vcol = 0;
16134 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016135 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016137 fp = var2fpos(&argvars[0], FALSE, &fnum);
16138 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16139 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 {
16141 getvvcol(curwin, fp, NULL, NULL, &vcol);
16142 ++vcol;
16143 }
16144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146}
16147
16148/*
16149 * "visualmode()" function
16150 */
16151/*ARGSUSED*/
16152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016153f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016154 typval_T *argvars;
16155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156{
16157#ifdef FEAT_VISUAL
16158 char_u str[2];
16159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016160 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 str[0] = curbuf->b_visual_mode_eval;
16162 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016163 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164
16165 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016166 if ((argvars[0].v_type == VAR_NUMBER
16167 && argvars[0].vval.v_number != 0)
16168 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016169 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 curbuf->b_visual_mode_eval = NUL;
16171#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016172 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173#endif
16174}
16175
16176/*
16177 * "winbufnr(nr)" function
16178 */
16179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016180f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 typval_T *argvars;
16182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183{
16184 win_T *wp;
16185
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016186 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016188 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016190 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191}
16192
16193/*
16194 * "wincol()" function
16195 */
16196/*ARGSUSED*/
16197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016198f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016199 typval_T *argvars;
16200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201{
16202 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016203 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204}
16205
16206/*
16207 * "winheight(nr)" function
16208 */
16209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016210f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016211 typval_T *argvars;
16212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213{
16214 win_T *wp;
16215
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016216 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016218 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016220 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221}
16222
16223/*
16224 * "winline()" function
16225 */
16226/*ARGSUSED*/
16227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016228f_winline(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016233 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234}
16235
16236/*
16237 * "winnr()" function
16238 */
16239/* ARGSUSED */
16240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016241f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 typval_T *argvars;
16243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244{
16245 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016246
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016248 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016250 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251}
16252
16253/*
16254 * "winrestcmd()" function
16255 */
16256/* ARGSUSED */
16257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016258f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016259 typval_T *argvars;
16260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261{
16262#ifdef FEAT_WINDOWS
16263 win_T *wp;
16264 int winnr = 1;
16265 garray_T ga;
16266 char_u buf[50];
16267
16268 ga_init2(&ga, (int)sizeof(char), 70);
16269 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16270 {
16271 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16272 ga_concat(&ga, buf);
16273# ifdef FEAT_VERTSPLIT
16274 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16275 ga_concat(&ga, buf);
16276# endif
16277 ++winnr;
16278 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016279 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016281 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016283 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016285 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286}
16287
16288/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016289 * "winrestview()" function
16290 */
16291/* ARGSUSED */
16292 static void
16293f_winrestview(argvars, rettv)
16294 typval_T *argvars;
16295 typval_T *rettv;
16296{
16297 dict_T *dict;
16298
16299 if (argvars[0].v_type != VAR_DICT
16300 || (dict = argvars[0].vval.v_dict) == NULL)
16301 EMSG(_(e_invarg));
16302 else
16303 {
16304 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16305 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16306#ifdef FEAT_VIRTUALEDIT
16307 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16308#endif
16309 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016310 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016311
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016312 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016313#ifdef FEAT_DIFF
16314 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16315#endif
16316 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16317 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16318
16319 check_cursor();
16320 changed_cline_bef_curs();
16321 invalidate_botline();
16322 redraw_later(VALID);
16323
16324 if (curwin->w_topline == 0)
16325 curwin->w_topline = 1;
16326 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16327 curwin->w_topline = curbuf->b_ml.ml_line_count;
16328#ifdef FEAT_DIFF
16329 check_topfill(curwin, TRUE);
16330#endif
16331 }
16332}
16333
16334/*
16335 * "winsaveview()" function
16336 */
16337/* ARGSUSED */
16338 static void
16339f_winsaveview(argvars, rettv)
16340 typval_T *argvars;
16341 typval_T *rettv;
16342{
16343 dict_T *dict;
16344
16345 dict = dict_alloc();
16346 if (dict == NULL)
16347 return;
16348 rettv->v_type = VAR_DICT;
16349 rettv->vval.v_dict = dict;
16350 ++dict->dv_refcount;
16351
16352 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16353 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16354#ifdef FEAT_VIRTUALEDIT
16355 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16356#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016357 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016358 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16359
16360 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16361#ifdef FEAT_DIFF
16362 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16363#endif
16364 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16365 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16366}
16367
16368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 * "winwidth(nr)" function
16370 */
16371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016372f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016373 typval_T *argvars;
16374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375{
16376 win_T *wp;
16377
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016378 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016380 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 else
16382#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016383 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016385 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386#endif
16387}
16388
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016390 * "writefile()" function
16391 */
16392 static void
16393f_writefile(argvars, rettv)
16394 typval_T *argvars;
16395 typval_T *rettv;
16396{
16397 int binary = FALSE;
16398 char_u *fname;
16399 FILE *fd;
16400 listitem_T *li;
16401 char_u *s;
16402 int ret = 0;
16403 int c;
16404
16405 if (argvars[0].v_type != VAR_LIST)
16406 {
16407 EMSG2(_(e_listarg), "writefile()");
16408 return;
16409 }
16410 if (argvars[0].vval.v_list == NULL)
16411 return;
16412
16413 if (argvars[2].v_type != VAR_UNKNOWN
16414 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16415 binary = TRUE;
16416
16417 /* Always open the file in binary mode, library functions have a mind of
16418 * their own about CR-LF conversion. */
16419 fname = get_tv_string(&argvars[1]);
16420 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16421 {
16422 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16423 ret = -1;
16424 }
16425 else
16426 {
16427 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16428 li = li->li_next)
16429 {
16430 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16431 {
16432 if (*s == '\n')
16433 c = putc(NUL, fd);
16434 else
16435 c = putc(*s, fd);
16436 if (c == EOF)
16437 {
16438 ret = -1;
16439 break;
16440 }
16441 }
16442 if (!binary || li->li_next != NULL)
16443 if (putc('\n', fd) == EOF)
16444 {
16445 ret = -1;
16446 break;
16447 }
16448 if (ret < 0)
16449 {
16450 EMSG(_(e_write));
16451 break;
16452 }
16453 }
16454 fclose(fd);
16455 }
16456
16457 rettv->vval.v_number = ret;
16458}
16459
16460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016462 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 */
16464 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016465var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016468 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016470 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016472 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473
Bram Moolenaara5525202006-03-02 22:52:09 +000016474 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016475 if (varp->v_type == VAR_LIST)
16476 {
16477 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016478 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016479 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016480
16481 l = varp->vval.v_list;
16482 if (l == NULL)
16483 return NULL;
16484
16485 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016486 pos.lnum = list_find_nr(l, 0L, &error);
16487 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016488 return NULL; /* invalid line number */
16489
16490 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016491 pos.col = list_find_nr(l, 1L, &error);
16492 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016493 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016494 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016495 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016496 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016497 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016498 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016499
Bram Moolenaara5525202006-03-02 22:52:09 +000016500#ifdef FEAT_VIRTUALEDIT
16501 /* Get the virtual offset. Defaults to zero. */
16502 pos.coladd = list_find_nr(l, 2L, &error);
16503 if (error)
16504 pos.coladd = 0;
16505#endif
16506
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016507 return &pos;
16508 }
16509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016510 name = get_tv_string_chk(varp);
16511 if (name == NULL)
16512 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 if (name[0] == '.') /* cursor */
16514 return &curwin->w_cursor;
16515 if (name[0] == '\'') /* mark */
16516 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016517 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16519 return NULL;
16520 return pp;
16521 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016522
16523#ifdef FEAT_VIRTUALEDIT
16524 pos.coladd = 0;
16525#endif
16526
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016527 if (name[0] == 'w' && lnum)
16528 {
16529 pos.col = 0;
16530 if (name[1] == '0') /* "w0": first visible line */
16531 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016532 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016533 pos.lnum = curwin->w_topline;
16534 return &pos;
16535 }
16536 else if (name[1] == '$') /* "w$": last visible line */
16537 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016538 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016539 pos.lnum = curwin->w_botline - 1;
16540 return &pos;
16541 }
16542 }
16543 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 {
16545 if (lnum)
16546 {
16547 pos.lnum = curbuf->b_ml.ml_line_count;
16548 pos.col = 0;
16549 }
16550 else
16551 {
16552 pos.lnum = curwin->w_cursor.lnum;
16553 pos.col = (colnr_T)STRLEN(ml_get_curline());
16554 }
16555 return &pos;
16556 }
16557 return NULL;
16558}
16559
16560/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016561 * Convert list in "arg" into a position and optional file number.
16562 * When "fnump" is NULL there is no file number, only 3 items.
16563 * Note that the column is passed on as-is, the caller may want to decrement
16564 * it to use 1 for the first column.
16565 * Return FAIL when conversion is not possible, doesn't check the position for
16566 * validity.
16567 */
16568 static int
16569list2fpos(arg, posp, fnump)
16570 typval_T *arg;
16571 pos_T *posp;
16572 int *fnump;
16573{
16574 list_T *l = arg->vval.v_list;
16575 long i = 0;
16576 long n;
16577
Bram Moolenaarbde35262006-07-23 20:12:24 +000016578 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16579 * when "fnump" isn't NULL and "coladd" is optional. */
16580 if (arg->v_type != VAR_LIST
16581 || l == NULL
16582 || l->lv_len < (fnump == NULL ? 2 : 3)
16583 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016584 return FAIL;
16585
16586 if (fnump != NULL)
16587 {
16588 n = list_find_nr(l, i++, NULL); /* fnum */
16589 if (n < 0)
16590 return FAIL;
16591 if (n == 0)
16592 n = curbuf->b_fnum; /* current buffer */
16593 *fnump = n;
16594 }
16595
16596 n = list_find_nr(l, i++, NULL); /* lnum */
16597 if (n < 0)
16598 return FAIL;
16599 posp->lnum = n;
16600
16601 n = list_find_nr(l, i++, NULL); /* col */
16602 if (n < 0)
16603 return FAIL;
16604 posp->col = n;
16605
16606#ifdef FEAT_VIRTUALEDIT
16607 n = list_find_nr(l, i, NULL);
16608 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016609 posp->coladd = 0;
16610 else
16611 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016612#endif
16613
16614 return OK;
16615}
16616
16617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 * Get the length of an environment variable name.
16619 * Advance "arg" to the first character after the name.
16620 * Return 0 for error.
16621 */
16622 static int
16623get_env_len(arg)
16624 char_u **arg;
16625{
16626 char_u *p;
16627 int len;
16628
16629 for (p = *arg; vim_isIDc(*p); ++p)
16630 ;
16631 if (p == *arg) /* no name found */
16632 return 0;
16633
16634 len = (int)(p - *arg);
16635 *arg = p;
16636 return len;
16637}
16638
16639/*
16640 * Get the length of the name of a function or internal variable.
16641 * "arg" is advanced to the first non-white character after the name.
16642 * Return 0 if something is wrong.
16643 */
16644 static int
16645get_id_len(arg)
16646 char_u **arg;
16647{
16648 char_u *p;
16649 int len;
16650
16651 /* Find the end of the name. */
16652 for (p = *arg; eval_isnamec(*p); ++p)
16653 ;
16654 if (p == *arg) /* no name found */
16655 return 0;
16656
16657 len = (int)(p - *arg);
16658 *arg = skipwhite(p);
16659
16660 return len;
16661}
16662
16663/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016664 * Get the length of the name of a variable or function.
16665 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016667 * Return -1 if curly braces expansion failed.
16668 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 * If the name contains 'magic' {}'s, expand them and return the
16670 * expanded name in an allocated string via 'alias' - caller must free.
16671 */
16672 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016673get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674 char_u **arg;
16675 char_u **alias;
16676 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016677 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678{
16679 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 char_u *p;
16681 char_u *expr_start;
16682 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683
16684 *alias = NULL; /* default to no alias */
16685
16686 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16687 && (*arg)[2] == (int)KE_SNR)
16688 {
16689 /* hard coded <SNR>, already translated */
16690 *arg += 3;
16691 return get_id_len(arg) + 3;
16692 }
16693 len = eval_fname_script(*arg);
16694 if (len > 0)
16695 {
16696 /* literal "<SID>", "s:" or "<SNR>" */
16697 *arg += len;
16698 }
16699
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016701 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016703 p = find_name_end(*arg, &expr_start, &expr_end,
16704 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705 if (expr_start != NULL)
16706 {
16707 char_u *temp_string;
16708
16709 if (!evaluate)
16710 {
16711 len += (int)(p - *arg);
16712 *arg = skipwhite(p);
16713 return len;
16714 }
16715
16716 /*
16717 * Include any <SID> etc in the expanded string:
16718 * Thus the -len here.
16719 */
16720 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16721 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016722 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 *alias = temp_string;
16724 *arg = skipwhite(p);
16725 return (int)STRLEN(temp_string);
16726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727
16728 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016729 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 EMSG2(_(e_invexpr2), *arg);
16731
16732 return len;
16733}
16734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016735/*
16736 * Find the end of a variable or function name, taking care of magic braces.
16737 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16738 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016739 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016740 * Return a pointer to just after the name. Equal to "arg" if there is no
16741 * valid name.
16742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016744find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 char_u *arg;
16746 char_u **expr_start;
16747 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016748 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750 int mb_nest = 0;
16751 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 char_u *p;
16753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016756 *expr_start = NULL;
16757 *expr_end = NULL;
16758 }
16759
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016760 /* Quick check for valid starting character. */
16761 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16762 return arg;
16763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016764 for (p = arg; *p != NUL
16765 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016766 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016767 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016768 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016769 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016771 if (*p == '\'')
16772 {
16773 /* skip over 'string' to avoid counting [ and ] inside it. */
16774 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16775 ;
16776 if (*p == NUL)
16777 break;
16778 }
16779 else if (*p == '"')
16780 {
16781 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16782 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16783 if (*p == '\\' && p[1] != NUL)
16784 ++p;
16785 if (*p == NUL)
16786 break;
16787 }
16788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016789 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016791 if (*p == '[')
16792 ++br_nest;
16793 else if (*p == ']')
16794 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016797 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799 if (*p == '{')
16800 {
16801 mb_nest++;
16802 if (expr_start != NULL && *expr_start == NULL)
16803 *expr_start = p;
16804 }
16805 else if (*p == '}')
16806 {
16807 mb_nest--;
16808 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16809 *expr_end = p;
16810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 }
16813
16814 return p;
16815}
16816
16817/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016818 * Expands out the 'magic' {}'s in a variable/function name.
16819 * Note that this can call itself recursively, to deal with
16820 * constructs like foo{bar}{baz}{bam}
16821 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16822 * "in_start" ^
16823 * "expr_start" ^
16824 * "expr_end" ^
16825 * "in_end" ^
16826 *
16827 * Returns a new allocated string, which the caller must free.
16828 * Returns NULL for failure.
16829 */
16830 static char_u *
16831make_expanded_name(in_start, expr_start, expr_end, in_end)
16832 char_u *in_start;
16833 char_u *expr_start;
16834 char_u *expr_end;
16835 char_u *in_end;
16836{
16837 char_u c1;
16838 char_u *retval = NULL;
16839 char_u *temp_result;
16840 char_u *nextcmd = NULL;
16841
16842 if (expr_end == NULL || in_end == NULL)
16843 return NULL;
16844 *expr_start = NUL;
16845 *expr_end = NUL;
16846 c1 = *in_end;
16847 *in_end = NUL;
16848
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016849 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016850 if (temp_result != NULL && nextcmd == NULL)
16851 {
16852 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16853 + (in_end - expr_end) + 1));
16854 if (retval != NULL)
16855 {
16856 STRCPY(retval, in_start);
16857 STRCAT(retval, temp_result);
16858 STRCAT(retval, expr_end + 1);
16859 }
16860 }
16861 vim_free(temp_result);
16862
16863 *in_end = c1; /* put char back for error messages */
16864 *expr_start = '{';
16865 *expr_end = '}';
16866
16867 if (retval != NULL)
16868 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016869 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016870 if (expr_start != NULL)
16871 {
16872 /* Further expansion! */
16873 temp_result = make_expanded_name(retval, expr_start,
16874 expr_end, temp_result);
16875 vim_free(retval);
16876 retval = temp_result;
16877 }
16878 }
16879
16880 return retval;
16881}
16882
16883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016885 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 */
16887 static int
16888eval_isnamec(c)
16889 int c;
16890{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016891 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16892}
16893
16894/*
16895 * Return TRUE if character "c" can be used as the first character in a
16896 * variable or function name (excluding '{' and '}').
16897 */
16898 static int
16899eval_isnamec1(c)
16900 int c;
16901{
16902 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903}
16904
16905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 * Set number v: variable to "val".
16907 */
16908 void
16909set_vim_var_nr(idx, val)
16910 int idx;
16911 long val;
16912{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016913 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914}
16915
16916/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016917 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 */
16919 long
16920get_vim_var_nr(idx)
16921 int idx;
16922{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016923 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924}
16925
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016926#if defined(FEAT_AUTOCMD) || defined(PROTO)
16927/*
16928 * Get string v: variable value. Uses a static buffer, can only be used once.
16929 */
16930 char_u *
16931get_vim_var_str(idx)
16932 int idx;
16933{
16934 return get_tv_string(&vimvars[idx].vv_tv);
16935}
16936#endif
16937
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938/*
16939 * Set v:count, v:count1 and v:prevcount.
16940 */
16941 void
16942set_vcount(count, count1)
16943 long count;
16944 long count1;
16945{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016946 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16947 vimvars[VV_COUNT].vv_nr = count;
16948 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949}
16950
16951/*
16952 * Set string v: variable to a copy of "val".
16953 */
16954 void
16955set_vim_var_string(idx, val, len)
16956 int idx;
16957 char_u *val;
16958 int len; /* length of "val" to use or -1 (whole string) */
16959{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016960 /* Need to do this (at least) once, since we can't initialize a union.
16961 * Will always be invoked when "v:progname" is set. */
16962 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16963
Bram Moolenaare9a41262005-01-15 22:18:47 +000016964 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016966 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016968 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016970 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971}
16972
16973/*
16974 * Set v:register if needed.
16975 */
16976 void
16977set_reg_var(c)
16978 int c;
16979{
16980 char_u regname;
16981
16982 if (c == 0 || c == ' ')
16983 regname = '"';
16984 else
16985 regname = c;
16986 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016987 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 set_vim_var_string(VV_REG, &regname, 1);
16989}
16990
16991/*
16992 * Get or set v:exception. If "oldval" == NULL, return the current value.
16993 * Otherwise, restore the value to "oldval" and return NULL.
16994 * Must always be called in pairs to save and restore v:exception! Does not
16995 * take care of memory allocations.
16996 */
16997 char_u *
16998v_exception(oldval)
16999 char_u *oldval;
17000{
17001 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017002 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003
Bram Moolenaare9a41262005-01-15 22:18:47 +000017004 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 return NULL;
17006}
17007
17008/*
17009 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17010 * Otherwise, restore the value to "oldval" and return NULL.
17011 * Must always be called in pairs to save and restore v:throwpoint! Does not
17012 * take care of memory allocations.
17013 */
17014 char_u *
17015v_throwpoint(oldval)
17016 char_u *oldval;
17017{
17018 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017019 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020
Bram Moolenaare9a41262005-01-15 22:18:47 +000017021 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 return NULL;
17023}
17024
17025#if defined(FEAT_AUTOCMD) || defined(PROTO)
17026/*
17027 * Set v:cmdarg.
17028 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17029 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17030 * Must always be called in pairs!
17031 */
17032 char_u *
17033set_cmdarg(eap, oldarg)
17034 exarg_T *eap;
17035 char_u *oldarg;
17036{
17037 char_u *oldval;
17038 char_u *newval;
17039 unsigned len;
17040
Bram Moolenaare9a41262005-01-15 22:18:47 +000017041 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017042 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017044 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017045 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017046 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 }
17048
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017049 if (eap->force_bin == FORCE_BIN)
17050 len = 6;
17051 else if (eap->force_bin == FORCE_NOBIN)
17052 len = 8;
17053 else
17054 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017055
17056 if (eap->read_edit)
17057 len += 7;
17058
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017059 if (eap->force_ff != 0)
17060 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17061# ifdef FEAT_MBYTE
17062 if (eap->force_enc != 0)
17063 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017064 if (eap->bad_char != 0)
17065 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017066# endif
17067
17068 newval = alloc(len + 1);
17069 if (newval == NULL)
17070 return NULL;
17071
17072 if (eap->force_bin == FORCE_BIN)
17073 sprintf((char *)newval, " ++bin");
17074 else if (eap->force_bin == FORCE_NOBIN)
17075 sprintf((char *)newval, " ++nobin");
17076 else
17077 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017078
17079 if (eap->read_edit)
17080 STRCAT(newval, " ++edit");
17081
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017082 if (eap->force_ff != 0)
17083 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17084 eap->cmd + eap->force_ff);
17085# ifdef FEAT_MBYTE
17086 if (eap->force_enc != 0)
17087 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17088 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017089 if (eap->bad_char != 0)
17090 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17091 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017092# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017093 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017094 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095}
17096#endif
17097
17098/*
17099 * Get the value of internal variable "name".
17100 * Return OK or FAIL.
17101 */
17102 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017103get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 char_u *name;
17105 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017106 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017107 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108{
17109 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017110 typval_T *tv = NULL;
17111 typval_T atv;
17112 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114
17115 /* truncate the name, so that we can use strcmp() */
17116 cc = name[len];
17117 name[len] = NUL;
17118
17119 /*
17120 * Check for "b:changedtick".
17121 */
17122 if (STRCMP(name, "b:changedtick") == 0)
17123 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017124 atv.v_type = VAR_NUMBER;
17125 atv.vval.v_number = curbuf->b_changedtick;
17126 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 }
17128
17129 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 * Check for user-defined variables.
17131 */
17132 else
17133 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017134 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017136 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 }
17138
Bram Moolenaare9a41262005-01-15 22:18:47 +000017139 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017141 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017142 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 ret = FAIL;
17144 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017145 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017146 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147
17148 name[len] = cc;
17149
17150 return ret;
17151}
17152
17153/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017154 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17155 * Also handle function call with Funcref variable: func(expr)
17156 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17157 */
17158 static int
17159handle_subscript(arg, rettv, evaluate, verbose)
17160 char_u **arg;
17161 typval_T *rettv;
17162 int evaluate; /* do more than finding the end */
17163 int verbose; /* give error messages */
17164{
17165 int ret = OK;
17166 dict_T *selfdict = NULL;
17167 char_u *s;
17168 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017169 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017170
17171 while (ret == OK
17172 && (**arg == '['
17173 || (**arg == '.' && rettv->v_type == VAR_DICT)
17174 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17175 && !vim_iswhite(*(*arg - 1)))
17176 {
17177 if (**arg == '(')
17178 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017179 /* need to copy the funcref so that we can clear rettv */
17180 functv = *rettv;
17181 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017182
17183 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017184 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017185 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017186 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17187 &len, evaluate, selfdict);
17188
17189 /* Clear the funcref afterwards, so that deleting it while
17190 * evaluating the arguments is possible (see test55). */
17191 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017192
17193 /* Stop the expression evaluation when immediately aborting on
17194 * error, or when an interrupt occurred or an exception was thrown
17195 * but not caught. */
17196 if (aborting())
17197 {
17198 if (ret == OK)
17199 clear_tv(rettv);
17200 ret = FAIL;
17201 }
17202 dict_unref(selfdict);
17203 selfdict = NULL;
17204 }
17205 else /* **arg == '[' || **arg == '.' */
17206 {
17207 dict_unref(selfdict);
17208 if (rettv->v_type == VAR_DICT)
17209 {
17210 selfdict = rettv->vval.v_dict;
17211 if (selfdict != NULL)
17212 ++selfdict->dv_refcount;
17213 }
17214 else
17215 selfdict = NULL;
17216 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17217 {
17218 clear_tv(rettv);
17219 ret = FAIL;
17220 }
17221 }
17222 }
17223 dict_unref(selfdict);
17224 return ret;
17225}
17226
17227/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017228 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17229 * value).
17230 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017231 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017232alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017233{
Bram Moolenaar33570922005-01-25 22:26:29 +000017234 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017235}
17236
17237/*
17238 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 * The string "s" must have been allocated, it is consumed.
17240 * Return NULL for out of memory, the variable otherwise.
17241 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017242 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017243alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 char_u *s;
17245{
Bram Moolenaar33570922005-01-25 22:26:29 +000017246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017248 rettv = alloc_tv();
17249 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017251 rettv->v_type = VAR_STRING;
17252 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 }
17254 else
17255 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017256 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257}
17258
17259/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017260 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017262 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017263free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017264 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265{
17266 if (varp != NULL)
17267 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017268 switch (varp->v_type)
17269 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017270 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017271 func_unref(varp->vval.v_string);
17272 /*FALLTHROUGH*/
17273 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017274 vim_free(varp->vval.v_string);
17275 break;
17276 case VAR_LIST:
17277 list_unref(varp->vval.v_list);
17278 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017279 case VAR_DICT:
17280 dict_unref(varp->vval.v_dict);
17281 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017282 case VAR_NUMBER:
17283 case VAR_UNKNOWN:
17284 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017285 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017286 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017287 break;
17288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 vim_free(varp);
17290 }
17291}
17292
17293/*
17294 * Free the memory for a variable value and set the value to NULL or 0.
17295 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017296 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017298 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299{
17300 if (varp != NULL)
17301 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017302 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017304 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017305 func_unref(varp->vval.v_string);
17306 /*FALLTHROUGH*/
17307 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017308 vim_free(varp->vval.v_string);
17309 varp->vval.v_string = NULL;
17310 break;
17311 case VAR_LIST:
17312 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017313 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017314 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017315 case VAR_DICT:
17316 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017317 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017318 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017320 varp->vval.v_number = 0;
17321 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017322 case VAR_UNKNOWN:
17323 break;
17324 default:
17325 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017327 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 }
17329}
17330
17331/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017332 * Set the value of a variable to NULL without freeing items.
17333 */
17334 static void
17335init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017336 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017337{
17338 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017339 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017340}
17341
17342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 * Get the number value of a variable.
17344 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017345 * For incompatible types, return 0.
17346 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17347 * caller of incompatible types: it sets *denote to TRUE if "denote"
17348 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 */
17350 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017351get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017352 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017354 int error = FALSE;
17355
17356 return get_tv_number_chk(varp, &error); /* return 0L on error */
17357}
17358
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017359 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017360get_tv_number_chk(varp, denote)
17361 typval_T *varp;
17362 int *denote;
17363{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017364 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017366 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017368 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017370 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017371 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017372 break;
17373 case VAR_STRING:
17374 if (varp->vval.v_string != NULL)
17375 vim_str2nr(varp->vval.v_string, NULL, NULL,
17376 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017378 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017379 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017380 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017381 case VAR_DICT:
17382 EMSG(_("E728: Using a Dictionary as a number"));
17383 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017384 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017385 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017386 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017388 if (denote == NULL) /* useful for values that must be unsigned */
17389 n = -1;
17390 else
17391 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017392 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393}
17394
17395/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017396 * Get the lnum from the first argument.
17397 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017398 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 */
17400 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017401get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403{
Bram Moolenaar33570922005-01-25 22:26:29 +000017404 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 linenr_T lnum;
17406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017407 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 if (lnum == 0) /* no valid number, try using line() */
17409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410 rettv.v_type = VAR_NUMBER;
17411 f_line(argvars, &rettv);
17412 lnum = rettv.vval.v_number;
17413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 }
17415 return lnum;
17416}
17417
17418/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017419 * Get the lnum from the first argument.
17420 * Also accepts "$", then "buf" is used.
17421 * Returns 0 on error.
17422 */
17423 static linenr_T
17424get_tv_lnum_buf(argvars, buf)
17425 typval_T *argvars;
17426 buf_T *buf;
17427{
17428 if (argvars[0].v_type == VAR_STRING
17429 && argvars[0].vval.v_string != NULL
17430 && argvars[0].vval.v_string[0] == '$'
17431 && buf != NULL)
17432 return buf->b_ml.ml_line_count;
17433 return get_tv_number_chk(&argvars[0], NULL);
17434}
17435
17436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 * Get the string value of a variable.
17438 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017439 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17440 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 * If the String variable has never been set, return an empty string.
17442 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017443 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17444 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 */
17446 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017447get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017448 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449{
17450 static char_u mybuf[NUMBUFLEN];
17451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017452 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453}
17454
17455 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017458 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017460 char_u *res = get_tv_string_buf_chk(varp, buf);
17461
17462 return res != NULL ? res : (char_u *)"";
17463}
17464
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017465 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017466get_tv_string_chk(varp)
17467 typval_T *varp;
17468{
17469 static char_u mybuf[NUMBUFLEN];
17470
17471 return get_tv_string_buf_chk(varp, mybuf);
17472}
17473
17474 static char_u *
17475get_tv_string_buf_chk(varp, buf)
17476 typval_T *varp;
17477 char_u *buf;
17478{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017479 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017481 case VAR_NUMBER:
17482 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17483 return buf;
17484 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017485 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017486 break;
17487 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017488 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017489 break;
17490 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017491 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017492 break;
17493 case VAR_STRING:
17494 if (varp->vval.v_string != NULL)
17495 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017496 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017497 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017498 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017501 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502}
17503
17504/*
17505 * Find variable "name" in the list of variables.
17506 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017507 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017508 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017511 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017512find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017514 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017517 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518
Bram Moolenaara7043832005-01-21 11:56:39 +000017519 ht = find_var_ht(name, &varname);
17520 if (htp != NULL)
17521 *htp = ht;
17522 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017524 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525}
17526
17527/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017529 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017532find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017533 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017534 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017535 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017536{
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 hashitem_T *hi;
17538
17539 if (*varname == NUL)
17540 {
17541 /* Must be something like "s:", otherwise "ht" would be NULL. */
17542 switch (varname[-2])
17543 {
17544 case 's': return &SCRIPT_SV(current_SID).sv_var;
17545 case 'g': return &globvars_var;
17546 case 'v': return &vimvars_var;
17547 case 'b': return &curbuf->b_bufvar;
17548 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017549#ifdef FEAT_WINDOWS
17550 case 't': return &curtab->tp_winvar;
17551#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017552 case 'l': return current_funccal == NULL
17553 ? NULL : &current_funccal->l_vars_var;
17554 case 'a': return current_funccal == NULL
17555 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 }
17557 return NULL;
17558 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017559
17560 hi = hash_find(ht, varname);
17561 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017562 {
17563 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017564 * worked find the variable again. Don't auto-load a script if it was
17565 * loaded already, otherwise it would be loaded every time when
17566 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017567 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017568 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017569 hi = hash_find(ht, varname);
17570 if (HASHITEM_EMPTY(hi))
17571 return NULL;
17572 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017573 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017574}
17575
17576/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017578 * Set "varname" to the start of name without ':'.
17579 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017581find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582 char_u *name;
17583 char_u **varname;
17584{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017585 hashitem_T *hi;
17586
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 if (name[1] != ':')
17588 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017589 /* The name must not start with a colon or #. */
17590 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 return NULL;
17592 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017593
17594 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017595 hi = hash_find(&compat_hashtab, name);
17596 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017597 return &compat_hashtab;
17598
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017600 return &globvarht; /* global variable */
17601 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 }
17603 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017604 if (*name == 'g') /* global variable */
17605 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017606 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17607 */
17608 if (vim_strchr(name + 2, ':') != NULL
17609 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017610 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017614 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017615#ifdef FEAT_WINDOWS
17616 if (*name == 't') /* tab page variable */
17617 return &curtab->tp_vars.dv_hashtab;
17618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017619 if (*name == 'v') /* v: variable */
17620 return &vimvarht;
17621 if (*name == 'a' && current_funccal != NULL) /* function argument */
17622 return &current_funccal->l_avars.dv_hashtab;
17623 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17624 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 if (*name == 's' /* script variable */
17626 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17627 return &SCRIPT_VARS(current_SID);
17628 return NULL;
17629}
17630
17631/*
17632 * Get the string value of a (global/local) variable.
17633 * Returns NULL when it doesn't exist.
17634 */
17635 char_u *
17636get_var_value(name)
17637 char_u *name;
17638{
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640
Bram Moolenaara7043832005-01-21 11:56:39 +000017641 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 if (v == NULL)
17643 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017644 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645}
17646
17647/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017648 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 * sourcing this script and when executing functions defined in the script.
17650 */
17651 void
17652new_script_vars(id)
17653 scid_T id;
17654{
Bram Moolenaara7043832005-01-21 11:56:39 +000017655 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017656 hashtab_T *ht;
17657 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017658
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17660 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017661 /* Re-allocating ga_data means that an ht_array pointing to
17662 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017663 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017664 for (i = 1; i <= ga_scripts.ga_len; ++i)
17665 {
17666 ht = &SCRIPT_VARS(i);
17667 if (ht->ht_mask == HT_INIT_SIZE - 1)
17668 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017669 sv = &SCRIPT_SV(i);
17670 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017671 }
17672
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 while (ga_scripts.ga_len < id)
17674 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017675 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17676 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 }
17679 }
17680}
17681
17682/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17684 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 */
17686 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017687init_var_dict(dict, dict_var)
17688 dict_T *dict;
17689 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690{
Bram Moolenaar33570922005-01-25 22:26:29 +000017691 hash_init(&dict->dv_hashtab);
17692 dict->dv_refcount = 99999;
17693 dict_var->di_tv.vval.v_dict = dict;
17694 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017695 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017696 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17697 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698}
17699
17700/*
17701 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017702 * Frees all allocated variables and the value they contain.
17703 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 */
17705 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017706vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017707 hashtab_T *ht;
17708{
17709 vars_clear_ext(ht, TRUE);
17710}
17711
17712/*
17713 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17714 */
17715 static void
17716vars_clear_ext(ht, free_val)
17717 hashtab_T *ht;
17718 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719{
Bram Moolenaara7043832005-01-21 11:56:39 +000017720 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017721 hashitem_T *hi;
17722 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723
Bram Moolenaar33570922005-01-25 22:26:29 +000017724 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017725 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017726 for (hi = ht->ht_array; todo > 0; ++hi)
17727 {
17728 if (!HASHITEM_EMPTY(hi))
17729 {
17730 --todo;
17731
Bram Moolenaar33570922005-01-25 22:26:29 +000017732 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017733 * ht_array might change then. hash_clear() takes care of it
17734 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017735 v = HI2DI(hi);
17736 if (free_val)
17737 clear_tv(&v->di_tv);
17738 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17739 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017740 }
17741 }
17742 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017743 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744}
17745
Bram Moolenaara7043832005-01-21 11:56:39 +000017746/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 * Delete a variable from hashtab "ht" at item "hi".
17748 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017751delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017752 hashtab_T *ht;
17753 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754{
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017756
17757 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017758 clear_tv(&di->di_tv);
17759 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760}
17761
17762/*
17763 * List the value of one internal variable.
17764 */
17765 static void
17766list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 char_u *prefix;
17769{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017770 char_u *tofree;
17771 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017772 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017773
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017774 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017776 s == NULL ? (char_u *)"" : s);
17777 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778}
17779
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 static void
17781list_one_var_a(prefix, name, type, string)
17782 char_u *prefix;
17783 char_u *name;
17784 int type;
17785 char_u *string;
17786{
17787 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17788 if (name != NULL) /* "a:" vars don't have a name stored */
17789 msg_puts(name);
17790 msg_putchar(' ');
17791 msg_advance(22);
17792 if (type == VAR_NUMBER)
17793 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017794 else if (type == VAR_FUNC)
17795 msg_putchar('*');
17796 else if (type == VAR_LIST)
17797 {
17798 msg_putchar('[');
17799 if (*string == '[')
17800 ++string;
17801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017802 else if (type == VAR_DICT)
17803 {
17804 msg_putchar('{');
17805 if (*string == '{')
17806 ++string;
17807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 else
17809 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017810
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017812
17813 if (type == VAR_FUNC)
17814 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815}
17816
17817/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017818 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 * If the variable already exists, the value is updated.
17820 * Otherwise the variable is created.
17821 */
17822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017823set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017825 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017826 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827{
Bram Moolenaar33570922005-01-25 22:26:29 +000017828 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017830 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017831 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017833 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017834 {
17835 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17836 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17837 ? name[2] : name[0]))
17838 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017839 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017840 return;
17841 }
17842 if (function_exists(name))
17843 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017844 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017845 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017846 return;
17847 }
17848 }
17849
Bram Moolenaara7043832005-01-21 11:56:39 +000017850 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017852 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017853 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017854 return;
17855 }
17856
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017857 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017858 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017860 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017861 if (var_check_ro(v->di_flags, name)
17862 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017863 return;
17864 if (v->di_tv.v_type != tv->v_type
17865 && !((v->di_tv.v_type == VAR_STRING
17866 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017867 && (tv->v_type == VAR_STRING
17868 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017869 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017870 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017871 return;
17872 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017873
17874 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017875 * Handle setting internal v: variables separately: we don't change
17876 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017877 */
17878 if (ht == &vimvarht)
17879 {
17880 if (v->di_tv.v_type == VAR_STRING)
17881 {
17882 vim_free(v->di_tv.vval.v_string);
17883 if (copy || tv->v_type != VAR_STRING)
17884 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17885 else
17886 {
17887 /* Take over the string to avoid an extra alloc/free. */
17888 v->di_tv.vval.v_string = tv->vval.v_string;
17889 tv->vval.v_string = NULL;
17890 }
17891 }
17892 else if (v->di_tv.v_type != VAR_NUMBER)
17893 EMSG2(_(e_intern2), "set_var()");
17894 else
17895 v->di_tv.vval.v_number = get_tv_number(tv);
17896 return;
17897 }
17898
17899 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900 }
17901 else /* add a new variable */
17902 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017903 /* Can't add "v:" variable. */
17904 if (ht == &vimvarht)
17905 {
17906 EMSG2(_(e_illvar), name);
17907 return;
17908 }
17909
Bram Moolenaar92124a32005-06-17 22:03:40 +000017910 /* Make sure the variable name is valid. */
17911 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017912 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17913 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017914 {
17915 EMSG2(_(e_illvar), varname);
17916 return;
17917 }
17918
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017919 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17920 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017921 if (v == NULL)
17922 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017923 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017924 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017926 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 return;
17928 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017929 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017932 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017933 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017934 else
17935 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017936 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017937 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940}
17941
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017942/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017943 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017944 * Also give an error message.
17945 */
17946 static int
17947var_check_ro(flags, name)
17948 int flags;
17949 char_u *name;
17950{
17951 if (flags & DI_FLAGS_RO)
17952 {
17953 EMSG2(_(e_readonlyvar), name);
17954 return TRUE;
17955 }
17956 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17957 {
17958 EMSG2(_(e_readonlysbx), name);
17959 return TRUE;
17960 }
17961 return FALSE;
17962}
17963
17964/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017965 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17966 * Also give an error message.
17967 */
17968 static int
17969var_check_fixed(flags, name)
17970 int flags;
17971 char_u *name;
17972{
17973 if (flags & DI_FLAGS_FIX)
17974 {
17975 EMSG2(_("E795: Cannot delete variable %s"), name);
17976 return TRUE;
17977 }
17978 return FALSE;
17979}
17980
17981/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017982 * Return TRUE if typeval "tv" is set to be locked (immutable).
17983 * Also give an error message, using "name".
17984 */
17985 static int
17986tv_check_lock(lock, name)
17987 int lock;
17988 char_u *name;
17989{
17990 if (lock & VAR_LOCKED)
17991 {
17992 EMSG2(_("E741: Value is locked: %s"),
17993 name == NULL ? (char_u *)_("Unknown") : name);
17994 return TRUE;
17995 }
17996 if (lock & VAR_FIXED)
17997 {
17998 EMSG2(_("E742: Cannot change value of %s"),
17999 name == NULL ? (char_u *)_("Unknown") : name);
18000 return TRUE;
18001 }
18002 return FALSE;
18003}
18004
18005/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018006 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018007 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018008 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018011copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018012 typval_T *from;
18013 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018015 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018016 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018017 switch (from->v_type)
18018 {
18019 case VAR_NUMBER:
18020 to->vval.v_number = from->vval.v_number;
18021 break;
18022 case VAR_STRING:
18023 case VAR_FUNC:
18024 if (from->vval.v_string == NULL)
18025 to->vval.v_string = NULL;
18026 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018027 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018028 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018029 if (from->v_type == VAR_FUNC)
18030 func_ref(to->vval.v_string);
18031 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018032 break;
18033 case VAR_LIST:
18034 if (from->vval.v_list == NULL)
18035 to->vval.v_list = NULL;
18036 else
18037 {
18038 to->vval.v_list = from->vval.v_list;
18039 ++to->vval.v_list->lv_refcount;
18040 }
18041 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018042 case VAR_DICT:
18043 if (from->vval.v_dict == NULL)
18044 to->vval.v_dict = NULL;
18045 else
18046 {
18047 to->vval.v_dict = from->vval.v_dict;
18048 ++to->vval.v_dict->dv_refcount;
18049 }
18050 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018051 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018052 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018053 break;
18054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055}
18056
18057/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018058 * Make a copy of an item.
18059 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018060 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18061 * reference to an already copied list/dict can be used.
18062 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018063 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018064 static int
18065item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018066 typval_T *from;
18067 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018068 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018069 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018070{
18071 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018072 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018073
Bram Moolenaar33570922005-01-25 22:26:29 +000018074 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018075 {
18076 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018077 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018078 }
18079 ++recurse;
18080
18081 switch (from->v_type)
18082 {
18083 case VAR_NUMBER:
18084 case VAR_STRING:
18085 case VAR_FUNC:
18086 copy_tv(from, to);
18087 break;
18088 case VAR_LIST:
18089 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018090 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018091 if (from->vval.v_list == NULL)
18092 to->vval.v_list = NULL;
18093 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18094 {
18095 /* use the copy made earlier */
18096 to->vval.v_list = from->vval.v_list->lv_copylist;
18097 ++to->vval.v_list->lv_refcount;
18098 }
18099 else
18100 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18101 if (to->vval.v_list == NULL)
18102 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018103 break;
18104 case VAR_DICT:
18105 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018106 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018107 if (from->vval.v_dict == NULL)
18108 to->vval.v_dict = NULL;
18109 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18110 {
18111 /* use the copy made earlier */
18112 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18113 ++to->vval.v_dict->dv_refcount;
18114 }
18115 else
18116 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18117 if (to->vval.v_dict == NULL)
18118 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018119 break;
18120 default:
18121 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018122 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018123 }
18124 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018125 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018126}
18127
18128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 * ":echo expr1 ..." print each argument separated with a space, add a
18130 * newline at the end.
18131 * ":echon expr1 ..." print each argument plain.
18132 */
18133 void
18134ex_echo(eap)
18135 exarg_T *eap;
18136{
18137 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018138 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018139 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 char_u *p;
18141 int needclr = TRUE;
18142 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018143 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144
18145 if (eap->skip)
18146 ++emsg_skip;
18147 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18148 {
18149 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018150 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 {
18152 /*
18153 * Report the invalid expression unless the expression evaluation
18154 * has been cancelled due to an aborting error, an interrupt, or an
18155 * exception.
18156 */
18157 if (!aborting())
18158 EMSG2(_(e_invexpr2), p);
18159 break;
18160 }
18161 if (!eap->skip)
18162 {
18163 if (atstart)
18164 {
18165 atstart = FALSE;
18166 /* Call msg_start() after eval1(), evaluating the expression
18167 * may cause a message to appear. */
18168 if (eap->cmdidx == CMD_echo)
18169 msg_start();
18170 }
18171 else if (eap->cmdidx == CMD_echo)
18172 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018173 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018174 if (p != NULL)
18175 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018177 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018179 if (*p != TAB && needclr)
18180 {
18181 /* remove any text still there from the command */
18182 msg_clr_eos();
18183 needclr = FALSE;
18184 }
18185 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 }
18187 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018188 {
18189#ifdef FEAT_MBYTE
18190 if (has_mbyte)
18191 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018192 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018193
18194 (void)msg_outtrans_len_attr(p, i, echo_attr);
18195 p += i - 1;
18196 }
18197 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018199 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018202 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018204 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 arg = skipwhite(arg);
18206 }
18207 eap->nextcmd = check_nextcmd(arg);
18208
18209 if (eap->skip)
18210 --emsg_skip;
18211 else
18212 {
18213 /* remove text that may still be there from the command */
18214 if (needclr)
18215 msg_clr_eos();
18216 if (eap->cmdidx == CMD_echo)
18217 msg_end();
18218 }
18219}
18220
18221/*
18222 * ":echohl {name}".
18223 */
18224 void
18225ex_echohl(eap)
18226 exarg_T *eap;
18227{
18228 int id;
18229
18230 id = syn_name2id(eap->arg);
18231 if (id == 0)
18232 echo_attr = 0;
18233 else
18234 echo_attr = syn_id2attr(id);
18235}
18236
18237/*
18238 * ":execute expr1 ..." execute the result of an expression.
18239 * ":echomsg expr1 ..." Print a message
18240 * ":echoerr expr1 ..." Print an error
18241 * Each gets spaces around each argument and a newline at the end for
18242 * echo commands
18243 */
18244 void
18245ex_execute(eap)
18246 exarg_T *eap;
18247{
18248 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018249 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 int ret = OK;
18251 char_u *p;
18252 garray_T ga;
18253 int len;
18254 int save_did_emsg;
18255
18256 ga_init2(&ga, 1, 80);
18257
18258 if (eap->skip)
18259 ++emsg_skip;
18260 while (*arg != NUL && *arg != '|' && *arg != '\n')
18261 {
18262 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018263 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 {
18265 /*
18266 * Report the invalid expression unless the expression evaluation
18267 * has been cancelled due to an aborting error, an interrupt, or an
18268 * exception.
18269 */
18270 if (!aborting())
18271 EMSG2(_(e_invexpr2), p);
18272 ret = FAIL;
18273 break;
18274 }
18275
18276 if (!eap->skip)
18277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018278 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 len = (int)STRLEN(p);
18280 if (ga_grow(&ga, len + 2) == FAIL)
18281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018282 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 ret = FAIL;
18284 break;
18285 }
18286 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 ga.ga_len += len;
18290 }
18291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018292 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 arg = skipwhite(arg);
18294 }
18295
18296 if (ret != FAIL && ga.ga_data != NULL)
18297 {
18298 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018301 out_flush();
18302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 else if (eap->cmdidx == CMD_echoerr)
18304 {
18305 /* We don't want to abort following commands, restore did_emsg. */
18306 save_did_emsg = did_emsg;
18307 EMSG((char_u *)ga.ga_data);
18308 if (!force_abort)
18309 did_emsg = save_did_emsg;
18310 }
18311 else if (eap->cmdidx == CMD_execute)
18312 do_cmdline((char_u *)ga.ga_data,
18313 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18314 }
18315
18316 ga_clear(&ga);
18317
18318 if (eap->skip)
18319 --emsg_skip;
18320
18321 eap->nextcmd = check_nextcmd(arg);
18322}
18323
18324/*
18325 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18326 * "arg" points to the "&" or '+' when called, to "option" when returning.
18327 * Returns NULL when no option name found. Otherwise pointer to the char
18328 * after the option name.
18329 */
18330 static char_u *
18331find_option_end(arg, opt_flags)
18332 char_u **arg;
18333 int *opt_flags;
18334{
18335 char_u *p = *arg;
18336
18337 ++p;
18338 if (*p == 'g' && p[1] == ':')
18339 {
18340 *opt_flags = OPT_GLOBAL;
18341 p += 2;
18342 }
18343 else if (*p == 'l' && p[1] == ':')
18344 {
18345 *opt_flags = OPT_LOCAL;
18346 p += 2;
18347 }
18348 else
18349 *opt_flags = 0;
18350
18351 if (!ASCII_ISALPHA(*p))
18352 return NULL;
18353 *arg = p;
18354
18355 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18356 p += 4; /* termcap option */
18357 else
18358 while (ASCII_ISALPHA(*p))
18359 ++p;
18360 return p;
18361}
18362
18363/*
18364 * ":function"
18365 */
18366 void
18367ex_function(eap)
18368 exarg_T *eap;
18369{
18370 char_u *theline;
18371 int j;
18372 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 char_u *name = NULL;
18375 char_u *p;
18376 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018377 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 garray_T newargs;
18379 garray_T newlines;
18380 int varargs = FALSE;
18381 int mustend = FALSE;
18382 int flags = 0;
18383 ufunc_T *fp;
18384 int indent;
18385 int nesting;
18386 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018387 dictitem_T *v;
18388 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018389 static int func_nr = 0; /* number for nameless function */
18390 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018391 hashtab_T *ht;
18392 int todo;
18393 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018394 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395
18396 /*
18397 * ":function" without argument: list functions.
18398 */
18399 if (ends_excmd(*eap->arg))
18400 {
18401 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018402 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018403 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018404 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018405 {
18406 if (!HASHITEM_EMPTY(hi))
18407 {
18408 --todo;
18409 fp = HI2UF(hi);
18410 if (!isdigit(*fp->uf_name))
18411 list_func_head(fp, FALSE);
18412 }
18413 }
18414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 eap->nextcmd = check_nextcmd(eap->arg);
18416 return;
18417 }
18418
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018419 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018420 * ":function /pat": list functions matching pattern.
18421 */
18422 if (*eap->arg == '/')
18423 {
18424 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18425 if (!eap->skip)
18426 {
18427 regmatch_T regmatch;
18428
18429 c = *p;
18430 *p = NUL;
18431 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18432 *p = c;
18433 if (regmatch.regprog != NULL)
18434 {
18435 regmatch.rm_ic = p_ic;
18436
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018437 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018438 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18439 {
18440 if (!HASHITEM_EMPTY(hi))
18441 {
18442 --todo;
18443 fp = HI2UF(hi);
18444 if (!isdigit(*fp->uf_name)
18445 && vim_regexec(&regmatch, fp->uf_name, 0))
18446 list_func_head(fp, FALSE);
18447 }
18448 }
18449 }
18450 }
18451 if (*p == '/')
18452 ++p;
18453 eap->nextcmd = check_nextcmd(p);
18454 return;
18455 }
18456
18457 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018458 * Get the function name. There are these situations:
18459 * func normal function name
18460 * "name" == func, "fudi.fd_dict" == NULL
18461 * dict.func new dictionary entry
18462 * "name" == NULL, "fudi.fd_dict" set,
18463 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18464 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018465 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018466 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18467 * dict.func existing dict entry that's not a Funcref
18468 * "name" == NULL, "fudi.fd_dict" set,
18469 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018472 name = trans_function_name(&p, eap->skip, 0, &fudi);
18473 paren = (vim_strchr(p, '(') != NULL);
18474 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 {
18476 /*
18477 * Return on an invalid expression in braces, unless the expression
18478 * evaluation has been cancelled due to an aborting error, an
18479 * interrupt, or an exception.
18480 */
18481 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018483 if (!eap->skip && fudi.fd_newkey != NULL)
18484 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018485 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 else
18489 eap->skip = TRUE;
18490 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018491
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 /* An error in a function call during evaluation of an expression in magic
18493 * braces should not cause the function not to be defined. */
18494 saved_did_emsg = did_emsg;
18495 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496
18497 /*
18498 * ":function func" with only function name: list function.
18499 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018500 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 {
18502 if (!ends_excmd(*skipwhite(p)))
18503 {
18504 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018505 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 }
18507 eap->nextcmd = check_nextcmd(p);
18508 if (eap->nextcmd != NULL)
18509 *p = NUL;
18510 if (!eap->skip && !got_int)
18511 {
18512 fp = find_func(name);
18513 if (fp != NULL)
18514 {
18515 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018516 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018518 if (FUNCLINE(fp, j) == NULL)
18519 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 msg_putchar('\n');
18521 msg_outnum((long)(j + 1));
18522 if (j < 9)
18523 msg_putchar(' ');
18524 if (j < 99)
18525 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018526 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 out_flush(); /* show a line at a time */
18528 ui_breakcheck();
18529 }
18530 if (!got_int)
18531 {
18532 msg_putchar('\n');
18533 msg_puts((char_u *)" endfunction");
18534 }
18535 }
18536 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018537 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018539 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 }
18541
18542 /*
18543 * ":function name(arg1, arg2)" Define function.
18544 */
18545 p = skipwhite(p);
18546 if (*p != '(')
18547 {
18548 if (!eap->skip)
18549 {
18550 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018551 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 }
18553 /* attempt to continue by skipping some text */
18554 if (vim_strchr(p, '(') != NULL)
18555 p = vim_strchr(p, '(');
18556 }
18557 p = skipwhite(p + 1);
18558
18559 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18560 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18561
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018562 if (!eap->skip)
18563 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018564 /* Check the name of the function. Unless it's a dictionary function
18565 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018566 if (name != NULL)
18567 arg = name;
18568 else
18569 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018570 if (arg != NULL && (fudi.fd_di == NULL
18571 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018572 {
18573 if (*arg == K_SPECIAL)
18574 j = 3;
18575 else
18576 j = 0;
18577 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18578 : eval_isnamec(arg[j])))
18579 ++j;
18580 if (arg[j] != NUL)
18581 emsg_funcname(_(e_invarg2), arg);
18582 }
18583 }
18584
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 /*
18586 * Isolate the arguments: "arg1, arg2, ...)"
18587 */
18588 while (*p != ')')
18589 {
18590 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18591 {
18592 varargs = TRUE;
18593 p += 3;
18594 mustend = TRUE;
18595 }
18596 else
18597 {
18598 arg = p;
18599 while (ASCII_ISALNUM(*p) || *p == '_')
18600 ++p;
18601 if (arg == p || isdigit(*arg)
18602 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18603 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18604 {
18605 if (!eap->skip)
18606 EMSG2(_("E125: Illegal argument: %s"), arg);
18607 break;
18608 }
18609 if (ga_grow(&newargs, 1) == FAIL)
18610 goto erret;
18611 c = *p;
18612 *p = NUL;
18613 arg = vim_strsave(arg);
18614 if (arg == NULL)
18615 goto erret;
18616 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18617 *p = c;
18618 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 if (*p == ',')
18620 ++p;
18621 else
18622 mustend = TRUE;
18623 }
18624 p = skipwhite(p);
18625 if (mustend && *p != ')')
18626 {
18627 if (!eap->skip)
18628 EMSG2(_(e_invarg2), eap->arg);
18629 break;
18630 }
18631 }
18632 ++p; /* skip the ')' */
18633
Bram Moolenaare9a41262005-01-15 22:18:47 +000018634 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 for (;;)
18636 {
18637 p = skipwhite(p);
18638 if (STRNCMP(p, "range", 5) == 0)
18639 {
18640 flags |= FC_RANGE;
18641 p += 5;
18642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018643 else if (STRNCMP(p, "dict", 4) == 0)
18644 {
18645 flags |= FC_DICT;
18646 p += 4;
18647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 else if (STRNCMP(p, "abort", 5) == 0)
18649 {
18650 flags |= FC_ABORT;
18651 p += 5;
18652 }
18653 else
18654 break;
18655 }
18656
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018657 /* When there is a line break use what follows for the function body.
18658 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18659 if (*p == '\n')
18660 line_arg = p + 1;
18661 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 EMSG(_(e_trailing));
18663
18664 /*
18665 * Read the body of the function, until ":endfunction" is found.
18666 */
18667 if (KeyTyped)
18668 {
18669 /* Check if the function already exists, don't let the user type the
18670 * whole function before telling him it doesn't work! For a script we
18671 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018672 if (!eap->skip && !eap->forceit)
18673 {
18674 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18675 EMSG(_(e_funcdict));
18676 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018677 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018680 if (!eap->skip && did_emsg)
18681 goto erret;
18682
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 msg_putchar('\n'); /* don't overwrite the function name */
18684 cmdline_row = msg_row;
18685 }
18686
18687 indent = 2;
18688 nesting = 0;
18689 for (;;)
18690 {
18691 msg_scroll = TRUE;
18692 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018693 sourcing_lnum_off = sourcing_lnum;
18694
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018695 if (line_arg != NULL)
18696 {
18697 /* Use eap->arg, split up in parts by line breaks. */
18698 theline = line_arg;
18699 p = vim_strchr(theline, '\n');
18700 if (p == NULL)
18701 line_arg += STRLEN(line_arg);
18702 else
18703 {
18704 *p = NUL;
18705 line_arg = p + 1;
18706 }
18707 }
18708 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 theline = getcmdline(':', 0L, indent);
18710 else
18711 theline = eap->getline(':', eap->cookie, indent);
18712 if (KeyTyped)
18713 lines_left = Rows - 1;
18714 if (theline == NULL)
18715 {
18716 EMSG(_("E126: Missing :endfunction"));
18717 goto erret;
18718 }
18719
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018720 /* Detect line continuation: sourcing_lnum increased more than one. */
18721 if (sourcing_lnum > sourcing_lnum_off + 1)
18722 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18723 else
18724 sourcing_lnum_off = 0;
18725
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726 if (skip_until != NULL)
18727 {
18728 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18729 * don't check for ":endfunc". */
18730 if (STRCMP(theline, skip_until) == 0)
18731 {
18732 vim_free(skip_until);
18733 skip_until = NULL;
18734 }
18735 }
18736 else
18737 {
18738 /* skip ':' and blanks*/
18739 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18740 ;
18741
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018742 /* Check for "endfunction". */
18743 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018745 if (line_arg == NULL)
18746 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 break;
18748 }
18749
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018750 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 * at "end". */
18752 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18753 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018754 else if (STRNCMP(p, "if", 2) == 0
18755 || STRNCMP(p, "wh", 2) == 0
18756 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 || STRNCMP(p, "try", 3) == 0)
18758 indent += 2;
18759
18760 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018761 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018763 if (*p == '!')
18764 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 p += eval_fname_script(p);
18766 if (ASCII_ISALPHA(*p))
18767 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018768 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 if (*skipwhite(p) == '(')
18770 {
18771 ++nesting;
18772 indent += 2;
18773 }
18774 }
18775 }
18776
18777 /* Check for ":append" or ":insert". */
18778 p = skip_range(p, NULL);
18779 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18780 || (p[0] == 'i'
18781 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18782 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18783 skip_until = vim_strsave((char_u *)".");
18784
18785 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18786 arg = skipwhite(skiptowhite(p));
18787 if (arg[0] == '<' && arg[1] =='<'
18788 && ((p[0] == 'p' && p[1] == 'y'
18789 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18790 || (p[0] == 'p' && p[1] == 'e'
18791 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18792 || (p[0] == 't' && p[1] == 'c'
18793 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18794 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18795 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018796 || (p[0] == 'm' && p[1] == 'z'
18797 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 ))
18799 {
18800 /* ":python <<" continues until a dot, like ":append" */
18801 p = skipwhite(arg + 2);
18802 if (*p == NUL)
18803 skip_until = vim_strsave((char_u *)".");
18804 else
18805 skip_until = vim_strsave(p);
18806 }
18807 }
18808
18809 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018810 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018811 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018812 if (line_arg == NULL)
18813 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018815 }
18816
18817 /* Copy the line to newly allocated memory. get_one_sourceline()
18818 * allocates 250 bytes per line, this saves 80% on average. The cost
18819 * is an extra alloc/free. */
18820 p = vim_strsave(theline);
18821 if (p != NULL)
18822 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018823 if (line_arg == NULL)
18824 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018825 theline = p;
18826 }
18827
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018828 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18829
18830 /* Add NULL lines for continuation lines, so that the line count is
18831 * equal to the index in the growarray. */
18832 while (sourcing_lnum_off-- > 0)
18833 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018834
18835 /* Check for end of eap->arg. */
18836 if (line_arg != NULL && *line_arg == NUL)
18837 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 }
18839
18840 /* Don't define the function when skipping commands or when an error was
18841 * detected. */
18842 if (eap->skip || did_emsg)
18843 goto erret;
18844
18845 /*
18846 * If there are no errors, add the function
18847 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018848 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018849 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018850 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018851 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018852 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018853 emsg_funcname("E707: Function name conflicts with variable: %s",
18854 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018855 goto erret;
18856 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018857
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018858 fp = find_func(name);
18859 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018861 if (!eap->forceit)
18862 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018863 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018864 goto erret;
18865 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018866 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018867 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018868 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018869 name);
18870 goto erret;
18871 }
18872 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018873 ga_clear_strings(&(fp->uf_args));
18874 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018875 vim_free(name);
18876 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 }
18879 else
18880 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018881 char numbuf[20];
18882
18883 fp = NULL;
18884 if (fudi.fd_newkey == NULL && !eap->forceit)
18885 {
18886 EMSG(_(e_funcdict));
18887 goto erret;
18888 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018889 if (fudi.fd_di == NULL)
18890 {
18891 /* Can't add a function to a locked dictionary */
18892 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18893 goto erret;
18894 }
18895 /* Can't change an existing function if it is locked */
18896 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18897 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018898
18899 /* Give the function a sequential number. Can only be used with a
18900 * Funcref! */
18901 vim_free(name);
18902 sprintf(numbuf, "%d", ++func_nr);
18903 name = vim_strsave((char_u *)numbuf);
18904 if (name == NULL)
18905 goto erret;
18906 }
18907
18908 if (fp == NULL)
18909 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018910 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018911 {
18912 int slen, plen;
18913 char_u *scriptname;
18914
18915 /* Check that the autoload name matches the script name. */
18916 j = FAIL;
18917 if (sourcing_name != NULL)
18918 {
18919 scriptname = autoload_name(name);
18920 if (scriptname != NULL)
18921 {
18922 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018923 plen = (int)STRLEN(p);
18924 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018925 if (slen > plen && fnamecmp(p,
18926 sourcing_name + slen - plen) == 0)
18927 j = OK;
18928 vim_free(scriptname);
18929 }
18930 }
18931 if (j == FAIL)
18932 {
18933 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18934 goto erret;
18935 }
18936 }
18937
18938 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 if (fp == NULL)
18940 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018941
18942 if (fudi.fd_dict != NULL)
18943 {
18944 if (fudi.fd_di == NULL)
18945 {
18946 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018947 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018948 if (fudi.fd_di == NULL)
18949 {
18950 vim_free(fp);
18951 goto erret;
18952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018953 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18954 {
18955 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018956 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018957 goto erret;
18958 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018959 }
18960 else
18961 /* overwrite existing dict entry */
18962 clear_tv(&fudi.fd_di->di_tv);
18963 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018964 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018965 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018966 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018967
18968 /* behave like "dict" was used */
18969 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018970 }
18971
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018973 STRCPY(fp->uf_name, name);
18974 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018976 fp->uf_args = newargs;
18977 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018978#ifdef FEAT_PROFILE
18979 fp->uf_tml_count = NULL;
18980 fp->uf_tml_total = NULL;
18981 fp->uf_tml_self = NULL;
18982 fp->uf_profiling = FALSE;
18983 if (prof_def_func())
18984 func_do_profile(fp);
18985#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018986 fp->uf_varargs = varargs;
18987 fp->uf_flags = flags;
18988 fp->uf_calls = 0;
18989 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018990 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991
18992erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 ga_clear_strings(&newargs);
18994 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018995ret_free:
18996 vim_free(skip_until);
18997 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000}
19001
19002/*
19003 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019004 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019006 * flags:
19007 * TFN_INT: internal function name OK
19008 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 * Advances "pp" to just after the function name (if no error).
19010 */
19011 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019012trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 char_u **pp;
19014 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019015 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019016 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019018 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 char_u *start;
19020 char_u *end;
19021 int lead;
19022 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019024 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019025
19026 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019027 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019029
19030 /* Check for hard coded <SNR>: already translated function ID (from a user
19031 * command). */
19032 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19033 && (*pp)[2] == (int)KE_SNR)
19034 {
19035 *pp += 3;
19036 len = get_id_len(pp) + 3;
19037 return vim_strnsave(start, len);
19038 }
19039
19040 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19041 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019043 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019045
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019046 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19047 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019048 if (end == start)
19049 {
19050 if (!skip)
19051 EMSG(_("E129: Function name required"));
19052 goto theend;
19053 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019054 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019055 {
19056 /*
19057 * Report an invalid expression in braces, unless the expression
19058 * evaluation has been cancelled due to an aborting error, an
19059 * interrupt, or an exception.
19060 */
19061 if (!aborting())
19062 {
19063 if (end != NULL)
19064 EMSG2(_(e_invarg2), start);
19065 }
19066 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019067 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019068 goto theend;
19069 }
19070
19071 if (lv.ll_tv != NULL)
19072 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019073 if (fdp != NULL)
19074 {
19075 fdp->fd_dict = lv.ll_dict;
19076 fdp->fd_newkey = lv.ll_newkey;
19077 lv.ll_newkey = NULL;
19078 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019079 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019080 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19081 {
19082 name = vim_strsave(lv.ll_tv->vval.v_string);
19083 *pp = end;
19084 }
19085 else
19086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019087 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19088 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019089 EMSG(_(e_funcref));
19090 else
19091 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019092 name = NULL;
19093 }
19094 goto theend;
19095 }
19096
19097 if (lv.ll_name == NULL)
19098 {
19099 /* Error found, but continue after the function name. */
19100 *pp = end;
19101 goto theend;
19102 }
19103
19104 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019105 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019106 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019107 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19108 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19109 {
19110 /* When there was "s:" already or the name expanded to get a
19111 * leading "s:" then remove it. */
19112 lv.ll_name += 2;
19113 len -= 2;
19114 lead = 2;
19115 }
19116 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019117 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019118 {
19119 if (lead == 2) /* skip over "s:" */
19120 lv.ll_name += 2;
19121 len = (int)(end - lv.ll_name);
19122 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019123
19124 /*
19125 * Copy the function name to allocated memory.
19126 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19127 * Accept <SNR>123_name() outside a script.
19128 */
19129 if (skip)
19130 lead = 0; /* do nothing */
19131 else if (lead > 0)
19132 {
19133 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019134 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19135 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019136 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019137 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019138 if (current_SID <= 0)
19139 {
19140 EMSG(_(e_usingsid));
19141 goto theend;
19142 }
19143 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19144 lead += (int)STRLEN(sid_buf);
19145 }
19146 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019147 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019148 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019149 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019150 goto theend;
19151 }
19152 name = alloc((unsigned)(len + lead + 1));
19153 if (name != NULL)
19154 {
19155 if (lead > 0)
19156 {
19157 name[0] = K_SPECIAL;
19158 name[1] = KS_EXTRA;
19159 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019160 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019161 STRCPY(name + 3, sid_buf);
19162 }
19163 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19164 name[len + lead] = NUL;
19165 }
19166 *pp = end;
19167
19168theend:
19169 clear_lval(&lv);
19170 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171}
19172
19173/*
19174 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19175 * Return 2 if "p" starts with "s:".
19176 * Return 0 otherwise.
19177 */
19178 static int
19179eval_fname_script(p)
19180 char_u *p;
19181{
19182 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19183 || STRNICMP(p + 1, "SNR>", 4) == 0))
19184 return 5;
19185 if (p[0] == 's' && p[1] == ':')
19186 return 2;
19187 return 0;
19188}
19189
19190/*
19191 * Return TRUE if "p" starts with "<SID>" or "s:".
19192 * Only works if eval_fname_script() returned non-zero for "p"!
19193 */
19194 static int
19195eval_fname_sid(p)
19196 char_u *p;
19197{
19198 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19199}
19200
19201/*
19202 * List the head of the function: "name(arg1, arg2)".
19203 */
19204 static void
19205list_func_head(fp, indent)
19206 ufunc_T *fp;
19207 int indent;
19208{
19209 int j;
19210
19211 msg_start();
19212 if (indent)
19213 MSG_PUTS(" ");
19214 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019215 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 {
19217 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019218 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 }
19220 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019221 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019223 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 {
19225 if (j)
19226 MSG_PUTS(", ");
19227 msg_puts(FUNCARG(fp, j));
19228 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019229 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 {
19231 if (j)
19232 MSG_PUTS(", ");
19233 MSG_PUTS("...");
19234 }
19235 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019236 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019237 if (p_verbose > 0)
19238 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239}
19240
19241/*
19242 * Find a function by name, return pointer to it in ufuncs.
19243 * Return NULL for unknown function.
19244 */
19245 static ufunc_T *
19246find_func(name)
19247 char_u *name;
19248{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019249 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019251 hi = hash_find(&func_hashtab, name);
19252 if (!HASHITEM_EMPTY(hi))
19253 return HI2UF(hi);
19254 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255}
19256
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019257#if defined(EXITFREE) || defined(PROTO)
19258 void
19259free_all_functions()
19260{
19261 hashitem_T *hi;
19262
19263 /* Need to start all over every time, because func_free() may change the
19264 * hash table. */
19265 while (func_hashtab.ht_used > 0)
19266 for (hi = func_hashtab.ht_array; ; ++hi)
19267 if (!HASHITEM_EMPTY(hi))
19268 {
19269 func_free(HI2UF(hi));
19270 break;
19271 }
19272}
19273#endif
19274
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019275/*
19276 * Return TRUE if a function "name" exists.
19277 */
19278 static int
19279function_exists(name)
19280 char_u *name;
19281{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019282 char_u *nm = name;
19283 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019284 int n = FALSE;
19285
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019286 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019287 nm = skipwhite(nm);
19288
19289 /* Only accept "funcname", "funcname ", "funcname (..." and
19290 * "funcname(...", not "funcname!...". */
19291 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019292 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019293 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019294 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019295 else
19296 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019297 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019298 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019299 return n;
19300}
19301
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019302/*
19303 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019304 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019305 */
19306 static int
19307builtin_function(name)
19308 char_u *name;
19309{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019310 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19311 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019312}
19313
Bram Moolenaar05159a02005-02-26 23:04:13 +000019314#if defined(FEAT_PROFILE) || defined(PROTO)
19315/*
19316 * Start profiling function "fp".
19317 */
19318 static void
19319func_do_profile(fp)
19320 ufunc_T *fp;
19321{
19322 fp->uf_tm_count = 0;
19323 profile_zero(&fp->uf_tm_self);
19324 profile_zero(&fp->uf_tm_total);
19325 if (fp->uf_tml_count == NULL)
19326 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19327 (sizeof(int) * fp->uf_lines.ga_len));
19328 if (fp->uf_tml_total == NULL)
19329 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19330 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19331 if (fp->uf_tml_self == NULL)
19332 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19333 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19334 fp->uf_tml_idx = -1;
19335 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19336 || fp->uf_tml_self == NULL)
19337 return; /* out of memory */
19338
19339 fp->uf_profiling = TRUE;
19340}
19341
19342/*
19343 * Dump the profiling results for all functions in file "fd".
19344 */
19345 void
19346func_dump_profile(fd)
19347 FILE *fd;
19348{
19349 hashitem_T *hi;
19350 int todo;
19351 ufunc_T *fp;
19352 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019353 ufunc_T **sorttab;
19354 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019355
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019356 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019357 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19358
Bram Moolenaar05159a02005-02-26 23:04:13 +000019359 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19360 {
19361 if (!HASHITEM_EMPTY(hi))
19362 {
19363 --todo;
19364 fp = HI2UF(hi);
19365 if (fp->uf_profiling)
19366 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019367 if (sorttab != NULL)
19368 sorttab[st_len++] = fp;
19369
Bram Moolenaar05159a02005-02-26 23:04:13 +000019370 if (fp->uf_name[0] == K_SPECIAL)
19371 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19372 else
19373 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19374 if (fp->uf_tm_count == 1)
19375 fprintf(fd, "Called 1 time\n");
19376 else
19377 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19378 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19379 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19380 fprintf(fd, "\n");
19381 fprintf(fd, "count total (s) self (s)\n");
19382
19383 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19384 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019385 if (FUNCLINE(fp, i) == NULL)
19386 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019387 prof_func_line(fd, fp->uf_tml_count[i],
19388 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019389 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19390 }
19391 fprintf(fd, "\n");
19392 }
19393 }
19394 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019395
19396 if (sorttab != NULL && st_len > 0)
19397 {
19398 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19399 prof_total_cmp);
19400 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19401 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19402 prof_self_cmp);
19403 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19404 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019405}
Bram Moolenaar73830342005-02-28 22:48:19 +000019406
19407 static void
19408prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19409 FILE *fd;
19410 ufunc_T **sorttab;
19411 int st_len;
19412 char *title;
19413 int prefer_self; /* when equal print only self time */
19414{
19415 int i;
19416 ufunc_T *fp;
19417
19418 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19419 fprintf(fd, "count total (s) self (s) function\n");
19420 for (i = 0; i < 20 && i < st_len; ++i)
19421 {
19422 fp = sorttab[i];
19423 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19424 prefer_self);
19425 if (fp->uf_name[0] == K_SPECIAL)
19426 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19427 else
19428 fprintf(fd, " %s()\n", fp->uf_name);
19429 }
19430 fprintf(fd, "\n");
19431}
19432
19433/*
19434 * Print the count and times for one function or function line.
19435 */
19436 static void
19437prof_func_line(fd, count, total, self, prefer_self)
19438 FILE *fd;
19439 int count;
19440 proftime_T *total;
19441 proftime_T *self;
19442 int prefer_self; /* when equal print only self time */
19443{
19444 if (count > 0)
19445 {
19446 fprintf(fd, "%5d ", count);
19447 if (prefer_self && profile_equal(total, self))
19448 fprintf(fd, " ");
19449 else
19450 fprintf(fd, "%s ", profile_msg(total));
19451 if (!prefer_self && profile_equal(total, self))
19452 fprintf(fd, " ");
19453 else
19454 fprintf(fd, "%s ", profile_msg(self));
19455 }
19456 else
19457 fprintf(fd, " ");
19458}
19459
19460/*
19461 * Compare function for total time sorting.
19462 */
19463 static int
19464#ifdef __BORLANDC__
19465_RTLENTRYF
19466#endif
19467prof_total_cmp(s1, s2)
19468 const void *s1;
19469 const void *s2;
19470{
19471 ufunc_T *p1, *p2;
19472
19473 p1 = *(ufunc_T **)s1;
19474 p2 = *(ufunc_T **)s2;
19475 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19476}
19477
19478/*
19479 * Compare function for self time sorting.
19480 */
19481 static int
19482#ifdef __BORLANDC__
19483_RTLENTRYF
19484#endif
19485prof_self_cmp(s1, s2)
19486 const void *s1;
19487 const void *s2;
19488{
19489 ufunc_T *p1, *p2;
19490
19491 p1 = *(ufunc_T **)s1;
19492 p2 = *(ufunc_T **)s2;
19493 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19494}
19495
Bram Moolenaar05159a02005-02-26 23:04:13 +000019496#endif
19497
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019498/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019499 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019500 * Return TRUE if a package was loaded.
19501 */
19502 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019503script_autoload(name, reload)
19504 char_u *name;
19505 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019506{
19507 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019508 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019509 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019510 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019511
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019512 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019513 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019514 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019515 return FALSE;
19516
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019517 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019518
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019519 /* Find the name in the list of previously loaded package names. Skip
19520 * "autoload/", it's always the same. */
19521 for (i = 0; i < ga_loaded.ga_len; ++i)
19522 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19523 break;
19524 if (!reload && i < ga_loaded.ga_len)
19525 ret = FALSE; /* was loaded already */
19526 else
19527 {
19528 /* Remember the name if it wasn't loaded already. */
19529 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19530 {
19531 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19532 tofree = NULL;
19533 }
19534
19535 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019536 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019537 ret = TRUE;
19538 }
19539
19540 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019541 return ret;
19542}
19543
19544/*
19545 * Return the autoload script name for a function or variable name.
19546 * Returns NULL when out of memory.
19547 */
19548 static char_u *
19549autoload_name(name)
19550 char_u *name;
19551{
19552 char_u *p;
19553 char_u *scriptname;
19554
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019555 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019556 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19557 if (scriptname == NULL)
19558 return FALSE;
19559 STRCPY(scriptname, "autoload/");
19560 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019561 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019562 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019563 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019564 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019565 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019566}
19567
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19569
19570/*
19571 * Function given to ExpandGeneric() to obtain the list of user defined
19572 * function names.
19573 */
19574 char_u *
19575get_user_func_name(xp, idx)
19576 expand_T *xp;
19577 int idx;
19578{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019579 static long_u done;
19580 static hashitem_T *hi;
19581 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582
19583 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019585 done = 0;
19586 hi = func_hashtab.ht_array;
19587 }
19588 if (done < func_hashtab.ht_used)
19589 {
19590 if (done++ > 0)
19591 ++hi;
19592 while (HASHITEM_EMPTY(hi))
19593 ++hi;
19594 fp = HI2UF(hi);
19595
19596 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19597 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598
19599 cat_func_name(IObuff, fp);
19600 if (xp->xp_context != EXPAND_USER_FUNC)
19601 {
19602 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019603 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 STRCAT(IObuff, ")");
19605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 return IObuff;
19607 }
19608 return NULL;
19609}
19610
19611#endif /* FEAT_CMDL_COMPL */
19612
19613/*
19614 * Copy the function name of "fp" to buffer "buf".
19615 * "buf" must be able to hold the function name plus three bytes.
19616 * Takes care of script-local function names.
19617 */
19618 static void
19619cat_func_name(buf, fp)
19620 char_u *buf;
19621 ufunc_T *fp;
19622{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019623 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 {
19625 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019626 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 }
19628 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019629 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630}
19631
19632/*
19633 * ":delfunction {name}"
19634 */
19635 void
19636ex_delfunction(eap)
19637 exarg_T *eap;
19638{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019639 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 char_u *p;
19641 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019642 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643
19644 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019645 name = trans_function_name(&p, eap->skip, 0, &fudi);
19646 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019648 {
19649 if (fudi.fd_dict != NULL && !eap->skip)
19650 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 if (!ends_excmd(*skipwhite(p)))
19654 {
19655 vim_free(name);
19656 EMSG(_(e_trailing));
19657 return;
19658 }
19659 eap->nextcmd = check_nextcmd(p);
19660 if (eap->nextcmd != NULL)
19661 *p = NUL;
19662
19663 if (!eap->skip)
19664 fp = find_func(name);
19665 vim_free(name);
19666
19667 if (!eap->skip)
19668 {
19669 if (fp == NULL)
19670 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019671 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 return;
19673 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019674 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 {
19676 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19677 return;
19678 }
19679
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019680 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019682 /* Delete the dict item that refers to the function, it will
19683 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019684 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019686 else
19687 func_free(fp);
19688 }
19689}
19690
19691/*
19692 * Free a function and remove it from the list of functions.
19693 */
19694 static void
19695func_free(fp)
19696 ufunc_T *fp;
19697{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019698 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019699
19700 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019701 ga_clear_strings(&(fp->uf_args));
19702 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019703#ifdef FEAT_PROFILE
19704 vim_free(fp->uf_tml_count);
19705 vim_free(fp->uf_tml_total);
19706 vim_free(fp->uf_tml_self);
19707#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019708
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019709 /* remove the function from the function hashtable */
19710 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19711 if (HASHITEM_EMPTY(hi))
19712 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019713 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019714 hash_remove(&func_hashtab, hi);
19715
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019716 vim_free(fp);
19717}
19718
19719/*
19720 * Unreference a Function: decrement the reference count and free it when it
19721 * becomes zero. Only for numbered functions.
19722 */
19723 static void
19724func_unref(name)
19725 char_u *name;
19726{
19727 ufunc_T *fp;
19728
19729 if (name != NULL && isdigit(*name))
19730 {
19731 fp = find_func(name);
19732 if (fp == NULL)
19733 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019734 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019735 {
19736 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019737 * when "uf_calls" becomes zero. */
19738 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019739 func_free(fp);
19740 }
19741 }
19742}
19743
19744/*
19745 * Count a reference to a Function.
19746 */
19747 static void
19748func_ref(name)
19749 char_u *name;
19750{
19751 ufunc_T *fp;
19752
19753 if (name != NULL && isdigit(*name))
19754 {
19755 fp = find_func(name);
19756 if (fp == NULL)
19757 EMSG2(_(e_intern2), "func_ref()");
19758 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019759 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 }
19761}
19762
19763/*
19764 * Call a user function.
19765 */
19766 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019767call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 ufunc_T *fp; /* pointer to function */
19769 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019770 typval_T *argvars; /* arguments */
19771 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 linenr_T firstline; /* first line of range */
19773 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019774 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775{
Bram Moolenaar33570922005-01-25 22:26:29 +000019776 char_u *save_sourcing_name;
19777 linenr_T save_sourcing_lnum;
19778 scid_T save_current_SID;
19779 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 int save_did_emsg;
19781 static int depth = 0;
19782 dictitem_T *v;
19783 int fixvar_idx = 0; /* index in fixvar[] */
19784 int i;
19785 int ai;
19786 char_u numbuf[NUMBUFLEN];
19787 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019788#ifdef FEAT_PROFILE
19789 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019790 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792
19793 /* If depth of calling is getting too high, don't execute the function */
19794 if (depth >= p_mfd)
19795 {
19796 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019797 rettv->v_type = VAR_NUMBER;
19798 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 return;
19800 }
19801 ++depth;
19802
19803 line_breakcheck(); /* check for CTRL-C hit */
19804
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019805 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019808 fc.rettv = rettv;
19809 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 fc.linenr = 0;
19811 fc.returned = FALSE;
19812 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019814 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 fc.dbg_tick = debug_tick;
19816
Bram Moolenaar33570922005-01-25 22:26:29 +000019817 /*
19818 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19819 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19820 * each argument variable and saves a lot of time.
19821 */
19822 /*
19823 * Init l: variables.
19824 */
19825 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019826 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019827 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019828 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19829 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019830 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019831 name = v->di_key;
19832 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019833 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19834 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19835 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019836 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019837 v->di_tv.vval.v_dict = selfdict;
19838 ++selfdict->dv_refcount;
19839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019840
Bram Moolenaar33570922005-01-25 22:26:29 +000019841 /*
19842 * Init a: variables.
19843 * Set a:0 to "argcount".
19844 * Set a:000 to a list with room for the "..." arguments.
19845 */
19846 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19847 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019848 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 v = &fc.fixvar[fixvar_idx++].var;
19850 STRCPY(v->di_key, "000");
19851 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19852 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19853 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019854 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019855 v->di_tv.vval.v_list = &fc.l_varlist;
19856 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19857 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019858 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019859
19860 /*
19861 * Set a:firstline to "firstline" and a:lastline to "lastline".
19862 * Set a:name to named arguments.
19863 * Set a:N to the "..." arguments.
19864 */
19865 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19866 (varnumber_T)firstline);
19867 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19868 (varnumber_T)lastline);
19869 for (i = 0; i < argcount; ++i)
19870 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019871 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019872 if (ai < 0)
19873 /* named argument a:name */
19874 name = FUNCARG(fp, i);
19875 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019876 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 /* "..." argument a:1, a:2, etc. */
19878 sprintf((char *)numbuf, "%d", ai + 1);
19879 name = numbuf;
19880 }
19881 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19882 {
19883 v = &fc.fixvar[fixvar_idx++].var;
19884 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19885 }
19886 else
19887 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019888 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19889 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019890 if (v == NULL)
19891 break;
19892 v->di_flags = DI_FLAGS_RO;
19893 }
19894 STRCPY(v->di_key, name);
19895 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019897 /* Note: the values are copied directly to avoid alloc/free.
19898 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019899 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019900 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019901
19902 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19903 {
19904 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19905 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019906 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019907 }
19908 }
19909
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 /* Don't redraw while executing the function. */
19911 ++RedrawingDisabled;
19912 save_sourcing_name = sourcing_name;
19913 save_sourcing_lnum = sourcing_lnum;
19914 sourcing_lnum = 1;
19915 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019916 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 if (sourcing_name != NULL)
19918 {
19919 if (save_sourcing_name != NULL
19920 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19921 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19922 else
19923 STRCPY(sourcing_name, "function ");
19924 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19925
19926 if (p_verbose >= 12)
19927 {
19928 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019929 verbose_enter_scroll();
19930
Bram Moolenaar555b2802005-05-19 21:08:39 +000019931 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 if (p_verbose >= 14)
19933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019935 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019936 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937
19938 msg_puts((char_u *)"(");
19939 for (i = 0; i < argcount; ++i)
19940 {
19941 if (i > 0)
19942 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019943 if (argvars[i].v_type == VAR_NUMBER)
19944 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 else
19946 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019947 trunc_string(tv2string(&argvars[i], &tofree,
19948 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019950 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 }
19952 }
19953 msg_puts((char_u *)")");
19954 }
19955 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019956
19957 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 --no_wait_return;
19959 }
19960 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019961#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019962 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019963 {
19964 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19965 func_do_profile(fp);
19966 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019967 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019968 {
19969 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019970 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019971 profile_zero(&fp->uf_tm_children);
19972 }
19973 script_prof_save(&wait_start);
19974 }
19975#endif
19976
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019978 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 save_did_emsg = did_emsg;
19980 did_emsg = FALSE;
19981
19982 /* call do_cmdline() to execute the lines */
19983 do_cmdline(NULL, get_func_line, (void *)&fc,
19984 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19985
19986 --RedrawingDisabled;
19987
19988 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019989 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019991 clear_tv(rettv);
19992 rettv->v_type = VAR_NUMBER;
19993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 }
19995
Bram Moolenaar05159a02005-02-26 23:04:13 +000019996#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019997 if (do_profiling == PROF_YES && (fp->uf_profiling
19998 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019999 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020000 profile_end(&call_start);
20001 profile_sub_wait(&wait_start, &call_start);
20002 profile_add(&fp->uf_tm_total, &call_start);
20003 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020004 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020005 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020006 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20007 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020008 }
20009 }
20010#endif
20011
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 /* when being verbose, mention the return value */
20013 if (p_verbose >= 12)
20014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020016 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020019 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020021 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20022 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020025 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020026 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020027 char_u *tofree;
20028
Bram Moolenaar555b2802005-05-19 21:08:39 +000020029 /* The value may be very long. Skip the middle part, so that we
20030 * have some idea how it starts and ends. smsg() would always
20031 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000020032 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020033 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000020034 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020035 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 }
20037 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020038
20039 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 --no_wait_return;
20041 }
20042
20043 vim_free(sourcing_name);
20044 sourcing_name = save_sourcing_name;
20045 sourcing_lnum = save_sourcing_lnum;
20046 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020047#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020048 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020049 script_prof_restore(&wait_start);
20050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051
20052 if (p_verbose >= 12 && sourcing_name != NULL)
20053 {
20054 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020055 verbose_enter_scroll();
20056
Bram Moolenaar555b2802005-05-19 21:08:39 +000020057 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020059
20060 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 --no_wait_return;
20062 }
20063
20064 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020065 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066
Bram Moolenaar33570922005-01-25 22:26:29 +000020067 /* The a: variables typevals were not alloced, only free the allocated
20068 * variables. */
20069 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20070
20071 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 --depth;
20073}
20074
20075/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020076 * Add a number variable "name" to dict "dp" with value "nr".
20077 */
20078 static void
20079add_nr_var(dp, v, name, nr)
20080 dict_T *dp;
20081 dictitem_T *v;
20082 char *name;
20083 varnumber_T nr;
20084{
20085 STRCPY(v->di_key, name);
20086 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20087 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20088 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020089 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020090 v->di_tv.vval.v_number = nr;
20091}
20092
20093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 * ":return [expr]"
20095 */
20096 void
20097ex_return(eap)
20098 exarg_T *eap;
20099{
20100 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020101 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 int returning = FALSE;
20103
20104 if (current_funccal == NULL)
20105 {
20106 EMSG(_("E133: :return not inside a function"));
20107 return;
20108 }
20109
20110 if (eap->skip)
20111 ++emsg_skip;
20112
20113 eap->nextcmd = NULL;
20114 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020115 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 {
20117 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020120 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 }
20122 /* It's safer to return also on error. */
20123 else if (!eap->skip)
20124 {
20125 /*
20126 * Return unless the expression evaluation has been cancelled due to an
20127 * aborting error, an interrupt, or an exception.
20128 */
20129 if (!aborting())
20130 returning = do_return(eap, FALSE, TRUE, NULL);
20131 }
20132
20133 /* When skipping or the return gets pending, advance to the next command
20134 * in this line (!returning). Otherwise, ignore the rest of the line.
20135 * Following lines will be ignored by get_func_line(). */
20136 if (returning)
20137 eap->nextcmd = NULL;
20138 else if (eap->nextcmd == NULL) /* no argument */
20139 eap->nextcmd = check_nextcmd(arg);
20140
20141 if (eap->skip)
20142 --emsg_skip;
20143}
20144
20145/*
20146 * Return from a function. Possibly makes the return pending. Also called
20147 * for a pending return at the ":endtry" or after returning from an extra
20148 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020149 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020150 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 * FALSE when the return gets pending.
20152 */
20153 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020154do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 exarg_T *eap;
20156 int reanimate;
20157 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020158 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159{
20160 int idx;
20161 struct condstack *cstack = eap->cstack;
20162
20163 if (reanimate)
20164 /* Undo the return. */
20165 current_funccal->returned = FALSE;
20166
20167 /*
20168 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20169 * not in its finally clause (which then is to be executed next) is found.
20170 * In this case, make the ":return" pending for execution at the ":endtry".
20171 * Otherwise, return normally.
20172 */
20173 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20174 if (idx >= 0)
20175 {
20176 cstack->cs_pending[idx] = CSTP_RETURN;
20177
20178 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020179 /* A pending return again gets pending. "rettv" points to an
20180 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020182 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 else
20184 {
20185 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020186 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020188 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 {
20192 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020193 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020194 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 else
20196 EMSG(_(e_outofmem));
20197 }
20198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020199 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200
20201 if (reanimate)
20202 {
20203 /* The pending return value could be overwritten by a ":return"
20204 * without argument in a finally clause; reset the default
20205 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020206 current_funccal->rettv->v_type = VAR_NUMBER;
20207 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 }
20209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020210 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 }
20212 else
20213 {
20214 current_funccal->returned = TRUE;
20215
20216 /* If the return is carried out now, store the return value. For
20217 * a return immediately after reanimation, the value is already
20218 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020219 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020221 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020222 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020224 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 }
20226 }
20227
20228 return idx < 0;
20229}
20230
20231/*
20232 * Free the variable with a pending return value.
20233 */
20234 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020235discard_pending_return(rettv)
20236 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237{
Bram Moolenaar33570922005-01-25 22:26:29 +000020238 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239}
20240
20241/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020242 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 * is an allocated string. Used by report_pending() for verbose messages.
20244 */
20245 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020246get_return_cmd(rettv)
20247 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020249 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020251 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020253 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020254 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020255 if (s == NULL)
20256 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020257
20258 STRCPY(IObuff, ":return ");
20259 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20260 if (STRLEN(s) + 8 >= IOSIZE)
20261 STRCPY(IObuff + IOSIZE - 4, "...");
20262 vim_free(tofree);
20263 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264}
20265
20266/*
20267 * Get next function line.
20268 * Called by do_cmdline() to get the next line.
20269 * Returns allocated string, or NULL for end of function.
20270 */
20271/* ARGSUSED */
20272 char_u *
20273get_func_line(c, cookie, indent)
20274 int c; /* not used */
20275 void *cookie;
20276 int indent; /* not used */
20277{
Bram Moolenaar33570922005-01-25 22:26:29 +000020278 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020279 ufunc_T *fp = fcp->func;
20280 char_u *retval;
20281 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282
20283 /* If breakpoints have been added/deleted need to check for it. */
20284 if (fcp->dbg_tick != debug_tick)
20285 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020286 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 sourcing_lnum);
20288 fcp->dbg_tick = debug_tick;
20289 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020290#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020291 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020292 func_line_end(cookie);
20293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294
Bram Moolenaar05159a02005-02-26 23:04:13 +000020295 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020296 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20297 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 retval = NULL;
20299 else
20300 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020301 /* Skip NULL lines (continuation lines). */
20302 while (fcp->linenr < gap->ga_len
20303 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20304 ++fcp->linenr;
20305 if (fcp->linenr >= gap->ga_len)
20306 retval = NULL;
20307 else
20308 {
20309 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20310 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020311#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020312 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020313 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020314#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 }
20317
20318 /* Did we encounter a breakpoint? */
20319 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20320 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020321 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020323 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 sourcing_lnum);
20325 fcp->dbg_tick = debug_tick;
20326 }
20327
20328 return retval;
20329}
20330
Bram Moolenaar05159a02005-02-26 23:04:13 +000020331#if defined(FEAT_PROFILE) || defined(PROTO)
20332/*
20333 * Called when starting to read a function line.
20334 * "sourcing_lnum" must be correct!
20335 * When skipping lines it may not actually be executed, but we won't find out
20336 * until later and we need to store the time now.
20337 */
20338 void
20339func_line_start(cookie)
20340 void *cookie;
20341{
20342 funccall_T *fcp = (funccall_T *)cookie;
20343 ufunc_T *fp = fcp->func;
20344
20345 if (fp->uf_profiling && sourcing_lnum >= 1
20346 && sourcing_lnum <= fp->uf_lines.ga_len)
20347 {
20348 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020349 /* Skip continuation lines. */
20350 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20351 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020352 fp->uf_tml_execed = FALSE;
20353 profile_start(&fp->uf_tml_start);
20354 profile_zero(&fp->uf_tml_children);
20355 profile_get_wait(&fp->uf_tml_wait);
20356 }
20357}
20358
20359/*
20360 * Called when actually executing a function line.
20361 */
20362 void
20363func_line_exec(cookie)
20364 void *cookie;
20365{
20366 funccall_T *fcp = (funccall_T *)cookie;
20367 ufunc_T *fp = fcp->func;
20368
20369 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20370 fp->uf_tml_execed = TRUE;
20371}
20372
20373/*
20374 * Called when done with a function line.
20375 */
20376 void
20377func_line_end(cookie)
20378 void *cookie;
20379{
20380 funccall_T *fcp = (funccall_T *)cookie;
20381 ufunc_T *fp = fcp->func;
20382
20383 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20384 {
20385 if (fp->uf_tml_execed)
20386 {
20387 ++fp->uf_tml_count[fp->uf_tml_idx];
20388 profile_end(&fp->uf_tml_start);
20389 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020390 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020391 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20392 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020393 }
20394 fp->uf_tml_idx = -1;
20395 }
20396}
20397#endif
20398
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399/*
20400 * Return TRUE if the currently active function should be ended, because a
20401 * return was encountered or an error occured. Used inside a ":while".
20402 */
20403 int
20404func_has_ended(cookie)
20405 void *cookie;
20406{
Bram Moolenaar33570922005-01-25 22:26:29 +000020407 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408
20409 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20410 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020411 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 || fcp->returned);
20413}
20414
20415/*
20416 * return TRUE if cookie indicates a function which "abort"s on errors.
20417 */
20418 int
20419func_has_abort(cookie)
20420 void *cookie;
20421{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020422 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423}
20424
20425#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20426typedef enum
20427{
20428 VAR_FLAVOUR_DEFAULT,
20429 VAR_FLAVOUR_SESSION,
20430 VAR_FLAVOUR_VIMINFO
20431} var_flavour_T;
20432
20433static var_flavour_T var_flavour __ARGS((char_u *varname));
20434
20435 static var_flavour_T
20436var_flavour(varname)
20437 char_u *varname;
20438{
20439 char_u *p = varname;
20440
20441 if (ASCII_ISUPPER(*p))
20442 {
20443 while (*(++p))
20444 if (ASCII_ISLOWER(*p))
20445 return VAR_FLAVOUR_SESSION;
20446 return VAR_FLAVOUR_VIMINFO;
20447 }
20448 else
20449 return VAR_FLAVOUR_DEFAULT;
20450}
20451#endif
20452
20453#if defined(FEAT_VIMINFO) || defined(PROTO)
20454/*
20455 * Restore global vars that start with a capital from the viminfo file
20456 */
20457 int
20458read_viminfo_varlist(virp, writing)
20459 vir_T *virp;
20460 int writing;
20461{
20462 char_u *tab;
20463 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020464 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465
20466 if (!writing && (find_viminfo_parameter('!') != NULL))
20467 {
20468 tab = vim_strchr(virp->vir_line + 1, '\t');
20469 if (tab != NULL)
20470 {
20471 *tab++ = '\0'; /* isolate the variable name */
20472 if (*tab == 'S') /* string var */
20473 is_string = TRUE;
20474
20475 tab = vim_strchr(tab, '\t');
20476 if (tab != NULL)
20477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 if (is_string)
20479 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020480 tv.v_type = VAR_STRING;
20481 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 }
20484 else
20485 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020486 tv.v_type = VAR_NUMBER;
20487 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020489 set_var(virp->vir_line + 1, &tv, FALSE);
20490 if (is_string)
20491 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 }
20493 }
20494 }
20495
20496 return viminfo_readline(virp);
20497}
20498
20499/*
20500 * Write global vars that start with a capital to the viminfo file
20501 */
20502 void
20503write_viminfo_varlist(fp)
20504 FILE *fp;
20505{
Bram Moolenaar33570922005-01-25 22:26:29 +000020506 hashitem_T *hi;
20507 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020508 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020509 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020510 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020511 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020512 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513
20514 if (find_viminfo_parameter('!') == NULL)
20515 return;
20516
20517 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020518
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020519 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020520 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020522 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020524 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020525 this_var = HI2DI(hi);
20526 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020527 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020528 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020529 {
20530 case VAR_STRING: s = "STR"; break;
20531 case VAR_NUMBER: s = "NUM"; break;
20532 default: continue;
20533 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020534 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020535 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020536 if (p != NULL)
20537 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020538 vim_free(tofree);
20539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 }
20541 }
20542}
20543#endif
20544
20545#if defined(FEAT_SESSION) || defined(PROTO)
20546 int
20547store_session_globals(fd)
20548 FILE *fd;
20549{
Bram Moolenaar33570922005-01-25 22:26:29 +000020550 hashitem_T *hi;
20551 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020552 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 char_u *p, *t;
20554
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020555 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020556 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020558 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020560 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020561 this_var = HI2DI(hi);
20562 if ((this_var->di_tv.v_type == VAR_NUMBER
20563 || this_var->di_tv.v_type == VAR_STRING)
20564 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020565 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020566 /* Escape special characters with a backslash. Turn a LF and
20567 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020568 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020569 (char_u *)"\\\"\n\r");
20570 if (p == NULL) /* out of memory */
20571 break;
20572 for (t = p; *t != NUL; ++t)
20573 if (*t == '\n')
20574 *t = 'n';
20575 else if (*t == '\r')
20576 *t = 'r';
20577 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 this_var->di_key,
20579 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20580 : ' ',
20581 p,
20582 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20583 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020584 || put_eol(fd) == FAIL)
20585 {
20586 vim_free(p);
20587 return FAIL;
20588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 vim_free(p);
20590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 }
20592 }
20593 return OK;
20594}
20595#endif
20596
Bram Moolenaar661b1822005-07-28 22:36:45 +000020597/*
20598 * Display script name where an item was last set.
20599 * Should only be invoked when 'verbose' is non-zero.
20600 */
20601 void
20602last_set_msg(scriptID)
20603 scid_T scriptID;
20604{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020605 char_u *p;
20606
Bram Moolenaar661b1822005-07-28 22:36:45 +000020607 if (scriptID != 0)
20608 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020609 p = home_replace_save(NULL, get_scriptname(scriptID));
20610 if (p != NULL)
20611 {
20612 verbose_enter();
20613 MSG_PUTS(_("\n\tLast set from "));
20614 MSG_PUTS(p);
20615 vim_free(p);
20616 verbose_leave();
20617 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020618 }
20619}
20620
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621#endif /* FEAT_EVAL */
20622
20623#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20624
20625
20626#ifdef WIN3264
20627/*
20628 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20629 */
20630static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20631static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20632static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20633
20634/*
20635 * Get the short pathname of a file.
20636 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20637 */
20638 static int
20639get_short_pathname(fnamep, bufp, fnamelen)
20640 char_u **fnamep;
20641 char_u **bufp;
20642 int *fnamelen;
20643{
20644 int l,len;
20645 char_u *newbuf;
20646
20647 len = *fnamelen;
20648
20649 l = GetShortPathName(*fnamep, *fnamep, len);
20650 if (l > len - 1)
20651 {
20652 /* If that doesn't work (not enough space), then save the string
20653 * and try again with a new buffer big enough
20654 */
20655 newbuf = vim_strnsave(*fnamep, l);
20656 if (newbuf == NULL)
20657 return 0;
20658
20659 vim_free(*bufp);
20660 *fnamep = *bufp = newbuf;
20661
20662 l = GetShortPathName(*fnamep,*fnamep,l+1);
20663
20664 /* Really should always succeed, as the buffer is big enough */
20665 }
20666
20667 *fnamelen = l;
20668 return 1;
20669}
20670
20671/*
20672 * Create a short path name. Returns the length of the buffer it needs.
20673 * Doesn't copy over the end of the buffer passed in.
20674 */
20675 static int
20676shortpath_for_invalid_fname(fname, bufp, fnamelen)
20677 char_u **fname;
20678 char_u **bufp;
20679 int *fnamelen;
20680{
20681 char_u *s, *p, *pbuf2, *pbuf3;
20682 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020683 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684
20685 /* Make a copy */
20686 len2 = *fnamelen;
20687 pbuf2 = vim_strnsave(*fname, len2);
20688 pbuf3 = NULL;
20689
20690 s = pbuf2 + len2 - 1; /* Find the end */
20691 slen = 1;
20692 plen = len2;
20693
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020694 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695 {
20696 --s;
20697 ++slen;
20698 --plen;
20699 }
20700
20701 do
20702 {
20703 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020704 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705 {
20706 --s;
20707 ++slen;
20708 --plen;
20709 }
20710 if (s <= pbuf2)
20711 break;
20712
20713 /* Remeber the character that is about to be blatted */
20714 ch = *s;
20715 *s = 0; /* get_short_pathname requires a null-terminated string */
20716
20717 /* Try it in situ */
20718 p = pbuf2;
20719 if (!get_short_pathname(&p, &pbuf3, &plen))
20720 {
20721 vim_free(pbuf2);
20722 return -1;
20723 }
20724 *s = ch; /* Preserve the string */
20725 } while (plen == 0);
20726
20727 if (plen > 0)
20728 {
20729 /* Remeber the length of the new string. */
20730 *fnamelen = len = plen + slen;
20731 vim_free(*bufp);
20732 if (len > len2)
20733 {
20734 /* If there's not enough space in the currently allocated string,
20735 * then copy it to a buffer big enough.
20736 */
20737 *fname= *bufp = vim_strnsave(p, len);
20738 if (*fname == NULL)
20739 return -1;
20740 }
20741 else
20742 {
20743 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20744 *fname = *bufp = pbuf2;
20745 if (p != pbuf2)
20746 strncpy(*fname, p, plen);
20747 pbuf2 = NULL;
20748 }
20749 /* Concat the next bit */
20750 strncpy(*fname + plen, s, slen);
20751 (*fname)[len] = '\0';
20752 }
20753 vim_free(pbuf3);
20754 vim_free(pbuf2);
20755 return 0;
20756}
20757
20758/*
20759 * Get a pathname for a partial path.
20760 */
20761 static int
20762shortpath_for_partial(fnamep, bufp, fnamelen)
20763 char_u **fnamep;
20764 char_u **bufp;
20765 int *fnamelen;
20766{
20767 int sepcount, len, tflen;
20768 char_u *p;
20769 char_u *pbuf, *tfname;
20770 int hasTilde;
20771
20772 /* Count up the path seperators from the RHS.. so we know which part
20773 * of the path to return.
20774 */
20775 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020776 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 if (vim_ispathsep(*p))
20778 ++sepcount;
20779
20780 /* Need full path first (use expand_env() to remove a "~/") */
20781 hasTilde = (**fnamep == '~');
20782 if (hasTilde)
20783 pbuf = tfname = expand_env_save(*fnamep);
20784 else
20785 pbuf = tfname = FullName_save(*fnamep, FALSE);
20786
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020787 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788
20789 if (!get_short_pathname(&tfname, &pbuf, &len))
20790 return -1;
20791
20792 if (len == 0)
20793 {
20794 /* Don't have a valid filename, so shorten the rest of the
20795 * path if we can. This CAN give us invalid 8.3 filenames, but
20796 * there's not a lot of point in guessing what it might be.
20797 */
20798 len = tflen;
20799 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20800 return -1;
20801 }
20802
20803 /* Count the paths backward to find the beginning of the desired string. */
20804 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020805 {
20806#ifdef FEAT_MBYTE
20807 if (has_mbyte)
20808 p -= mb_head_off(tfname, p);
20809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 if (vim_ispathsep(*p))
20811 {
20812 if (sepcount == 0 || (hasTilde && sepcount == 1))
20813 break;
20814 else
20815 sepcount --;
20816 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818 if (hasTilde)
20819 {
20820 --p;
20821 if (p >= tfname)
20822 *p = '~';
20823 else
20824 return -1;
20825 }
20826 else
20827 ++p;
20828
20829 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20830 vim_free(*bufp);
20831 *fnamelen = (int)STRLEN(p);
20832 *bufp = pbuf;
20833 *fnamep = p;
20834
20835 return 0;
20836}
20837#endif /* WIN3264 */
20838
20839/*
20840 * Adjust a filename, according to a string of modifiers.
20841 * *fnamep must be NUL terminated when called. When returning, the length is
20842 * determined by *fnamelen.
20843 * Returns valid flags.
20844 * When there is an error, *fnamep is set to NULL.
20845 */
20846 int
20847modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20848 char_u *src; /* string with modifiers */
20849 int *usedlen; /* characters after src that are used */
20850 char_u **fnamep; /* file name so far */
20851 char_u **bufp; /* buffer for allocated file name or NULL */
20852 int *fnamelen; /* length of fnamep */
20853{
20854 int valid = 0;
20855 char_u *tail;
20856 char_u *s, *p, *pbuf;
20857 char_u dirname[MAXPATHL];
20858 int c;
20859 int has_fullname = 0;
20860#ifdef WIN3264
20861 int has_shortname = 0;
20862#endif
20863
20864repeat:
20865 /* ":p" - full path/file_name */
20866 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20867 {
20868 has_fullname = 1;
20869
20870 valid |= VALID_PATH;
20871 *usedlen += 2;
20872
20873 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20874 if ((*fnamep)[0] == '~'
20875#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20876 && ((*fnamep)[1] == '/'
20877# ifdef BACKSLASH_IN_FILENAME
20878 || (*fnamep)[1] == '\\'
20879# endif
20880 || (*fnamep)[1] == NUL)
20881
20882#endif
20883 )
20884 {
20885 *fnamep = expand_env_save(*fnamep);
20886 vim_free(*bufp); /* free any allocated file name */
20887 *bufp = *fnamep;
20888 if (*fnamep == NULL)
20889 return -1;
20890 }
20891
20892 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020893 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 {
20895 if (vim_ispathsep(*p)
20896 && p[1] == '.'
20897 && (p[2] == NUL
20898 || vim_ispathsep(p[2])
20899 || (p[2] == '.'
20900 && (p[3] == NUL || vim_ispathsep(p[3])))))
20901 break;
20902 }
20903
20904 /* FullName_save() is slow, don't use it when not needed. */
20905 if (*p != NUL || !vim_isAbsName(*fnamep))
20906 {
20907 *fnamep = FullName_save(*fnamep, *p != NUL);
20908 vim_free(*bufp); /* free any allocated file name */
20909 *bufp = *fnamep;
20910 if (*fnamep == NULL)
20911 return -1;
20912 }
20913
20914 /* Append a path separator to a directory. */
20915 if (mch_isdir(*fnamep))
20916 {
20917 /* Make room for one or two extra characters. */
20918 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20919 vim_free(*bufp); /* free any allocated file name */
20920 *bufp = *fnamep;
20921 if (*fnamep == NULL)
20922 return -1;
20923 add_pathsep(*fnamep);
20924 }
20925 }
20926
20927 /* ":." - path relative to the current directory */
20928 /* ":~" - path relative to the home directory */
20929 /* ":8" - shortname path - postponed till after */
20930 while (src[*usedlen] == ':'
20931 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20932 {
20933 *usedlen += 2;
20934 if (c == '8')
20935 {
20936#ifdef WIN3264
20937 has_shortname = 1; /* Postpone this. */
20938#endif
20939 continue;
20940 }
20941 pbuf = NULL;
20942 /* Need full path first (use expand_env() to remove a "~/") */
20943 if (!has_fullname)
20944 {
20945 if (c == '.' && **fnamep == '~')
20946 p = pbuf = expand_env_save(*fnamep);
20947 else
20948 p = pbuf = FullName_save(*fnamep, FALSE);
20949 }
20950 else
20951 p = *fnamep;
20952
20953 has_fullname = 0;
20954
20955 if (p != NULL)
20956 {
20957 if (c == '.')
20958 {
20959 mch_dirname(dirname, MAXPATHL);
20960 s = shorten_fname(p, dirname);
20961 if (s != NULL)
20962 {
20963 *fnamep = s;
20964 if (pbuf != NULL)
20965 {
20966 vim_free(*bufp); /* free any allocated file name */
20967 *bufp = pbuf;
20968 pbuf = NULL;
20969 }
20970 }
20971 }
20972 else
20973 {
20974 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20975 /* Only replace it when it starts with '~' */
20976 if (*dirname == '~')
20977 {
20978 s = vim_strsave(dirname);
20979 if (s != NULL)
20980 {
20981 *fnamep = s;
20982 vim_free(*bufp);
20983 *bufp = s;
20984 }
20985 }
20986 }
20987 vim_free(pbuf);
20988 }
20989 }
20990
20991 tail = gettail(*fnamep);
20992 *fnamelen = (int)STRLEN(*fnamep);
20993
20994 /* ":h" - head, remove "/file_name", can be repeated */
20995 /* Don't remove the first "/" or "c:\" */
20996 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20997 {
20998 valid |= VALID_HEAD;
20999 *usedlen += 2;
21000 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021001 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 --tail;
21003 *fnamelen = (int)(tail - *fnamep);
21004#ifdef VMS
21005 if (*fnamelen > 0)
21006 *fnamelen += 1; /* the path separator is part of the path */
21007#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021008 while (tail > s && !after_pathsep(s, tail))
21009 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 }
21011
21012 /* ":8" - shortname */
21013 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21014 {
21015 *usedlen += 2;
21016#ifdef WIN3264
21017 has_shortname = 1;
21018#endif
21019 }
21020
21021#ifdef WIN3264
21022 /* Check shortname after we have done 'heads' and before we do 'tails'
21023 */
21024 if (has_shortname)
21025 {
21026 pbuf = NULL;
21027 /* Copy the string if it is shortened by :h */
21028 if (*fnamelen < (int)STRLEN(*fnamep))
21029 {
21030 p = vim_strnsave(*fnamep, *fnamelen);
21031 if (p == 0)
21032 return -1;
21033 vim_free(*bufp);
21034 *bufp = *fnamep = p;
21035 }
21036
21037 /* Split into two implementations - makes it easier. First is where
21038 * there isn't a full name already, second is where there is.
21039 */
21040 if (!has_fullname && !vim_isAbsName(*fnamep))
21041 {
21042 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21043 return -1;
21044 }
21045 else
21046 {
21047 int l;
21048
21049 /* Simple case, already have the full-name
21050 * Nearly always shorter, so try first time. */
21051 l = *fnamelen;
21052 if (!get_short_pathname(fnamep, bufp, &l))
21053 return -1;
21054
21055 if (l == 0)
21056 {
21057 /* Couldn't find the filename.. search the paths.
21058 */
21059 l = *fnamelen;
21060 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21061 return -1;
21062 }
21063 *fnamelen = l;
21064 }
21065 }
21066#endif /* WIN3264 */
21067
21068 /* ":t" - tail, just the basename */
21069 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21070 {
21071 *usedlen += 2;
21072 *fnamelen -= (int)(tail - *fnamep);
21073 *fnamep = tail;
21074 }
21075
21076 /* ":e" - extension, can be repeated */
21077 /* ":r" - root, without extension, can be repeated */
21078 while (src[*usedlen] == ':'
21079 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21080 {
21081 /* find a '.' in the tail:
21082 * - for second :e: before the current fname
21083 * - otherwise: The last '.'
21084 */
21085 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21086 s = *fnamep - 2;
21087 else
21088 s = *fnamep + *fnamelen - 1;
21089 for ( ; s > tail; --s)
21090 if (s[0] == '.')
21091 break;
21092 if (src[*usedlen + 1] == 'e') /* :e */
21093 {
21094 if (s > tail)
21095 {
21096 *fnamelen += (int)(*fnamep - (s + 1));
21097 *fnamep = s + 1;
21098#ifdef VMS
21099 /* cut version from the extension */
21100 s = *fnamep + *fnamelen - 1;
21101 for ( ; s > *fnamep; --s)
21102 if (s[0] == ';')
21103 break;
21104 if (s > *fnamep)
21105 *fnamelen = s - *fnamep;
21106#endif
21107 }
21108 else if (*fnamep <= tail)
21109 *fnamelen = 0;
21110 }
21111 else /* :r */
21112 {
21113 if (s > tail) /* remove one extension */
21114 *fnamelen = (int)(s - *fnamep);
21115 }
21116 *usedlen += 2;
21117 }
21118
21119 /* ":s?pat?foo?" - substitute */
21120 /* ":gs?pat?foo?" - global substitute */
21121 if (src[*usedlen] == ':'
21122 && (src[*usedlen + 1] == 's'
21123 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21124 {
21125 char_u *str;
21126 char_u *pat;
21127 char_u *sub;
21128 int sep;
21129 char_u *flags;
21130 int didit = FALSE;
21131
21132 flags = (char_u *)"";
21133 s = src + *usedlen + 2;
21134 if (src[*usedlen + 1] == 'g')
21135 {
21136 flags = (char_u *)"g";
21137 ++s;
21138 }
21139
21140 sep = *s++;
21141 if (sep)
21142 {
21143 /* find end of pattern */
21144 p = vim_strchr(s, sep);
21145 if (p != NULL)
21146 {
21147 pat = vim_strnsave(s, (int)(p - s));
21148 if (pat != NULL)
21149 {
21150 s = p + 1;
21151 /* find end of substitution */
21152 p = vim_strchr(s, sep);
21153 if (p != NULL)
21154 {
21155 sub = vim_strnsave(s, (int)(p - s));
21156 str = vim_strnsave(*fnamep, *fnamelen);
21157 if (sub != NULL && str != NULL)
21158 {
21159 *usedlen = (int)(p + 1 - src);
21160 s = do_string_sub(str, pat, sub, flags);
21161 if (s != NULL)
21162 {
21163 *fnamep = s;
21164 *fnamelen = (int)STRLEN(s);
21165 vim_free(*bufp);
21166 *bufp = s;
21167 didit = TRUE;
21168 }
21169 }
21170 vim_free(sub);
21171 vim_free(str);
21172 }
21173 vim_free(pat);
21174 }
21175 }
21176 /* after using ":s", repeat all the modifiers */
21177 if (didit)
21178 goto repeat;
21179 }
21180 }
21181
21182 return valid;
21183}
21184
21185/*
21186 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21187 * "flags" can be "g" to do a global substitute.
21188 * Returns an allocated string, NULL for error.
21189 */
21190 char_u *
21191do_string_sub(str, pat, sub, flags)
21192 char_u *str;
21193 char_u *pat;
21194 char_u *sub;
21195 char_u *flags;
21196{
21197 int sublen;
21198 regmatch_T regmatch;
21199 int i;
21200 int do_all;
21201 char_u *tail;
21202 garray_T ga;
21203 char_u *ret;
21204 char_u *save_cpo;
21205
21206 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21207 save_cpo = p_cpo;
21208 p_cpo = (char_u *)"";
21209
21210 ga_init2(&ga, 1, 200);
21211
21212 do_all = (flags[0] == 'g');
21213
21214 regmatch.rm_ic = p_ic;
21215 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21216 if (regmatch.regprog != NULL)
21217 {
21218 tail = str;
21219 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21220 {
21221 /*
21222 * Get some space for a temporary buffer to do the substitution
21223 * into. It will contain:
21224 * - The text up to where the match is.
21225 * - The substituted text.
21226 * - The text after the match.
21227 */
21228 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21229 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21230 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21231 {
21232 ga_clear(&ga);
21233 break;
21234 }
21235
21236 /* copy the text up to where the match is */
21237 i = (int)(regmatch.startp[0] - tail);
21238 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21239 /* add the substituted text */
21240 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21241 + ga.ga_len + i, TRUE, TRUE, FALSE);
21242 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 /* avoid getting stuck on a match with an empty string */
21244 if (tail == regmatch.endp[0])
21245 {
21246 if (*tail == NUL)
21247 break;
21248 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21249 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 }
21251 else
21252 {
21253 tail = regmatch.endp[0];
21254 if (*tail == NUL)
21255 break;
21256 }
21257 if (!do_all)
21258 break;
21259 }
21260
21261 if (ga.ga_data != NULL)
21262 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21263
21264 vim_free(regmatch.regprog);
21265 }
21266
21267 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21268 ga_clear(&ga);
21269 p_cpo = save_cpo;
21270
21271 return ret;
21272}
21273
21274#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */