blob: c777136568ae340fb87050405ff79a90bf3a5ba0 [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 */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000195 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000197static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000199/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000200static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
201
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000202/* list heads for garbage collection */
203static dict_T *first_dict = NULL; /* list of all dicts */
204static list_T *first_list = NULL; /* list of all lists */
205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000206/* From user function to hashitem and back. */
207static ufunc_T dumuf;
208#define UF2HIKEY(fp) ((fp)->uf_name)
209#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
210#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
211
212#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
213#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar33570922005-01-25 22:26:29 +0000215#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
216#define VAR_SHORT_LEN 20 /* short variable name length */
217#define FIXVAR_CNT 12 /* number of fixed variables */
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000220typedef struct funccall_S funccall_T;
221
222struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 ufunc_T *func; /* function being called */
225 int linenr; /* next line to be executed */
226 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000227 struct /* fixed variables for arguments */
228 {
229 dictitem_T var; /* variable (without room for name) */
230 char_u room[VAR_SHORT_LEN]; /* room for the name */
231 } fixvar[FIXVAR_CNT];
232 dict_T l_vars; /* l: local function variables */
233 dictitem_T l_vars_var; /* variable for l: scope */
234 dict_T l_avars; /* a: argument variables */
235 dictitem_T l_avars_var; /* variable for a: scope */
236 list_T l_varlist; /* list for a:000 */
237 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
238 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 linenr_T breakpoint; /* next line with breakpoint or zero */
240 int dbg_tick; /* debug_tick when breakpoint was set */
241 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000242#ifdef FEAT_PROFILE
243 proftime_T prof_child; /* time spent in a child */
244#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000245 funccall_T *caller; /* calling function or NULL */
246};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000249 * Info used by a ":for" loop.
250 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000251typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000252{
253 int fi_semicolon; /* TRUE if ending in '; var]' */
254 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 listwatch_T fi_lw; /* keep an eye on the item used. */
256 list_T *fi_list; /* list being used */
257} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000260 * Struct used by trans_function_name()
261 */
262typedef struct
263{
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000265 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 dictitem_T *fd_di; /* Dictionary item used */
267} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268
Bram Moolenaara7043832005-01-21 11:56:39 +0000269
270/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 * Array to hold the value of v: variables.
272 * The value is in a dictitem, so that it can also be used in the v: scope.
273 * The reason to use this table anyway is for very quick access to the
274 * variables with the VV_ defines.
275 */
276#include "version.h"
277
278/* values for vv_flags: */
279#define VV_COMPAT 1 /* compatible, also used without "v:" */
280#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000281#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000283#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000284
285static struct vimvar
286{
287 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288 dictitem_T vv_di; /* value and name for key */
289 char vv_filler[16]; /* space for LONGEST name below!!! */
290 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
291} vimvars[VV_LEN] =
292{
293 /*
294 * The order here must match the VV_ defines in vim.h!
295 * Initializing a union does not work, leave tv.vval empty to get zero's.
296 */
297 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
298 {VV_NAME("count1", VAR_NUMBER), VV_RO},
299 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
300 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
301 {VV_NAME("warningmsg", VAR_STRING), 0},
302 {VV_NAME("statusmsg", VAR_STRING), 0},
303 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
305 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
307 {VV_NAME("termresponse", VAR_STRING), VV_RO},
308 {VV_NAME("fname", VAR_STRING), VV_RO},
309 {VV_NAME("lang", VAR_STRING), VV_RO},
310 {VV_NAME("lc_time", VAR_STRING), VV_RO},
311 {VV_NAME("ctype", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
313 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
314 {VV_NAME("fname_in", VAR_STRING), VV_RO},
315 {VV_NAME("fname_out", VAR_STRING), VV_RO},
316 {VV_NAME("fname_new", VAR_STRING), VV_RO},
317 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
318 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
319 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
322 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("progname", VAR_STRING), VV_RO},
324 {VV_NAME("servername", VAR_STRING), VV_RO},
325 {VV_NAME("dying", VAR_NUMBER), VV_RO},
326 {VV_NAME("exception", VAR_STRING), VV_RO},
327 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
328 {VV_NAME("register", VAR_STRING), VV_RO},
329 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
330 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000331 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
332 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000333 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000334 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
335 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000336 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000341 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000342 {VV_NAME("swapname", VAR_STRING), VV_RO},
343 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000344 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000346};
347
348/* shorthand */
349#define vv_type vv_di.di_tv.v_type
350#define vv_nr vv_di.di_tv.vval.v_number
351#define vv_str vv_di.di_tv.vval.v_string
352#define vv_tv vv_di.di_tv
353
354/*
355 * The v: variables are stored in dictionary "vimvardict".
356 * "vimvars_var" is the variable that is used for the "l:" scope.
357 */
358static dict_T vimvardict;
359static dictitem_T vimvars_var;
360#define vimvarht vimvardict.dv_hashtab
361
Bram Moolenaara40058a2005-07-11 22:42:07 +0000362static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
363static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
364#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
365static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
366#endif
367static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
368static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
369static char_u *skip_var_one __ARGS((char_u *arg));
370static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
371static void list_glob_vars __ARGS((void));
372static void list_buf_vars __ARGS((void));
373static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000374#ifdef FEAT_WINDOWS
375static void list_tab_vars __ARGS((void));
376#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000377static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000378static void list_script_vars __ARGS((void));
379static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
381static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
382static int check_changedtick __ARGS((char_u *arg));
383static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
384static void clear_lval __ARGS((lval_T *lp));
385static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
386static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
387static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
388static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
389static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
390static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
391static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
392static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
393static void item_lock __ARGS((typval_T *tv, int deep, int lock));
394static int tv_islocked __ARGS((typval_T *tv));
395
Bram Moolenaar33570922005-01-25 22:26:29 +0000396static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
397static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000404
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000405static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000410static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static listitem_T *listitem_alloc __ARGS((void));
412static void listitem_free __ARGS((listitem_T *item));
413static void listitem_remove __ARGS((list_T *l, listitem_T *item));
414static long list_len __ARGS((list_T *l));
415static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
416static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
417static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000419static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static void list_append __ARGS((list_T *l, listitem_T *item));
422static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000423static int list_append_string __ARGS((list_T *l, char_u *str, int len));
424static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
426static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
427static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000430static char_u *list2string __ARGS((typval_T *tv, int copyID));
431static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000432static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
433static void set_ref_in_list __ARGS((list_T *l, int copyID));
434static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000436static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_alloc __ARGS((char_u *key));
438static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
439static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
440static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int dict_add __ARGS((dict_T *d, dictitem_T *item));
443static long dict_len __ARGS((dict_T *d));
444static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
448static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static char_u *string_quote __ARGS((char_u *str, int function));
450static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
451static int find_internal_func __ARGS((char_u *name));
452static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
453static 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));
454static 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 +0000455static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
457static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000473static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000477#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000478static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
487static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000500static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000514static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000516static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000522static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000530static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000531static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000534static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000555static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000556static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000561static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000577static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000579static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000583#ifdef vim_mkdir
584static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
585#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000589static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000591static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000592static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000594static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000595static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000608static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000610static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000617static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000618static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000619static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000621static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000623static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000626static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000627static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000630static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631#ifdef HAVE_STRFTIME
632static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
634static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000646static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000647static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000648static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000649static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000650static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000652static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000666static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000669static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000671static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
672static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static int get_env_len __ARGS((char_u **arg));
674static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000675static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000676static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
677#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
678#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
679 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000680static 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 +0000681static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000682static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000683static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
684static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static typval_T *alloc_tv __ARGS((void));
686static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void init_tv __ARGS((typval_T *varp));
688static long get_tv_number __ARGS((typval_T *varp));
689static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000690static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static char_u *get_tv_string __ARGS((typval_T *varp));
692static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000693static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000695static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
697static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
698static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
699static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
700static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
701static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
702static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000703static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000704static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000706static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
708static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
709static int eval_fname_script __ARGS((char_u *p));
710static int eval_fname_sid __ARGS((char_u *p));
711static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static ufunc_T *find_func __ARGS((char_u *name));
713static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000714static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000715#ifdef FEAT_PROFILE
716static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000717static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
718static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
719static int
720# ifdef __BORLANDC__
721 _RTLENTRYF
722# endif
723 prof_total_cmp __ARGS((const void *s1, const void *s2));
724static int
725# ifdef __BORLANDC__
726 _RTLENTRYF
727# endif
728 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000729#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000731static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000732static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void func_free __ARGS((ufunc_T *fp));
734static void func_unref __ARGS((char_u *name));
735static void func_ref __ARGS((char_u *name));
736static 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));
737static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000738static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
739static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000740static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000741static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000742static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000744/* Character used as separated in autoload function/variable names. */
745#define AUTOLOAD_CHAR '#'
746
Bram Moolenaar33570922005-01-25 22:26:29 +0000747/*
748 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000749 */
750 void
751eval_init()
752{
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 int i;
754 struct vimvar *p;
755
756 init_var_dict(&globvardict, &globvars_var);
757 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000758 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000759 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000760
761 for (i = 0; i < VV_LEN; ++i)
762 {
763 p = &vimvars[i];
764 STRCPY(p->vv_di.di_key, p->vv_name);
765 if (p->vv_flags & VV_RO)
766 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
767 else if (p->vv_flags & VV_RO_SBX)
768 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
769 else
770 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771
772 /* add to v: scope dict, unless the value is not always available */
773 if (p->vv_type != VAR_UNKNOWN)
774 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000775 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000776 /* add to compat scope dict */
777 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000779}
780
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000781#if defined(EXITFREE) || defined(PROTO)
782 void
783eval_clear()
784{
785 int i;
786 struct vimvar *p;
787
788 for (i = 0; i < VV_LEN; ++i)
789 {
790 p = &vimvars[i];
791 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000792 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000793 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794 p->vv_di.di_tv.vval.v_string = NULL;
795 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000796 }
797 hash_clear(&vimvarht);
798 hash_clear(&compat_hashtab);
799
800 /* script-local variables */
801 for (i = 1; i <= ga_scripts.ga_len; ++i)
802 vars_clear(&SCRIPT_VARS(i));
803 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000804 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000805
806 /* global variables */
807 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000808
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000809 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000810 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000811 hash_clear(&func_hashtab);
812
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000813 /* autoloaded script names */
814 ga_clear_strings(&ga_loaded);
815
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000816 /* unreferenced lists and dicts */
817 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000818}
819#endif
820
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Return the name of the executed function.
823 */
824 char_u *
825func_name(cookie)
826 void *cookie;
827{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
831/*
832 * Return the address holding the next breakpoint line for a funccall cookie.
833 */
834 linenr_T *
835func_breakpoint(cookie)
836 void *cookie;
837{
Bram Moolenaar33570922005-01-25 22:26:29 +0000838 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/*
842 * Return the address holding the debug tick for a funccall cookie.
843 */
844 int *
845func_dbg_tick(cookie)
846 void *cookie;
847{
Bram Moolenaar33570922005-01-25 22:26:29 +0000848 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
851/*
852 * Return the nesting level for a funccall cookie.
853 */
854 int
855func_level(cookie)
856 void *cookie;
857{
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
861/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000862funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864/*
865 * Return TRUE when a function was ended by a ":return" command.
866 */
867 int
868current_func_returned()
869{
870 return current_funccal->returned;
871}
872
873
874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Set an internal variable to a string value. Creates the variable if it does
876 * not already exist.
877 */
878 void
879set_internal_string_var(name, value)
880 char_u *name;
881 char_u *value;
882{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000883 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886 val = vim_strsave(value);
887 if (val != NULL)
888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000889 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000890 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000892 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000893 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895 }
896}
897
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898static lval_T *redir_lval = NULL;
899static char_u *redir_endp = NULL;
900static char_u *redir_varname = NULL;
901
902/*
903 * Start recording command output to a variable
904 * Returns OK if successfully completed the setup. FAIL otherwise.
905 */
906 int
907var_redir_start(name, append)
908 char_u *name;
909 int append; /* append to an existing variable */
910{
911 int save_emsg;
912 int err;
913 typval_T tv;
914
915 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000916 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 {
918 EMSG(_(e_invarg));
919 return FAIL;
920 }
921
922 redir_varname = vim_strsave(name);
923 if (redir_varname == NULL)
924 return FAIL;
925
926 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
927 if (redir_lval == NULL)
928 {
929 var_redir_stop();
930 return FAIL;
931 }
932
933 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000934 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
935 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000936 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
937 {
938 if (redir_endp != NULL && *redir_endp != NUL)
939 /* Trailing characters are present after the variable name */
940 EMSG(_(e_trailing));
941 else
942 EMSG(_(e_invarg));
943 var_redir_stop();
944 return FAIL;
945 }
946
947 /* check if we can write to the variable: set it to or append an empty
948 * string */
949 save_emsg = did_emsg;
950 did_emsg = FALSE;
951 tv.v_type = VAR_STRING;
952 tv.vval.v_string = (char_u *)"";
953 if (append)
954 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
955 else
956 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
957 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000958 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000959 if (err)
960 {
961 var_redir_stop();
962 return FAIL;
963 }
964 if (redir_lval->ll_newkey != NULL)
965 {
966 /* Dictionary item was created, don't do it again. */
967 vim_free(redir_lval->ll_newkey);
968 redir_lval->ll_newkey = NULL;
969 }
970
971 return OK;
972}
973
974/*
975 * Append "value[len]" to the variable set by var_redir_start().
976 */
977 void
978var_redir_str(value, len)
979 char_u *value;
980 int len;
981{
982 char_u *val;
983 typval_T tv;
984 int save_emsg;
985 int err;
986
987 if (redir_lval == NULL)
988 return;
989
990 if (len == -1)
991 /* Append the entire string */
992 val = vim_strsave(value);
993 else
994 /* Append only the specified number of characters */
995 val = vim_strnsave(value, len);
996 if (val == NULL)
997 return;
998
999 tv.v_type = VAR_STRING;
1000 tv.vval.v_string = val;
1001
1002 save_emsg = did_emsg;
1003 did_emsg = FALSE;
1004 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1005 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001006 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 if (err)
1008 var_redir_stop();
1009
1010 vim_free(tv.vval.v_string);
1011}
1012
1013/*
1014 * Stop redirecting command output to a variable.
1015 */
1016 void
1017var_redir_stop()
1018{
1019 if (redir_lval != NULL)
1020 {
1021 clear_lval(redir_lval);
1022 vim_free(redir_lval);
1023 redir_lval = NULL;
1024 }
1025 vim_free(redir_varname);
1026 redir_varname = NULL;
1027}
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029# if defined(FEAT_MBYTE) || defined(PROTO)
1030 int
1031eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1032 char_u *enc_from;
1033 char_u *enc_to;
1034 char_u *fname_from;
1035 char_u *fname_to;
1036{
1037 int err = FALSE;
1038
1039 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1040 set_vim_var_string(VV_CC_TO, enc_to, -1);
1041 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1042 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1043 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1044 err = TRUE;
1045 set_vim_var_string(VV_CC_FROM, NULL, -1);
1046 set_vim_var_string(VV_CC_TO, NULL, -1);
1047 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1048 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1049
1050 if (err)
1051 return FAIL;
1052 return OK;
1053}
1054# endif
1055
1056# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1057 int
1058eval_printexpr(fname, args)
1059 char_u *fname;
1060 char_u *args;
1061{
1062 int err = FALSE;
1063
1064 set_vim_var_string(VV_FNAME_IN, fname, -1);
1065 set_vim_var_string(VV_CMDARG, args, -1);
1066 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1067 err = TRUE;
1068 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1069 set_vim_var_string(VV_CMDARG, NULL, -1);
1070
1071 if (err)
1072 {
1073 mch_remove(fname);
1074 return FAIL;
1075 }
1076 return OK;
1077}
1078# endif
1079
1080# if defined(FEAT_DIFF) || defined(PROTO)
1081 void
1082eval_diff(origfile, newfile, outfile)
1083 char_u *origfile;
1084 char_u *newfile;
1085 char_u *outfile;
1086{
1087 int err = FALSE;
1088
1089 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1090 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1091 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1092 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1093 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1094 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1095 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1096}
1097
1098 void
1099eval_patch(origfile, difffile, outfile)
1100 char_u *origfile;
1101 char_u *difffile;
1102 char_u *outfile;
1103{
1104 int err;
1105
1106 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1107 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1108 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1109 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113}
1114# endif
1115
1116/*
1117 * Top level evaluation function, returning a boolean.
1118 * Sets "error" to TRUE if there was an error.
1119 * Return TRUE or FALSE.
1120 */
1121 int
1122eval_to_bool(arg, error, nextcmd, skip)
1123 char_u *arg;
1124 int *error;
1125 char_u **nextcmd;
1126 int skip; /* only parse, don't execute */
1127{
Bram Moolenaar33570922005-01-25 22:26:29 +00001128 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int retval = FALSE;
1130
1131 if (skip)
1132 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 else
1136 {
1137 *error = FALSE;
1138 if (!skip)
1139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001140 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 }
1144 if (skip)
1145 --emsg_skip;
1146
1147 return retval;
1148}
1149
1150/*
1151 * Top level evaluation function, returning a string. If "skip" is TRUE,
1152 * only parsing to "nextcmd" is done, without reporting errors. Return
1153 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1154 */
1155 char_u *
1156eval_to_string_skip(arg, nextcmd, skip)
1157 char_u *arg;
1158 char_u **nextcmd;
1159 int skip; /* only parse, don't execute */
1160{
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u *retval;
1163
1164 if (skip)
1165 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = NULL;
1168 else
1169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 retval = vim_strsave(get_tv_string(&tv));
1171 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173 if (skip)
1174 --emsg_skip;
1175
1176 return retval;
1177}
1178
1179/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001180 * Skip over an expression at "*pp".
1181 * Return FAIL for an error, OK otherwise.
1182 */
1183 int
1184skip_expr(pp)
1185 char_u **pp;
1186{
Bram Moolenaar33570922005-01-25 22:26:29 +00001187 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001188
1189 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001191}
1192
1193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 * Top level evaluation function, returning a string.
1195 * Return pointer to allocated memory, or NULL for failure.
1196 */
1197 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001198eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 char_u *arg;
1200 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001201 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
Bram Moolenaar33570922005-01-25 22:26:29 +00001203 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001205 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 retval = NULL;
1209 else
1210 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001211 if (dolist && tv.v_type == VAR_LIST)
1212 {
1213 ga_init2(&ga, (int)sizeof(char), 80);
1214 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1215 ga_append(&ga, NUL);
1216 retval = (char_u *)ga.ga_data;
1217 }
1218 else
1219 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222
1223 return retval;
1224}
1225
1226/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001227 * Call eval_to_string() without using current local variables and using
1228 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 */
1230 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001231eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 char_u *arg;
1233 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001234 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 char_u *retval;
1237 void *save_funccalp;
1238
1239 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001240 if (use_sandbox)
1241 ++sandbox;
1242 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001243 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 if (use_sandbox)
1245 --sandbox;
1246 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 restore_funccal(save_funccalp);
1248 return retval;
1249}
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251/*
1252 * Top level evaluation function, returning a number.
1253 * Evaluates "expr" silently.
1254 * Returns -1 for an error.
1255 */
1256 int
1257eval_to_number(expr)
1258 char_u *expr;
1259{
Bram Moolenaar33570922005-01-25 22:26:29 +00001260 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001262 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 ++emsg_off;
1265
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 retval = -1;
1268 else
1269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001270 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 --emsg_off;
1274
1275 return retval;
1276}
1277
Bram Moolenaara40058a2005-07-11 22:42:07 +00001278/*
1279 * Prepare v: variable "idx" to be used.
1280 * Save the current typeval in "save_tv".
1281 * When not used yet add the variable to the v: hashtable.
1282 */
1283 static void
1284prepare_vimvar(idx, save_tv)
1285 int idx;
1286 typval_T *save_tv;
1287{
1288 *save_tv = vimvars[idx].vv_tv;
1289 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1290 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1291}
1292
1293/*
1294 * Restore v: variable "idx" to typeval "save_tv".
1295 * When no longer defined, remove the variable from the v: hashtable.
1296 */
1297 static void
1298restore_vimvar(idx, save_tv)
1299 int idx;
1300 typval_T *save_tv;
1301{
1302 hashitem_T *hi;
1303
1304 clear_tv(&vimvars[idx].vv_tv);
1305 vimvars[idx].vv_tv = *save_tv;
1306 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1307 {
1308 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1309 if (HASHITEM_EMPTY(hi))
1310 EMSG2(_(e_intern2), "restore_vimvar()");
1311 else
1312 hash_remove(&vimvarht, hi);
1313 }
1314}
1315
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001316#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001317/*
1318 * Evaluate an expression to a list with suggestions.
1319 * For the "expr:" part of 'spellsuggest'.
1320 */
1321 list_T *
1322eval_spell_expr(badword, expr)
1323 char_u *badword;
1324 char_u *expr;
1325{
1326 typval_T save_val;
1327 typval_T rettv;
1328 list_T *list = NULL;
1329 char_u *p = skipwhite(expr);
1330
1331 /* Set "v:val" to the bad word. */
1332 prepare_vimvar(VV_VAL, &save_val);
1333 vimvars[VV_VAL].vv_type = VAR_STRING;
1334 vimvars[VV_VAL].vv_str = badword;
1335 if (p_verbose == 0)
1336 ++emsg_off;
1337
1338 if (eval1(&p, &rettv, TRUE) == OK)
1339 {
1340 if (rettv.v_type != VAR_LIST)
1341 clear_tv(&rettv);
1342 else
1343 list = rettv.vval.v_list;
1344 }
1345
1346 if (p_verbose == 0)
1347 --emsg_off;
1348 vimvars[VV_VAL].vv_str = NULL;
1349 restore_vimvar(VV_VAL, &save_val);
1350
1351 return list;
1352}
1353
1354/*
1355 * "list" is supposed to contain two items: a word and a number. Return the
1356 * word in "pp" and the number as the return value.
1357 * Return -1 if anything isn't right.
1358 * Used to get the good word and score from the eval_spell_expr() result.
1359 */
1360 int
1361get_spellword(list, pp)
1362 list_T *list;
1363 char_u **pp;
1364{
1365 listitem_T *li;
1366
1367 li = list->lv_first;
1368 if (li == NULL)
1369 return -1;
1370 *pp = get_tv_string(&li->li_tv);
1371
1372 li = li->li_next;
1373 if (li == NULL)
1374 return -1;
1375 return get_tv_number(&li->li_tv);
1376}
1377#endif
1378
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001379/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001380 * Top level evaluation function.
1381 * Returns an allocated typval_T with the result.
1382 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001383 */
1384 typval_T *
1385eval_expr(arg, nextcmd)
1386 char_u *arg;
1387 char_u **nextcmd;
1388{
1389 typval_T *tv;
1390
1391 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001392 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001393 {
1394 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001395 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396 }
1397
1398 return tv;
1399}
1400
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1403/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001404 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 static int
1409call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 char_u *func;
1411 int argc;
1412 char_u **argv;
1413 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
Bram Moolenaar33570922005-01-25 22:26:29 +00001416 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 long n;
1418 int len;
1419 int i;
1420 int doesrange;
1421 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001424 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 for (i = 0; i < argc; i++)
1429 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001430 /* Pass a NULL or empty argument as an empty string */
1431 if (argv[i] == NULL || *argv[i] == NUL)
1432 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001433 argvars[i].v_type = VAR_STRING;
1434 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001435 continue;
1436 }
1437
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 /* Recognize a number argument, the others must be strings. */
1439 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1440 if (len != 0 && len == (int)STRLEN(argv[i]))
1441 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001442 argvars[i].v_type = VAR_NUMBER;
1443 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445 else
1446 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001447 argvars[i].v_type = VAR_STRING;
1448 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450 }
1451
1452 if (safe)
1453 {
1454 save_funccalp = save_funccal();
1455 ++sandbox;
1456 }
1457
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001458 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1459 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (safe)
1463 {
1464 --sandbox;
1465 restore_funccal(save_funccalp);
1466 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001467 vim_free(argvars);
1468
1469 if (ret == FAIL)
1470 clear_tv(rettv);
1471
1472 return ret;
1473}
1474
1475/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001476 * Call vimL function "func" and return the result as a string.
1477 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 * Uses argv[argc] for the function arguments.
1479 */
1480 void *
1481call_func_retstr(func, argc, argv, safe)
1482 char_u *func;
1483 int argc;
1484 char_u **argv;
1485 int safe; /* use the sandbox */
1486{
1487 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001488 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001489
1490 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1491 return NULL;
1492
1493 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 return retval;
1496}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001498#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001499/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001500 * Call vimL function "func" and return the result as a number.
1501 * Returns -1 when calling the function fails.
1502 * Uses argv[argc] for the function arguments.
1503 */
1504 long
1505call_func_retnr(func, argc, argv, safe)
1506 char_u *func;
1507 int argc;
1508 char_u **argv;
1509 int safe; /* use the sandbox */
1510{
1511 typval_T rettv;
1512 long retval;
1513
1514 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1515 return -1;
1516
1517 retval = get_tv_number_chk(&rettv, NULL);
1518 clear_tv(&rettv);
1519 return retval;
1520}
1521#endif
1522
1523/*
1524 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001525 * Uses argv[argc] for the function arguments.
1526 */
1527 void *
1528call_func_retlist(func, argc, argv, safe)
1529 char_u *func;
1530 int argc;
1531 char_u **argv;
1532 int safe; /* use the sandbox */
1533{
1534 typval_T rettv;
1535
1536 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1537 return NULL;
1538
1539 if (rettv.v_type != VAR_LIST)
1540 {
1541 clear_tv(&rettv);
1542 return NULL;
1543 }
1544
1545 return rettv.vval.v_list;
1546}
1547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548#endif
1549
1550/*
1551 * Save the current function call pointer, and set it to NULL.
1552 * Used when executing autocommands and for ":source".
1553 */
1554 void *
1555save_funccal()
1556{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001557 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 current_funccal = NULL;
1560 return (void *)fc;
1561}
1562
1563 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001564restore_funccal(vfc)
1565 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001567 funccall_T *fc = (funccall_T *)vfc;
1568
1569 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570}
1571
Bram Moolenaar05159a02005-02-26 23:04:13 +00001572#if defined(FEAT_PROFILE) || defined(PROTO)
1573/*
1574 * Prepare profiling for entering a child or something else that is not
1575 * counted for the script/function itself.
1576 * Should always be called in pair with prof_child_exit().
1577 */
1578 void
1579prof_child_enter(tm)
1580 proftime_T *tm; /* place to store waittime */
1581{
1582 funccall_T *fc = current_funccal;
1583
1584 if (fc != NULL && fc->func->uf_profiling)
1585 profile_start(&fc->prof_child);
1586 script_prof_save(tm);
1587}
1588
1589/*
1590 * Take care of time spent in a child.
1591 * Should always be called after prof_child_enter().
1592 */
1593 void
1594prof_child_exit(tm)
1595 proftime_T *tm; /* where waittime was stored */
1596{
1597 funccall_T *fc = current_funccal;
1598
1599 if (fc != NULL && fc->func->uf_profiling)
1600 {
1601 profile_end(&fc->prof_child);
1602 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1603 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1604 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1605 }
1606 script_prof_restore(tm);
1607}
1608#endif
1609
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611#ifdef FEAT_FOLDING
1612/*
1613 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1614 * it in "*cp". Doesn't give error messages.
1615 */
1616 int
1617eval_foldexpr(arg, cp)
1618 char_u *arg;
1619 int *cp;
1620{
Bram Moolenaar33570922005-01-25 22:26:29 +00001621 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 int retval;
1623 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001624 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1625 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001628 if (use_sandbox)
1629 ++sandbox;
1630 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 retval = 0;
1634 else
1635 {
1636 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 if (tv.v_type == VAR_NUMBER)
1638 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001639 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 retval = 0;
1641 else
1642 {
1643 /* If the result is a string, check if there is a non-digit before
1644 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (!VIM_ISDIGIT(*s) && *s != '-')
1647 *cp = *s++;
1648 retval = atol((char *)s);
1649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 }
1652 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001653 if (use_sandbox)
1654 --sandbox;
1655 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
1657 return retval;
1658}
1659#endif
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 * ":let" list all variable values
1663 * ":let var1 var2" list variable values
1664 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 * ":let var += expr" assignment command.
1666 * ":let var -= expr" assignment command.
1667 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 */
1670 void
1671ex_let(eap)
1672 exarg_T *eap;
1673{
1674 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001676 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 int var_count = 0;
1679 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001680 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001681 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaardb552d602006-03-23 22:59:57 +00001683 argend = skip_var_list(arg, &var_count, &semicolon);
1684 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001686 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1687 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001688 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001691 /*
1692 * ":let" without "=": list variables
1693 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 if (*arg == '[')
1695 EMSG(_(e_invarg));
1696 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 /* ":let var1 var2" */
1698 arg = list_arg_vars(eap, arg);
1699 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001701 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001702 list_glob_vars();
1703 list_buf_vars();
1704 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001705#ifdef FEAT_WINDOWS
1706 list_tab_vars();
1707#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001708 list_script_vars();
1709 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001710 list_vim_vars();
1711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 eap->nextcmd = check_nextcmd(arg);
1713 }
1714 else
1715 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 op[0] = '=';
1717 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001718 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 {
1720 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1721 op[0] = expr[-1]; /* +=, -= or .= */
1722 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (eap->skip)
1726 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (eap->skip)
1729 {
1730 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001731 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 --emsg_skip;
1733 }
1734 else if (i != FAIL)
1735 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 }
1741}
1742
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743/*
1744 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1745 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 * When "nextchars" is not NULL it points to a string with characters that
1747 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1748 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 * Returns OK or FAIL;
1750 */
1751 static int
1752ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1753 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001754 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 int copy; /* copy values from "tv", don't move */
1756 int semicolon; /* from skip_var_list() */
1757 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759{
1760 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001761 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 listitem_T *item;
1764 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765
1766 if (*arg != '[')
1767 {
1768 /*
1769 * ":let var = expr" or ":for var in list"
1770 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 return FAIL;
1773 return OK;
1774 }
1775
1776 /*
1777 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1778 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001779 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 {
1781 EMSG(_(e_listreq));
1782 return FAIL;
1783 }
1784
1785 i = list_len(l);
1786 if (semicolon == 0 && var_count < i)
1787 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001788 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 return FAIL;
1790 }
1791 if (var_count - semicolon > i)
1792 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001793 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 return FAIL;
1795 }
1796
1797 item = l->lv_first;
1798 while (*arg != ']')
1799 {
1800 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001801 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 item = item->li_next;
1803 if (arg == NULL)
1804 return FAIL;
1805
1806 arg = skipwhite(arg);
1807 if (*arg == ';')
1808 {
1809 /* Put the rest of the list (may be empty) in the var after ';'.
1810 * Create a new list for this. */
1811 l = list_alloc();
1812 if (l == NULL)
1813 return FAIL;
1814 while (item != NULL)
1815 {
1816 list_append_tv(l, &item->li_tv);
1817 item = item->li_next;
1818 }
1819
1820 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001821 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 ltv.vval.v_list = l;
1823 l->lv_refcount = 1;
1824
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1826 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 clear_tv(&ltv);
1828 if (arg == NULL)
1829 return FAIL;
1830 break;
1831 }
1832 else if (*arg != ',' && *arg != ']')
1833 {
1834 EMSG2(_(e_intern2), "ex_let_vars()");
1835 return FAIL;
1836 }
1837 }
1838
1839 return OK;
1840}
1841
1842/*
1843 * Skip over assignable variable "var" or list of variables "[var, var]".
1844 * Used for ":let varvar = expr" and ":for varvar in expr".
1845 * For "[var, var]" increment "*var_count" for each variable.
1846 * for "[var, var; var]" set "semicolon".
1847 * Return NULL for an error.
1848 */
1849 static char_u *
1850skip_var_list(arg, var_count, semicolon)
1851 char_u *arg;
1852 int *var_count;
1853 int *semicolon;
1854{
1855 char_u *p, *s;
1856
1857 if (*arg == '[')
1858 {
1859 /* "[var, var]": find the matching ']'. */
1860 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001861 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 {
1863 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1864 s = skip_var_one(p);
1865 if (s == p)
1866 {
1867 EMSG2(_(e_invarg2), p);
1868 return NULL;
1869 }
1870 ++*var_count;
1871
1872 p = skipwhite(s);
1873 if (*p == ']')
1874 break;
1875 else if (*p == ';')
1876 {
1877 if (*semicolon == 1)
1878 {
1879 EMSG(_("Double ; in list of variables"));
1880 return NULL;
1881 }
1882 *semicolon = 1;
1883 }
1884 else if (*p != ',')
1885 {
1886 EMSG2(_(e_invarg2), p);
1887 return NULL;
1888 }
1889 }
1890 return p + 1;
1891 }
1892 else
1893 return skip_var_one(arg);
1894}
1895
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001896/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001897 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1898 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001899 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 static char_u *
1901skip_var_one(arg)
1902 char_u *arg;
1903{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001904 if (*arg == '@' && arg[1] != NUL)
1905 return arg + 2;
1906 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1907 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908}
1909
Bram Moolenaara7043832005-01-21 11:56:39 +00001910/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 * List variables for hashtab "ht" with prefix "prefix".
1912 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001913 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001915list_hashtable_vars(ht, prefix, empty)
1916 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001917 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001919{
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 hashitem_T *hi;
1921 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 int todo;
1923
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001924 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1926 {
1927 if (!HASHITEM_EMPTY(hi))
1928 {
1929 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 di = HI2DI(hi);
1931 if (empty || di->di_tv.v_type != VAR_STRING
1932 || di->di_tv.vval.v_string != NULL)
1933 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 }
1935 }
1936}
1937
1938/*
1939 * List global variables.
1940 */
1941 static void
1942list_glob_vars()
1943{
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001945}
1946
1947/*
1948 * List buffer variables.
1949 */
1950 static void
1951list_buf_vars()
1952{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001953 char_u numbuf[NUMBUFLEN];
1954
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956
1957 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1958 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001959}
1960
1961/*
1962 * List window variables.
1963 */
1964 static void
1965list_win_vars()
1966{
Bram Moolenaar33570922005-01-25 22:26:29 +00001967 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968}
1969
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001970#ifdef FEAT_WINDOWS
1971/*
1972 * List tab page variables.
1973 */
1974 static void
1975list_tab_vars()
1976{
1977 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1978}
1979#endif
1980
Bram Moolenaara7043832005-01-21 11:56:39 +00001981/*
1982 * List Vim variables.
1983 */
1984 static void
1985list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986{
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988}
1989
1990/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001991 * List script-local variables, if there is a script.
1992 */
1993 static void
1994list_script_vars()
1995{
1996 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1997 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1998}
1999
2000/*
2001 * List function variables, if there is a function.
2002 */
2003 static void
2004list_func_vars()
2005{
2006 if (current_funccal != NULL)
2007 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2008 (char_u *)"l:", FALSE);
2009}
2010
2011/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 * List variables in "arg".
2013 */
2014 static char_u *
2015list_arg_vars(eap, arg)
2016 exarg_T *eap;
2017 char_u *arg;
2018{
2019 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002020 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 char_u *name_start;
2023 char_u *arg_subsc;
2024 char_u *tofree;
2025 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026
2027 while (!ends_excmd(*arg) && !got_int)
2028 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002029 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002031 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002032 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2033 {
2034 emsg_severe = TRUE;
2035 EMSG(_(e_trailing));
2036 break;
2037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 /* get_name_len() takes care of expanding curly braces */
2042 name_start = name = arg;
2043 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2044 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002046 /* This is mainly to keep test 49 working: when expanding
2047 * curly braces fails overrule the exception error message. */
2048 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050 emsg_severe = TRUE;
2051 EMSG2(_(e_invarg2), arg);
2052 break;
2053 }
2054 error = TRUE;
2055 }
2056 else
2057 {
2058 if (tofree != NULL)
2059 name = tofree;
2060 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 else
2063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064 /* handle d.key, l[idx], f(expr) */
2065 arg_subsc = arg;
2066 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002070 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 case 'g': list_glob_vars(); break;
2075 case 'b': list_buf_vars(); break;
2076 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002077#ifdef FEAT_WINDOWS
2078 case 't': list_tab_vars(); break;
2079#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002080 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002081 case 's': list_script_vars(); break;
2082 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 default:
2084 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 }
2087 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088 {
2089 char_u numbuf[NUMBUFLEN];
2090 char_u *tf;
2091 int c;
2092 char_u *s;
2093
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002094 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002095 c = *arg;
2096 *arg = NUL;
2097 list_one_var_a((char_u *)"",
2098 arg == arg_subsc ? name : name_start,
2099 tv.v_type, s == NULL ? (char_u *)"" : s);
2100 *arg = c;
2101 vim_free(tf);
2102 }
2103 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 }
2106 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107
2108 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110
2111 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 }
2113
2114 return arg;
2115}
2116
2117/*
2118 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2119 * Returns a pointer to the char just after the var name.
2120 * Returns NULL if there is an error.
2121 */
2122 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002123ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129{
2130 int c1;
2131 char_u *name;
2132 char_u *p;
2133 char_u *arg_end = NULL;
2134 int len;
2135 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137
2138 /*
2139 * ":let $VAR = expr": Set environment variable.
2140 */
2141 if (*arg == '$')
2142 {
2143 /* Find the end of the name. */
2144 ++arg;
2145 name = arg;
2146 len = get_env_len(&arg);
2147 if (len == 0)
2148 EMSG2(_(e_invarg2), name - 1);
2149 else
2150 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 if (op != NULL && (*op == '+' || *op == '-'))
2152 EMSG2(_(e_letwrong), op);
2153 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002154 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 EMSG(_(e_letunexp));
2156 else
2157 {
2158 c1 = name[len];
2159 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002160 p = get_tv_string_chk(tv);
2161 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 {
2163 int mustfree = FALSE;
2164 char_u *s = vim_getenv(name, &mustfree);
2165
2166 if (s != NULL)
2167 {
2168 p = tofree = concat_str(s, p);
2169 if (mustfree)
2170 vim_free(s);
2171 }
2172 }
2173 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002174 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 if (STRICMP(name, "HOME") == 0)
2177 init_homedir();
2178 else if (didset_vim && STRICMP(name, "VIM") == 0)
2179 didset_vim = FALSE;
2180 else if (didset_vimruntime
2181 && STRICMP(name, "VIMRUNTIME") == 0)
2182 didset_vimruntime = FALSE;
2183 arg_end = arg;
2184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 }
2188 }
2189 }
2190
2191 /*
2192 * ":let &option = expr": Set option value.
2193 * ":let &l:option = expr": Set local option value.
2194 * ":let &g:option = expr": Set global option value.
2195 */
2196 else if (*arg == '&')
2197 {
2198 /* Find the end of the name. */
2199 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002200 if (p == NULL || (endchars != NULL
2201 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 EMSG(_(e_letunexp));
2203 else
2204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 long n;
2206 int opt_type;
2207 long numval;
2208 char_u *stringval = NULL;
2209 char_u *s;
2210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 c1 = *p;
2212 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002213
2214 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002215 s = get_tv_string_chk(tv); /* != NULL if number or string */
2216 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217 {
2218 opt_type = get_option_value(arg, &numval,
2219 &stringval, opt_flags);
2220 if ((opt_type == 1 && *op == '.')
2221 || (opt_type == 0 && *op != '.'))
2222 EMSG2(_(e_letwrong), op);
2223 else
2224 {
2225 if (opt_type == 1) /* number */
2226 {
2227 if (*op == '+')
2228 n = numval + n;
2229 else
2230 n = numval - n;
2231 }
2232 else if (opt_type == 0 && stringval != NULL) /* string */
2233 {
2234 s = concat_str(stringval, s);
2235 vim_free(stringval);
2236 stringval = s;
2237 }
2238 }
2239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002240 if (s != NULL)
2241 {
2242 set_option_value(arg, n, s, opt_flags);
2243 arg_end = p;
2244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002246 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 }
2248 }
2249
2250 /*
2251 * ":let @r = expr": Set register contents.
2252 */
2253 else if (*arg == '@')
2254 {
2255 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002256 if (op != NULL && (*op == '+' || *op == '-'))
2257 EMSG2(_(e_letwrong), op);
2258 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002259 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 EMSG(_(e_letunexp));
2261 else
2262 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002263 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 char_u *s;
2265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002266 p = get_tv_string_chk(tv);
2267 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002269 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 if (s != NULL)
2271 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002272 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002273 vim_free(s);
2274 }
2275 }
2276 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002278 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 arg_end = arg + 1;
2280 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002281 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283 }
2284
2285 /*
2286 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002289 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002291 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002293 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2297 EMSG(_(e_letunexp));
2298 else
2299 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 arg_end = p;
2302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
2306
2307 else
2308 EMSG2(_(e_invarg2), arg);
2309
2310 return arg_end;
2311}
2312
2313/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002314 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2315 */
2316 static int
2317check_changedtick(arg)
2318 char_u *arg;
2319{
2320 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2321 {
2322 EMSG2(_(e_readonlyvar), arg);
2323 return TRUE;
2324 }
2325 return FALSE;
2326}
2327
2328/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 * Get an lval: variable, Dict item or List item that can be assigned a value
2330 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2331 * "name.key", "name.key[expr]" etc.
2332 * Indexing only works if "name" is an existing List or Dictionary.
2333 * "name" points to the start of the name.
2334 * If "rettv" is not NULL it points to the value to be assigned.
2335 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2336 * wrong; must end in space or cmd separator.
2337 *
2338 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 */
2342 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002343get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002345 typval_T *rettv;
2346 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 int unlet;
2348 int skip;
2349 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002350 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 char_u *expr_start, *expr_end;
2354 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002355 dictitem_T *v;
2356 typval_T var1;
2357 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002359 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002362 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002365 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366
2367 if (skip)
2368 {
2369 /* When skipping just find the end of the name. */
2370 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002371 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 }
2373
2374 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002375 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (expr_start != NULL)
2377 {
2378 /* Don't expand the name when we already know there is an error. */
2379 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2380 && *p != '[' && *p != '.')
2381 {
2382 EMSG(_(e_trailing));
2383 return NULL;
2384 }
2385
2386 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2387 if (lp->ll_exp_name == NULL)
2388 {
2389 /* Report an invalid expression in braces, unless the
2390 * expression evaluation has been cancelled due to an
2391 * aborting error, an interrupt, or an exception. */
2392 if (!aborting() && !quiet)
2393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002394 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 EMSG2(_(e_invarg2), name);
2396 return NULL;
2397 }
2398 }
2399 lp->ll_name = lp->ll_exp_name;
2400 }
2401 else
2402 lp->ll_name = name;
2403
2404 /* Without [idx] or .key we are done. */
2405 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2406 return p;
2407
2408 cc = *p;
2409 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002410 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (v == NULL && !quiet)
2412 EMSG2(_(e_undefvar), lp->ll_name);
2413 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 if (v == NULL)
2415 return NULL;
2416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 /*
2418 * Loop until no more [idx] or .key is following.
2419 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002420 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2424 && !(lp->ll_tv->v_type == VAR_DICT
2425 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 if (!quiet)
2428 EMSG(_("E689: Can only index a List or Dictionary"));
2429 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (!quiet)
2434 EMSG(_("E708: [:] must come last"));
2435 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002437
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 len = -1;
2439 if (*p == '.')
2440 {
2441 key = p + 1;
2442 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2443 ;
2444 if (len == 0)
2445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (!quiet)
2447 EMSG(_(e_emptykey));
2448 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 }
2450 p = key + len;
2451 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002452 else
2453 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002455 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 if (*p == ':')
2457 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002458 else
2459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 empty1 = FALSE;
2461 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 if (get_tv_string_chk(&var1) == NULL)
2464 {
2465 /* not a number or string */
2466 clear_tv(&var1);
2467 return NULL;
2468 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 }
2470
2471 /* Optionally get the second index [ :expr]. */
2472 if (*p == ':')
2473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002478 if (!empty1)
2479 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (rettv != NULL && (rettv->v_type != VAR_LIST
2483 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (!quiet)
2486 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 if (!empty1)
2488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 }
2491 p = skipwhite(p + 1);
2492 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 else
2495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2498 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (!empty1)
2500 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 if (get_tv_string_chk(&var2) == NULL)
2504 {
2505 /* not a number or string */
2506 if (!empty1)
2507 clear_tv(&var1);
2508 clear_tv(&var2);
2509 return NULL;
2510 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (!quiet)
2520 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 if (!empty1)
2522 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 }
2527
2528 /* Skip to past ']'. */
2529 ++p;
2530 }
2531
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 {
2534 if (len == -1)
2535 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 if (*key == NUL)
2539 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (!quiet)
2541 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 }
2545 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 lp->ll_list = NULL;
2547 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002548 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002551 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 if (len == -1)
2557 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 }
2560 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 if (len == -1)
2565 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 p = NULL;
2568 break;
2569 }
2570 if (len == -1)
2571 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 }
2574 else
2575 {
2576 /*
2577 * Get the number and item for the only or first index of the List.
2578 */
2579 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 else
2582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002583 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 clear_tv(&var1);
2585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002586 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 lp->ll_list = lp->ll_tv->vval.v_list;
2588 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2589 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002591 if (lp->ll_n1 < 0)
2592 {
2593 lp->ll_n1 = 0;
2594 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2595 }
2596 }
2597 if (lp->ll_li == NULL)
2598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 }
2603
2604 /*
2605 * May need to find the item or absolute index for the second
2606 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * When no index given: "lp->ll_empty2" is TRUE.
2608 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002612 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 }
2621
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2623 if (lp->ll_n1 < 0)
2624 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2625 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 }
2628
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 }
2632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 return p;
2634}
2635
2636/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002637 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 */
2639 static void
2640clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002641 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642{
2643 vim_free(lp->ll_exp_name);
2644 vim_free(lp->ll_newkey);
2645}
2646
2647/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002648 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 */
2652 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002653set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002658 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659{
2660 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002661 listitem_T *ri;
2662 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663
2664 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 cc = *endp;
2669 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 if (op != NULL && *op != '=')
2671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002672 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002674 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002675 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002676 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 {
2678 if (tv_op(&tv, rettv, op) == OK)
2679 set_var(lp->ll_name, &tv, FALSE);
2680 clear_tv(&tv);
2681 }
2682 }
2683 else
2684 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002688 else if (tv_check_lock(lp->ll_newkey == NULL
2689 ? lp->ll_tv->v_lock
2690 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2691 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 else if (lp->ll_range)
2693 {
2694 /*
2695 * Assign the List values to the list items.
2696 */
2697 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 if (op != NULL && *op != '=')
2700 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2701 else
2702 {
2703 clear_tv(&lp->ll_li->li_tv);
2704 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 ri = ri->li_next;
2707 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2708 break;
2709 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002712 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 ri = NULL;
2715 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_li = lp->ll_li->li_next;
2719 ++lp->ll_n1;
2720 }
2721 if (ri != NULL)
2722 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 else if (lp->ll_empty2
2724 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 : lp->ll_n1 != lp->ll_n2)
2726 EMSG(_("E711: List value has not enough items"));
2727 }
2728 else
2729 {
2730 /*
2731 * Assign to a List or Dictionary item.
2732 */
2733 if (lp->ll_newkey != NULL)
2734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 if (op != NULL && *op != '=')
2736 {
2737 EMSG2(_(e_letwrong), op);
2738 return;
2739 }
2740
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002742 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (di == NULL)
2744 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002745 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2746 {
2747 vim_free(di);
2748 return;
2749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 else if (op != NULL && *op != '=')
2753 {
2754 tv_op(lp->ll_tv, rettv, op);
2755 return;
2756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002757 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 /*
2761 * Assign the value to the variable or list item.
2762 */
2763 if (copy)
2764 copy_tv(rettv, lp->ll_tv);
2765 else
2766 {
2767 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002768 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002770 }
2771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002772}
2773
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002774/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002775 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2776 * Returns OK or FAIL.
2777 */
2778 static int
2779tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002780 typval_T *tv1;
2781 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 char_u *op;
2783{
2784 long n;
2785 char_u numbuf[NUMBUFLEN];
2786 char_u *s;
2787
2788 /* Can't do anything with a Funcref or a Dict on the right. */
2789 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2790 {
2791 switch (tv1->v_type)
2792 {
2793 case VAR_DICT:
2794 case VAR_FUNC:
2795 break;
2796
2797 case VAR_LIST:
2798 if (*op != '+' || tv2->v_type != VAR_LIST)
2799 break;
2800 /* List += List */
2801 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2802 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2803 return OK;
2804
2805 case VAR_NUMBER:
2806 case VAR_STRING:
2807 if (tv2->v_type == VAR_LIST)
2808 break;
2809 if (*op == '+' || *op == '-')
2810 {
2811 /* nr += nr or nr -= nr*/
2812 n = get_tv_number(tv1);
2813 if (*op == '+')
2814 n += get_tv_number(tv2);
2815 else
2816 n -= get_tv_number(tv2);
2817 clear_tv(tv1);
2818 tv1->v_type = VAR_NUMBER;
2819 tv1->vval.v_number = n;
2820 }
2821 else
2822 {
2823 /* str .= str */
2824 s = get_tv_string(tv1);
2825 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2826 clear_tv(tv1);
2827 tv1->v_type = VAR_STRING;
2828 tv1->vval.v_string = s;
2829 }
2830 return OK;
2831 }
2832 }
2833
2834 EMSG2(_(e_letwrong), op);
2835 return FAIL;
2836}
2837
2838/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839 * Add a watcher to a list.
2840 */
2841 static void
2842list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002843 list_T *l;
2844 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002845{
2846 lw->lw_next = l->lv_watch;
2847 l->lv_watch = lw;
2848}
2849
2850/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002851 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 * No warning when it isn't found...
2853 */
2854 static void
2855list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002856 list_T *l;
2857 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858{
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860
2861 lwp = &l->lv_watch;
2862 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2863 {
2864 if (lw == lwrem)
2865 {
2866 *lwp = lw->lw_next;
2867 break;
2868 }
2869 lwp = &lw->lw_next;
2870 }
2871}
2872
2873/*
2874 * Just before removing an item from a list: advance watchers to the next
2875 * item.
2876 */
2877 static void
2878list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 list_T *l;
2880 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881{
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883
2884 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2885 if (lw->lw_item == item)
2886 lw->lw_item = item->li_next;
2887}
2888
2889/*
2890 * Evaluate the expression used in a ":for var in expr" command.
2891 * "arg" points to "var".
2892 * Set "*errp" to TRUE for an error, FALSE otherwise;
2893 * Return a pointer that holds the info. Null when there is an error.
2894 */
2895 void *
2896eval_for_line(arg, errp, nextcmdp, skip)
2897 char_u *arg;
2898 int *errp;
2899 char_u **nextcmdp;
2900 int skip;
2901{
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 typval_T tv;
2905 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906
2907 *errp = TRUE; /* default: there is an error */
2908
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910 if (fi == NULL)
2911 return NULL;
2912
2913 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2914 if (expr == NULL)
2915 return fi;
2916
2917 expr = skipwhite(expr);
2918 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2919 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002920 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002921 return fi;
2922 }
2923
2924 if (skip)
2925 ++emsg_skip;
2926 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2927 {
2928 *errp = FALSE;
2929 if (!skip)
2930 {
2931 l = tv.vval.v_list;
2932 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002933 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002934 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002935 clear_tv(&tv);
2936 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937 else
2938 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002939 /* No need to increment the refcount, it's already set for the
2940 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941 fi->fi_list = l;
2942 list_add_watch(l, &fi->fi_lw);
2943 fi->fi_lw.lw_item = l->lv_first;
2944 }
2945 }
2946 }
2947 if (skip)
2948 --emsg_skip;
2949
2950 return fi;
2951}
2952
2953/*
2954 * Use the first item in a ":for" list. Advance to the next.
2955 * Assign the values to the variable (list). "arg" points to the first one.
2956 * Return TRUE when a valid item was found, FALSE when at end of list or
2957 * something wrong.
2958 */
2959 int
2960next_for_item(fi_void, arg)
2961 void *fi_void;
2962 char_u *arg;
2963{
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002965 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967
2968 item = fi->fi_lw.lw_item;
2969 if (item == NULL)
2970 result = FALSE;
2971 else
2972 {
2973 fi->fi_lw.lw_item = item->li_next;
2974 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2975 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2976 }
2977 return result;
2978}
2979
2980/*
2981 * Free the structure used to store info used by ":for".
2982 */
2983 void
2984free_for_info(fi_void)
2985 void *fi_void;
2986{
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002988
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002989 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002990 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002992 list_unref(fi->fi_list);
2993 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 vim_free(fi);
2995}
2996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2998
2999 void
3000set_context_for_expression(xp, arg, cmdidx)
3001 expand_T *xp;
3002 char_u *arg;
3003 cmdidx_T cmdidx;
3004{
3005 int got_eq = FALSE;
3006 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 if (cmdidx == CMD_let)
3010 {
3011 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003012 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 {
3014 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003015 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003016 {
3017 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019 if (vim_iswhite(*p))
3020 break;
3021 }
3022 return;
3023 }
3024 }
3025 else
3026 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3027 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 while ((xp->xp_pattern = vim_strpbrk(arg,
3029 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3030 {
3031 c = *xp->xp_pattern;
3032 if (c == '&')
3033 {
3034 c = xp->xp_pattern[1];
3035 if (c == '&')
3036 {
3037 ++xp->xp_pattern;
3038 xp->xp_context = cmdidx != CMD_let || got_eq
3039 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3040 }
3041 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003044 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3045 xp->xp_pattern += 2;
3046
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
3049 else if (c == '$')
3050 {
3051 /* environment variable */
3052 xp->xp_context = EXPAND_ENV_VARS;
3053 }
3054 else if (c == '=')
3055 {
3056 got_eq = TRUE;
3057 xp->xp_context = EXPAND_EXPRESSION;
3058 }
3059 else if (c == '<'
3060 && xp->xp_context == EXPAND_FUNCTIONS
3061 && vim_strchr(xp->xp_pattern, '(') == NULL)
3062 {
3063 /* Function name can start with "<SNR>" */
3064 break;
3065 }
3066 else if (cmdidx != CMD_let || got_eq)
3067 {
3068 if (c == '"') /* string */
3069 {
3070 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3071 if (c == '\\' && xp->xp_pattern[1] != NUL)
3072 ++xp->xp_pattern;
3073 xp->xp_context = EXPAND_NOTHING;
3074 }
3075 else if (c == '\'') /* literal string */
3076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003077 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3079 /* skip */ ;
3080 xp->xp_context = EXPAND_NOTHING;
3081 }
3082 else if (c == '|')
3083 {
3084 if (xp->xp_pattern[1] == '|')
3085 {
3086 ++xp->xp_pattern;
3087 xp->xp_context = EXPAND_EXPRESSION;
3088 }
3089 else
3090 xp->xp_context = EXPAND_COMMANDS;
3091 }
3092 else
3093 xp->xp_context = EXPAND_EXPRESSION;
3094 }
3095 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096 /* Doesn't look like something valid, expand as an expression
3097 * anyway. */
3098 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 arg = xp->xp_pattern;
3100 if (*arg != NUL)
3101 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3102 /* skip */ ;
3103 }
3104 xp->xp_pattern = arg;
3105}
3106
3107#endif /* FEAT_CMDL_COMPL */
3108
3109/*
3110 * ":1,25call func(arg1, arg2)" function call.
3111 */
3112 void
3113ex_call(eap)
3114 exarg_T *eap;
3115{
3116 char_u *arg = eap->arg;
3117 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 linenr_T lnum;
3123 int doesrange;
3124 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003125 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003127 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3128 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 if (tofree == NULL)
3130 return;
3131
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003132 /* Increase refcount on dictionary, it could get deleted when evaluating
3133 * the arguments. */
3134 if (fudi.fd_dict != NULL)
3135 ++fudi.fd_dict->dv_refcount;
3136
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003138 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
Bram Moolenaar532c7802005-01-27 14:44:31 +00003141 /* Skip white space to allow ":call func ()". Not good, but required for
3142 * backward compatibility. */
3143 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003144 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 if (*startarg != '(')
3147 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003148 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 goto end;
3150 }
3151
3152 /*
3153 * When skipping, evaluate the function once, to find the end of the
3154 * arguments.
3155 * When the function takes a range, this is discovered after the first
3156 * call, and the loop is broken.
3157 */
3158 if (eap->skip)
3159 {
3160 ++emsg_skip;
3161 lnum = eap->line2; /* do it once, also with an invalid range */
3162 }
3163 else
3164 lnum = eap->line1;
3165 for ( ; lnum <= eap->line2; ++lnum)
3166 {
3167 if (!eap->skip && eap->addr_count > 0)
3168 {
3169 curwin->w_cursor.lnum = lnum;
3170 curwin->w_cursor.col = 0;
3171 }
3172 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003173 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003174 eap->line1, eap->line2, &doesrange,
3175 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
3177 failed = TRUE;
3178 break;
3179 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003180 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (doesrange || eap->skip)
3182 break;
3183 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003184 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003185 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003186 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (aborting())
3188 break;
3189 }
3190 if (eap->skip)
3191 --emsg_skip;
3192
3193 if (!failed)
3194 {
3195 /* Check for trailing illegal characters and a following command. */
3196 if (!ends_excmd(*arg))
3197 {
3198 emsg_severe = TRUE;
3199 EMSG(_(e_trailing));
3200 }
3201 else
3202 eap->nextcmd = check_nextcmd(arg);
3203 }
3204
3205end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003206 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003207 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208}
3209
3210/*
3211 * ":unlet[!] var1 ... " command.
3212 */
3213 void
3214ex_unlet(eap)
3215 exarg_T *eap;
3216{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003217 ex_unletlock(eap, eap->arg, 0);
3218}
3219
3220/*
3221 * ":lockvar" and ":unlockvar" commands
3222 */
3223 void
3224ex_lockvar(eap)
3225 exarg_T *eap;
3226{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003228 int deep = 2;
3229
3230 if (eap->forceit)
3231 deep = -1;
3232 else if (vim_isdigit(*arg))
3233 {
3234 deep = getdigits(&arg);
3235 arg = skipwhite(arg);
3236 }
3237
3238 ex_unletlock(eap, arg, deep);
3239}
3240
3241/*
3242 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3243 */
3244 static void
3245ex_unletlock(eap, argstart, deep)
3246 exarg_T *eap;
3247 char_u *argstart;
3248 int deep;
3249{
3250 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003253 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 do
3256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003258 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3259 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 if (lv.ll_name == NULL)
3261 error = TRUE; /* error but continue parsing */
3262 if (name_end == NULL || (!vim_iswhite(*name_end)
3263 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 if (name_end != NULL)
3266 {
3267 emsg_severe = TRUE;
3268 EMSG(_(e_trailing));
3269 }
3270 if (!(eap->skip || error))
3271 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 break;
3273 }
3274
3275 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003276 {
3277 if (eap->cmdidx == CMD_unlet)
3278 {
3279 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3280 error = TRUE;
3281 }
3282 else
3283 {
3284 if (do_lock_var(&lv, name_end, deep,
3285 eap->cmdidx == CMD_lockvar) == FAIL)
3286 error = TRUE;
3287 }
3288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003290 if (!eap->skip)
3291 clear_lval(&lv);
3292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 arg = skipwhite(name_end);
3294 } while (!ends_excmd(*arg));
3295
3296 eap->nextcmd = check_nextcmd(arg);
3297}
3298
Bram Moolenaar8c711452005-01-14 21:53:12 +00003299 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003300do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003301 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003302 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003303 int forceit;
3304{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003305 int ret = OK;
3306 int cc;
3307
3308 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003310 cc = *name_end;
3311 *name_end = NUL;
3312
3313 /* Normal name or expanded name. */
3314 if (check_changedtick(lp->ll_name))
3315 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003316 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003317 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003318 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003319 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003320 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3321 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003322 else if (lp->ll_range)
3323 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003325
3326 /* Delete a range of List items. */
3327 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3328 {
3329 li = lp->ll_li->li_next;
3330 listitem_remove(lp->ll_list, lp->ll_li);
3331 lp->ll_li = li;
3332 ++lp->ll_n1;
3333 }
3334 }
3335 else
3336 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003337 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003338 /* unlet a List item. */
3339 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003340 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003341 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003342 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003343 }
3344
3345 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003346}
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348/*
3349 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003350 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 */
3352 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003353do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003355 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
Bram Moolenaar33570922005-01-25 22:26:29 +00003357 hashtab_T *ht;
3358 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003359 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 ht = find_var_ht(name, &varname);
3362 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003364 hi = hash_find(ht, varname);
3365 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003366 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003367 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3368 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003369 if (var_check_ro(HI2DI(hi)->di_flags, name))
3370 return FAIL;
3371 delete_var(ht, hi);
3372 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003375 if (forceit)
3376 return OK;
3377 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 return FAIL;
3379}
3380
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003381/*
3382 * Lock or unlock variable indicated by "lp".
3383 * "deep" is the levels to go (-1 for unlimited);
3384 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3385 */
3386 static int
3387do_lock_var(lp, name_end, deep, lock)
3388 lval_T *lp;
3389 char_u *name_end;
3390 int deep;
3391 int lock;
3392{
3393 int ret = OK;
3394 int cc;
3395 dictitem_T *di;
3396
3397 if (deep == 0) /* nothing to do */
3398 return OK;
3399
3400 if (lp->ll_tv == NULL)
3401 {
3402 cc = *name_end;
3403 *name_end = NUL;
3404
3405 /* Normal name or expanded name. */
3406 if (check_changedtick(lp->ll_name))
3407 ret = FAIL;
3408 else
3409 {
3410 di = find_var(lp->ll_name, NULL);
3411 if (di == NULL)
3412 ret = FAIL;
3413 else
3414 {
3415 if (lock)
3416 di->di_flags |= DI_FLAGS_LOCK;
3417 else
3418 di->di_flags &= ~DI_FLAGS_LOCK;
3419 item_lock(&di->di_tv, deep, lock);
3420 }
3421 }
3422 *name_end = cc;
3423 }
3424 else if (lp->ll_range)
3425 {
3426 listitem_T *li = lp->ll_li;
3427
3428 /* (un)lock a range of List items. */
3429 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3430 {
3431 item_lock(&li->li_tv, deep, lock);
3432 li = li->li_next;
3433 ++lp->ll_n1;
3434 }
3435 }
3436 else if (lp->ll_list != NULL)
3437 /* (un)lock a List item. */
3438 item_lock(&lp->ll_li->li_tv, deep, lock);
3439 else
3440 /* un(lock) a Dictionary item. */
3441 item_lock(&lp->ll_di->di_tv, deep, lock);
3442
3443 return ret;
3444}
3445
3446/*
3447 * Lock or unlock an item. "deep" is nr of levels to go.
3448 */
3449 static void
3450item_lock(tv, deep, lock)
3451 typval_T *tv;
3452 int deep;
3453 int lock;
3454{
3455 static int recurse = 0;
3456 list_T *l;
3457 listitem_T *li;
3458 dict_T *d;
3459 hashitem_T *hi;
3460 int todo;
3461
3462 if (recurse >= DICT_MAXNEST)
3463 {
3464 EMSG(_("E743: variable nested too deep for (un)lock"));
3465 return;
3466 }
3467 if (deep == 0)
3468 return;
3469 ++recurse;
3470
3471 /* lock/unlock the item itself */
3472 if (lock)
3473 tv->v_lock |= VAR_LOCKED;
3474 else
3475 tv->v_lock &= ~VAR_LOCKED;
3476
3477 switch (tv->v_type)
3478 {
3479 case VAR_LIST:
3480 if ((l = tv->vval.v_list) != NULL)
3481 {
3482 if (lock)
3483 l->lv_lock |= VAR_LOCKED;
3484 else
3485 l->lv_lock &= ~VAR_LOCKED;
3486 if (deep < 0 || deep > 1)
3487 /* recursive: lock/unlock the items the List contains */
3488 for (li = l->lv_first; li != NULL; li = li->li_next)
3489 item_lock(&li->li_tv, deep - 1, lock);
3490 }
3491 break;
3492 case VAR_DICT:
3493 if ((d = tv->vval.v_dict) != NULL)
3494 {
3495 if (lock)
3496 d->dv_lock |= VAR_LOCKED;
3497 else
3498 d->dv_lock &= ~VAR_LOCKED;
3499 if (deep < 0 || deep > 1)
3500 {
3501 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003502 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003503 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3504 {
3505 if (!HASHITEM_EMPTY(hi))
3506 {
3507 --todo;
3508 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3509 }
3510 }
3511 }
3512 }
3513 }
3514 --recurse;
3515}
3516
Bram Moolenaara40058a2005-07-11 22:42:07 +00003517/*
3518 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3519 * it refers to a List or Dictionary that is locked.
3520 */
3521 static int
3522tv_islocked(tv)
3523 typval_T *tv;
3524{
3525 return (tv->v_lock & VAR_LOCKED)
3526 || (tv->v_type == VAR_LIST
3527 && tv->vval.v_list != NULL
3528 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3529 || (tv->v_type == VAR_DICT
3530 && tv->vval.v_dict != NULL
3531 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3532}
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3535/*
3536 * Delete all "menutrans_" variables.
3537 */
3538 void
3539del_menutrans_vars()
3540{
Bram Moolenaar33570922005-01-25 22:26:29 +00003541 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003542 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar33570922005-01-25 22:26:29 +00003544 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003545 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003547 {
3548 if (!HASHITEM_EMPTY(hi))
3549 {
3550 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3552 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003553 }
3554 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003555 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556}
3557#endif
3558
3559#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3560
3561/*
3562 * Local string buffer for the next two functions to store a variable name
3563 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3564 * get_user_var_name().
3565 */
3566
3567static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3568
3569static char_u *varnamebuf = NULL;
3570static int varnamebuflen = 0;
3571
3572/*
3573 * Function to concatenate a prefix and a variable name.
3574 */
3575 static char_u *
3576cat_prefix_varname(prefix, name)
3577 int prefix;
3578 char_u *name;
3579{
3580 int len;
3581
3582 len = (int)STRLEN(name) + 3;
3583 if (len > varnamebuflen)
3584 {
3585 vim_free(varnamebuf);
3586 len += 10; /* some additional space */
3587 varnamebuf = alloc(len);
3588 if (varnamebuf == NULL)
3589 {
3590 varnamebuflen = 0;
3591 return NULL;
3592 }
3593 varnamebuflen = len;
3594 }
3595 *varnamebuf = prefix;
3596 varnamebuf[1] = ':';
3597 STRCPY(varnamebuf + 2, name);
3598 return varnamebuf;
3599}
3600
3601/*
3602 * Function given to ExpandGeneric() to obtain the list of user defined
3603 * (global/buffer/window/built-in) variable names.
3604 */
3605/*ARGSUSED*/
3606 char_u *
3607get_user_var_name(xp, idx)
3608 expand_T *xp;
3609 int idx;
3610{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003611 static long_u gdone;
3612 static long_u bdone;
3613 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003614#ifdef FEAT_WINDOWS
3615 static long_u tdone;
3616#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003617 static int vidx;
3618 static hashitem_T *hi;
3619 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
3621 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003622 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003623 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003624#ifdef FEAT_WINDOWS
3625 tdone = 0;
3626#endif
3627 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003628
3629 /* Global variables */
3630 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003632 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003633 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003634 else
3635 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003636 while (HASHITEM_EMPTY(hi))
3637 ++hi;
3638 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3639 return cat_prefix_varname('g', hi->hi_key);
3640 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003642
3643 /* b: variables */
3644 ht = &curbuf->b_vars.dv_hashtab;
3645 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003647 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003649 else
3650 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003651 while (HASHITEM_EMPTY(hi))
3652 ++hi;
3653 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003657 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return (char_u *)"b:changedtick";
3659 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003660
3661 /* w: variables */
3662 ht = &curwin->w_vars.dv_hashtab;
3663 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003665 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003666 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003667 else
3668 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003669 while (HASHITEM_EMPTY(hi))
3670 ++hi;
3671 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003673
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003674#ifdef FEAT_WINDOWS
3675 /* t: variables */
3676 ht = &curtab->tp_vars.dv_hashtab;
3677 if (tdone < ht->ht_used)
3678 {
3679 if (tdone++ == 0)
3680 hi = ht->ht_array;
3681 else
3682 ++hi;
3683 while (HASHITEM_EMPTY(hi))
3684 ++hi;
3685 return cat_prefix_varname('t', hi->hi_key);
3686 }
3687#endif
3688
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 /* v: variables */
3690 if (vidx < VV_LEN)
3691 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693 vim_free(varnamebuf);
3694 varnamebuf = NULL;
3695 varnamebuflen = 0;
3696 return NULL;
3697}
3698
3699#endif /* FEAT_CMDL_COMPL */
3700
3701/*
3702 * types for expressions.
3703 */
3704typedef enum
3705{
3706 TYPE_UNKNOWN = 0
3707 , TYPE_EQUAL /* == */
3708 , TYPE_NEQUAL /* != */
3709 , TYPE_GREATER /* > */
3710 , TYPE_GEQUAL /* >= */
3711 , TYPE_SMALLER /* < */
3712 , TYPE_SEQUAL /* <= */
3713 , TYPE_MATCH /* =~ */
3714 , TYPE_NOMATCH /* !~ */
3715} exptype_T;
3716
3717/*
3718 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003719 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3721 */
3722
3723/*
3724 * Handle zero level expression.
3725 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003726 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003727 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 * Return OK or FAIL.
3729 */
3730 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003731eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 char_u **nextcmd;
3735 int evaluate;
3736{
3737 int ret;
3738 char_u *p;
3739
3740 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003741 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (ret == FAIL || !ends_excmd(*p))
3743 {
3744 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003745 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 /*
3747 * Report the invalid expression unless the expression evaluation has
3748 * been cancelled due to an aborting error, an interrupt, or an
3749 * exception.
3750 */
3751 if (!aborting())
3752 EMSG2(_(e_invexpr2), arg);
3753 ret = FAIL;
3754 }
3755 if (nextcmd != NULL)
3756 *nextcmd = check_nextcmd(p);
3757
3758 return ret;
3759}
3760
3761/*
3762 * Handle top level expression:
3763 * expr1 ? expr0 : expr0
3764 *
3765 * "arg" must point to the first non-white of the expression.
3766 * "arg" is advanced to the next non-white after the recognized expression.
3767 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003768 * Note: "rettv.v_lock" is not set.
3769 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 * Return OK or FAIL.
3771 */
3772 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003773eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 int evaluate;
3777{
3778 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780
3781 /*
3782 * Get the first variable.
3783 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 return FAIL;
3786
3787 if ((*arg)[0] == '?')
3788 {
3789 result = FALSE;
3790 if (evaluate)
3791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 int error = FALSE;
3793
3794 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 if (error)
3798 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800
3801 /*
3802 * Get the second variable.
3803 */
3804 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 return FAIL;
3807
3808 /*
3809 * Check for the ":".
3810 */
3811 if ((*arg)[0] != ':')
3812 {
3813 EMSG(_("E109: Missing ':' after '?'"));
3814 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 return FAIL;
3817 }
3818
3819 /*
3820 * Get the third variable.
3821 */
3822 *arg = skipwhite(*arg + 1);
3823 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3824 {
3825 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 return FAIL;
3828 }
3829 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832
3833 return OK;
3834}
3835
3836/*
3837 * Handle first level expression:
3838 * expr2 || expr2 || expr2 logical OR
3839 *
3840 * "arg" must point to the first non-white of the expression.
3841 * "arg" is advanced to the next non-white after the recognized expression.
3842 *
3843 * Return OK or FAIL.
3844 */
3845 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 int evaluate;
3850{
Bram Moolenaar33570922005-01-25 22:26:29 +00003851 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 long result;
3853 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856 /*
3857 * Get the first variable.
3858 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003859 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 return FAIL;
3861
3862 /*
3863 * Repeat until there is no following "||".
3864 */
3865 first = TRUE;
3866 result = FALSE;
3867 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3868 {
3869 if (evaluate && first)
3870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003873 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003874 if (error)
3875 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 first = FALSE;
3877 }
3878
3879 /*
3880 * Get the second variable.
3881 */
3882 *arg = skipwhite(*arg + 2);
3883 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3884 return FAIL;
3885
3886 /*
3887 * Compute the result.
3888 */
3889 if (evaluate && !result)
3890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003891 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003893 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003894 if (error)
3895 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897 if (evaluate)
3898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 rettv->v_type = VAR_NUMBER;
3900 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902 }
3903
3904 return OK;
3905}
3906
3907/*
3908 * Handle second level expression:
3909 * expr3 && expr3 && expr3 logical AND
3910 *
3911 * "arg" must point to the first non-white of the expression.
3912 * "arg" is advanced to the next non-white after the recognized expression.
3913 *
3914 * Return OK or FAIL.
3915 */
3916 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003917eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 int evaluate;
3921{
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 long result;
3924 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 /*
3928 * Get the first variable.
3929 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 return FAIL;
3932
3933 /*
3934 * Repeat until there is no following "&&".
3935 */
3936 first = TRUE;
3937 result = TRUE;
3938 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3939 {
3940 if (evaluate && first)
3941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003942 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 if (error)
3946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 first = FALSE;
3948 }
3949
3950 /*
3951 * Get the second variable.
3952 */
3953 *arg = skipwhite(*arg + 2);
3954 if (eval4(arg, &var2, evaluate && result) == FAIL)
3955 return FAIL;
3956
3957 /*
3958 * Compute the result.
3959 */
3960 if (evaluate && result)
3961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003962 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003964 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003965 if (error)
3966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968 if (evaluate)
3969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003970 rettv->v_type = VAR_NUMBER;
3971 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 }
3974
3975 return OK;
3976}
3977
3978/*
3979 * Handle third level expression:
3980 * var1 == var2
3981 * var1 =~ var2
3982 * var1 != var2
3983 * var1 !~ var2
3984 * var1 > var2
3985 * var1 >= var2
3986 * var1 < var2
3987 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003988 * var1 is var2
3989 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 *
3991 * "arg" must point to the first non-white of the expression.
3992 * "arg" is advanced to the next non-white after the recognized expression.
3993 *
3994 * Return OK or FAIL.
3995 */
3996 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 int evaluate;
4001{
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *p;
4004 int i;
4005 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004006 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 int len = 2;
4008 long n1, n2;
4009 char_u *s1, *s2;
4010 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4011 regmatch_T regmatch;
4012 int ic;
4013 char_u *save_cpo;
4014
4015 /*
4016 * Get the first variable.
4017 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return FAIL;
4020
4021 p = *arg;
4022 switch (p[0])
4023 {
4024 case '=': if (p[1] == '=')
4025 type = TYPE_EQUAL;
4026 else if (p[1] == '~')
4027 type = TYPE_MATCH;
4028 break;
4029 case '!': if (p[1] == '=')
4030 type = TYPE_NEQUAL;
4031 else if (p[1] == '~')
4032 type = TYPE_NOMATCH;
4033 break;
4034 case '>': if (p[1] != '=')
4035 {
4036 type = TYPE_GREATER;
4037 len = 1;
4038 }
4039 else
4040 type = TYPE_GEQUAL;
4041 break;
4042 case '<': if (p[1] != '=')
4043 {
4044 type = TYPE_SMALLER;
4045 len = 1;
4046 }
4047 else
4048 type = TYPE_SEQUAL;
4049 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004050 case 'i': if (p[1] == 's')
4051 {
4052 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4053 len = 5;
4054 if (!vim_isIDc(p[len]))
4055 {
4056 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4057 type_is = TRUE;
4058 }
4059 }
4060 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062
4063 /*
4064 * If there is a comparitive operator, use it.
4065 */
4066 if (type != TYPE_UNKNOWN)
4067 {
4068 /* extra question mark appended: ignore case */
4069 if (p[len] == '?')
4070 {
4071 ic = TRUE;
4072 ++len;
4073 }
4074 /* extra '#' appended: match case */
4075 else if (p[len] == '#')
4076 {
4077 ic = FALSE;
4078 ++len;
4079 }
4080 /* nothing appened: use 'ignorecase' */
4081 else
4082 ic = p_ic;
4083
4084 /*
4085 * Get the second variable.
4086 */
4087 *arg = skipwhite(p + len);
4088 if (eval5(arg, &var2, evaluate) == FAIL)
4089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 return FAIL;
4092 }
4093
4094 if (evaluate)
4095 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004096 if (type_is && rettv->v_type != var2.v_type)
4097 {
4098 /* For "is" a different type always means FALSE, for "notis"
4099 * it means TRUE. */
4100 n1 = (type == TYPE_NEQUAL);
4101 }
4102 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4103 {
4104 if (type_is)
4105 {
4106 n1 = (rettv->v_type == var2.v_type
4107 && rettv->vval.v_list == var2.vval.v_list);
4108 if (type == TYPE_NEQUAL)
4109 n1 = !n1;
4110 }
4111 else if (rettv->v_type != var2.v_type
4112 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4113 {
4114 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004115 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004116 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004117 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004118 clear_tv(rettv);
4119 clear_tv(&var2);
4120 return FAIL;
4121 }
4122 else
4123 {
4124 /* Compare two Lists for being equal or unequal. */
4125 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4126 if (type == TYPE_NEQUAL)
4127 n1 = !n1;
4128 }
4129 }
4130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004131 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4132 {
4133 if (type_is)
4134 {
4135 n1 = (rettv->v_type == var2.v_type
4136 && rettv->vval.v_dict == var2.vval.v_dict);
4137 if (type == TYPE_NEQUAL)
4138 n1 = !n1;
4139 }
4140 else if (rettv->v_type != var2.v_type
4141 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4142 {
4143 if (rettv->v_type != var2.v_type)
4144 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4145 else
4146 EMSG(_("E736: Invalid operation for Dictionary"));
4147 clear_tv(rettv);
4148 clear_tv(&var2);
4149 return FAIL;
4150 }
4151 else
4152 {
4153 /* Compare two Dictionaries for being equal or unequal. */
4154 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4155 if (type == TYPE_NEQUAL)
4156 n1 = !n1;
4157 }
4158 }
4159
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004160 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4161 {
4162 if (rettv->v_type != var2.v_type
4163 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4164 {
4165 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004168 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004169 clear_tv(rettv);
4170 clear_tv(&var2);
4171 return FAIL;
4172 }
4173 else
4174 {
4175 /* Compare two Funcrefs for being equal or unequal. */
4176 if (rettv->vval.v_string == NULL
4177 || var2.vval.v_string == NULL)
4178 n1 = FALSE;
4179 else
4180 n1 = STRCMP(rettv->vval.v_string,
4181 var2.vval.v_string) == 0;
4182 if (type == TYPE_NEQUAL)
4183 n1 = !n1;
4184 }
4185 }
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 /*
4188 * If one of the two variables is a number, compare as a number.
4189 * When using "=~" or "!~", always compare as string.
4190 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004191 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 n1 = get_tv_number(rettv);
4195 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 switch (type)
4197 {
4198 case TYPE_EQUAL: n1 = (n1 == n2); break;
4199 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4200 case TYPE_GREATER: n1 = (n1 > n2); break;
4201 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4202 case TYPE_SMALLER: n1 = (n1 < n2); break;
4203 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4204 case TYPE_UNKNOWN:
4205 case TYPE_MATCH:
4206 case TYPE_NOMATCH: break; /* avoid gcc warning */
4207 }
4208 }
4209 else
4210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 s1 = get_tv_string_buf(rettv, buf1);
4212 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4214 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4215 else
4216 i = 0;
4217 n1 = FALSE;
4218 switch (type)
4219 {
4220 case TYPE_EQUAL: n1 = (i == 0); break;
4221 case TYPE_NEQUAL: n1 = (i != 0); break;
4222 case TYPE_GREATER: n1 = (i > 0); break;
4223 case TYPE_GEQUAL: n1 = (i >= 0); break;
4224 case TYPE_SMALLER: n1 = (i < 0); break;
4225 case TYPE_SEQUAL: n1 = (i <= 0); break;
4226
4227 case TYPE_MATCH:
4228 case TYPE_NOMATCH:
4229 /* avoid 'l' flag in 'cpoptions' */
4230 save_cpo = p_cpo;
4231 p_cpo = (char_u *)"";
4232 regmatch.regprog = vim_regcomp(s2,
4233 RE_MAGIC + RE_STRING);
4234 regmatch.rm_ic = ic;
4235 if (regmatch.regprog != NULL)
4236 {
4237 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4238 vim_free(regmatch.regprog);
4239 if (type == TYPE_NOMATCH)
4240 n1 = !n1;
4241 }
4242 p_cpo = save_cpo;
4243 break;
4244
4245 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4246 }
4247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 clear_tv(rettv);
4249 clear_tv(&var2);
4250 rettv->v_type = VAR_NUMBER;
4251 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253 }
4254
4255 return OK;
4256}
4257
4258/*
4259 * Handle fourth level expression:
4260 * + number addition
4261 * - number subtraction
4262 * . string concatenation
4263 *
4264 * "arg" must point to the first non-white of the expression.
4265 * "arg" is advanced to the next non-white after the recognized expression.
4266 *
4267 * Return OK or FAIL.
4268 */
4269 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 int evaluate;
4274{
Bram Moolenaar33570922005-01-25 22:26:29 +00004275 typval_T var2;
4276 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 int op;
4278 long n1, n2;
4279 char_u *s1, *s2;
4280 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4281 char_u *p;
4282
4283 /*
4284 * Get the first variable.
4285 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 return FAIL;
4288
4289 /*
4290 * Repeat computing, until no '+', '-' or '.' is following.
4291 */
4292 for (;;)
4293 {
4294 op = **arg;
4295 if (op != '+' && op != '-' && op != '.')
4296 break;
4297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 if (op != '+' || rettv->v_type != VAR_LIST)
4299 {
4300 /* For "list + ...", an illegal use of the first operand as
4301 * a number cannot be determined before evaluating the 2nd
4302 * operand: if this is also a list, all is ok.
4303 * For "something . ...", "something - ..." or "non-list + ...",
4304 * we know that the first operand needs to be a string or number
4305 * without evaluating the 2nd operand. So check before to avoid
4306 * side effects after an error. */
4307 if (evaluate && get_tv_string_chk(rettv) == NULL)
4308 {
4309 clear_tv(rettv);
4310 return FAIL;
4311 }
4312 }
4313
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 /*
4315 * Get the second variable.
4316 */
4317 *arg = skipwhite(*arg + 1);
4318 if (eval6(arg, &var2, evaluate) == FAIL)
4319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 return FAIL;
4322 }
4323
4324 if (evaluate)
4325 {
4326 /*
4327 * Compute the result.
4328 */
4329 if (op == '.')
4330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4332 s2 = get_tv_string_buf_chk(&var2, buf2);
4333 if (s2 == NULL) /* type error ? */
4334 {
4335 clear_tv(rettv);
4336 clear_tv(&var2);
4337 return FAIL;
4338 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004339 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
4341 rettv->v_type = VAR_STRING;
4342 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004344 else if (op == '+' && rettv->v_type == VAR_LIST
4345 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346 {
4347 /* concatenate Lists */
4348 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4349 &var3) == FAIL)
4350 {
4351 clear_tv(rettv);
4352 clear_tv(&var2);
4353 return FAIL;
4354 }
4355 clear_tv(rettv);
4356 *rettv = var3;
4357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 else
4359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 int error = FALSE;
4361
4362 n1 = get_tv_number_chk(rettv, &error);
4363 if (error)
4364 {
4365 /* This can only happen for "list + non-list".
4366 * For "non-list + ..." or "something - ...", we returned
4367 * before evaluating the 2nd operand. */
4368 clear_tv(rettv);
4369 return FAIL;
4370 }
4371 n2 = get_tv_number_chk(&var2, &error);
4372 if (error)
4373 {
4374 clear_tv(rettv);
4375 clear_tv(&var2);
4376 return FAIL;
4377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 if (op == '+')
4380 n1 = n1 + n2;
4381 else
4382 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 rettv->v_type = VAR_NUMBER;
4384 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
4388 }
4389 return OK;
4390}
4391
4392/*
4393 * Handle fifth level expression:
4394 * * number multiplication
4395 * / number division
4396 * % number modulo
4397 *
4398 * "arg" must point to the first non-white of the expression.
4399 * "arg" is advanced to the next non-white after the recognized expression.
4400 *
4401 * Return OK or FAIL.
4402 */
4403 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 int evaluate;
4408{
Bram Moolenaar33570922005-01-25 22:26:29 +00004409 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 int op;
4411 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 /*
4415 * Get the first variable.
4416 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 return FAIL;
4419
4420 /*
4421 * Repeat computing, until no '*', '/' or '%' is following.
4422 */
4423 for (;;)
4424 {
4425 op = **arg;
4426 if (op != '*' && op != '/' && op != '%')
4427 break;
4428
4429 if (evaluate)
4430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004431 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004433 if (error)
4434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436 else
4437 n1 = 0;
4438
4439 /*
4440 * Get the second variable.
4441 */
4442 *arg = skipwhite(*arg + 1);
4443 if (eval7(arg, &var2, evaluate) == FAIL)
4444 return FAIL;
4445
4446 if (evaluate)
4447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004448 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004450 if (error)
4451 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453 /*
4454 * Compute the result.
4455 */
4456 if (op == '*')
4457 n1 = n1 * n2;
4458 else if (op == '/')
4459 {
4460 if (n2 == 0) /* give an error message? */
4461 n1 = 0x7fffffffL;
4462 else
4463 n1 = n1 / n2;
4464 }
4465 else
4466 {
4467 if (n2 == 0) /* give an error message? */
4468 n1 = 0;
4469 else
4470 n1 = n1 % n2;
4471 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004472 rettv->v_type = VAR_NUMBER;
4473 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 }
4476
4477 return OK;
4478}
4479
4480/*
4481 * Handle sixth level expression:
4482 * number number constant
4483 * "string" string contstant
4484 * 'string' literal string contstant
4485 * &option-name option value
4486 * @r register contents
4487 * identifier variable value
4488 * function() function call
4489 * $VAR environment variable
4490 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004491 * [expr, expr] List
4492 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 *
4494 * Also handle:
4495 * ! in front logical NOT
4496 * - in front unary minus
4497 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498 * trailing [] subscript in String or List
4499 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 *
4501 * "arg" must point to the first non-white of the expression.
4502 * "arg" is advanced to the next non-white after the recognized expression.
4503 *
4504 * Return OK or FAIL.
4505 */
4506 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004507eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 int evaluate;
4511{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 long n;
4513 int len;
4514 char_u *s;
4515 int val;
4516 char_u *start_leader, *end_leader;
4517 int ret = OK;
4518 char_u *alias;
4519
4520 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004524 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
4526 /*
4527 * Skip '!' and '-' characters. They are handled later.
4528 */
4529 start_leader = *arg;
4530 while (**arg == '!' || **arg == '-' || **arg == '+')
4531 *arg = skipwhite(*arg + 1);
4532 end_leader = *arg;
4533
4534 switch (**arg)
4535 {
4536 /*
4537 * Number constant.
4538 */
4539 case '0':
4540 case '1':
4541 case '2':
4542 case '3':
4543 case '4':
4544 case '5':
4545 case '6':
4546 case '7':
4547 case '8':
4548 case '9':
4549 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4550 *arg += len;
4551 if (evaluate)
4552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004553 rettv->v_type = VAR_NUMBER;
4554 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556 break;
4557
4558 /*
4559 * String constant: "string".
4560 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 break;
4563
4564 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004567 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004568 break;
4569
4570 /*
4571 * List: [expr, expr]
4572 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 break;
4575
4576 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 * Dictionary: {key: val, key: val}
4578 */
4579 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4580 break;
4581
4582 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004583 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004585 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 break;
4587
4588 /*
4589 * Environment variable: $VAR.
4590 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 break;
4593
4594 /*
4595 * Register contents: @r.
4596 */
4597 case '@': ++*arg;
4598 if (evaluate)
4599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004601 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 if (**arg != NUL)
4604 ++*arg;
4605 break;
4606
4607 /*
4608 * nested expression: (expression).
4609 */
4610 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (**arg == ')')
4613 ++*arg;
4614 else if (ret == OK)
4615 {
4616 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 ret = FAIL;
4619 }
4620 break;
4621
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004625
4626 if (ret == NOTDONE)
4627 {
4628 /*
4629 * Must be a variable or function name.
4630 * Can also be a curly-braces kind of name: {expr}.
4631 */
4632 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004633 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634 if (alias != NULL)
4635 s = alias;
4636
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004637 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 ret = FAIL;
4639 else
4640 {
4641 if (**arg == '(') /* recursive! */
4642 {
4643 /* If "s" is the name of a variable of type VAR_FUNC
4644 * use its contents. */
4645 s = deref_func_name(s, &len);
4646
4647 /* Invoke the function. */
4648 ret = get_func_tv(s, len, rettv, arg,
4649 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004650 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651 /* Stop the expression evaluation when immediately
4652 * aborting on error, or when an interrupt occurred or
4653 * an exception was thrown but not caught. */
4654 if (aborting())
4655 {
4656 if (ret == OK)
4657 clear_tv(rettv);
4658 ret = FAIL;
4659 }
4660 }
4661 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004662 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004663 else
4664 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 }
4666
4667 if (alias != NULL)
4668 vim_free(alias);
4669 }
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 *arg = skipwhite(*arg);
4672
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004673 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4674 * expr(expr). */
4675 if (ret == OK)
4676 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 /*
4679 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4680 */
4681 if (ret == OK && evaluate && end_leader > start_leader)
4682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 int error = FALSE;
4684
4685 val = get_tv_number_chk(rettv, &error);
4686 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 clear_tv(rettv);
4689 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 else
4692 {
4693 while (end_leader > start_leader)
4694 {
4695 --end_leader;
4696 if (*end_leader == '!')
4697 val = !val;
4698 else if (*end_leader == '-')
4699 val = -val;
4700 }
4701 clear_tv(rettv);
4702 rettv->v_type = VAR_NUMBER;
4703 rettv->vval.v_number = val;
4704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 }
4706
4707 return ret;
4708}
4709
4710/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004711 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4712 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4714 */
4715 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004716eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004718 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004720 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721{
4722 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004723 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725 long len = -1;
4726 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004728 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004732 if (verbose)
4733 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 return FAIL;
4735 }
4736
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 /*
4740 * dict.name
4741 */
4742 key = *arg + 1;
4743 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4744 ;
4745 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004747 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 }
4749 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 /*
4752 * something[idx]
4753 *
4754 * Get the (first) variable from inside the [].
4755 */
4756 *arg = skipwhite(*arg + 1);
4757 if (**arg == ':')
4758 empty1 = TRUE;
4759 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4760 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4762 {
4763 /* not a number or string */
4764 clear_tv(&var1);
4765 return FAIL;
4766 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767
4768 /*
4769 * Get the second variable from inside the [:].
4770 */
4771 if (**arg == ':')
4772 {
4773 range = TRUE;
4774 *arg = skipwhite(*arg + 1);
4775 if (**arg == ']')
4776 empty2 = TRUE;
4777 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 if (!empty1)
4780 clear_tv(&var1);
4781 return FAIL;
4782 }
4783 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4784 {
4785 /* not a number or string */
4786 if (!empty1)
4787 clear_tv(&var1);
4788 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 return FAIL;
4790 }
4791 }
4792
4793 /* Check for the ']'. */
4794 if (**arg != ']')
4795 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004796 if (verbose)
4797 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 clear_tv(&var1);
4799 if (range)
4800 clear_tv(&var2);
4801 return FAIL;
4802 }
4803 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 }
4805
4806 if (evaluate)
4807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004808 n1 = 0;
4809 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 n1 = get_tv_number(&var1);
4812 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 }
4814 if (range)
4815 {
4816 if (empty2)
4817 n2 = -1;
4818 else
4819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004820 n2 = get_tv_number(&var2);
4821 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 }
4823 }
4824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 {
4827 case VAR_NUMBER:
4828 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 len = (long)STRLEN(s);
4831 if (range)
4832 {
4833 /* The resulting variable is a substring. If the indexes
4834 * are out of range the result is empty. */
4835 if (n1 < 0)
4836 {
4837 n1 = len + n1;
4838 if (n1 < 0)
4839 n1 = 0;
4840 }
4841 if (n2 < 0)
4842 n2 = len + n2;
4843 else if (n2 >= len)
4844 n2 = len;
4845 if (n1 >= len || n2 < 0 || n1 > n2)
4846 s = NULL;
4847 else
4848 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4849 }
4850 else
4851 {
4852 /* The resulting variable is a string of a single
4853 * character. If the index is too big or negative the
4854 * result is empty. */
4855 if (n1 >= len || n1 < 0)
4856 s = NULL;
4857 else
4858 s = vim_strnsave(s + n1, 1);
4859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 clear_tv(rettv);
4861 rettv->v_type = VAR_STRING;
4862 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004863 break;
4864
4865 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004866 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004867 if (n1 < 0)
4868 n1 = len + n1;
4869 if (!empty1 && (n1 < 0 || n1 >= len))
4870 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004871 /* For a range we allow invalid values and return an empty
4872 * list. A list index out of range is an error. */
4873 if (!range)
4874 {
4875 if (verbose)
4876 EMSGN(_(e_listidx), n1);
4877 return FAIL;
4878 }
4879 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880 }
4881 if (range)
4882 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004883 list_T *l;
4884 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004885
4886 if (n2 < 0)
4887 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004888 else if (n2 >= len)
4889 n2 = len - 1;
4890 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004891 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 l = list_alloc();
4893 if (l == NULL)
4894 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004896 n1 <= n2; ++n1)
4897 {
4898 if (list_append_tv(l, &item->li_tv) == FAIL)
4899 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004900 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 return FAIL;
4902 }
4903 item = item->li_next;
4904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 clear_tv(rettv);
4906 rettv->v_type = VAR_LIST;
4907 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004908 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004909 }
4910 else
4911 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004912 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(rettv);
4914 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004915 }
4916 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004917
4918 case VAR_DICT:
4919 if (range)
4920 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004921 if (verbose)
4922 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 if (len == -1)
4924 clear_tv(&var1);
4925 return FAIL;
4926 }
4927 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004928 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004929
4930 if (len == -1)
4931 {
4932 key = get_tv_string(&var1);
4933 if (*key == NUL)
4934 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004935 if (verbose)
4936 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937 clear_tv(&var1);
4938 return FAIL;
4939 }
4940 }
4941
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004942 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004943
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004944 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004945 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004946 if (len == -1)
4947 clear_tv(&var1);
4948 if (item == NULL)
4949 return FAIL;
4950
4951 copy_tv(&item->di_tv, &var1);
4952 clear_tv(rettv);
4953 *rettv = var1;
4954 }
4955 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004956 }
4957 }
4958
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 return OK;
4960}
4961
4962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 * Get an option value.
4964 * "arg" points to the '&' or '+' before the option name.
4965 * "arg" is advanced to character after the option name.
4966 * Return OK or FAIL.
4967 */
4968 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004969get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004971 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 int evaluate;
4973{
4974 char_u *option_end;
4975 long numval;
4976 char_u *stringval;
4977 int opt_type;
4978 int c;
4979 int working = (**arg == '+'); /* has("+option") */
4980 int ret = OK;
4981 int opt_flags;
4982
4983 /*
4984 * Isolate the option name and find its value.
4985 */
4986 option_end = find_option_end(arg, &opt_flags);
4987 if (option_end == NULL)
4988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004989 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 EMSG2(_("E112: Option name missing: %s"), *arg);
4991 return FAIL;
4992 }
4993
4994 if (!evaluate)
4995 {
4996 *arg = option_end;
4997 return OK;
4998 }
4999
5000 c = *option_end;
5001 *option_end = NUL;
5002 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005003 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
5005 if (opt_type == -3) /* invalid name */
5006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 EMSG2(_("E113: Unknown option: %s"), *arg);
5009 ret = FAIL;
5010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005011 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 if (opt_type == -2) /* hidden string option */
5014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005015 rettv->v_type = VAR_STRING;
5016 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 else if (opt_type == -1) /* hidden number option */
5019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005020 rettv->v_type = VAR_NUMBER;
5021 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 else if (opt_type == 1) /* number option */
5024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 rettv->v_type = VAR_NUMBER;
5026 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 else /* string option */
5029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005030 rettv->v_type = VAR_STRING;
5031 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 }
5034 else if (working && (opt_type == -2 || opt_type == -1))
5035 ret = FAIL;
5036
5037 *option_end = c; /* put back for error messages */
5038 *arg = option_end;
5039
5040 return ret;
5041}
5042
5043/*
5044 * Allocate a variable for a string constant.
5045 * Return OK or FAIL.
5046 */
5047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005048get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 int evaluate;
5052{
5053 char_u *p;
5054 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 int extra = 0;
5056
5057 /*
5058 * Find the end of the string, skipping backslashed characters.
5059 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 if (*p == '\\' && p[1] != NUL)
5063 {
5064 ++p;
5065 /* A "\<x>" form occupies at least 4 characters, and produces up
5066 * to 6 characters: reserve space for 2 extra */
5067 if (*p == '<')
5068 extra += 2;
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071
5072 if (*p != '"')
5073 {
5074 EMSG2(_("E114: Missing quote: %s"), *arg);
5075 return FAIL;
5076 }
5077
5078 /* If only parsing, set *arg and return here */
5079 if (!evaluate)
5080 {
5081 *arg = p + 1;
5082 return OK;
5083 }
5084
5085 /*
5086 * Copy the string into allocated memory, handling backslashed
5087 * characters.
5088 */
5089 name = alloc((unsigned)(p - *arg + extra));
5090 if (name == NULL)
5091 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 rettv->v_type = VAR_STRING;
5093 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 if (*p == '\\')
5098 {
5099 switch (*++p)
5100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 case 'b': *name++ = BS; ++p; break;
5102 case 'e': *name++ = ESC; ++p; break;
5103 case 'f': *name++ = FF; ++p; break;
5104 case 'n': *name++ = NL; ++p; break;
5105 case 'r': *name++ = CAR; ++p; break;
5106 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 case 'X': /* hex: "\x1", "\x12" */
5109 case 'x':
5110 case 'u': /* Unicode: "\u0023" */
5111 case 'U':
5112 if (vim_isxdigit(p[1]))
5113 {
5114 int n, nr;
5115 int c = toupper(*p);
5116
5117 if (c == 'X')
5118 n = 2;
5119 else
5120 n = 4;
5121 nr = 0;
5122 while (--n >= 0 && vim_isxdigit(p[1]))
5123 {
5124 ++p;
5125 nr = (nr << 4) + hex2nr(*p);
5126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128#ifdef FEAT_MBYTE
5129 /* For "\u" store the number according to
5130 * 'encoding'. */
5131 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 else
5134#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 break;
5138
5139 /* octal: "\1", "\12", "\123" */
5140 case '0':
5141 case '1':
5142 case '2':
5143 case '3':
5144 case '4':
5145 case '5':
5146 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 case '7': *name = *p++ - '0';
5148 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 *name = (*name << 3) + *p++ - '0';
5151 if (*p >= '0' && *p <= '7')
5152 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 break;
5156
5157 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (extra != 0)
5160 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163 }
5164 /* FALLTHROUGH */
5165
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168 }
5169 }
5170 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 *arg = p + 1;
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 return OK;
5178}
5179
5180/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 * Return OK or FAIL.
5183 */
5184 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 int evaluate;
5189{
5190 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005191 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005192 int reduce = 0;
5193
5194 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005196 */
5197 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5198 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005200 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005202 break;
5203 ++reduce;
5204 ++p;
5205 }
5206 }
5207
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005209 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005211 return FAIL;
5212 }
5213
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005215 if (!evaluate)
5216 {
5217 *arg = p + 1;
5218 return OK;
5219 }
5220
5221 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005223 */
5224 str = alloc((unsigned)((p - *arg) - reduce));
5225 if (str == NULL)
5226 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 rettv->v_type = VAR_STRING;
5228 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005229
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005231 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005235 break;
5236 ++p;
5237 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 *arg = p + 1;
5242
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 return OK;
5244}
5245
5246/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 * Allocate a variable for a List and fill it from "*arg".
5248 * Return OK or FAIL.
5249 */
5250 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005251get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005253 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 int evaluate;
5255{
Bram Moolenaar33570922005-01-25 22:26:29 +00005256 list_T *l = NULL;
5257 typval_T tv;
5258 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259
5260 if (evaluate)
5261 {
5262 l = list_alloc();
5263 if (l == NULL)
5264 return FAIL;
5265 }
5266
5267 *arg = skipwhite(*arg + 1);
5268 while (**arg != ']' && **arg != NUL)
5269 {
5270 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5271 goto failret;
5272 if (evaluate)
5273 {
5274 item = listitem_alloc();
5275 if (item != NULL)
5276 {
5277 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005278 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 list_append(l, item);
5280 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005281 else
5282 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 }
5284
5285 if (**arg == ']')
5286 break;
5287 if (**arg != ',')
5288 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 goto failret;
5291 }
5292 *arg = skipwhite(*arg + 1);
5293 }
5294
5295 if (**arg != ']')
5296 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298failret:
5299 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005300 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 return FAIL;
5302 }
5303
5304 *arg = skipwhite(*arg + 1);
5305 if (evaluate)
5306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005307 rettv->v_type = VAR_LIST;
5308 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 ++l->lv_refcount;
5310 }
5311
5312 return OK;
5313}
5314
5315/*
5316 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005317 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005319 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320list_alloc()
5321{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005322 list_T *l;
5323
5324 l = (list_T *)alloc_clear(sizeof(list_T));
5325 if (l != NULL)
5326 {
5327 /* Prepend the list to the list of lists for garbage collection. */
5328 if (first_list != NULL)
5329 first_list->lv_used_prev = l;
5330 l->lv_used_prev = NULL;
5331 l->lv_used_next = first_list;
5332 first_list = l;
5333 }
5334 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335}
5336
5337/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005338 * Allocate an empty list for a return value.
5339 * Returns OK or FAIL.
5340 */
5341 static int
5342rettv_list_alloc(rettv)
5343 typval_T *rettv;
5344{
5345 list_T *l = list_alloc();
5346
5347 if (l == NULL)
5348 return FAIL;
5349
5350 rettv->vval.v_list = l;
5351 rettv->v_type = VAR_LIST;
5352 ++l->lv_refcount;
5353 return OK;
5354}
5355
5356/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 * Unreference a list: decrement the reference count and free it when it
5358 * becomes zero.
5359 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005360 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005362 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005364 if (l != NULL && --l->lv_refcount <= 0)
5365 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366}
5367
5368/*
5369 * Free a list, including all items it points to.
5370 * Ignores the reference count.
5371 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005372 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005373list_free(l, recurse)
5374 list_T *l;
5375 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376{
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005379 /* Remove the list from the list of lists for garbage collection. */
5380 if (l->lv_used_prev == NULL)
5381 first_list = l->lv_used_next;
5382 else
5383 l->lv_used_prev->lv_used_next = l->lv_used_next;
5384 if (l->lv_used_next != NULL)
5385 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5386
Bram Moolenaard9fba312005-06-26 22:34:35 +00005387 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005389 /* Remove the item before deleting it. */
5390 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005391 if (recurse || (item->li_tv.v_type != VAR_LIST
5392 && item->li_tv.v_type != VAR_DICT))
5393 clear_tv(&item->li_tv);
5394 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 }
5396 vim_free(l);
5397}
5398
5399/*
5400 * Allocate a list item.
5401 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403listitem_alloc()
5404{
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406}
5407
5408/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005409 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 */
5411 static void
5412listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 vim_free(item);
5417}
5418
5419/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005420 * Remove a list item from a List and free it. Also clears the value.
5421 */
5422 static void
5423listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005424 list_T *l;
5425 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005426{
5427 list_remove(l, item, item);
5428 listitem_free(item);
5429}
5430
5431/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 * Get the number of items in a list.
5433 */
5434 static long
5435list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 if (l == NULL)
5439 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441}
5442
5443/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005444 * Return TRUE when two lists have exactly the same values.
5445 */
5446 static int
5447list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005448 list_T *l1;
5449 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005450 int ic; /* ignore case for strings */
5451{
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005453
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005454 if (l1 == l2)
5455 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005456 if (list_len(l1) != list_len(l2))
5457 return FALSE;
5458
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005459 for (item1 = l1->lv_first, item2 = l2->lv_first;
5460 item1 != NULL && item2 != NULL;
5461 item1 = item1->li_next, item2 = item2->li_next)
5462 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5463 return FALSE;
5464 return item1 == NULL && item2 == NULL;
5465}
5466
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005467#if defined(FEAT_PYTHON) || defined(PROTO)
5468/*
5469 * Return the dictitem that an entry in a hashtable points to.
5470 */
5471 dictitem_T *
5472dict_lookup(hi)
5473 hashitem_T *hi;
5474{
5475 return HI2DI(hi);
5476}
5477#endif
5478
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005479/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005480 * Return TRUE when two dictionaries have exactly the same key/values.
5481 */
5482 static int
5483dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005484 dict_T *d1;
5485 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005486 int ic; /* ignore case for strings */
5487{
Bram Moolenaar33570922005-01-25 22:26:29 +00005488 hashitem_T *hi;
5489 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005490 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005491
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005492 if (d1 == d2)
5493 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005494 if (dict_len(d1) != dict_len(d2))
5495 return FALSE;
5496
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005497 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005500 if (!HASHITEM_EMPTY(hi))
5501 {
5502 item2 = dict_find(d2, hi->hi_key, -1);
5503 if (item2 == NULL)
5504 return FALSE;
5505 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5506 return FALSE;
5507 --todo;
5508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 }
5510 return TRUE;
5511}
5512
5513/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005514 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005515 * Compares the items just like "==" would compare them, but strings and
5516 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005517 */
5518 static int
5519tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 typval_T *tv1;
5521 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005522 int ic; /* ignore case */
5523{
5524 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005525 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005526 static int recursive = 0; /* cach recursive loops */
5527 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005528
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005529 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005530 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005531 /* Catch lists and dicts that have an endless loop by limiting
5532 * recursiveness to 1000. We guess they are equal then. */
5533 if (recursive >= 1000)
5534 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005535
5536 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005537 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005538 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005539 ++recursive;
5540 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5541 --recursive;
5542 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005543
5544 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005545 ++recursive;
5546 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5547 --recursive;
5548 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005549
5550 case VAR_FUNC:
5551 return (tv1->vval.v_string != NULL
5552 && tv2->vval.v_string != NULL
5553 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5554
5555 case VAR_NUMBER:
5556 return tv1->vval.v_number == tv2->vval.v_number;
5557
5558 case VAR_STRING:
5559 s1 = get_tv_string_buf(tv1, buf1);
5560 s2 = get_tv_string_buf(tv2, buf2);
5561 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005562 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005563
5564 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565 return TRUE;
5566}
5567
5568/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 * Locate item with index "n" in list "l" and return it.
5570 * A negative index is counted from the end; -1 is the last item.
5571 * Returns NULL when "n" is out of range.
5572 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005575 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576 long n;
5577{
Bram Moolenaar33570922005-01-25 22:26:29 +00005578 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 long idx;
5580
5581 if (l == NULL)
5582 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005583
5584 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005586 n = l->lv_len + n;
5587
5588 /* Check for index out of range. */
5589 if (n < 0 || n >= l->lv_len)
5590 return NULL;
5591
5592 /* When there is a cached index may start search from there. */
5593 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005595 if (n < l->lv_idx / 2)
5596 {
5597 /* closest to the start of the list */
5598 item = l->lv_first;
5599 idx = 0;
5600 }
5601 else if (n > (l->lv_idx + l->lv_len) / 2)
5602 {
5603 /* closest to the end of the list */
5604 item = l->lv_last;
5605 idx = l->lv_len - 1;
5606 }
5607 else
5608 {
5609 /* closest to the cached index */
5610 item = l->lv_idx_item;
5611 idx = l->lv_idx;
5612 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 }
5614 else
5615 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005616 if (n < l->lv_len / 2)
5617 {
5618 /* closest to the start of the list */
5619 item = l->lv_first;
5620 idx = 0;
5621 }
5622 else
5623 {
5624 /* closest to the end of the list */
5625 item = l->lv_last;
5626 idx = l->lv_len - 1;
5627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005629
5630 while (n > idx)
5631 {
5632 /* search forward */
5633 item = item->li_next;
5634 ++idx;
5635 }
5636 while (n < idx)
5637 {
5638 /* search backward */
5639 item = item->li_prev;
5640 --idx;
5641 }
5642
5643 /* cache the used index */
5644 l->lv_idx = idx;
5645 l->lv_idx_item = item;
5646
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 return item;
5648}
5649
5650/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005651 * Get list item "l[idx]" as a number.
5652 */
5653 static long
5654list_find_nr(l, idx, errorp)
5655 list_T *l;
5656 long idx;
5657 int *errorp; /* set to TRUE when something wrong */
5658{
5659 listitem_T *li;
5660
5661 li = list_find(l, idx);
5662 if (li == NULL)
5663 {
5664 if (errorp != NULL)
5665 *errorp = TRUE;
5666 return -1L;
5667 }
5668 return get_tv_number_chk(&li->li_tv, errorp);
5669}
5670
5671/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005672 * Locate "item" list "l" and return its index.
5673 * Returns -1 when "item" is not in the list.
5674 */
5675 static long
5676list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005677 list_T *l;
5678 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005679{
5680 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005681 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005682
5683 if (l == NULL)
5684 return -1;
5685 idx = 0;
5686 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5687 ++idx;
5688 if (li == NULL)
5689 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005690 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005691}
5692
5693/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 * Append item "item" to the end of list "l".
5695 */
5696 static void
5697list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005698 list_T *l;
5699 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700{
5701 if (l->lv_last == NULL)
5702 {
5703 /* empty list */
5704 l->lv_first = item;
5705 l->lv_last = item;
5706 item->li_prev = NULL;
5707 }
5708 else
5709 {
5710 l->lv_last->li_next = item;
5711 item->li_prev = l->lv_last;
5712 l->lv_last = item;
5713 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005714 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 item->li_next = NULL;
5716}
5717
5718/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005719 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005720 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 */
5722 static int
5723list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 list_T *l;
5725 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005727 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728
Bram Moolenaar05159a02005-02-26 23:04:13 +00005729 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005731 copy_tv(tv, &li->li_tv);
5732 list_append(l, li);
5733 return OK;
5734}
5735
5736/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005737 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005738 * Return FAIL when out of memory.
5739 */
5740 int
5741list_append_dict(list, dict)
5742 list_T *list;
5743 dict_T *dict;
5744{
5745 listitem_T *li = listitem_alloc();
5746
5747 if (li == NULL)
5748 return FAIL;
5749 li->li_tv.v_type = VAR_DICT;
5750 li->li_tv.v_lock = 0;
5751 li->li_tv.vval.v_dict = dict;
5752 list_append(list, li);
5753 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 return OK;
5755}
5756
5757/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005758 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005759 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005760 * Returns FAIL when out of memory.
5761 */
5762 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005763list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005764 list_T *l;
5765 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005766 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005767{
5768 listitem_T *li = listitem_alloc();
5769
5770 if (li == NULL)
5771 return FAIL;
5772 list_append(l, li);
5773 li->li_tv.v_type = VAR_STRING;
5774 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005775 if (str == NULL)
5776 li->li_tv.vval.v_string = NULL;
5777 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005778 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005779 return FAIL;
5780 return OK;
5781}
5782
5783/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005784 * Append "n" to list "l".
5785 * Returns FAIL when out of memory.
5786 */
5787 static int
5788list_append_number(l, n)
5789 list_T *l;
5790 varnumber_T n;
5791{
5792 listitem_T *li;
5793
5794 li = listitem_alloc();
5795 if (li == NULL)
5796 return FAIL;
5797 li->li_tv.v_type = VAR_NUMBER;
5798 li->li_tv.v_lock = 0;
5799 li->li_tv.vval.v_number = n;
5800 list_append(l, li);
5801 return OK;
5802}
5803
5804/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005806 * If "item" is NULL append at the end.
5807 * Return FAIL when out of memory.
5808 */
5809 static int
5810list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005811 list_T *l;
5812 typval_T *tv;
5813 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005814{
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005816
5817 if (ni == NULL)
5818 return FAIL;
5819 copy_tv(tv, &ni->li_tv);
5820 if (item == NULL)
5821 /* Append new item at end of list. */
5822 list_append(l, ni);
5823 else
5824 {
5825 /* Insert new item before existing item. */
5826 ni->li_prev = item->li_prev;
5827 ni->li_next = item;
5828 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005829 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005830 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005831 ++l->lv_idx;
5832 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005833 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005834 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005835 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005836 l->lv_idx_item = NULL;
5837 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005838 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005839 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840 }
5841 return OK;
5842}
5843
5844/*
5845 * Extend "l1" with "l2".
5846 * If "bef" is NULL append at the end, otherwise insert before this item.
5847 * Returns FAIL when out of memory.
5848 */
5849 static int
5850list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005851 list_T *l1;
5852 list_T *l2;
5853 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005854{
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005856
5857 for (item = l2->lv_first; item != NULL; item = item->li_next)
5858 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5859 return FAIL;
5860 return OK;
5861}
5862
5863/*
5864 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5865 * Return FAIL when out of memory.
5866 */
5867 static int
5868list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005869 list_T *l1;
5870 list_T *l2;
5871 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005872{
Bram Moolenaar33570922005-01-25 22:26:29 +00005873 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005874
5875 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005876 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005877 if (l == NULL)
5878 return FAIL;
5879 tv->v_type = VAR_LIST;
5880 tv->vval.v_list = l;
5881
5882 /* append all items from the second list */
5883 return list_extend(l, l2, NULL);
5884}
5885
5886/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005887 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005889 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 * Returns NULL when out of memory.
5891 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005892 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005893list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005896 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897{
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 list_T *copy;
5899 listitem_T *item;
5900 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901
5902 if (orig == NULL)
5903 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904
5905 copy = list_alloc();
5906 if (copy != NULL)
5907 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005908 if (copyID != 0)
5909 {
5910 /* Do this before adding the items, because one of the items may
5911 * refer back to this list. */
5912 orig->lv_copyID = copyID;
5913 orig->lv_copylist = copy;
5914 }
5915 for (item = orig->lv_first; item != NULL && !got_int;
5916 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 {
5918 ni = listitem_alloc();
5919 if (ni == NULL)
5920 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005921 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 {
5923 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5924 {
5925 vim_free(ni);
5926 break;
5927 }
5928 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005930 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 list_append(copy, ni);
5932 }
5933 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005934 if (item != NULL)
5935 {
5936 list_unref(copy);
5937 copy = NULL;
5938 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 }
5940
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 return copy;
5942}
5943
5944/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005945 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005946 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005948 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005949list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 list_T *l;
5951 listitem_T *item;
5952 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005953{
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005956 /* notify watchers */
5957 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005959 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005960 list_fix_watch(l, ip);
5961 if (ip == item2)
5962 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964
5965 if (item2->li_next == NULL)
5966 l->lv_last = item->li_prev;
5967 else
5968 item2->li_next->li_prev = item->li_prev;
5969 if (item->li_prev == NULL)
5970 l->lv_first = item2->li_next;
5971 else
5972 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005973 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974}
5975
5976/*
5977 * Return an allocated string with the string representation of a list.
5978 * May return NULL.
5979 */
5980 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005981list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005982 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005983 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984{
5985 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986
5987 if (tv->vval.v_list == NULL)
5988 return NULL;
5989 ga_init2(&ga, (int)sizeof(char), 80);
5990 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005991 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005992 {
5993 vim_free(ga.ga_data);
5994 return NULL;
5995 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 ga_append(&ga, ']');
5997 ga_append(&ga, NUL);
5998 return (char_u *)ga.ga_data;
5999}
6000
6001/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006002 * Join list "l" into a string in "*gap", using separator "sep".
6003 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006004 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006005 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006006 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006008 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006010 char_u *sep;
6011 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006012 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013{
6014 int first = TRUE;
6015 char_u *tofree;
6016 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006018 char_u *s;
6019
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006020 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006021 {
6022 if (first)
6023 first = FALSE;
6024 else
6025 ga_concat(gap, sep);
6026
6027 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006028 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006029 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006030 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006031 if (s != NULL)
6032 ga_concat(gap, s);
6033 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006034 if (s == NULL)
6035 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006037 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006038}
6039
6040/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006041 * Garbage collection for lists and dictionaries.
6042 *
6043 * We use reference counts to be able to free most items right away when they
6044 * are no longer used. But for composite items it's possible that it becomes
6045 * unused while the reference count is > 0: When there is a recursive
6046 * reference. Example:
6047 * :let l = [1, 2, 3]
6048 * :let d = {9: l}
6049 * :let l[1] = d
6050 *
6051 * Since this is quite unusual we handle this with garbage collection: every
6052 * once in a while find out which lists and dicts are not referenced from any
6053 * variable.
6054 *
6055 * Here is a good reference text about garbage collection (refers to Python
6056 * but it applies to all reference-counting mechanisms):
6057 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006058 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006059
6060/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006061 * Do garbage collection for lists and dicts.
6062 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006063 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064 int
6065garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006066{
6067 dict_T *dd;
6068 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069 int copyID = ++current_copyID;
6070 buf_T *buf;
6071 win_T *wp;
6072 int i;
6073 funccall_T *fc;
6074 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006075#ifdef FEAT_WINDOWS
6076 tabpage_T *tp;
6077#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006078
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006079 /* Only do this once. */
6080 want_garbage_collect = FALSE;
6081 may_garbage_collect = FALSE;
6082
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006083 /*
6084 * 1. Go through all accessible variables and mark all lists and dicts
6085 * with copyID.
6086 */
6087 /* script-local variables */
6088 for (i = 1; i <= ga_scripts.ga_len; ++i)
6089 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6090
6091 /* buffer-local variables */
6092 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6093 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6094
6095 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006096 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006097 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6098
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006099#ifdef FEAT_WINDOWS
6100 /* tabpage-local variables */
6101 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6102 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6103#endif
6104
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006105 /* global variables */
6106 set_ref_in_ht(&globvarht, copyID);
6107
6108 /* function-local variables */
6109 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6110 {
6111 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6112 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6113 }
6114
6115 /*
6116 * 2. Go through the list of dicts and free items without the copyID.
6117 */
6118 for (dd = first_dict; dd != NULL; )
6119 if (dd->dv_copyID != copyID)
6120 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006121 /* Free the Dictionary and ordinary items it contains, but don't
6122 * recurse into Lists and Dictionaries, they will be in the list
6123 * of dicts or list of lists. */
6124 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006125 did_free = TRUE;
6126
6127 /* restart, next dict may also have been freed */
6128 dd = first_dict;
6129 }
6130 else
6131 dd = dd->dv_used_next;
6132
6133 /*
6134 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006135 * But don't free a list that has a watcher (used in a for loop), these
6136 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006137 */
6138 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006139 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006140 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006141 /* Free the List and ordinary items it contains, but don't recurse
6142 * into Lists and Dictionaries, they will be in the list of dicts
6143 * or list of lists. */
6144 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006145 did_free = TRUE;
6146
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006147 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006148 ll = first_list;
6149 }
6150 else
6151 ll = ll->lv_used_next;
6152
6153 return did_free;
6154}
6155
6156/*
6157 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6158 */
6159 static void
6160set_ref_in_ht(ht, copyID)
6161 hashtab_T *ht;
6162 int copyID;
6163{
6164 int todo;
6165 hashitem_T *hi;
6166
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006167 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006168 for (hi = ht->ht_array; todo > 0; ++hi)
6169 if (!HASHITEM_EMPTY(hi))
6170 {
6171 --todo;
6172 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6173 }
6174}
6175
6176/*
6177 * Mark all lists and dicts referenced through list "l" with "copyID".
6178 */
6179 static void
6180set_ref_in_list(l, copyID)
6181 list_T *l;
6182 int copyID;
6183{
6184 listitem_T *li;
6185
6186 for (li = l->lv_first; li != NULL; li = li->li_next)
6187 set_ref_in_item(&li->li_tv, copyID);
6188}
6189
6190/*
6191 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6192 */
6193 static void
6194set_ref_in_item(tv, copyID)
6195 typval_T *tv;
6196 int copyID;
6197{
6198 dict_T *dd;
6199 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006200
6201 switch (tv->v_type)
6202 {
6203 case VAR_DICT:
6204 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006205 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006206 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006207 /* Didn't see this dict yet. */
6208 dd->dv_copyID = copyID;
6209 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006210 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006211 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006212
6213 case VAR_LIST:
6214 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006215 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006216 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006217 /* Didn't see this list yet. */
6218 ll->lv_copyID = copyID;
6219 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006220 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006221 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006222 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006223 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006224}
6225
6226/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006227 * Allocate an empty header for a dictionary.
6228 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006229 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006230dict_alloc()
6231{
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006233
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006235 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006236 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006237 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006238 if (first_dict != NULL)
6239 first_dict->dv_used_prev = d;
6240 d->dv_used_next = first_dict;
6241 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006242 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006243
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006245 d->dv_lock = 0;
6246 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006247 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006248 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006250}
6251
6252/*
6253 * Unreference a Dictionary: decrement the reference count and free it when it
6254 * becomes zero.
6255 */
6256 static void
6257dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006260 if (d != NULL && --d->dv_refcount <= 0)
6261 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262}
6263
6264/*
6265 * Free a Dictionary, including all items it contains.
6266 * Ignores the reference count.
6267 */
6268 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006269dict_free(d, recurse)
6270 dict_T *d;
6271 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006272{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006273 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006275 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006277 /* Remove the dict from the list of dicts for garbage collection. */
6278 if (d->dv_used_prev == NULL)
6279 first_dict = d->dv_used_next;
6280 else
6281 d->dv_used_prev->dv_used_next = d->dv_used_next;
6282 if (d->dv_used_next != NULL)
6283 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6284
6285 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006286 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006287 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006290 if (!HASHITEM_EMPTY(hi))
6291 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006292 /* Remove the item before deleting it, just in case there is
6293 * something recursive causing trouble. */
6294 di = HI2DI(hi);
6295 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006296 if (recurse || (di->di_tv.v_type != VAR_LIST
6297 && di->di_tv.v_type != VAR_DICT))
6298 clear_tv(&di->di_tv);
6299 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300 --todo;
6301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006302 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006303 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304 vim_free(d);
6305}
6306
6307/*
6308 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006309 * The "key" is copied to the new item.
6310 * Note that the value of the item "di_tv" still needs to be initialized!
6311 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006314dictitem_alloc(key)
6315 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316{
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006318
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006319 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006323 di->di_flags = 0;
6324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006326}
6327
6328/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329 * Make a copy of a Dictionary item.
6330 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006332dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006334{
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006336
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006337 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6338 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006339 if (di != NULL)
6340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006341 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006342 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006343 copy_tv(&org->di_tv, &di->di_tv);
6344 }
6345 return di;
6346}
6347
6348/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006349 * Remove item "item" from Dictionary "dict" and free it.
6350 */
6351 static void
6352dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 dict_T *dict;
6354 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006355{
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006357
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006359 if (HASHITEM_EMPTY(hi))
6360 EMSG2(_(e_intern2), "dictitem_remove()");
6361 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006362 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363 dictitem_free(item);
6364}
6365
6366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006367 * Free a dict item. Also clears the value.
6368 */
6369 static void
6370dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006372{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 clear_tv(&item->di_tv);
6374 vim_free(item);
6375}
6376
6377/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6379 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006380 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006381 * Returns NULL when out of memory.
6382 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006384dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006387 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388{
Bram Moolenaar33570922005-01-25 22:26:29 +00006389 dict_T *copy;
6390 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006391 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393
6394 if (orig == NULL)
6395 return NULL;
6396
6397 copy = dict_alloc();
6398 if (copy != NULL)
6399 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 if (copyID != 0)
6401 {
6402 orig->dv_copyID = copyID;
6403 orig->dv_copydict = copy;
6404 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006405 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006406 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410 --todo;
6411
6412 di = dictitem_alloc(hi->hi_key);
6413 if (di == NULL)
6414 break;
6415 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006416 {
6417 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6418 copyID) == FAIL)
6419 {
6420 vim_free(di);
6421 break;
6422 }
6423 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006424 else
6425 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6426 if (dict_add(copy, di) == FAIL)
6427 {
6428 dictitem_free(di);
6429 break;
6430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006433
Bram Moolenaare9a41262005-01-15 22:18:47 +00006434 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006435 if (todo > 0)
6436 {
6437 dict_unref(copy);
6438 copy = NULL;
6439 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006440 }
6441
6442 return copy;
6443}
6444
6445/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006446 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006447 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006448 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006449 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006450dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 dict_T *d;
6452 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006453{
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006455}
6456
Bram Moolenaar8c711452005-01-14 21:53:12 +00006457/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006458 * Add a number or string entry to dictionary "d".
6459 * When "str" is NULL use number "nr", otherwise use "str".
6460 * Returns FAIL when out of memory and when key already exists.
6461 */
6462 int
6463dict_add_nr_str(d, key, nr, str)
6464 dict_T *d;
6465 char *key;
6466 long nr;
6467 char_u *str;
6468{
6469 dictitem_T *item;
6470
6471 item = dictitem_alloc((char_u *)key);
6472 if (item == NULL)
6473 return FAIL;
6474 item->di_tv.v_lock = 0;
6475 if (str == NULL)
6476 {
6477 item->di_tv.v_type = VAR_NUMBER;
6478 item->di_tv.vval.v_number = nr;
6479 }
6480 else
6481 {
6482 item->di_tv.v_type = VAR_STRING;
6483 item->di_tv.vval.v_string = vim_strsave(str);
6484 }
6485 if (dict_add(d, item) == FAIL)
6486 {
6487 dictitem_free(item);
6488 return FAIL;
6489 }
6490 return OK;
6491}
6492
6493/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 * Get the number of items in a Dictionary.
6495 */
6496 static long
6497dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006500 if (d == NULL)
6501 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006502 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006503}
6504
6505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506 * Find item "key[len]" in Dictionary "d".
6507 * If "len" is negative use strlen(key).
6508 * Returns NULL when not found.
6509 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006511dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006512 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006513 char_u *key;
6514 int len;
6515{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006516#define AKEYLEN 200
6517 char_u buf[AKEYLEN];
6518 char_u *akey;
6519 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006520 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006521
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006522 if (len < 0)
6523 akey = key;
6524 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006525 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006526 tofree = akey = vim_strnsave(key, len);
6527 if (akey == NULL)
6528 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006529 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006530 else
6531 {
6532 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006533 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006534 akey = buf;
6535 }
6536
Bram Moolenaar33570922005-01-25 22:26:29 +00006537 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006538 vim_free(tofree);
6539 if (HASHITEM_EMPTY(hi))
6540 return NULL;
6541 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006542}
6543
6544/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006545 * Get a string item from a dictionary.
6546 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006547 * Returns NULL if the entry doesn't exist or out of memory.
6548 */
6549 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006550get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006551 dict_T *d;
6552 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006553 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006554{
6555 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006556 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006557
6558 di = dict_find(d, key, -1);
6559 if (di == NULL)
6560 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006561 s = get_tv_string(&di->di_tv);
6562 if (save && s != NULL)
6563 s = vim_strsave(s);
6564 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006565}
6566
6567/*
6568 * Get a number item from a dictionary.
6569 * Returns 0 if the entry doesn't exist or out of memory.
6570 */
6571 long
6572get_dict_number(d, key)
6573 dict_T *d;
6574 char_u *key;
6575{
6576 dictitem_T *di;
6577
6578 di = dict_find(d, key, -1);
6579 if (di == NULL)
6580 return 0;
6581 return get_tv_number(&di->di_tv);
6582}
6583
6584/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006585 * Return an allocated string with the string representation of a Dictionary.
6586 * May return NULL.
6587 */
6588 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006589dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006591 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592{
6593 garray_T ga;
6594 int first = TRUE;
6595 char_u *tofree;
6596 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006598 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006600 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006602 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603 return NULL;
6604 ga_init2(&ga, (int)sizeof(char), 80);
6605 ga_append(&ga, '{');
6606
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006607 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006610 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006612 --todo;
6613
6614 if (first)
6615 first = FALSE;
6616 else
6617 ga_concat(&ga, (char_u *)", ");
6618
6619 tofree = string_quote(hi->hi_key, FALSE);
6620 if (tofree != NULL)
6621 {
6622 ga_concat(&ga, tofree);
6623 vim_free(tofree);
6624 }
6625 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006627 if (s != NULL)
6628 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006630 if (s == NULL)
6631 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006632 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006633 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 if (todo > 0)
6635 {
6636 vim_free(ga.ga_data);
6637 return NULL;
6638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006639
6640 ga_append(&ga, '}');
6641 ga_append(&ga, NUL);
6642 return (char_u *)ga.ga_data;
6643}
6644
6645/*
6646 * Allocate a variable for a Dictionary and fill it from "*arg".
6647 * Return OK or FAIL. Returns NOTDONE for {expr}.
6648 */
6649 static int
6650get_dict_tv(arg, rettv, evaluate)
6651 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006652 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653 int evaluate;
6654{
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 dict_T *d = NULL;
6656 typval_T tvkey;
6657 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006658 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006662
6663 /*
6664 * First check if it's not a curly-braces thing: {expr}.
6665 * Must do this without evaluating, otherwise a function may be called
6666 * twice. Unfortunately this means we need to call eval1() twice for the
6667 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006668 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006670 if (*start != '}')
6671 {
6672 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6673 return FAIL;
6674 if (*start == '}')
6675 return NOTDONE;
6676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006677
6678 if (evaluate)
6679 {
6680 d = dict_alloc();
6681 if (d == NULL)
6682 return FAIL;
6683 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006684 tvkey.v_type = VAR_UNKNOWN;
6685 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686
6687 *arg = skipwhite(*arg + 1);
6688 while (**arg != '}' && **arg != NUL)
6689 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006690 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006691 goto failret;
6692 if (**arg != ':')
6693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006694 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006695 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006696 goto failret;
6697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006698 key = get_tv_string_buf_chk(&tvkey, buf);
6699 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006701 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6702 if (key != NULL)
6703 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006704 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006705 goto failret;
6706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707
6708 *arg = skipwhite(*arg + 1);
6709 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6710 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006711 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712 goto failret;
6713 }
6714 if (evaluate)
6715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006716 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006717 if (item != NULL)
6718 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006719 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006720 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006721 clear_tv(&tv);
6722 goto failret;
6723 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006724 item = dictitem_alloc(key);
6725 clear_tv(&tvkey);
6726 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006728 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006729 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006730 if (dict_add(d, item) == FAIL)
6731 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006732 }
6733 }
6734
6735 if (**arg == '}')
6736 break;
6737 if (**arg != ',')
6738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006739 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740 goto failret;
6741 }
6742 *arg = skipwhite(*arg + 1);
6743 }
6744
6745 if (**arg != '}')
6746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006747 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748failret:
6749 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006750 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751 return FAIL;
6752 }
6753
6754 *arg = skipwhite(*arg + 1);
6755 if (evaluate)
6756 {
6757 rettv->v_type = VAR_DICT;
6758 rettv->vval.v_dict = d;
6759 ++d->dv_refcount;
6760 }
6761
6762 return OK;
6763}
6764
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006766 * Return a string with the string representation of a variable.
6767 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006768 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006770 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006771 * May return NULL;
6772 */
6773 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006774echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006775 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006776 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006777 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006778 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006779{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006780 static int recurse = 0;
6781 char_u *r = NULL;
6782
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006784 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006785 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006786 *tofree = NULL;
6787 return NULL;
6788 }
6789 ++recurse;
6790
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 switch (tv->v_type)
6792 {
6793 case VAR_FUNC:
6794 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006795 r = tv->vval.v_string;
6796 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006797
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006798 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006799 if (tv->vval.v_list == NULL)
6800 {
6801 *tofree = NULL;
6802 r = NULL;
6803 }
6804 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6805 {
6806 *tofree = NULL;
6807 r = (char_u *)"[...]";
6808 }
6809 else
6810 {
6811 tv->vval.v_list->lv_copyID = copyID;
6812 *tofree = list2string(tv, copyID);
6813 r = *tofree;
6814 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006815 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006816
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006818 if (tv->vval.v_dict == NULL)
6819 {
6820 *tofree = NULL;
6821 r = NULL;
6822 }
6823 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6824 {
6825 *tofree = NULL;
6826 r = (char_u *)"{...}";
6827 }
6828 else
6829 {
6830 tv->vval.v_dict->dv_copyID = copyID;
6831 *tofree = dict2string(tv, copyID);
6832 r = *tofree;
6833 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006834 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006836 case VAR_STRING:
6837 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006838 *tofree = NULL;
6839 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006840 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006841
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006842 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006843 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006846
6847 --recurse;
6848 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006849}
6850
6851/*
6852 * Return a string with the string representation of a variable.
6853 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6854 * "numbuf" is used for a number.
6855 * Puts quotes around strings, so that they can be parsed back by eval().
6856 * May return NULL;
6857 */
6858 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006859tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006860 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006861 char_u **tofree;
6862 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006863 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006864{
6865 switch (tv->v_type)
6866 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006867 case VAR_FUNC:
6868 *tofree = string_quote(tv->vval.v_string, TRUE);
6869 return *tofree;
6870 case VAR_STRING:
6871 *tofree = string_quote(tv->vval.v_string, FALSE);
6872 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006873 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006875 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006876 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006877 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006878 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006879 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006880 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006881}
6882
6883/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006884 * Return string "str" in ' quotes, doubling ' characters.
6885 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006886 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006887 */
6888 static char_u *
6889string_quote(str, function)
6890 char_u *str;
6891 int function;
6892{
Bram Moolenaar33570922005-01-25 22:26:29 +00006893 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006894 char_u *p, *r, *s;
6895
Bram Moolenaar33570922005-01-25 22:26:29 +00006896 len = (function ? 13 : 3);
6897 if (str != NULL)
6898 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006899 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 for (p = str; *p != NUL; mb_ptr_adv(p))
6901 if (*p == '\'')
6902 ++len;
6903 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006904 s = r = alloc(len);
6905 if (r != NULL)
6906 {
6907 if (function)
6908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006910 r += 10;
6911 }
6912 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006913 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006914 if (str != NULL)
6915 for (p = str; *p != NUL; )
6916 {
6917 if (*p == '\'')
6918 *r++ = '\'';
6919 MB_COPY_CHAR(p, r);
6920 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006921 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006922 if (function)
6923 *r++ = ')';
6924 *r++ = NUL;
6925 }
6926 return s;
6927}
6928
6929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 * Get the value of an environment variable.
6931 * "arg" is pointing to the '$'. It is advanced to after the name.
6932 * If the environment variable was not set, silently assume it is empty.
6933 * Always return OK.
6934 */
6935 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006936get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 int evaluate;
6940{
6941 char_u *string = NULL;
6942 int len;
6943 int cc;
6944 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006945 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
6947 ++*arg;
6948 name = *arg;
6949 len = get_env_len(arg);
6950 if (evaluate)
6951 {
6952 if (len != 0)
6953 {
6954 cc = name[len];
6955 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006956 /* first try vim_getenv(), fast for normal environment vars */
6957 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006959 {
6960 if (!mustfree)
6961 string = vim_strsave(string);
6962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 else
6964 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006965 if (mustfree)
6966 vim_free(string);
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 /* next try expanding things like $VIM and ${HOME} */
6969 string = expand_env_save(name - 1);
6970 if (string != NULL && *string == '$')
6971 {
6972 vim_free(string);
6973 string = NULL;
6974 }
6975 }
6976 name[len] = cc;
6977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006978 rettv->v_type = VAR_STRING;
6979 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 }
6981
6982 return OK;
6983}
6984
6985/*
6986 * Array with names and number of arguments of all internal functions
6987 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6988 */
6989static struct fst
6990{
6991 char *f_name; /* function name */
6992 char f_min_argc; /* minimal number of arguments */
6993 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006995 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996} functions[] =
6997{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006998 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {"append", 2, 2, f_append},
7000 {"argc", 0, 0, f_argc},
7001 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007002 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007004 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {"bufexists", 1, 1, f_bufexists},
7006 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7007 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7008 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7009 {"buflisted", 1, 1, f_buflisted},
7010 {"bufloaded", 1, 1, f_bufloaded},
7011 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007012 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {"bufwinnr", 1, 1, f_bufwinnr},
7014 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007015 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007016 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007017 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {"char2nr", 1, 1, f_char2nr},
7019 {"cindent", 1, 1, f_cindent},
7020 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007021#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007022 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007023 {"complete_add", 1, 1, f_complete_add},
7024 {"complete_check", 0, 0, f_complete_check},
7025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007027 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007028 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007030 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007031 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {"delete", 1, 1, f_delete},
7033 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007034 {"diff_filler", 1, 1, f_diff_filler},
7035 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007036 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007038 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"eventhandler", 0, 0, f_eventhandler},
7040 {"executable", 1, 1, f_executable},
7041 {"exists", 1, 1, f_exists},
7042 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007043 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007044 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7046 {"filereadable", 1, 1, f_filereadable},
7047 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007048 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007049 {"finddir", 1, 3, f_finddir},
7050 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {"fnamemodify", 2, 2, f_fnamemodify},
7052 {"foldclosed", 1, 1, f_foldclosed},
7053 {"foldclosedend", 1, 1, f_foldclosedend},
7054 {"foldlevel", 1, 1, f_foldlevel},
7055 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007056 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007058 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007059 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007060 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007061 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {"getbufvar", 2, 2, f_getbufvar},
7063 {"getchar", 0, 1, f_getchar},
7064 {"getcharmod", 0, 0, f_getcharmod},
7065 {"getcmdline", 0, 0, f_getcmdline},
7066 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007067 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007069 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007070 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"getfsize", 1, 1, f_getfsize},
7072 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007073 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007074 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007075 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007076 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007077 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007078 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007080 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 {"getwinposx", 0, 0, f_getwinposx},
7082 {"getwinposy", 0, 0, f_getwinposy},
7083 {"getwinvar", 2, 2, f_getwinvar},
7084 {"glob", 1, 1, f_glob},
7085 {"globpath", 2, 2, f_globpath},
7086 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007087 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007088 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7090 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7091 {"histadd", 2, 2, f_histadd},
7092 {"histdel", 1, 2, f_histdel},
7093 {"histget", 1, 2, f_histget},
7094 {"histnr", 1, 1, f_histnr},
7095 {"hlID", 1, 1, f_hlID},
7096 {"hlexists", 1, 1, f_hlexists},
7097 {"hostname", 0, 0, f_hostname},
7098 {"iconv", 3, 3, f_iconv},
7099 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007100 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007101 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007103 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {"inputrestore", 0, 0, f_inputrestore},
7105 {"inputsave", 0, 0, f_inputsave},
7106 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007107 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007109 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007110 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007111 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {"libcall", 3, 3, f_libcall},
7116 {"libcallnr", 3, 3, f_libcallnr},
7117 {"line", 1, 1, f_line},
7118 {"line2byte", 1, 1, f_line2byte},
7119 {"lispindent", 1, 1, f_lispindent},
7120 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007121 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007122 {"maparg", 1, 3, f_maparg},
7123 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007124 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007125 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007126 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007127 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007128 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007129 {"max", 1, 1, f_max},
7130 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007131#ifdef vim_mkdir
7132 {"mkdir", 1, 3, f_mkdir},
7133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {"mode", 0, 0, f_mode},
7135 {"nextnonblank", 1, 1, f_nextnonblank},
7136 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007137 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007139 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007140 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007142 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007143 {"reltime", 0, 2, f_reltime},
7144 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {"remote_expr", 2, 3, f_remote_expr},
7146 {"remote_foreground", 1, 1, f_remote_foreground},
7147 {"remote_peek", 1, 2, f_remote_peek},
7148 {"remote_read", 1, 1, f_remote_read},
7149 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007150 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007152 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007154 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007155 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007156 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007157 {"searchpair", 3, 6, f_searchpair},
7158 {"searchpairpos", 3, 6, f_searchpairpos},
7159 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {"server2client", 2, 2, f_server2client},
7161 {"serverlist", 0, 0, f_serverlist},
7162 {"setbufvar", 3, 3, f_setbufvar},
7163 {"setcmdpos", 1, 1, f_setcmdpos},
7164 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007165 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007166 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007167 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007169 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007171 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007173 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007174 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007175 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007176 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007177 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007178 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179#ifdef HAVE_STRFTIME
7180 {"strftime", 1, 2, f_strftime},
7181#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007183 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {"strlen", 1, 1, f_strlen},
7185 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007186 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {"strtrans", 1, 1, f_strtrans},
7188 {"submatch", 1, 1, f_submatch},
7189 {"substitute", 4, 4, f_substitute},
7190 {"synID", 3, 3, f_synID},
7191 {"synIDattr", 2, 3, f_synIDattr},
7192 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007193 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007194 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007195 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007196 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007197 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007198 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007200 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {"tolower", 1, 1, f_tolower},
7202 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007203 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {"virtcol", 1, 1, f_virtcol},
7207 {"visualmode", 0, 1, f_visualmode},
7208 {"winbufnr", 1, 1, f_winbufnr},
7209 {"wincol", 0, 0, f_wincol},
7210 {"winheight", 1, 1, f_winheight},
7211 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007212 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007214 {"winrestview", 1, 1, f_winrestview},
7215 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007217 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218};
7219
7220#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7221
7222/*
7223 * Function given to ExpandGeneric() to obtain the list of internal
7224 * or user defined function names.
7225 */
7226 char_u *
7227get_function_name(xp, idx)
7228 expand_T *xp;
7229 int idx;
7230{
7231 static int intidx = -1;
7232 char_u *name;
7233
7234 if (idx == 0)
7235 intidx = -1;
7236 if (intidx < 0)
7237 {
7238 name = get_user_func_name(xp, idx);
7239 if (name != NULL)
7240 return name;
7241 }
7242 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7243 {
7244 STRCPY(IObuff, functions[intidx].f_name);
7245 STRCAT(IObuff, "(");
7246 if (functions[intidx].f_max_argc == 0)
7247 STRCAT(IObuff, ")");
7248 return IObuff;
7249 }
7250
7251 return NULL;
7252}
7253
7254/*
7255 * Function given to ExpandGeneric() to obtain the list of internal or
7256 * user defined variable or function names.
7257 */
7258/*ARGSUSED*/
7259 char_u *
7260get_expr_name(xp, idx)
7261 expand_T *xp;
7262 int idx;
7263{
7264 static int intidx = -1;
7265 char_u *name;
7266
7267 if (idx == 0)
7268 intidx = -1;
7269 if (intidx < 0)
7270 {
7271 name = get_function_name(xp, idx);
7272 if (name != NULL)
7273 return name;
7274 }
7275 return get_user_var_name(xp, ++intidx);
7276}
7277
7278#endif /* FEAT_CMDL_COMPL */
7279
7280/*
7281 * Find internal function in table above.
7282 * Return index, or -1 if not found
7283 */
7284 static int
7285find_internal_func(name)
7286 char_u *name; /* name of the function */
7287{
7288 int first = 0;
7289 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7290 int cmp;
7291 int x;
7292
7293 /*
7294 * Find the function name in the table. Binary search.
7295 */
7296 while (first <= last)
7297 {
7298 x = first + ((unsigned)(last - first) >> 1);
7299 cmp = STRCMP(name, functions[x].f_name);
7300 if (cmp < 0)
7301 last = x - 1;
7302 else if (cmp > 0)
7303 first = x + 1;
7304 else
7305 return x;
7306 }
7307 return -1;
7308}
7309
7310/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007311 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7312 * name it contains, otherwise return "name".
7313 */
7314 static char_u *
7315deref_func_name(name, lenp)
7316 char_u *name;
7317 int *lenp;
7318{
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007320 int cc;
7321
7322 cc = name[*lenp];
7323 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007324 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007325 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007327 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007329 {
7330 *lenp = 0;
7331 return (char_u *)""; /* just in case */
7332 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007335 }
7336
7337 return name;
7338}
7339
7340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 * Allocate a variable for the result of a function.
7342 * Return OK or FAIL.
7343 */
7344 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7346 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 char_u *name; /* name of the function */
7348 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 char_u **arg; /* argument, pointing to the '(' */
7351 linenr_T firstline; /* first line of range */
7352 linenr_T lastline; /* last line of range */
7353 int *doesrange; /* return: function handled range */
7354 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357 char_u *argp;
7358 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007359 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 int argcount = 0; /* number of arguments found */
7361
7362 /*
7363 * Get the arguments.
7364 */
7365 argp = *arg;
7366 while (argcount < MAX_FUNC_ARGS)
7367 {
7368 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7369 if (*argp == ')' || *argp == ',' || *argp == NUL)
7370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7372 {
7373 ret = FAIL;
7374 break;
7375 }
7376 ++argcount;
7377 if (*argp != ',')
7378 break;
7379 }
7380 if (*argp == ')')
7381 ++argp;
7382 else
7383 ret = FAIL;
7384
7385 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007386 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 {
7390 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007393 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
7396 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007397 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
7399 *arg = skipwhite(argp);
7400 return ret;
7401}
7402
7403
7404/*
7405 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007406 * Return OK when the function can't be called, FAIL otherwise.
7407 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 */
7409 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007410call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 char_u *name; /* name of the function */
7413 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007416 typval_T *argvars; /* vars for arguments, must have "argcount"
7417 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 linenr_T firstline; /* first line of range */
7419 linenr_T lastline; /* last line of range */
7420 int *doesrange; /* return: function handled range */
7421 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423{
7424 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425#define ERROR_UNKNOWN 0
7426#define ERROR_TOOMANY 1
7427#define ERROR_TOOFEW 2
7428#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429#define ERROR_DICT 4
7430#define ERROR_NONE 5
7431#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 int error = ERROR_NONE;
7433 int i;
7434 int llen;
7435 ufunc_T *fp;
7436 int cc;
7437#define FLEN_FIXED 40
7438 char_u fname_buf[FLEN_FIXED + 1];
7439 char_u *fname;
7440
7441 /*
7442 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7443 * Change <SNR>123_name() to K_SNR 123_name().
7444 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7445 */
7446 cc = name[len];
7447 name[len] = NUL;
7448 llen = eval_fname_script(name);
7449 if (llen > 0)
7450 {
7451 fname_buf[0] = K_SPECIAL;
7452 fname_buf[1] = KS_EXTRA;
7453 fname_buf[2] = (int)KE_SNR;
7454 i = 3;
7455 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7456 {
7457 if (current_SID <= 0)
7458 error = ERROR_SCRIPT;
7459 else
7460 {
7461 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7462 i = (int)STRLEN(fname_buf);
7463 }
7464 }
7465 if (i + STRLEN(name + llen) < FLEN_FIXED)
7466 {
7467 STRCPY(fname_buf + i, name + llen);
7468 fname = fname_buf;
7469 }
7470 else
7471 {
7472 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7473 if (fname == NULL)
7474 error = ERROR_OTHER;
7475 else
7476 {
7477 mch_memmove(fname, fname_buf, (size_t)i);
7478 STRCPY(fname + i, name + llen);
7479 }
7480 }
7481 }
7482 else
7483 fname = name;
7484
7485 *doesrange = FALSE;
7486
7487
7488 /* execute the function if no errors detected and executing */
7489 if (evaluate && error == ERROR_NONE)
7490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007491 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 error = ERROR_UNKNOWN;
7493
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007494 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {
7496 /*
7497 * User defined function.
7498 */
7499 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007500
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007502 /* Trigger FuncUndefined event, may load the function. */
7503 if (fp == NULL
7504 && apply_autocmds(EVENT_FUNCUNDEFINED,
7505 fname, fname, TRUE, NULL)
7506 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007508 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 fp = find_func(fname);
7510 }
7511#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007512 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007513 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007514 {
7515 /* loaded a package, search for the function again */
7516 fp = find_func(fname);
7517 }
7518
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (fp != NULL)
7520 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007521 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007523 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007525 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007527 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007528 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 else
7530 {
7531 /*
7532 * Call the user function.
7533 * Save and restore search patterns, script variables and
7534 * redo buffer.
7535 */
7536 save_search_patterns();
7537 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007538 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007539 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007540 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007541 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7542 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7543 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007544 /* Function was unreferenced while being used, free it
7545 * now. */
7546 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 restoreRedobuff();
7548 restore_search_patterns();
7549 error = ERROR_NONE;
7550 }
7551 }
7552 }
7553 else
7554 {
7555 /*
7556 * Find the function name in the table, call its implementation.
7557 */
7558 i = find_internal_func(fname);
7559 if (i >= 0)
7560 {
7561 if (argcount < functions[i].f_min_argc)
7562 error = ERROR_TOOFEW;
7563 else if (argcount > functions[i].f_max_argc)
7564 error = ERROR_TOOMANY;
7565 else
7566 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007568 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 error = ERROR_NONE;
7570 }
7571 }
7572 }
7573 /*
7574 * The function call (or "FuncUndefined" autocommand sequence) might
7575 * have been aborted by an error, an interrupt, or an explicitly thrown
7576 * exception that has not been caught so far. This situation can be
7577 * tested for by calling aborting(). For an error in an internal
7578 * function or for the "E132" error in call_user_func(), however, the
7579 * throw point at which the "force_abort" flag (temporarily reset by
7580 * emsg()) is normally updated has not been reached yet. We need to
7581 * update that flag first to make aborting() reliable.
7582 */
7583 update_force_abort();
7584 }
7585 if (error == ERROR_NONE)
7586 ret = OK;
7587
7588 /*
7589 * Report an error unless the argument evaluation or function call has been
7590 * cancelled due to an aborting error, an interrupt, or an exception.
7591 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 if (!aborting())
7593 {
7594 switch (error)
7595 {
7596 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007597 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 break;
7599 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007600 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 break;
7602 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007603 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 name);
7605 break;
7606 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007607 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 name);
7609 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007611 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007612 name);
7613 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 }
7615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616
7617 name[len] = cc;
7618 if (fname != name && fname != fname_buf)
7619 vim_free(fname);
7620
7621 return ret;
7622}
7623
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007624/*
7625 * Give an error message with a function name. Handle <SNR> things.
7626 */
7627 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007628emsg_funcname(ermsg, name)
7629 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007630 char_u *name;
7631{
7632 char_u *p;
7633
7634 if (*name == K_SPECIAL)
7635 p = concat_str((char_u *)"<SNR>", name + 3);
7636 else
7637 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007638 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007639 if (p != name)
7640 vim_free(p);
7641}
7642
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643/*********************************************
7644 * Implementation of the built-in functions
7645 */
7646
7647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007648 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 */
7650 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007651f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 typval_T *argvars;
7653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007660 if ((l = argvars[0].vval.v_list) != NULL
7661 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7662 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007663 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007664 }
7665 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007666 EMSG(_(e_listreq));
7667}
7668
7669/*
7670 * "append(lnum, string/list)" function
7671 */
7672 static void
7673f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 typval_T *argvars;
7675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007676{
7677 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007678 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 list_T *l = NULL;
7680 listitem_T *li = NULL;
7681 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007682 long added = 0;
7683
Bram Moolenaar0d660222005-01-07 21:51:51 +00007684 lnum = get_tv_lnum(argvars);
7685 if (lnum >= 0
7686 && lnum <= curbuf->b_ml.ml_line_count
7687 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007688 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007689 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007691 l = argvars[1].vval.v_list;
7692 if (l == NULL)
7693 return;
7694 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007696 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007697 for (;;)
7698 {
7699 if (l == NULL)
7700 tv = &argvars[1]; /* append a string */
7701 else if (li == NULL)
7702 break; /* end of list */
7703 else
7704 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007705 line = get_tv_string_chk(tv);
7706 if (line == NULL) /* type error */
7707 {
7708 rettv->vval.v_number = 1; /* Failed */
7709 break;
7710 }
7711 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007712 ++added;
7713 if (l == NULL)
7714 break;
7715 li = li->li_next;
7716 }
7717
7718 appended_lines_mark(lnum, added);
7719 if (curwin->w_cursor.lnum > lnum)
7720 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007722 else
7723 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724}
7725
7726/*
7727 * "argc()" function
7728 */
7729/* ARGSUSED */
7730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 typval_T *argvars;
7733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007735 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737
7738/*
7739 * "argidx()" function
7740 */
7741/* ARGSUSED */
7742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007744 typval_T *argvars;
7745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007747 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748}
7749
7750/*
7751 * "argv(nr)" function
7752 */
7753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 typval_T *argvars;
7756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
7758 int idx;
7759
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007760 if (argvars[0].v_type != VAR_UNKNOWN)
7761 {
7762 idx = get_tv_number_chk(&argvars[0], NULL);
7763 if (idx >= 0 && idx < ARGCOUNT)
7764 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7765 else
7766 rettv->vval.v_string = NULL;
7767 rettv->v_type = VAR_STRING;
7768 }
7769 else if (rettv_list_alloc(rettv) == OK)
7770 for (idx = 0; idx < ARGCOUNT; ++idx)
7771 list_append_string(rettv->vval.v_list,
7772 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
7775/*
7776 * "browse(save, title, initdir, default)" function
7777 */
7778/* ARGSUSED */
7779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 typval_T *argvars;
7782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784#ifdef FEAT_BROWSE
7785 int save;
7786 char_u *title;
7787 char_u *initdir;
7788 char_u *defname;
7789 char_u buf[NUMBUFLEN];
7790 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007791 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007793 save = get_tv_number_chk(&argvars[0], &error);
7794 title = get_tv_string_chk(&argvars[1]);
7795 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7796 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007798 if (error || title == NULL || initdir == NULL || defname == NULL)
7799 rettv->vval.v_string = NULL;
7800 else
7801 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007802 do_browse(save ? BROWSE_SAVE : 0,
7803 title, defname, NULL, initdir, NULL, curbuf);
7804#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007806#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007808}
7809
7810/*
7811 * "browsedir(title, initdir)" function
7812 */
7813/* ARGSUSED */
7814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 typval_T *argvars;
7817 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007818{
7819#ifdef FEAT_BROWSE
7820 char_u *title;
7821 char_u *initdir;
7822 char_u buf[NUMBUFLEN];
7823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007824 title = get_tv_string_chk(&argvars[0]);
7825 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007827 if (title == NULL || initdir == NULL)
7828 rettv->vval.v_string = NULL;
7829 else
7830 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007831 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007833 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836}
7837
Bram Moolenaar33570922005-01-25 22:26:29 +00007838static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840/*
7841 * Find a buffer by number or exact name.
7842 */
7843 static buf_T *
7844find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
7847 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007849 if (avar->v_type == VAR_NUMBER)
7850 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007851 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007853 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007854 if (buf == NULL)
7855 {
7856 /* No full path name match, try a match with a URL or a "nofile"
7857 * buffer, these don't use the full path. */
7858 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7859 if (buf->b_fname != NULL
7860 && (path_with_url(buf->b_fname)
7861#ifdef FEAT_QUICKFIX
7862 || bt_nofile(buf)
7863#endif
7864 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007866 break;
7867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 }
7869 return buf;
7870}
7871
7872/*
7873 * "bufexists(expr)" function
7874 */
7875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007876f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007877 typval_T *argvars;
7878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881}
7882
7883/*
7884 * "buflisted(expr)" function
7885 */
7886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 typval_T *argvars;
7889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
7891 buf_T *buf;
7892
7893 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895}
7896
7897/*
7898 * "bufloaded(expr)" function
7899 */
7900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901f_bufloaded(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{
7905 buf_T *buf;
7906
7907 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007908 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909}
7910
Bram Moolenaar33570922005-01-25 22:26:29 +00007911static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007912
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913/*
7914 * Get buffer by number or pattern.
7915 */
7916 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007917get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 int save_magic;
7922 char_u *save_cpo;
7923 buf_T *buf;
7924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007925 if (tv->v_type == VAR_NUMBER)
7926 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007927 if (tv->v_type != VAR_STRING)
7928 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 if (name == NULL || *name == NUL)
7930 return curbuf;
7931 if (name[0] == '$' && name[1] == NUL)
7932 return lastbuf;
7933
7934 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7935 save_magic = p_magic;
7936 p_magic = TRUE;
7937 save_cpo = p_cpo;
7938 p_cpo = (char_u *)"";
7939
7940 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7941 TRUE, FALSE));
7942
7943 p_magic = save_magic;
7944 p_cpo = save_cpo;
7945
7946 /* If not found, try expanding the name, like done for bufexists(). */
7947 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
7950 return buf;
7951}
7952
7953/*
7954 * "bufname(expr)" function
7955 */
7956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 typval_T *argvars;
7959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961 buf_T *buf;
7962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007963 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 buf = get_buf_tv(&argvars[0]);
7966 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007970 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 --emsg_off;
7972}
7973
7974/*
7975 * "bufnr(expr)" function
7976 */
7977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 typval_T *argvars;
7980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981{
7982 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007983 int error = FALSE;
7984 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007986 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007989 --emsg_off;
7990
7991 /* If the buffer isn't found and the second argument is not zero create a
7992 * new buffer. */
7993 if (buf == NULL
7994 && argvars[1].v_type != VAR_UNKNOWN
7995 && get_tv_number_chk(&argvars[1], &error) != 0
7996 && !error
7997 && (name = get_tv_string_chk(&argvars[0])) != NULL
7998 && !error)
7999 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008002 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005}
8006
8007/*
8008 * "bufwinnr(nr)" function
8009 */
8010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008012 typval_T *argvars;
8013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
8015#ifdef FEAT_WINDOWS
8016 win_T *wp;
8017 int winnr = 0;
8018#endif
8019 buf_T *buf;
8020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008021 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024#ifdef FEAT_WINDOWS
8025 for (wp = firstwin; wp; wp = wp->w_next)
8026 {
8027 ++winnr;
8028 if (wp->w_buffer == buf)
8029 break;
8030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008033 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034#endif
8035 --emsg_off;
8036}
8037
8038/*
8039 * "byte2line(byte)" function
8040 */
8041/*ARGSUSED*/
8042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 typval_T *argvars;
8045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
8047#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049#else
8050 long boff = 0;
8051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008052 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 (linenr_T)0, &boff);
8058#endif
8059}
8060
8061/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008062 * "byteidx()" function
8063 */
8064/*ARGSUSED*/
8065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008066f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 typval_T *argvars;
8068 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008069{
8070#ifdef FEAT_MBYTE
8071 char_u *t;
8072#endif
8073 char_u *str;
8074 long idx;
8075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008076 str = get_tv_string_chk(&argvars[0]);
8077 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008078 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008079 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008080 return;
8081
8082#ifdef FEAT_MBYTE
8083 t = str;
8084 for ( ; idx > 0; idx--)
8085 {
8086 if (*t == NUL) /* EOL reached */
8087 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008088 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008089 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008090 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008091#else
8092 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008093 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008094#endif
8095}
8096
8097/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008098 * "call(func, arglist)" function
8099 */
8100 static void
8101f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008102 typval_T *argvars;
8103 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008104{
8105 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008106 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008107 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008109 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008111
8112 rettv->vval.v_number = 0;
8113 if (argvars[1].v_type != VAR_LIST)
8114 {
8115 EMSG(_(e_listreq));
8116 return;
8117 }
8118 if (argvars[1].vval.v_list == NULL)
8119 return;
8120
8121 if (argvars[0].v_type == VAR_FUNC)
8122 func = argvars[0].vval.v_string;
8123 else
8124 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008125 if (*func == NUL)
8126 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008127
Bram Moolenaare9a41262005-01-15 22:18:47 +00008128 if (argvars[2].v_type != VAR_UNKNOWN)
8129 {
8130 if (argvars[2].v_type != VAR_DICT)
8131 {
8132 EMSG(_(e_dictreq));
8133 return;
8134 }
8135 selfdict = argvars[2].vval.v_dict;
8136 }
8137
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008138 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8139 item = item->li_next)
8140 {
8141 if (argc == MAX_FUNC_ARGS)
8142 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008143 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008144 break;
8145 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008146 /* Make a copy of each argument. This is needed to be able to set
8147 * v_lock to VAR_FIXED in the copy without changing the original list.
8148 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008149 copy_tv(&item->li_tv, &argv[argc++]);
8150 }
8151
8152 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008153 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008154 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8155 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008156
8157 /* Free the arguments. */
8158 while (argc > 0)
8159 clear_tv(&argv[--argc]);
8160}
8161
8162/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008163 * "changenr()" function
8164 */
8165/*ARGSUSED*/
8166 static void
8167f_changenr(argvars, rettv)
8168 typval_T *argvars;
8169 typval_T *rettv;
8170{
8171 rettv->vval.v_number = curbuf->b_u_seq_cur;
8172}
8173
8174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 * "char2nr(string)" function
8176 */
8177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008178f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008179 typval_T *argvars;
8180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
8182#ifdef FEAT_MBYTE
8183 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008184 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 else
8186#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008187 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188}
8189
8190/*
8191 * "cindent(lnum)" function
8192 */
8193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008194f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 typval_T *argvars;
8196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198#ifdef FEAT_CINDENT
8199 pos_T pos;
8200 linenr_T lnum;
8201
8202 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8205 {
8206 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008207 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 curwin->w_cursor = pos;
8209 }
8210 else
8211#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008212 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213}
8214
8215/*
8216 * "col(string)" function
8217 */
8218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219f_col(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 colnr_T col = 0;
8224 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008225 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008227 fp = var2fpos(&argvars[0], FALSE, &fnum);
8228 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {
8230 if (fp->col == MAXCOL)
8231 {
8232 /* '> can be MAXCOL, get the length of the line then */
8233 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008234 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 else
8236 col = MAXCOL;
8237 }
8238 else
8239 {
8240 col = fp->col + 1;
8241#ifdef FEAT_VIRTUALEDIT
8242 /* col(".") when the cursor is on the NUL at the end of the line
8243 * because of "coladd" can be seen as an extra column. */
8244 if (virtual_active() && fp == &curwin->w_cursor)
8245 {
8246 char_u *p = ml_get_cursor();
8247
8248 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8249 curwin->w_virtcol - curwin->w_cursor.coladd))
8250 {
8251# ifdef FEAT_MBYTE
8252 int l;
8253
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008254 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 col += l;
8256# else
8257 if (*p != NUL && p[1] == NUL)
8258 ++col;
8259# endif
8260 }
8261 }
8262#endif
8263 }
8264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008265 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266}
8267
Bram Moolenaar572cb562005-08-05 21:35:02 +00008268#if defined(FEAT_INS_EXPAND)
8269/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008270 * "complete()" function
8271 */
8272/*ARGSUSED*/
8273 static void
8274f_complete(argvars, rettv)
8275 typval_T *argvars;
8276 typval_T *rettv;
8277{
8278 int startcol;
8279
8280 if ((State & INSERT) == 0)
8281 {
8282 EMSG(_("E785: complete() can only be used in Insert mode"));
8283 return;
8284 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008285
8286 /* Check for undo allowed here, because if something was already inserted
8287 * the line was already saved for undo and this check isn't done. */
8288 if (!undo_allowed())
8289 return;
8290
Bram Moolenaarade00832006-03-10 21:46:58 +00008291 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8292 {
8293 EMSG(_(e_invarg));
8294 return;
8295 }
8296
8297 startcol = get_tv_number_chk(&argvars[0], NULL);
8298 if (startcol <= 0)
8299 return;
8300
8301 set_completion(startcol - 1, argvars[1].vval.v_list);
8302}
8303
8304/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008305 * "complete_add()" function
8306 */
8307/*ARGSUSED*/
8308 static void
8309f_complete_add(argvars, rettv)
8310 typval_T *argvars;
8311 typval_T *rettv;
8312{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008313 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008314}
8315
8316/*
8317 * "complete_check()" function
8318 */
8319/*ARGSUSED*/
8320 static void
8321f_complete_check(argvars, rettv)
8322 typval_T *argvars;
8323 typval_T *rettv;
8324{
8325 int saved = RedrawingDisabled;
8326
8327 RedrawingDisabled = 0;
8328 ins_compl_check_keys(0);
8329 rettv->vval.v_number = compl_interrupted;
8330 RedrawingDisabled = saved;
8331}
8332#endif
8333
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334/*
8335 * "confirm(message, buttons[, default [, type]])" function
8336 */
8337/*ARGSUSED*/
8338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 typval_T *argvars;
8341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
8343#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8344 char_u *message;
8345 char_u *buttons = NULL;
8346 char_u buf[NUMBUFLEN];
8347 char_u buf2[NUMBUFLEN];
8348 int def = 1;
8349 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008350 char_u *typestr;
8351 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008353 message = get_tv_string_chk(&argvars[0]);
8354 if (message == NULL)
8355 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008356 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008358 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8359 if (buttons == NULL)
8360 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008361 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008363 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008364 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008366 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8367 if (typestr == NULL)
8368 error = TRUE;
8369 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008371 switch (TOUPPER_ASC(*typestr))
8372 {
8373 case 'E': type = VIM_ERROR; break;
8374 case 'Q': type = VIM_QUESTION; break;
8375 case 'I': type = VIM_INFO; break;
8376 case 'W': type = VIM_WARNING; break;
8377 case 'G': type = VIM_GENERIC; break;
8378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 }
8380 }
8381 }
8382 }
8383
8384 if (buttons == NULL || *buttons == NUL)
8385 buttons = (char_u *)_("&Ok");
8386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008387 if (error)
8388 rettv->vval.v_number = 0;
8389 else
8390 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 def, NULL);
8392#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#endif
8395}
8396
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008397/*
8398 * "copy()" function
8399 */
8400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 typval_T *argvars;
8403 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008404{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008405 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008406}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
8408/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008409 * "count()" function
8410 */
8411 static void
8412f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 typval_T *argvars;
8414 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008415{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008416 long n = 0;
8417 int ic = FALSE;
8418
Bram Moolenaare9a41262005-01-15 22:18:47 +00008419 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008420 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008421 listitem_T *li;
8422 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008423 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008424
Bram Moolenaare9a41262005-01-15 22:18:47 +00008425 if ((l = argvars[0].vval.v_list) != NULL)
8426 {
8427 li = l->lv_first;
8428 if (argvars[2].v_type != VAR_UNKNOWN)
8429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008430 int error = FALSE;
8431
8432 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008433 if (argvars[3].v_type != VAR_UNKNOWN)
8434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008435 idx = get_tv_number_chk(&argvars[3], &error);
8436 if (!error)
8437 {
8438 li = list_find(l, idx);
8439 if (li == NULL)
8440 EMSGN(_(e_listidx), idx);
8441 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008443 if (error)
8444 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008445 }
8446
8447 for ( ; li != NULL; li = li->li_next)
8448 if (tv_equal(&li->li_tv, &argvars[1], ic))
8449 ++n;
8450 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008451 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008452 else if (argvars[0].v_type == VAR_DICT)
8453 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008454 int todo;
8455 dict_T *d;
8456 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008457
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008458 if ((d = argvars[0].vval.v_dict) != NULL)
8459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460 int error = FALSE;
8461
Bram Moolenaare9a41262005-01-15 22:18:47 +00008462 if (argvars[2].v_type != VAR_UNKNOWN)
8463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008465 if (argvars[3].v_type != VAR_UNKNOWN)
8466 EMSG(_(e_invarg));
8467 }
8468
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008469 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008470 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008471 {
8472 if (!HASHITEM_EMPTY(hi))
8473 {
8474 --todo;
8475 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8476 ++n;
8477 }
8478 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 }
8480 }
8481 else
8482 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008483 rettv->vval.v_number = n;
8484}
8485
8486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8488 *
8489 * Checks the existence of a cscope connection.
8490 */
8491/*ARGSUSED*/
8492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008494 typval_T *argvars;
8495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
8497#ifdef FEAT_CSCOPE
8498 int num = 0;
8499 char_u *dbpath = NULL;
8500 char_u *prepend = NULL;
8501 char_u buf[NUMBUFLEN];
8502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008503 if (argvars[0].v_type != VAR_UNKNOWN
8504 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008506 num = (int)get_tv_number(&argvars[0]);
8507 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 }
8511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#endif
8516}
8517
8518/*
8519 * "cursor(lnum, col)" function
8520 *
8521 * Moves the cursor to the specified line and column
8522 */
8523/*ARGSUSED*/
8524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008526 typval_T *argvars;
8527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008530#ifdef FEAT_VIRTUALEDIT
8531 long coladd = 0;
8532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
Bram Moolenaara5525202006-03-02 22:52:09 +00008534 if (argvars[1].v_type == VAR_UNKNOWN)
8535 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008536 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008537
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008538 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008539 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008540 line = pos.lnum;
8541 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008542#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008543 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008544#endif
8545 }
8546 else
8547 {
8548 line = get_tv_lnum(argvars);
8549 col = get_tv_number_chk(&argvars[1], NULL);
8550#ifdef FEAT_VIRTUALEDIT
8551 if (argvars[2].v_type != VAR_UNKNOWN)
8552 coladd = get_tv_number_chk(&argvars[2], NULL);
8553#endif
8554 }
8555 if (line < 0 || col < 0
8556#ifdef FEAT_VIRTUALEDIT
8557 || coladd < 0
8558#endif
8559 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008560 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 if (line > 0)
8562 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 if (col > 0)
8564 curwin->w_cursor.col = col - 1;
8565#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008566 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567#endif
8568
8569 /* Make sure the cursor is in a valid position. */
8570 check_cursor();
8571#ifdef FEAT_MBYTE
8572 /* Correct cursor for multi-byte character. */
8573 if (has_mbyte)
8574 mb_adjust_cursor();
8575#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008576
8577 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578}
8579
8580/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 */
8583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008585 typval_T *argvars;
8586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008588 int noref = 0;
8589
8590 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008591 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008592 if (noref < 0 || noref > 1)
8593 EMSG(_(e_invarg));
8594 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008595 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596}
8597
8598/*
8599 * "delete()" function
8600 */
8601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 typval_T *argvars;
8604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605{
8606 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610}
8611
8612/*
8613 * "did_filetype()" function
8614 */
8615/*ARGSUSED*/
8616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *argvars;
8619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625#endif
8626}
8627
8628/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008629 * "diff_filler()" function
8630 */
8631/*ARGSUSED*/
8632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008633f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 typval_T *argvars;
8635 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008636{
8637#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008639#endif
8640}
8641
8642/*
8643 * "diff_hlID()" function
8644 */
8645/*ARGSUSED*/
8646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008648 typval_T *argvars;
8649 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008650{
8651#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008652 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008653 static linenr_T prev_lnum = 0;
8654 static int changedtick = 0;
8655 static int fnum = 0;
8656 static int change_start = 0;
8657 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008658 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008659 int filler_lines;
8660 int col;
8661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008662 if (lnum < 0) /* ignore type error in {lnum} arg */
8663 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008664 if (lnum != prev_lnum
8665 || changedtick != curbuf->b_changedtick
8666 || fnum != curbuf->b_fnum)
8667 {
8668 /* New line, buffer, change: need to get the values. */
8669 filler_lines = diff_check(curwin, lnum);
8670 if (filler_lines < 0)
8671 {
8672 if (filler_lines == -1)
8673 {
8674 change_start = MAXCOL;
8675 change_end = -1;
8676 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8677 hlID = HLF_ADD; /* added line */
8678 else
8679 hlID = HLF_CHD; /* changed line */
8680 }
8681 else
8682 hlID = HLF_ADD; /* added line */
8683 }
8684 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008685 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008686 prev_lnum = lnum;
8687 changedtick = curbuf->b_changedtick;
8688 fnum = curbuf->b_fnum;
8689 }
8690
8691 if (hlID == HLF_CHD || hlID == HLF_TXD)
8692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008693 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008694 if (col >= change_start && col <= change_end)
8695 hlID = HLF_TXD; /* changed text */
8696 else
8697 hlID = HLF_CHD; /* changed line */
8698 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008699 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008700#endif
8701}
8702
8703/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008704 * "empty({expr})" function
8705 */
8706 static void
8707f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 typval_T *argvars;
8709 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008710{
8711 int n;
8712
8713 switch (argvars[0].v_type)
8714 {
8715 case VAR_STRING:
8716 case VAR_FUNC:
8717 n = argvars[0].vval.v_string == NULL
8718 || *argvars[0].vval.v_string == NUL;
8719 break;
8720 case VAR_NUMBER:
8721 n = argvars[0].vval.v_number == 0;
8722 break;
8723 case VAR_LIST:
8724 n = argvars[0].vval.v_list == NULL
8725 || argvars[0].vval.v_list->lv_first == NULL;
8726 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008727 case VAR_DICT:
8728 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008730 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008731 default:
8732 EMSG2(_(e_intern2), "f_empty()");
8733 n = 0;
8734 }
8735
8736 rettv->vval.v_number = n;
8737}
8738
8739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 * "escape({string}, {chars})" function
8741 */
8742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008744 typval_T *argvars;
8745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
8747 char_u buf[NUMBUFLEN];
8748
Bram Moolenaar758711c2005-02-02 23:11:38 +00008749 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8750 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752}
8753
8754/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008755 * "eval()" function
8756 */
8757/*ARGSUSED*/
8758 static void
8759f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008760 typval_T *argvars;
8761 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008762{
8763 char_u *s;
8764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765 s = get_tv_string_chk(&argvars[0]);
8766 if (s != NULL)
8767 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008769 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8770 {
8771 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008772 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008774 else if (*s != NUL)
8775 EMSG(_(e_trailing));
8776}
8777
8778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 * "eventhandler()" function
8780 */
8781/*ARGSUSED*/
8782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008784 typval_T *argvars;
8785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789
8790/*
8791 * "executable()" function
8792 */
8793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008795 typval_T *argvars;
8796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800
8801/*
8802 * "exists()" function
8803 */
8804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 typval_T *argvars;
8807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
8809 char_u *p;
8810 char_u *name;
8811 int n = FALSE;
8812 int len = 0;
8813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 if (*p == '$') /* environment variable */
8816 {
8817 /* first try "normal" environment variables (fast) */
8818 if (mch_getenv(p + 1) != NULL)
8819 n = TRUE;
8820 else
8821 {
8822 /* try expanding things like $VIM and ${HOME} */
8823 p = expand_env_save(p);
8824 if (p != NULL && *p != '$')
8825 n = TRUE;
8826 vim_free(p);
8827 }
8828 }
8829 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008832 if (*skipwhite(p) != NUL)
8833 n = FALSE; /* trailing garbage */
8834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 else if (*p == '*') /* internal or user defined function */
8836 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008837 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 }
8839 else if (*p == ':')
8840 {
8841 n = cmd_exists(p + 1);
8842 }
8843 else if (*p == '#')
8844 {
8845#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008846 if (p[1] == '#')
8847 n = autocmd_supported(p + 2);
8848 else
8849 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850#endif
8851 }
8852 else /* internal variable */
8853 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008854 char_u *tofree;
8855 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008857 /* get_name_len() takes care of expanding curly braces */
8858 name = p;
8859 len = get_name_len(&p, &tofree, TRUE, FALSE);
8860 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008862 if (tofree != NULL)
8863 name = tofree;
8864 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8865 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008867 /* handle d.key, l[idx], f(expr) */
8868 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8869 if (n)
8870 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008873 if (*p != NUL)
8874 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008876 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 }
8878
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880}
8881
8882/*
8883 * "expand()" function
8884 */
8885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008886f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008887 typval_T *argvars;
8888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890 char_u *s;
8891 int len;
8892 char_u *errormsg;
8893 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8894 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008897 rettv->v_type = VAR_STRING;
8898 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 if (*s == '%' || *s == '#' || *s == '<')
8900 {
8901 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 --emsg_off;
8904 }
8905 else
8906 {
8907 /* When the optional second argument is non-zero, don't remove matches
8908 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 if (argvars[1].v_type != VAR_UNKNOWN
8910 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008912 if (!error)
8913 {
8914 ExpandInit(&xpc);
8915 xpc.xp_context = EXPAND_FILES;
8916 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 }
8918 else
8919 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 }
8921}
8922
8923/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008924 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008925 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008926 */
8927 static void
8928f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008929 typval_T *argvars;
8930 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008931{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008932 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008933 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008934 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 list_T *l1, *l2;
8936 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008937 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008939
Bram Moolenaare9a41262005-01-15 22:18:47 +00008940 l1 = argvars[0].vval.v_list;
8941 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008942 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8943 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 {
8945 if (argvars[2].v_type != VAR_UNKNOWN)
8946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 before = get_tv_number_chk(&argvars[2], &error);
8948 if (error)
8949 return; /* type error; errmsg already given */
8950
Bram Moolenaar758711c2005-02-02 23:11:38 +00008951 if (before == l1->lv_len)
8952 item = NULL;
8953 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008954 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008955 item = list_find(l1, before);
8956 if (item == NULL)
8957 {
8958 EMSGN(_(e_listidx), before);
8959 return;
8960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008961 }
8962 }
8963 else
8964 item = NULL;
8965 list_extend(l1, l2, item);
8966
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 copy_tv(&argvars[0], rettv);
8968 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008970 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8971 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008972 dict_T *d1, *d2;
8973 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008974 char_u *action;
8975 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008977 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008978
8979 d1 = argvars[0].vval.v_dict;
8980 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008981 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8982 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008983 {
8984 /* Check the third argument. */
8985 if (argvars[2].v_type != VAR_UNKNOWN)
8986 {
8987 static char *(av[]) = {"keep", "force", "error"};
8988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008989 action = get_tv_string_chk(&argvars[2]);
8990 if (action == NULL)
8991 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008992 for (i = 0; i < 3; ++i)
8993 if (STRCMP(action, av[i]) == 0)
8994 break;
8995 if (i == 3)
8996 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008997 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008998 return;
8999 }
9000 }
9001 else
9002 action = (char_u *)"force";
9003
9004 /* Go over all entries in the second dict and add them to the
9005 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009006 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009009 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009011 --todo;
9012 di1 = dict_find(d1, hi2->hi_key, -1);
9013 if (di1 == NULL)
9014 {
9015 di1 = dictitem_copy(HI2DI(hi2));
9016 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9017 dictitem_free(di1);
9018 }
9019 else if (*action == 'e')
9020 {
9021 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9022 break;
9023 }
9024 else if (*action == 'f')
9025 {
9026 clear_tv(&di1->di_tv);
9027 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9028 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009029 }
9030 }
9031
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 copy_tv(&argvars[0], rettv);
9033 }
9034 }
9035 else
9036 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009037}
9038
9039/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009040 * "feedkeys()" function
9041 */
9042/*ARGSUSED*/
9043 static void
9044f_feedkeys(argvars, rettv)
9045 typval_T *argvars;
9046 typval_T *rettv;
9047{
9048 int remap = TRUE;
9049 char_u *keys, *flags;
9050 char_u nbuf[NUMBUFLEN];
9051 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009052 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009053
9054 rettv->vval.v_number = 0;
9055 keys = get_tv_string(&argvars[0]);
9056 if (*keys != NUL)
9057 {
9058 if (argvars[1].v_type != VAR_UNKNOWN)
9059 {
9060 flags = get_tv_string_buf(&argvars[1], nbuf);
9061 for ( ; *flags != NUL; ++flags)
9062 {
9063 switch (*flags)
9064 {
9065 case 'n': remap = FALSE; break;
9066 case 'm': remap = TRUE; break;
9067 case 't': typed = TRUE; break;
9068 }
9069 }
9070 }
9071
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009072 /* Need to escape K_SPECIAL and CSI before putting the string in the
9073 * typeahead buffer. */
9074 keys_esc = vim_strsave_escape_csi(keys);
9075 if (keys_esc != NULL)
9076 {
9077 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009078 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009079 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009080 if (vgetc_busy)
9081 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009082 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009083 }
9084}
9085
9086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 * "filereadable()" function
9088 */
9089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 typval_T *argvars;
9092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093{
9094 FILE *fd;
9095 char_u *p;
9096 int n;
9097
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9100 {
9101 n = TRUE;
9102 fclose(fd);
9103 }
9104 else
9105 n = FALSE;
9106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108}
9109
9110/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009111 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 * rights to write into.
9113 */
9114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_filewritable(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{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009119 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120}
9121
Bram Moolenaar33570922005-01-25 22:26:29 +00009122static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009123
9124 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009125findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009126 typval_T *argvars;
9127 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009128 int dir;
9129{
9130#ifdef FEAT_SEARCHPATH
9131 char_u *fname;
9132 char_u *fresult = NULL;
9133 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9134 char_u *p;
9135 char_u pathbuf[NUMBUFLEN];
9136 int count = 1;
9137 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009138 int error = FALSE;
9139#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009140
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009141 rettv->vval.v_string = NULL;
9142 rettv->v_type = VAR_STRING;
9143
9144#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009146
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009147 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009149 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9150 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009151 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009152 else
9153 {
9154 if (*p != NUL)
9155 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009158 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009159 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009160 }
9161
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009162 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9163 error = TRUE;
9164
9165 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 do
9168 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009169 if (rettv->v_type == VAR_STRING)
9170 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 fresult = find_file_in_path_option(first ? fname : NULL,
9172 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009173 0, first, path, dir, NULL,
9174 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009176
9177 if (fresult != NULL && rettv->v_type == VAR_LIST)
9178 list_append_string(rettv->vval.v_list, fresult, -1);
9179
9180 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009181 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009182
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009183 if (rettv->v_type == VAR_STRING)
9184 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009185#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009186}
9187
Bram Moolenaar33570922005-01-25 22:26:29 +00009188static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9189static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009190
9191/*
9192 * Implementation of map() and filter().
9193 */
9194 static void
9195filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009196 typval_T *argvars;
9197 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009198 int map;
9199{
9200 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009201 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009202 listitem_T *li, *nli;
9203 list_T *l = NULL;
9204 dictitem_T *di;
9205 hashtab_T *ht;
9206 hashitem_T *hi;
9207 dict_T *d = NULL;
9208 typval_T save_val;
9209 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009210 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009211 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009212 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009213 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009214
9215 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009216 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009217 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009218 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009219 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009220 return;
9221 }
9222 else if (argvars[0].v_type == VAR_DICT)
9223 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009224 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009225 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009226 return;
9227 }
9228 else
9229 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009230 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009231 return;
9232 }
9233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 expr = get_tv_string_buf_chk(&argvars[1], buf);
9235 /* On type errors, the preceding call has already displayed an error
9236 * message. Avoid a misleading error message for an empty string that
9237 * was not passed as argument. */
9238 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 prepare_vimvar(VV_VAL, &save_val);
9241 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009242
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009243 /* We reset "did_emsg" to be able to detect whether an error
9244 * occurred during evaluation of the expression. */
9245 save_did_emsg = did_emsg;
9246 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 prepare_vimvar(VV_KEY, &save_key);
9251 vimvars[VV_KEY].vv_type = VAR_STRING;
9252
9253 ht = &d->dv_hashtab;
9254 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009255 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 if (!HASHITEM_EMPTY(hi))
9259 {
9260 --todo;
9261 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009262 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 break;
9264 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009265 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009266 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 break;
9268 if (!map && rem)
9269 dictitem_remove(d, di);
9270 clear_tv(&vimvars[VV_KEY].vv_tv);
9271 }
9272 }
9273 hash_unlock(ht);
9274
9275 restore_vimvar(VV_KEY, &save_key);
9276 }
9277 else
9278 {
9279 for (li = l->lv_first; li != NULL; li = nli)
9280 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009281 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009282 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009283 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009284 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009285 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009286 break;
9287 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009288 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009289 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009290 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009292 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009293
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009294 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009295 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009296
9297 copy_tv(&argvars[0], rettv);
9298}
9299
9300 static int
9301filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009302 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 char_u *expr;
9304 int map;
9305 int *remp;
9306{
Bram Moolenaar33570922005-01-25 22:26:29 +00009307 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009308 char_u *s;
9309
Bram Moolenaar33570922005-01-25 22:26:29 +00009310 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 s = expr;
9312 if (eval1(&s, &rettv, TRUE) == FAIL)
9313 return FAIL;
9314 if (*s != NUL) /* check for trailing chars after expr */
9315 {
9316 EMSG2(_(e_invexpr2), s);
9317 return FAIL;
9318 }
9319 if (map)
9320 {
9321 /* map(): replace the list item value */
9322 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009323 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009324 *tv = rettv;
9325 }
9326 else
9327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009328 int error = FALSE;
9329
Bram Moolenaare9a41262005-01-15 22:18:47 +00009330 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009332 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009333 /* On type error, nothing has been removed; return FAIL to stop the
9334 * loop. The error message was given by get_tv_number_chk(). */
9335 if (error)
9336 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009337 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009338 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009339 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009340}
9341
9342/*
9343 * "filter()" function
9344 */
9345 static void
9346f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009347 typval_T *argvars;
9348 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009349{
9350 filter_map(argvars, rettv, FALSE);
9351}
9352
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009354 * "finddir({fname}[, {path}[, {count}]])" function
9355 */
9356 static void
9357f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009358 typval_T *argvars;
9359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009360{
9361 findfilendir(argvars, rettv, TRUE);
9362}
9363
9364/*
9365 * "findfile({fname}[, {path}[, {count}]])" function
9366 */
9367 static void
9368f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009371{
9372 findfilendir(argvars, rettv, FALSE);
9373}
9374
9375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 * "fnamemodify({fname}, {mods})" function
9377 */
9378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009380 typval_T *argvars;
9381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 char_u *fname;
9384 char_u *mods;
9385 int usedlen = 0;
9386 int len;
9387 char_u *fbuf = NULL;
9388 char_u buf[NUMBUFLEN];
9389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009390 fname = get_tv_string_chk(&argvars[0]);
9391 mods = get_tv_string_buf_chk(&argvars[1], buf);
9392 if (fname == NULL || mods == NULL)
9393 fname = NULL;
9394 else
9395 {
9396 len = (int)STRLEN(fname);
9397 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 vim_free(fbuf);
9406}
9407
Bram Moolenaar33570922005-01-25 22:26:29 +00009408static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409
9410/*
9411 * "foldclosed()" function
9412 */
9413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009415 typval_T *argvars;
9416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 int end;
9418{
9419#ifdef FEAT_FOLDING
9420 linenr_T lnum;
9421 linenr_T first, last;
9422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9425 {
9426 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9427 {
9428 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 return;
9433 }
9434 }
9435#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437}
9438
9439/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009440 * "foldclosed()" function
9441 */
9442 static void
9443f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009444 typval_T *argvars;
9445 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009446{
9447 foldclosed_both(argvars, rettv, FALSE);
9448}
9449
9450/*
9451 * "foldclosedend()" function
9452 */
9453 static void
9454f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009455 typval_T *argvars;
9456 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009457{
9458 foldclosed_both(argvars, rettv, TRUE);
9459}
9460
9461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 * "foldlevel()" function
9463 */
9464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009466 typval_T *argvars;
9467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469#ifdef FEAT_FOLDING
9470 linenr_T lnum;
9471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 else
9476#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478}
9479
9480/*
9481 * "foldtext()" function
9482 */
9483/*ARGSUSED*/
9484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009486 typval_T *argvars;
9487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489#ifdef FEAT_FOLDING
9490 linenr_T lnum;
9491 char_u *s;
9492 char_u *r;
9493 int len;
9494 char *txt;
9495#endif
9496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->v_type = VAR_STRING;
9498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009500 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9501 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9502 <= curbuf->b_ml.ml_line_count
9503 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 {
9505 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009506 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9507 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 {
9509 if (!linewhite(lnum))
9510 break;
9511 ++lnum;
9512 }
9513
9514 /* Find interesting text in this line. */
9515 s = skipwhite(ml_get(lnum));
9516 /* skip C comment-start */
9517 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009520 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009521 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009522 {
9523 s = skipwhite(ml_get(lnum + 1));
9524 if (*s == '*')
9525 s = skipwhite(s + 1);
9526 }
9527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 txt = _("+-%s%3ld lines: ");
9529 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009530 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 + 20 /* for %3ld */
9532 + STRLEN(s))); /* concatenated */
9533 if (r != NULL)
9534 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009535 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9536 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9537 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 len = (int)STRLEN(r);
9539 STRCAT(r, s);
9540 /* remove 'foldmarker' and 'commentstring' */
9541 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 }
9544 }
9545#endif
9546}
9547
9548/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009549 * "foldtextresult(lnum)" function
9550 */
9551/*ARGSUSED*/
9552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009554 typval_T *argvars;
9555 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009556{
9557#ifdef FEAT_FOLDING
9558 linenr_T lnum;
9559 char_u *text;
9560 char_u buf[51];
9561 foldinfo_T foldinfo;
9562 int fold_count;
9563#endif
9564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->v_type = VAR_STRING;
9566 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009567#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 /* treat illegal types and illegal string values for {lnum} the same */
9570 if (lnum < 0)
9571 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572 fold_count = foldedCount(curwin, lnum, &foldinfo);
9573 if (fold_count > 0)
9574 {
9575 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9576 &foldinfo, buf);
9577 if (text == buf)
9578 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009580 }
9581#endif
9582}
9583
9584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 * "foreground()" function
9586 */
9587/*ARGSUSED*/
9588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009590 typval_T *argvars;
9591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594#ifdef FEAT_GUI
9595 if (gui.in_use)
9596 gui_mch_set_foreground();
9597#else
9598# ifdef WIN32
9599 win32_set_foreground();
9600# endif
9601#endif
9602}
9603
9604/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009605 * "function()" function
9606 */
9607/*ARGSUSED*/
9608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 typval_T *argvars;
9611 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612{
9613 char_u *s;
9614
Bram Moolenaara7043832005-01-21 11:56:39 +00009615 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009617 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009618 EMSG2(_(e_invarg2), s);
9619 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009620 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621 else
9622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_string = vim_strsave(s);
9624 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009625 }
9626}
9627
9628/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009629 * "garbagecollect()" function
9630 */
9631/*ARGSUSED*/
9632 static void
9633f_garbagecollect(argvars, rettv)
9634 typval_T *argvars;
9635 typval_T *rettv;
9636{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009637 /* This is postponed until we are back at the toplevel, because we may be
9638 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9639 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009640}
9641
9642/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009643 * "get()" function
9644 */
9645 static void
9646f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009647 typval_T *argvars;
9648 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009649{
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 listitem_T *li;
9651 list_T *l;
9652 dictitem_T *di;
9653 dict_T *d;
9654 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009655
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009657 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009658 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 int error = FALSE;
9661
9662 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9663 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009664 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009665 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009667 else if (argvars[0].v_type == VAR_DICT)
9668 {
9669 if ((d = argvars[0].vval.v_dict) != NULL)
9670 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009671 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009672 if (di != NULL)
9673 tv = &di->di_tv;
9674 }
9675 }
9676 else
9677 EMSG2(_(e_listdictarg), "get()");
9678
9679 if (tv == NULL)
9680 {
9681 if (argvars[2].v_type == VAR_UNKNOWN)
9682 rettv->vval.v_number = 0;
9683 else
9684 copy_tv(&argvars[2], rettv);
9685 }
9686 else
9687 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009688}
9689
Bram Moolenaar342337a2005-07-21 21:11:17 +00009690static 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 +00009691
9692/*
9693 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009694 * Return a range (from start to end) of lines in rettv from the specified
9695 * buffer.
9696 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009697 */
9698 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009699get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009700 buf_T *buf;
9701 linenr_T start;
9702 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009703 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009704 typval_T *rettv;
9705{
9706 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009707
Bram Moolenaar342337a2005-07-21 21:11:17 +00009708 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009709 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009710 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009711 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009712 }
9713 else
9714 rettv->vval.v_number = 0;
9715
9716 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9717 return;
9718
9719 if (!retlist)
9720 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009721 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9722 p = ml_get_buf(buf, start, FALSE);
9723 else
9724 p = (char_u *)"";
9725
9726 rettv->v_type = VAR_STRING;
9727 rettv->vval.v_string = vim_strsave(p);
9728 }
9729 else
9730 {
9731 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009732 return;
9733
9734 if (start < 1)
9735 start = 1;
9736 if (end > buf->b_ml.ml_line_count)
9737 end = buf->b_ml.ml_line_count;
9738 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009739 if (list_append_string(rettv->vval.v_list,
9740 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009741 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009742 }
9743}
9744
9745/*
9746 * "getbufline()" function
9747 */
9748 static void
9749f_getbufline(argvars, rettv)
9750 typval_T *argvars;
9751 typval_T *rettv;
9752{
9753 linenr_T lnum;
9754 linenr_T end;
9755 buf_T *buf;
9756
9757 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9758 ++emsg_off;
9759 buf = get_buf_tv(&argvars[0]);
9760 --emsg_off;
9761
Bram Moolenaar661b1822005-07-28 22:36:45 +00009762 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009763 if (argvars[2].v_type == VAR_UNKNOWN)
9764 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009765 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009766 end = get_tv_lnum_buf(&argvars[2], buf);
9767
Bram Moolenaar342337a2005-07-21 21:11:17 +00009768 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009769}
9770
Bram Moolenaar0d660222005-01-07 21:51:51 +00009771/*
9772 * "getbufvar()" function
9773 */
9774 static void
9775f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009776 typval_T *argvars;
9777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009778{
9779 buf_T *buf;
9780 buf_T *save_curbuf;
9781 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009782 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9785 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009786 ++emsg_off;
9787 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009788
9789 rettv->v_type = VAR_STRING;
9790 rettv->vval.v_string = NULL;
9791
9792 if (buf != NULL && varname != NULL)
9793 {
9794 if (*varname == '&') /* buffer-local-option */
9795 {
9796 /* set curbuf to be our buf, temporarily */
9797 save_curbuf = curbuf;
9798 curbuf = buf;
9799
9800 get_option_tv(&varname, rettv, TRUE);
9801
9802 /* restore previous notion of curbuf */
9803 curbuf = save_curbuf;
9804 }
9805 else
9806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009807 if (*varname == NUL)
9808 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9809 * scope prefix before the NUL byte is required by
9810 * find_var_in_ht(). */
9811 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009812 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009813 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009814 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009815 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009816 }
9817 }
9818
9819 --emsg_off;
9820}
9821
9822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 * "getchar()" function
9824 */
9825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009827 typval_T *argvars;
9828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009831 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009833 /* Position the cursor. Needed after a message that ends in a space. */
9834 windgoto(msg_row, msg_col);
9835
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 ++no_mapping;
9837 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009838 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 /* getchar(): blocking wait. */
9840 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009841 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 /* getchar(1): only check if char avail */
9843 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 else if (error || vpeekc() == NUL)
9845 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 n = 0;
9847 else
9848 /* getchar(0) and char avail: return char */
9849 n = safe_vgetc();
9850 --no_mapping;
9851 --allow_keys;
9852
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 if (IS_SPECIAL(n) || mod_mask != 0)
9855 {
9856 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9857 int i = 0;
9858
9859 /* Turn a special key into three bytes, plus modifier. */
9860 if (mod_mask != 0)
9861 {
9862 temp[i++] = K_SPECIAL;
9863 temp[i++] = KS_MODIFIER;
9864 temp[i++] = mod_mask;
9865 }
9866 if (IS_SPECIAL(n))
9867 {
9868 temp[i++] = K_SPECIAL;
9869 temp[i++] = K_SECOND(n);
9870 temp[i++] = K_THIRD(n);
9871 }
9872#ifdef FEAT_MBYTE
9873 else if (has_mbyte)
9874 i += (*mb_char2bytes)(n, temp + i);
9875#endif
9876 else
9877 temp[i++] = n;
9878 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879 rettv->v_type = VAR_STRING;
9880 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 }
9882}
9883
9884/*
9885 * "getcharmod()" function
9886 */
9887/*ARGSUSED*/
9888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 typval_T *argvars;
9891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009893 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894}
9895
9896/*
9897 * "getcmdline()" function
9898 */
9899/*ARGSUSED*/
9900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 typval_T *argvars;
9903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905 rettv->v_type = VAR_STRING;
9906 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907}
9908
9909/*
9910 * "getcmdpos()" function
9911 */
9912/*ARGSUSED*/
9913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 typval_T *argvars;
9916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919}
9920
9921/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009922 * "getcmdtype()" function
9923 */
9924/*ARGSUSED*/
9925 static void
9926f_getcmdtype(argvars, rettv)
9927 typval_T *argvars;
9928 typval_T *rettv;
9929{
9930 rettv->v_type = VAR_STRING;
9931 rettv->vval.v_string = alloc(2);
9932 if (rettv->vval.v_string != NULL)
9933 {
9934 rettv->vval.v_string[0] = get_cmdline_type();
9935 rettv->vval.v_string[1] = NUL;
9936 }
9937}
9938
9939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 * "getcwd()" function
9941 */
9942/*ARGSUSED*/
9943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 typval_T *argvars;
9946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
9948 char_u cwd[MAXPATHL];
9949
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009950 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 else
9954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009957 if (rettv->vval.v_string != NULL)
9958 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959#endif
9960 }
9961}
9962
9963/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009964 * "getfontname()" function
9965 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009966/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 typval_T *argvars;
9970 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 rettv->v_type = VAR_STRING;
9973 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009974#ifdef FEAT_GUI
9975 if (gui.in_use)
9976 {
9977 GuiFont font;
9978 char_u *name = NULL;
9979
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009980 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009981 {
9982 /* Get the "Normal" font. Either the name saved by
9983 * hl_set_font_name() or from the font ID. */
9984 font = gui.norm_font;
9985 name = hl_get_font_name();
9986 }
9987 else
9988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009990 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9991 return;
9992 font = gui_mch_get_font(name, FALSE);
9993 if (font == NOFONT)
9994 return; /* Invalid font name, return empty string. */
9995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009997 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009998 gui_mch_free_font(font);
9999 }
10000#endif
10001}
10002
10003/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010004 * "getfperm({fname})" function
10005 */
10006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010008 typval_T *argvars;
10009 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010010{
10011 char_u *fname;
10012 struct stat st;
10013 char_u *perm = NULL;
10014 char_u flags[] = "rwx";
10015 int i;
10016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010020 if (mch_stat((char *)fname, &st) >= 0)
10021 {
10022 perm = vim_strsave((char_u *)"---------");
10023 if (perm != NULL)
10024 {
10025 for (i = 0; i < 9; i++)
10026 {
10027 if (st.st_mode & (1 << (8 - i)))
10028 perm[i] = flags[i % 3];
10029 }
10030 }
10031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010033}
10034
10035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 * "getfsize({fname})" function
10037 */
10038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *argvars;
10041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
10043 char_u *fname;
10044 struct stat st;
10045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
10050 if (mch_stat((char *)fname, &st) >= 0)
10051 {
10052 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 }
10057 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059}
10060
10061/*
10062 * "getftime({fname})" function
10063 */
10064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010066 typval_T *argvars;
10067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068{
10069 char_u *fname;
10070 struct stat st;
10071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
10074 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
10080/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010081 * "getftype({fname})" function
10082 */
10083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010085 typval_T *argvars;
10086 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010087{
10088 char_u *fname;
10089 struct stat st;
10090 char_u *type = NULL;
10091 char *t;
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_lstat((char *)fname, &st) >= 0)
10097 {
10098#ifdef S_ISREG
10099 if (S_ISREG(st.st_mode))
10100 t = "file";
10101 else if (S_ISDIR(st.st_mode))
10102 t = "dir";
10103# ifdef S_ISLNK
10104 else if (S_ISLNK(st.st_mode))
10105 t = "link";
10106# endif
10107# ifdef S_ISBLK
10108 else if (S_ISBLK(st.st_mode))
10109 t = "bdev";
10110# endif
10111# ifdef S_ISCHR
10112 else if (S_ISCHR(st.st_mode))
10113 t = "cdev";
10114# endif
10115# ifdef S_ISFIFO
10116 else if (S_ISFIFO(st.st_mode))
10117 t = "fifo";
10118# endif
10119# ifdef S_ISSOCK
10120 else if (S_ISSOCK(st.st_mode))
10121 t = "fifo";
10122# endif
10123 else
10124 t = "other";
10125#else
10126# ifdef S_IFMT
10127 switch (st.st_mode & S_IFMT)
10128 {
10129 case S_IFREG: t = "file"; break;
10130 case S_IFDIR: t = "dir"; break;
10131# ifdef S_IFLNK
10132 case S_IFLNK: t = "link"; break;
10133# endif
10134# ifdef S_IFBLK
10135 case S_IFBLK: t = "bdev"; break;
10136# endif
10137# ifdef S_IFCHR
10138 case S_IFCHR: t = "cdev"; break;
10139# endif
10140# ifdef S_IFIFO
10141 case S_IFIFO: t = "fifo"; break;
10142# endif
10143# ifdef S_IFSOCK
10144 case S_IFSOCK: t = "socket"; break;
10145# endif
10146 default: t = "other";
10147 }
10148# else
10149 if (mch_isdir(fname))
10150 t = "dir";
10151 else
10152 t = "file";
10153# endif
10154#endif
10155 type = vim_strsave((char_u *)t);
10156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010158}
10159
10160/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010161 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010162 */
10163 static void
10164f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010165 typval_T *argvars;
10166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010167{
10168 linenr_T lnum;
10169 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010170 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010171
10172 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010173 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010174 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010175 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010176 retlist = FALSE;
10177 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010178 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010179 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010180 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010181 retlist = TRUE;
10182 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010183
Bram Moolenaar342337a2005-07-21 21:11:17 +000010184 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010185}
10186
10187/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010188 * "getpos(string)" function
10189 */
10190 static void
10191f_getpos(argvars, rettv)
10192 typval_T *argvars;
10193 typval_T *rettv;
10194{
10195 pos_T *fp;
10196 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010197 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010198
10199 if (rettv_list_alloc(rettv) == OK)
10200 {
10201 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010202 fp = var2fpos(&argvars[0], TRUE, &fnum);
10203 if (fnum != -1)
10204 list_append_number(l, (varnumber_T)fnum);
10205 else
10206 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010207 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10208 : (varnumber_T)0);
10209 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10210 : (varnumber_T)0);
10211 list_append_number(l,
10212#ifdef FEAT_VIRTUALEDIT
10213 (fp != NULL) ? (varnumber_T)fp->coladd :
10214#endif
10215 (varnumber_T)0);
10216 }
10217 else
10218 rettv->vval.v_number = FALSE;
10219}
10220
10221/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010222 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010223 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010224/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010225 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010226f_getqflist(argvars, rettv)
10227 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010228 typval_T *rettv;
10229{
10230#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010231 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010232#endif
10233
10234 rettv->vval.v_number = FALSE;
10235#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010236 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010237 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010238 wp = NULL;
10239 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10240 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010241 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010242 if (wp == NULL)
10243 return;
10244 }
10245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010246 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010247 }
10248#endif
10249}
10250
10251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 * "getreg()" function
10253 */
10254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010256 typval_T *argvars;
10257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
10259 char_u *strregname;
10260 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010261 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010264 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 strregname = get_tv_string_chk(&argvars[0]);
10267 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010268 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010269 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 regname = (strregname == NULL ? '"' : *strregname);
10274 if (regname == 0)
10275 regname = '"';
10276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 rettv->vval.v_string = error ? NULL :
10279 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280}
10281
10282/*
10283 * "getregtype()" function
10284 */
10285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 typval_T *argvars;
10288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289{
10290 char_u *strregname;
10291 int regname;
10292 char_u buf[NUMBUFLEN + 2];
10293 long reglen = 0;
10294
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010295 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 {
10297 strregname = get_tv_string_chk(&argvars[0]);
10298 if (strregname == NULL) /* type error; errmsg already given */
10299 {
10300 rettv->v_type = VAR_STRING;
10301 rettv->vval.v_string = NULL;
10302 return;
10303 }
10304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 else
10306 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308
10309 regname = (strregname == NULL ? '"' : *strregname);
10310 if (regname == 0)
10311 regname = '"';
10312
10313 buf[0] = NUL;
10314 buf[1] = NUL;
10315 switch (get_reg_type(regname, &reglen))
10316 {
10317 case MLINE: buf[0] = 'V'; break;
10318 case MCHAR: buf[0] = 'v'; break;
10319#ifdef FEAT_VISUAL
10320 case MBLOCK:
10321 buf[0] = Ctrl_V;
10322 sprintf((char *)buf + 1, "%ld", reglen + 1);
10323 break;
10324#endif
10325 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 rettv->v_type = VAR_STRING;
10327 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328}
10329
10330/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010331 * "gettabwinvar()" function
10332 */
10333 static void
10334f_gettabwinvar(argvars, rettv)
10335 typval_T *argvars;
10336 typval_T *rettv;
10337{
10338 getwinvar(argvars, rettv, 1);
10339}
10340
10341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 * "getwinposx()" function
10343 */
10344/*ARGSUSED*/
10345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010347 typval_T *argvars;
10348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351#ifdef FEAT_GUI
10352 if (gui.in_use)
10353 {
10354 int x, y;
10355
10356 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
10359#endif
10360}
10361
10362/*
10363 * "getwinposy()" function
10364 */
10365/*ARGSUSED*/
10366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010367f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010368 typval_T *argvars;
10369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372#ifdef FEAT_GUI
10373 if (gui.in_use)
10374 {
10375 int x, y;
10376
10377 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 }
10380#endif
10381}
10382
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010383/*
10384 * Find window specifed by "vp" in tabpage "tp".
10385 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010386 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010387find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010388 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010389 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010390{
10391#ifdef FEAT_WINDOWS
10392 win_T *wp;
10393#endif
10394 int nr;
10395
10396 nr = get_tv_number_chk(vp, NULL);
10397
10398#ifdef FEAT_WINDOWS
10399 if (nr < 0)
10400 return NULL;
10401 if (nr == 0)
10402 return curwin;
10403
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010404 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10405 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010406 if (--nr <= 0)
10407 break;
10408 return wp;
10409#else
10410 if (nr == 0 || nr == 1)
10411 return curwin;
10412 return NULL;
10413#endif
10414}
10415
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416/*
10417 * "getwinvar()" function
10418 */
10419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010420f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010421 typval_T *argvars;
10422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010424 getwinvar(argvars, rettv, 0);
10425}
10426
10427/*
10428 * getwinvar() and gettabwinvar()
10429 */
10430 static void
10431getwinvar(argvars, rettv, off)
10432 typval_T *argvars;
10433 typval_T *rettv;
10434 int off; /* 1 for gettabwinvar() */
10435{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 win_T *win, *oldcurwin;
10437 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010438 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010439 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010441#ifdef FEAT_WINDOWS
10442 if (off == 1)
10443 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10444 else
10445 tp = curtab;
10446#endif
10447 win = find_win_by_nr(&argvars[off], tp);
10448 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451 rettv->v_type = VAR_STRING;
10452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453
10454 if (win != NULL && varname != NULL)
10455 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010456 /* Set curwin to be our win, temporarily. Also set curbuf, so
10457 * that we can get buffer-local options. */
10458 oldcurwin = curwin;
10459 curwin = win;
10460 curbuf = win->w_buffer;
10461
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 else
10465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 if (*varname == NUL)
10467 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10468 * scope prefix before the NUL byte is required by
10469 * find_var_in_ht(). */
10470 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010472 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010474 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010476
10477 /* restore previous notion of curwin */
10478 curwin = oldcurwin;
10479 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 }
10481
10482 --emsg_off;
10483}
10484
10485/*
10486 * "glob()" function
10487 */
10488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010490 typval_T *argvars;
10491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 expand_T xpc;
10494
10495 ExpandInit(&xpc);
10496 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497 rettv->v_type = VAR_STRING;
10498 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500}
10501
10502/*
10503 * "globpath()" function
10504 */
10505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 typval_T *argvars;
10508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 if (file == NULL)
10515 rettv->vval.v_string = NULL;
10516 else
10517 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518}
10519
10520/*
10521 * "has()" function
10522 */
10523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010525 typval_T *argvars;
10526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527{
10528 int i;
10529 char_u *name;
10530 int n = FALSE;
10531 static char *(has_list[]) =
10532 {
10533#ifdef AMIGA
10534 "amiga",
10535# ifdef FEAT_ARP
10536 "arp",
10537# endif
10538#endif
10539#ifdef __BEOS__
10540 "beos",
10541#endif
10542#ifdef MSDOS
10543# ifdef DJGPP
10544 "dos32",
10545# else
10546 "dos16",
10547# endif
10548#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010549#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 "mac",
10551#endif
10552#if defined(MACOS_X_UNIX)
10553 "macunix",
10554#endif
10555#ifdef OS2
10556 "os2",
10557#endif
10558#ifdef __QNX__
10559 "qnx",
10560#endif
10561#ifdef RISCOS
10562 "riscos",
10563#endif
10564#ifdef UNIX
10565 "unix",
10566#endif
10567#ifdef VMS
10568 "vms",
10569#endif
10570#ifdef WIN16
10571 "win16",
10572#endif
10573#ifdef WIN32
10574 "win32",
10575#endif
10576#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10577 "win32unix",
10578#endif
10579#ifdef WIN64
10580 "win64",
10581#endif
10582#ifdef EBCDIC
10583 "ebcdic",
10584#endif
10585#ifndef CASE_INSENSITIVE_FILENAME
10586 "fname_case",
10587#endif
10588#ifdef FEAT_ARABIC
10589 "arabic",
10590#endif
10591#ifdef FEAT_AUTOCMD
10592 "autocmd",
10593#endif
10594#ifdef FEAT_BEVAL
10595 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010596# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10597 "balloon_multiline",
10598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599#endif
10600#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10601 "builtin_terms",
10602# ifdef ALL_BUILTIN_TCAPS
10603 "all_builtin_terms",
10604# endif
10605#endif
10606#ifdef FEAT_BYTEOFF
10607 "byte_offset",
10608#endif
10609#ifdef FEAT_CINDENT
10610 "cindent",
10611#endif
10612#ifdef FEAT_CLIENTSERVER
10613 "clientserver",
10614#endif
10615#ifdef FEAT_CLIPBOARD
10616 "clipboard",
10617#endif
10618#ifdef FEAT_CMDL_COMPL
10619 "cmdline_compl",
10620#endif
10621#ifdef FEAT_CMDHIST
10622 "cmdline_hist",
10623#endif
10624#ifdef FEAT_COMMENTS
10625 "comments",
10626#endif
10627#ifdef FEAT_CRYPT
10628 "cryptv",
10629#endif
10630#ifdef FEAT_CSCOPE
10631 "cscope",
10632#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010633#ifdef CURSOR_SHAPE
10634 "cursorshape",
10635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636#ifdef DEBUG
10637 "debug",
10638#endif
10639#ifdef FEAT_CON_DIALOG
10640 "dialog_con",
10641#endif
10642#ifdef FEAT_GUI_DIALOG
10643 "dialog_gui",
10644#endif
10645#ifdef FEAT_DIFF
10646 "diff",
10647#endif
10648#ifdef FEAT_DIGRAPHS
10649 "digraphs",
10650#endif
10651#ifdef FEAT_DND
10652 "dnd",
10653#endif
10654#ifdef FEAT_EMACS_TAGS
10655 "emacs_tags",
10656#endif
10657 "eval", /* always present, of course! */
10658#ifdef FEAT_EX_EXTRA
10659 "ex_extra",
10660#endif
10661#ifdef FEAT_SEARCH_EXTRA
10662 "extra_search",
10663#endif
10664#ifdef FEAT_FKMAP
10665 "farsi",
10666#endif
10667#ifdef FEAT_SEARCHPATH
10668 "file_in_path",
10669#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010670#if defined(UNIX) && !defined(USE_SYSTEM)
10671 "filterpipe",
10672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673#ifdef FEAT_FIND_ID
10674 "find_in_path",
10675#endif
10676#ifdef FEAT_FOLDING
10677 "folding",
10678#endif
10679#ifdef FEAT_FOOTER
10680 "footer",
10681#endif
10682#if !defined(USE_SYSTEM) && defined(UNIX)
10683 "fork",
10684#endif
10685#ifdef FEAT_GETTEXT
10686 "gettext",
10687#endif
10688#ifdef FEAT_GUI
10689 "gui",
10690#endif
10691#ifdef FEAT_GUI_ATHENA
10692# ifdef FEAT_GUI_NEXTAW
10693 "gui_neXtaw",
10694# else
10695 "gui_athena",
10696# endif
10697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698#ifdef FEAT_GUI_GTK
10699 "gui_gtk",
10700# ifdef HAVE_GTK2
10701 "gui_gtk2",
10702# endif
10703#endif
10704#ifdef FEAT_GUI_MAC
10705 "gui_mac",
10706#endif
10707#ifdef FEAT_GUI_MOTIF
10708 "gui_motif",
10709#endif
10710#ifdef FEAT_GUI_PHOTON
10711 "gui_photon",
10712#endif
10713#ifdef FEAT_GUI_W16
10714 "gui_win16",
10715#endif
10716#ifdef FEAT_GUI_W32
10717 "gui_win32",
10718#endif
10719#ifdef FEAT_HANGULIN
10720 "hangul_input",
10721#endif
10722#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10723 "iconv",
10724#endif
10725#ifdef FEAT_INS_EXPAND
10726 "insert_expand",
10727#endif
10728#ifdef FEAT_JUMPLIST
10729 "jumplist",
10730#endif
10731#ifdef FEAT_KEYMAP
10732 "keymap",
10733#endif
10734#ifdef FEAT_LANGMAP
10735 "langmap",
10736#endif
10737#ifdef FEAT_LIBCALL
10738 "libcall",
10739#endif
10740#ifdef FEAT_LINEBREAK
10741 "linebreak",
10742#endif
10743#ifdef FEAT_LISP
10744 "lispindent",
10745#endif
10746#ifdef FEAT_LISTCMDS
10747 "listcmds",
10748#endif
10749#ifdef FEAT_LOCALMAP
10750 "localmap",
10751#endif
10752#ifdef FEAT_MENU
10753 "menu",
10754#endif
10755#ifdef FEAT_SESSION
10756 "mksession",
10757#endif
10758#ifdef FEAT_MODIFY_FNAME
10759 "modify_fname",
10760#endif
10761#ifdef FEAT_MOUSE
10762 "mouse",
10763#endif
10764#ifdef FEAT_MOUSESHAPE
10765 "mouseshape",
10766#endif
10767#if defined(UNIX) || defined(VMS)
10768# ifdef FEAT_MOUSE_DEC
10769 "mouse_dec",
10770# endif
10771# ifdef FEAT_MOUSE_GPM
10772 "mouse_gpm",
10773# endif
10774# ifdef FEAT_MOUSE_JSB
10775 "mouse_jsbterm",
10776# endif
10777# ifdef FEAT_MOUSE_NET
10778 "mouse_netterm",
10779# endif
10780# ifdef FEAT_MOUSE_PTERM
10781 "mouse_pterm",
10782# endif
10783# ifdef FEAT_MOUSE_XTERM
10784 "mouse_xterm",
10785# endif
10786#endif
10787#ifdef FEAT_MBYTE
10788 "multi_byte",
10789#endif
10790#ifdef FEAT_MBYTE_IME
10791 "multi_byte_ime",
10792#endif
10793#ifdef FEAT_MULTI_LANG
10794 "multi_lang",
10795#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010796#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010797#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010798 "mzscheme",
10799#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801#ifdef FEAT_OLE
10802 "ole",
10803#endif
10804#ifdef FEAT_OSFILETYPE
10805 "osfiletype",
10806#endif
10807#ifdef FEAT_PATH_EXTRA
10808 "path_extra",
10809#endif
10810#ifdef FEAT_PERL
10811#ifndef DYNAMIC_PERL
10812 "perl",
10813#endif
10814#endif
10815#ifdef FEAT_PYTHON
10816#ifndef DYNAMIC_PYTHON
10817 "python",
10818#endif
10819#endif
10820#ifdef FEAT_POSTSCRIPT
10821 "postscript",
10822#endif
10823#ifdef FEAT_PRINTER
10824 "printer",
10825#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010826#ifdef FEAT_PROFILE
10827 "profile",
10828#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010829#ifdef FEAT_RELTIME
10830 "reltime",
10831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832#ifdef FEAT_QUICKFIX
10833 "quickfix",
10834#endif
10835#ifdef FEAT_RIGHTLEFT
10836 "rightleft",
10837#endif
10838#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10839 "ruby",
10840#endif
10841#ifdef FEAT_SCROLLBIND
10842 "scrollbind",
10843#endif
10844#ifdef FEAT_CMDL_INFO
10845 "showcmd",
10846 "cmdline_info",
10847#endif
10848#ifdef FEAT_SIGNS
10849 "signs",
10850#endif
10851#ifdef FEAT_SMARTINDENT
10852 "smartindent",
10853#endif
10854#ifdef FEAT_SNIFF
10855 "sniff",
10856#endif
10857#ifdef FEAT_STL_OPT
10858 "statusline",
10859#endif
10860#ifdef FEAT_SUN_WORKSHOP
10861 "sun_workshop",
10862#endif
10863#ifdef FEAT_NETBEANS_INTG
10864 "netbeans_intg",
10865#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010866#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010867 "spell",
10868#endif
10869#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 "syntax",
10871#endif
10872#if defined(USE_SYSTEM) || !defined(UNIX)
10873 "system",
10874#endif
10875#ifdef FEAT_TAG_BINS
10876 "tag_binary",
10877#endif
10878#ifdef FEAT_TAG_OLDSTATIC
10879 "tag_old_static",
10880#endif
10881#ifdef FEAT_TAG_ANYWHITE
10882 "tag_any_white",
10883#endif
10884#ifdef FEAT_TCL
10885# ifndef DYNAMIC_TCL
10886 "tcl",
10887# endif
10888#endif
10889#ifdef TERMINFO
10890 "terminfo",
10891#endif
10892#ifdef FEAT_TERMRESPONSE
10893 "termresponse",
10894#endif
10895#ifdef FEAT_TEXTOBJ
10896 "textobjects",
10897#endif
10898#ifdef HAVE_TGETENT
10899 "tgetent",
10900#endif
10901#ifdef FEAT_TITLE
10902 "title",
10903#endif
10904#ifdef FEAT_TOOLBAR
10905 "toolbar",
10906#endif
10907#ifdef FEAT_USR_CMDS
10908 "user-commands", /* was accidentally included in 5.4 */
10909 "user_commands",
10910#endif
10911#ifdef FEAT_VIMINFO
10912 "viminfo",
10913#endif
10914#ifdef FEAT_VERTSPLIT
10915 "vertsplit",
10916#endif
10917#ifdef FEAT_VIRTUALEDIT
10918 "virtualedit",
10919#endif
10920#ifdef FEAT_VISUAL
10921 "visual",
10922#endif
10923#ifdef FEAT_VISUALEXTRA
10924 "visualextra",
10925#endif
10926#ifdef FEAT_VREPLACE
10927 "vreplace",
10928#endif
10929#ifdef FEAT_WILDIGN
10930 "wildignore",
10931#endif
10932#ifdef FEAT_WILDMENU
10933 "wildmenu",
10934#endif
10935#ifdef FEAT_WINDOWS
10936 "windows",
10937#endif
10938#ifdef FEAT_WAK
10939 "winaltkeys",
10940#endif
10941#ifdef FEAT_WRITEBACKUP
10942 "writebackup",
10943#endif
10944#ifdef FEAT_XIM
10945 "xim",
10946#endif
10947#ifdef FEAT_XFONTSET
10948 "xfontset",
10949#endif
10950#ifdef USE_XSMP
10951 "xsmp",
10952#endif
10953#ifdef USE_XSMP_INTERACT
10954 "xsmp_interact",
10955#endif
10956#ifdef FEAT_XCLIPBOARD
10957 "xterm_clipboard",
10958#endif
10959#ifdef FEAT_XTERM_SAVE
10960 "xterm_save",
10961#endif
10962#if defined(UNIX) && defined(FEAT_X11)
10963 "X11",
10964#endif
10965 NULL
10966 };
10967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 for (i = 0; has_list[i] != NULL; ++i)
10970 if (STRICMP(name, has_list[i]) == 0)
10971 {
10972 n = TRUE;
10973 break;
10974 }
10975
10976 if (n == FALSE)
10977 {
10978 if (STRNICMP(name, "patch", 5) == 0)
10979 n = has_patch(atoi((char *)name + 5));
10980 else if (STRICMP(name, "vim_starting") == 0)
10981 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010982#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10983 else if (STRICMP(name, "balloon_multiline") == 0)
10984 n = multiline_balloon_available();
10985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986#ifdef DYNAMIC_TCL
10987 else if (STRICMP(name, "tcl") == 0)
10988 n = tcl_enabled(FALSE);
10989#endif
10990#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10991 else if (STRICMP(name, "iconv") == 0)
10992 n = iconv_enabled(FALSE);
10993#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010994#ifdef DYNAMIC_MZSCHEME
10995 else if (STRICMP(name, "mzscheme") == 0)
10996 n = mzscheme_enabled(FALSE);
10997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998#ifdef DYNAMIC_RUBY
10999 else if (STRICMP(name, "ruby") == 0)
11000 n = ruby_enabled(FALSE);
11001#endif
11002#ifdef DYNAMIC_PYTHON
11003 else if (STRICMP(name, "python") == 0)
11004 n = python_enabled(FALSE);
11005#endif
11006#ifdef DYNAMIC_PERL
11007 else if (STRICMP(name, "perl") == 0)
11008 n = perl_enabled(FALSE);
11009#endif
11010#ifdef FEAT_GUI
11011 else if (STRICMP(name, "gui_running") == 0)
11012 n = (gui.in_use || gui.starting);
11013# ifdef FEAT_GUI_W32
11014 else if (STRICMP(name, "gui_win32s") == 0)
11015 n = gui_is_win32s();
11016# endif
11017# ifdef FEAT_BROWSE
11018 else if (STRICMP(name, "browse") == 0)
11019 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11020# endif
11021#endif
11022#ifdef FEAT_SYN_HL
11023 else if (STRICMP(name, "syntax_items") == 0)
11024 n = syntax_present(curbuf);
11025#endif
11026#if defined(WIN3264)
11027 else if (STRICMP(name, "win95") == 0)
11028 n = mch_windows95();
11029#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011030#ifdef FEAT_NETBEANS_INTG
11031 else if (STRICMP(name, "netbeans_enabled") == 0)
11032 n = usingNetbeans;
11033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 }
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037}
11038
11039/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011040 * "has_key()" function
11041 */
11042 static void
11043f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011044 typval_T *argvars;
11045 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011046{
11047 rettv->vval.v_number = 0;
11048 if (argvars[0].v_type != VAR_DICT)
11049 {
11050 EMSG(_(e_dictreq));
11051 return;
11052 }
11053 if (argvars[0].vval.v_dict == NULL)
11054 return;
11055
11056 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011057 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011058}
11059
11060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 * "hasmapto()" function
11062 */
11063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011065 typval_T *argvars;
11066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067{
11068 char_u *name;
11069 char_u *mode;
11070 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011071 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011074 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 mode = (char_u *)"nvo";
11076 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011079 if (argvars[2].v_type != VAR_UNKNOWN)
11080 abbr = get_tv_number(&argvars[2]);
11081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082
Bram Moolenaar2c932302006-03-18 21:42:09 +000011083 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087}
11088
11089/*
11090 * "histadd()" function
11091 */
11092/*ARGSUSED*/
11093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011095 typval_T *argvars;
11096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097{
11098#ifdef FEAT_CMDHIST
11099 int histype;
11100 char_u *str;
11101 char_u buf[NUMBUFLEN];
11102#endif
11103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 if (check_restricted() || check_secure())
11106 return;
11107#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011108 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11109 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 if (histype >= 0)
11111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 if (*str != NUL)
11114 {
11115 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 return;
11118 }
11119 }
11120#endif
11121}
11122
11123/*
11124 * "histdel()" function
11125 */
11126/*ARGSUSED*/
11127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
11132#ifdef FEAT_CMDHIST
11133 int n;
11134 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011137 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11138 if (str == NULL)
11139 n = 0;
11140 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011143 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 else
11148 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011149 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 get_tv_string_buf(&argvars[1], buf));
11151 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154#endif
11155}
11156
11157/*
11158 * "histget()" function
11159 */
11160/*ARGSUSED*/
11161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 typval_T *argvars;
11164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165{
11166#ifdef FEAT_CMDHIST
11167 int type;
11168 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11172 if (str == NULL)
11173 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 {
11176 type = get_histtype(str);
11177 if (argvars[1].v_type == VAR_UNKNOWN)
11178 idx = get_history_idx(type);
11179 else
11180 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11181 /* -1 on type error */
11182 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188}
11189
11190/*
11191 * "histnr()" function
11192 */
11193/*ARGSUSED*/
11194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 typval_T *argvars;
11197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198{
11199 int i;
11200
11201#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 char_u *history = get_tv_string_chk(&argvars[0]);
11203
11204 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 if (i >= HIST_CMD && i < HIST_COUNT)
11206 i = get_history_idx(i);
11207 else
11208#endif
11209 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211}
11212
11213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 * "highlightID(name)" function
11215 */
11216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011218 typval_T *argvars;
11219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222}
11223
11224/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011225 * "highlight_exists()" function
11226 */
11227 static void
11228f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011229 typval_T *argvars;
11230 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011231{
11232 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11233}
11234
11235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 * "hostname()" function
11237 */
11238/*ARGSUSED*/
11239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011241 typval_T *argvars;
11242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243{
11244 char_u hostname[256];
11245
11246 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 rettv->v_type = VAR_STRING;
11248 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249}
11250
11251/*
11252 * iconv() function
11253 */
11254/*ARGSUSED*/
11255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011257 typval_T *argvars;
11258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259{
11260#ifdef FEAT_MBYTE
11261 char_u buf1[NUMBUFLEN];
11262 char_u buf2[NUMBUFLEN];
11263 char_u *from, *to, *str;
11264 vimconv_T vimconv;
11265#endif
11266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 rettv->v_type = VAR_STRING;
11268 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269
11270#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271 str = get_tv_string(&argvars[0]);
11272 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11273 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 vimconv.vc_type = CONV_NONE;
11275 convert_setup(&vimconv, from, to);
11276
11277 /* If the encodings are equal, no conversion needed. */
11278 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282
11283 convert_setup(&vimconv, NULL, NULL);
11284 vim_free(from);
11285 vim_free(to);
11286#endif
11287}
11288
11289/*
11290 * "indent()" function
11291 */
11292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293f_indent(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{
11297 linenr_T lnum;
11298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304}
11305
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011306/*
11307 * "index()" function
11308 */
11309 static void
11310f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011311 typval_T *argvars;
11312 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011313{
Bram Moolenaar33570922005-01-25 22:26:29 +000011314 list_T *l;
11315 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011316 long idx = 0;
11317 int ic = FALSE;
11318
11319 rettv->vval.v_number = -1;
11320 if (argvars[0].v_type != VAR_LIST)
11321 {
11322 EMSG(_(e_listreq));
11323 return;
11324 }
11325 l = argvars[0].vval.v_list;
11326 if (l != NULL)
11327 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011328 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011329 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 int error = FALSE;
11332
Bram Moolenaar758711c2005-02-02 23:11:38 +000011333 /* Start at specified item. Use the cached index that list_find()
11334 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011336 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011337 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 ic = get_tv_number_chk(&argvars[3], &error);
11339 if (error)
11340 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011341 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011342
Bram Moolenaar758711c2005-02-02 23:11:38 +000011343 for ( ; item != NULL; item = item->li_next, ++idx)
11344 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011345 {
11346 rettv->vval.v_number = idx;
11347 break;
11348 }
11349 }
11350}
11351
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352static int inputsecret_flag = 0;
11353
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011354static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11355
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011357 * This function is used by f_input() and f_inputdialog() functions. The third
11358 * argument to f_input() specifies the type of completion to use at the
11359 * prompt. The third argument to f_inputdialog() specifies the value to return
11360 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 */
11362 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011363get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *argvars;
11365 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011366 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 char_u *p = NULL;
11370 int c;
11371 char_u buf[NUMBUFLEN];
11372 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011374 int xp_type = EXPAND_NOTHING;
11375 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378
11379#ifdef NO_CONSOLE_INPUT
11380 /* While starting up, there is no place to enter text. */
11381 if (no_console_input())
11382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384 return;
11385 }
11386#endif
11387
11388 cmd_silent = FALSE; /* Want to see the prompt. */
11389 if (prompt != NULL)
11390 {
11391 /* Only the part of the message after the last NL is considered as
11392 * prompt for the command line */
11393 p = vim_strrchr(prompt, '\n');
11394 if (p == NULL)
11395 p = prompt;
11396 else
11397 {
11398 ++p;
11399 c = *p;
11400 *p = NUL;
11401 msg_start();
11402 msg_clr_eos();
11403 msg_puts_attr(prompt, echo_attr);
11404 msg_didout = FALSE;
11405 msg_starthere();
11406 *p = c;
11407 }
11408 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 if (argvars[1].v_type != VAR_UNKNOWN)
11411 {
11412 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11413 if (defstr != NULL)
11414 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011416 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011417 {
11418 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011419 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011420 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011421
Bram Moolenaar4463f292005-09-25 22:20:24 +000011422 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011423
Bram Moolenaar4463f292005-09-25 22:20:24 +000011424 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11425 if (xp_name == NULL)
11426 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011427
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011428 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011429
Bram Moolenaar4463f292005-09-25 22:20:24 +000011430 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11431 &xp_arg) == FAIL)
11432 return;
11433 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011434 }
11435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 if (defstr != NULL)
11437 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011438 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11439 xp_type, xp_arg);
11440
11441 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 /* since the user typed this, no need to wait for return */
11444 need_wait_return = FALSE;
11445 msg_didout = FALSE;
11446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 cmd_silent = cmd_silent_save;
11448}
11449
11450/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011451 * "input()" function
11452 * Also handles inputsecret() when inputsecret is set.
11453 */
11454 static void
11455f_input(argvars, rettv)
11456 typval_T *argvars;
11457 typval_T *rettv;
11458{
11459 get_user_input(argvars, rettv, FALSE);
11460}
11461
11462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 * "inputdialog()" function
11464 */
11465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011467 typval_T *argvars;
11468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469{
11470#if defined(FEAT_GUI_TEXTDIALOG)
11471 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11472 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11473 {
11474 char_u *message;
11475 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011476 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 message = get_tv_string_chk(&argvars[0]);
11479 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011480 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011481 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 else
11483 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 if (message != NULL && defstr != NULL
11485 && do_dialog(VIM_QUESTION, NULL, message,
11486 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 else
11489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490 if (message != NULL && defstr != NULL
11491 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011492 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 rettv->vval.v_string = vim_strsave(
11494 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 }
11500 else
11501#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011502 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503}
11504
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011505/*
11506 * "inputlist()" function
11507 */
11508 static void
11509f_inputlist(argvars, rettv)
11510 typval_T *argvars;
11511 typval_T *rettv;
11512{
11513 listitem_T *li;
11514 int selected;
11515 int mouse_used;
11516
11517 rettv->vval.v_number = 0;
11518#ifdef NO_CONSOLE_INPUT
11519 /* While starting up, there is no place to enter text. */
11520 if (no_console_input())
11521 return;
11522#endif
11523 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11524 {
11525 EMSG2(_(e_listarg), "inputlist()");
11526 return;
11527 }
11528
11529 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011530 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011531 lines_left = Rows; /* avoid more prompt */
11532 msg_scroll = TRUE;
11533 msg_clr_eos();
11534
11535 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11536 {
11537 msg_puts(get_tv_string(&li->li_tv));
11538 msg_putchar('\n');
11539 }
11540
11541 /* Ask for choice. */
11542 selected = prompt_for_number(&mouse_used);
11543 if (mouse_used)
11544 selected -= lines_left;
11545
11546 rettv->vval.v_number = selected;
11547}
11548
11549
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11551
11552/*
11553 * "inputrestore()" function
11554 */
11555/*ARGSUSED*/
11556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011558 typval_T *argvars;
11559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560{
11561 if (ga_userinput.ga_len > 0)
11562 {
11563 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11565 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 }
11568 else if (p_verbose > 1)
11569 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011570 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 }
11573}
11574
11575/*
11576 * "inputsave()" function
11577 */
11578/*ARGSUSED*/
11579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011580f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 typval_T *argvars;
11582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583{
11584 /* Add an entry to the stack of typehead storage. */
11585 if (ga_grow(&ga_userinput, 1) == OK)
11586 {
11587 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11588 + ga_userinput.ga_len);
11589 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011590 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 }
11592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594}
11595
11596/*
11597 * "inputsecret()" function
11598 */
11599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011601 typval_T *argvars;
11602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603{
11604 ++cmdline_star;
11605 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 --cmdline_star;
11608 --inputsecret_flag;
11609}
11610
11611/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011612 * "insert()" function
11613 */
11614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011618{
11619 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011620 listitem_T *item;
11621 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011623
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011624 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011625 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011626 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011627 else if ((l = argvars[0].vval.v_list) != NULL
11628 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011629 {
11630 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 before = get_tv_number_chk(&argvars[2], &error);
11632 if (error)
11633 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011634
Bram Moolenaar758711c2005-02-02 23:11:38 +000011635 if (before == l->lv_len)
11636 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011637 else
11638 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011639 item = list_find(l, before);
11640 if (item == NULL)
11641 {
11642 EMSGN(_(e_listidx), before);
11643 l = NULL;
11644 }
11645 }
11646 if (l != NULL)
11647 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011648 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011649 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011650 }
11651 }
11652}
11653
11654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 * "isdirectory()" function
11656 */
11657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011659 typval_T *argvars;
11660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663}
11664
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011665/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011666 * "islocked()" function
11667 */
11668 static void
11669f_islocked(argvars, rettv)
11670 typval_T *argvars;
11671 typval_T *rettv;
11672{
11673 lval_T lv;
11674 char_u *end;
11675 dictitem_T *di;
11676
11677 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011678 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11679 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011680 if (end != NULL && lv.ll_name != NULL)
11681 {
11682 if (*end != NUL)
11683 EMSG(_(e_trailing));
11684 else
11685 {
11686 if (lv.ll_tv == NULL)
11687 {
11688 if (check_changedtick(lv.ll_name))
11689 rettv->vval.v_number = 1; /* always locked */
11690 else
11691 {
11692 di = find_var(lv.ll_name, NULL);
11693 if (di != NULL)
11694 {
11695 /* Consider a variable locked when:
11696 * 1. the variable itself is locked
11697 * 2. the value of the variable is locked.
11698 * 3. the List or Dict value is locked.
11699 */
11700 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11701 || tv_islocked(&di->di_tv));
11702 }
11703 }
11704 }
11705 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011706 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011707 else if (lv.ll_newkey != NULL)
11708 EMSG2(_(e_dictkey), lv.ll_newkey);
11709 else if (lv.ll_list != NULL)
11710 /* List item. */
11711 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11712 else
11713 /* Dictionary item. */
11714 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11715 }
11716 }
11717
11718 clear_lval(&lv);
11719}
11720
Bram Moolenaar33570922005-01-25 22:26:29 +000011721static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011722
11723/*
11724 * Turn a dict into a list:
11725 * "what" == 0: list of keys
11726 * "what" == 1: list of values
11727 * "what" == 2: list of items
11728 */
11729 static void
11730dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011731 typval_T *argvars;
11732 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011733 int what;
11734{
Bram Moolenaar33570922005-01-25 22:26:29 +000011735 list_T *l2;
11736 dictitem_T *di;
11737 hashitem_T *hi;
11738 listitem_T *li;
11739 listitem_T *li2;
11740 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011741 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011742
11743 rettv->vval.v_number = 0;
11744 if (argvars[0].v_type != VAR_DICT)
11745 {
11746 EMSG(_(e_dictreq));
11747 return;
11748 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011749 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011750 return;
11751
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011752 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011753 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011754
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011755 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011756 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011757 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011758 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011759 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011760 --todo;
11761 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011762
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011763 li = listitem_alloc();
11764 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011765 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011766 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011767
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011768 if (what == 0)
11769 {
11770 /* keys() */
11771 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011772 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011773 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11774 }
11775 else if (what == 1)
11776 {
11777 /* values() */
11778 copy_tv(&di->di_tv, &li->li_tv);
11779 }
11780 else
11781 {
11782 /* items() */
11783 l2 = list_alloc();
11784 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011785 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011786 li->li_tv.vval.v_list = l2;
11787 if (l2 == NULL)
11788 break;
11789 ++l2->lv_refcount;
11790
11791 li2 = listitem_alloc();
11792 if (li2 == NULL)
11793 break;
11794 list_append(l2, li2);
11795 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011796 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011797 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11798
11799 li2 = listitem_alloc();
11800 if (li2 == NULL)
11801 break;
11802 list_append(l2, li2);
11803 copy_tv(&di->di_tv, &li2->li_tv);
11804 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011805 }
11806 }
11807}
11808
11809/*
11810 * "items(dict)" function
11811 */
11812 static void
11813f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011814 typval_T *argvars;
11815 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011816{
11817 dict_list(argvars, rettv, 2);
11818}
11819
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011821 * "join()" function
11822 */
11823 static void
11824f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011825 typval_T *argvars;
11826 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011827{
11828 garray_T ga;
11829 char_u *sep;
11830
11831 rettv->vval.v_number = 0;
11832 if (argvars[0].v_type != VAR_LIST)
11833 {
11834 EMSG(_(e_listreq));
11835 return;
11836 }
11837 if (argvars[0].vval.v_list == NULL)
11838 return;
11839 if (argvars[1].v_type == VAR_UNKNOWN)
11840 sep = (char_u *)" ";
11841 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011842 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011843
11844 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845
11846 if (sep != NULL)
11847 {
11848 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011849 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 ga_append(&ga, NUL);
11851 rettv->vval.v_string = (char_u *)ga.ga_data;
11852 }
11853 else
11854 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011855}
11856
11857/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011858 * "keys()" function
11859 */
11860 static void
11861f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011862 typval_T *argvars;
11863 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011864{
11865 dict_list(argvars, rettv, 0);
11866}
11867
11868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 * "last_buffer_nr()" function.
11870 */
11871/*ARGSUSED*/
11872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011874 typval_T *argvars;
11875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
11877 int n = 0;
11878 buf_T *buf;
11879
11880 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11881 if (n < buf->b_fnum)
11882 n = buf->b_fnum;
11883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011885}
11886
11887/*
11888 * "len()" function
11889 */
11890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011891f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011892 typval_T *argvars;
11893 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011894{
11895 switch (argvars[0].v_type)
11896 {
11897 case VAR_STRING:
11898 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_number = (varnumber_T)STRLEN(
11900 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011901 break;
11902 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011903 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011904 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011905 case VAR_DICT:
11906 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11907 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011908 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011909 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011910 break;
11911 }
11912}
11913
Bram Moolenaar33570922005-01-25 22:26:29 +000011914static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011915
11916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011917libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *argvars;
11919 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011920 int type;
11921{
11922#ifdef FEAT_LIBCALL
11923 char_u *string_in;
11924 char_u **string_result;
11925 int nr_result;
11926#endif
11927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011929 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011931 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011932 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011933
11934 if (check_restricted() || check_secure())
11935 return;
11936
11937#ifdef FEAT_LIBCALL
11938 /* The first two args must be strings, otherwise its meaningless */
11939 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11940 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011941 string_in = NULL;
11942 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011943 string_in = argvars[2].vval.v_string;
11944 if (type == VAR_NUMBER)
11945 string_result = NULL;
11946 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011948 if (mch_libcall(argvars[0].vval.v_string,
11949 argvars[1].vval.v_string,
11950 string_in,
11951 argvars[2].vval.v_number,
11952 string_result,
11953 &nr_result) == OK
11954 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011956 }
11957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958}
11959
11960/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011961 * "libcall()" function
11962 */
11963 static void
11964f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011965 typval_T *argvars;
11966 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011967{
11968 libcall_common(argvars, rettv, VAR_STRING);
11969}
11970
11971/*
11972 * "libcallnr()" function
11973 */
11974 static void
11975f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *argvars;
11977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011978{
11979 libcall_common(argvars, rettv, VAR_NUMBER);
11980}
11981
11982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 * "line(string)" function
11984 */
11985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011987 typval_T *argvars;
11988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989{
11990 linenr_T lnum = 0;
11991 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011992 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011994 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 if (fp != NULL)
11996 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998}
11999
12000/*
12001 * "line2byte(lnum)" function
12002 */
12003/*ARGSUSED*/
12004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012006 typval_T *argvars;
12007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008{
12009#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011#else
12012 linenr_T lnum;
12013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12019 if (rettv->vval.v_number >= 0)
12020 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021#endif
12022}
12023
12024/*
12025 * "lispindent(lnum)" function
12026 */
12027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012029 typval_T *argvars;
12030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031{
12032#ifdef FEAT_LISP
12033 pos_T pos;
12034 linenr_T lnum;
12035
12036 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12039 {
12040 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 curwin->w_cursor = pos;
12043 }
12044 else
12045#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047}
12048
12049/*
12050 * "localtime()" function
12051 */
12052/*ARGSUSED*/
12053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012054f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012055 typval_T *argvars;
12056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059}
12060
Bram Moolenaar33570922005-01-25 22:26:29 +000012061static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062
12063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 typval_T *argvars;
12066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 int exact;
12068{
12069 char_u *keys;
12070 char_u *which;
12071 char_u buf[NUMBUFLEN];
12072 char_u *keys_buf = NULL;
12073 char_u *rhs;
12074 int mode;
12075 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012076 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077
12078 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 rettv->v_type = VAR_STRING;
12080 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 if (*keys == NUL)
12084 return;
12085
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012086 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012088 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012089 if (argvars[2].v_type != VAR_UNKNOWN)
12090 abbr = get_tv_number(&argvars[2]);
12091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 else
12093 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012094 if (which == NULL)
12095 return;
12096
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 mode = get_map_mode(&which, 0);
12098
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012099 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012100 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 vim_free(keys_buf);
12102 if (rhs != NULL)
12103 {
12104 ga_init(&ga);
12105 ga.ga_itemsize = 1;
12106 ga.ga_growsize = 40;
12107
12108 while (*rhs != NUL)
12109 ga_concat(&ga, str2special(&rhs, FALSE));
12110
12111 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 }
12114}
12115
12116/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012117 * "map()" function
12118 */
12119 static void
12120f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012121 typval_T *argvars;
12122 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012123{
12124 filter_map(argvars, rettv, TRUE);
12125}
12126
12127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012128 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 */
12130 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012131f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 typval_T *argvars;
12133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012135 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136}
12137
12138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012139 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 */
12141 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012142f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012143 typval_T *argvars;
12144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012146 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147}
12148
Bram Moolenaar33570922005-01-25 22:26:29 +000012149static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150
12151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012153 typval_T *argvars;
12154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 int type;
12156{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012157 char_u *str = NULL;
12158 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 char_u *pat;
12160 regmatch_T regmatch;
12161 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012162 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 char_u *save_cpo;
12164 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012165 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012166 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012167 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012168 list_T *l = NULL;
12169 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012170 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012171 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172
12173 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12174 save_cpo = p_cpo;
12175 p_cpo = (char_u *)"";
12176
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012177 rettv->vval.v_number = -1;
12178 if (type == 3)
12179 {
12180 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012181 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012182 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012183 }
12184 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186 rettv->v_type = VAR_STRING;
12187 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012190 if (argvars[0].v_type == VAR_LIST)
12191 {
12192 if ((l = argvars[0].vval.v_list) == NULL)
12193 goto theend;
12194 li = l->lv_first;
12195 }
12196 else
12197 expr = str = get_tv_string(&argvars[0]);
12198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012199 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12200 if (pat == NULL)
12201 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012202
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012203 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012205 int error = FALSE;
12206
12207 start = get_tv_number_chk(&argvars[2], &error);
12208 if (error)
12209 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012210 if (l != NULL)
12211 {
12212 li = list_find(l, start);
12213 if (li == NULL)
12214 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012215 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012216 }
12217 else
12218 {
12219 if (start < 0)
12220 start = 0;
12221 if (start > (long)STRLEN(str))
12222 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012223 /* When "count" argument is there ignore matches before "start",
12224 * otherwise skip part of the string. Differs when pattern is "^"
12225 * or "\<". */
12226 if (argvars[3].v_type != VAR_UNKNOWN)
12227 startcol = start;
12228 else
12229 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012230 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012232 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012233 nth = get_tv_number_chk(&argvars[3], &error);
12234 if (error)
12235 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 }
12237
12238 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12239 if (regmatch.regprog != NULL)
12240 {
12241 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012242
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012243 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012244 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012245 if (l != NULL)
12246 {
12247 if (li == NULL)
12248 {
12249 match = FALSE;
12250 break;
12251 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012252 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012253 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012254 if (str == NULL)
12255 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012256 }
12257
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012258 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012259
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012260 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012261 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012262 if (l == NULL && !match)
12263 break;
12264
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012265 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012266 if (l != NULL)
12267 {
12268 li = li->li_next;
12269 ++idx;
12270 }
12271 else
12272 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012273#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012274 startcol = (colnr_T)(regmatch.startp[0]
12275 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012276#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012277 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012278#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012279 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012280 }
12281
12282 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012284 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012286 int i;
12287
12288 /* return list with matched string and submatches */
12289 for (i = 0; i < NSUBEXP; ++i)
12290 {
12291 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012292 {
12293 if (list_append_string(rettv->vval.v_list,
12294 (char_u *)"", 0) == FAIL)
12295 break;
12296 }
12297 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012298 regmatch.startp[i],
12299 (int)(regmatch.endp[i] - regmatch.startp[i]))
12300 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012301 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012302 }
12303 }
12304 else if (type == 2)
12305 {
12306 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012307 if (l != NULL)
12308 copy_tv(&li->li_tv, rettv);
12309 else
12310 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012312 }
12313 else if (l != NULL)
12314 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 else
12316 {
12317 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012318 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319 (varnumber_T)(regmatch.startp[0] - str);
12320 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012323 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 }
12325 }
12326 vim_free(regmatch.regprog);
12327 }
12328
12329theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012330 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 p_cpo = save_cpo;
12332}
12333
12334/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335 * "match()" function
12336 */
12337 static void
12338f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012339 typval_T *argvars;
12340 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012341{
12342 find_some_match(argvars, rettv, 1);
12343}
12344
12345/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012346 * "matcharg()" function
12347 */
12348 static void
12349f_matcharg(argvars, rettv)
12350 typval_T *argvars;
12351 typval_T *rettv;
12352{
12353 if (rettv_list_alloc(rettv) == OK)
12354 {
12355#ifdef FEAT_SEARCH_EXTRA
12356 int mi = get_tv_number(&argvars[0]);
12357
12358 if (mi >= 1 && mi <= 3)
12359 {
12360 list_append_string(rettv->vval.v_list,
12361 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12362 list_append_string(rettv->vval.v_list,
12363 curwin->w_match_pat[mi - 1], -1);
12364 }
12365#endif
12366 }
12367}
12368
12369/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012370 * "matchend()" function
12371 */
12372 static void
12373f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012374 typval_T *argvars;
12375 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012376{
12377 find_some_match(argvars, rettv, 0);
12378}
12379
12380/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012381 * "matchlist()" function
12382 */
12383 static void
12384f_matchlist(argvars, rettv)
12385 typval_T *argvars;
12386 typval_T *rettv;
12387{
12388 find_some_match(argvars, rettv, 3);
12389}
12390
12391/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392 * "matchstr()" function
12393 */
12394 static void
12395f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012396 typval_T *argvars;
12397 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012398{
12399 find_some_match(argvars, rettv, 2);
12400}
12401
Bram Moolenaar33570922005-01-25 22:26:29 +000012402static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012403
12404 static void
12405max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012406 typval_T *argvars;
12407 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012408 int domax;
12409{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012410 long n = 0;
12411 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012412 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012413
12414 if (argvars[0].v_type == VAR_LIST)
12415 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012416 list_T *l;
12417 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012418
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012419 l = argvars[0].vval.v_list;
12420 if (l != NULL)
12421 {
12422 li = l->lv_first;
12423 if (li != NULL)
12424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012425 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012426 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012427 {
12428 li = li->li_next;
12429 if (li == NULL)
12430 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012431 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012432 if (domax ? i > n : i < n)
12433 n = i;
12434 }
12435 }
12436 }
12437 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012438 else if (argvars[0].v_type == VAR_DICT)
12439 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012440 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012441 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012442 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012443 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012444
12445 d = argvars[0].vval.v_dict;
12446 if (d != NULL)
12447 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012448 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012450 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012451 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012452 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012453 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012454 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012455 if (first)
12456 {
12457 n = i;
12458 first = FALSE;
12459 }
12460 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012461 n = i;
12462 }
12463 }
12464 }
12465 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012466 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012467 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012469}
12470
12471/*
12472 * "max()" function
12473 */
12474 static void
12475f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 typval_T *argvars;
12477 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012478{
12479 max_min(argvars, rettv, TRUE);
12480}
12481
12482/*
12483 * "min()" function
12484 */
12485 static void
12486f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012487 typval_T *argvars;
12488 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012489{
12490 max_min(argvars, rettv, FALSE);
12491}
12492
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012493static int mkdir_recurse __ARGS((char_u *dir, int prot));
12494
12495/*
12496 * Create the directory in which "dir" is located, and higher levels when
12497 * needed.
12498 */
12499 static int
12500mkdir_recurse(dir, prot)
12501 char_u *dir;
12502 int prot;
12503{
12504 char_u *p;
12505 char_u *updir;
12506 int r = FAIL;
12507
12508 /* Get end of directory name in "dir".
12509 * We're done when it's "/" or "c:/". */
12510 p = gettail_sep(dir);
12511 if (p <= get_past_head(dir))
12512 return OK;
12513
12514 /* If the directory exists we're done. Otherwise: create it.*/
12515 updir = vim_strnsave(dir, (int)(p - dir));
12516 if (updir == NULL)
12517 return FAIL;
12518 if (mch_isdir(updir))
12519 r = OK;
12520 else if (mkdir_recurse(updir, prot) == OK)
12521 r = vim_mkdir_emsg(updir, prot);
12522 vim_free(updir);
12523 return r;
12524}
12525
12526#ifdef vim_mkdir
12527/*
12528 * "mkdir()" function
12529 */
12530 static void
12531f_mkdir(argvars, rettv)
12532 typval_T *argvars;
12533 typval_T *rettv;
12534{
12535 char_u *dir;
12536 char_u buf[NUMBUFLEN];
12537 int prot = 0755;
12538
12539 rettv->vval.v_number = FAIL;
12540 if (check_restricted() || check_secure())
12541 return;
12542
12543 dir = get_tv_string_buf(&argvars[0], buf);
12544 if (argvars[1].v_type != VAR_UNKNOWN)
12545 {
12546 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012547 prot = get_tv_number_chk(&argvars[2], NULL);
12548 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012549 mkdir_recurse(dir, prot);
12550 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012551 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012552}
12553#endif
12554
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 * "mode()" function
12557 */
12558/*ARGSUSED*/
12559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012561 typval_T *argvars;
12562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563{
12564 char_u buf[2];
12565
12566#ifdef FEAT_VISUAL
12567 if (VIsual_active)
12568 {
12569 if (VIsual_select)
12570 buf[0] = VIsual_mode + 's' - 'v';
12571 else
12572 buf[0] = VIsual_mode;
12573 }
12574 else
12575#endif
12576 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12577 buf[0] = 'r';
12578 else if (State & INSERT)
12579 {
12580 if (State & REPLACE_FLAG)
12581 buf[0] = 'R';
12582 else
12583 buf[0] = 'i';
12584 }
12585 else if (State & CMDLINE)
12586 buf[0] = 'c';
12587 else
12588 buf[0] = 'n';
12589
12590 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->vval.v_string = vim_strsave(buf);
12592 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593}
12594
12595/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012596 * "nextnonblank()" function
12597 */
12598 static void
12599f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012600 typval_T *argvars;
12601 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012602{
12603 linenr_T lnum;
12604
12605 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012607 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608 {
12609 lnum = 0;
12610 break;
12611 }
12612 if (*skipwhite(ml_get(lnum)) != NUL)
12613 break;
12614 }
12615 rettv->vval.v_number = lnum;
12616}
12617
12618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619 * "nr2char()" function
12620 */
12621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012622f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012623 typval_T *argvars;
12624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625{
12626 char_u buf[NUMBUFLEN];
12627
12628#ifdef FEAT_MBYTE
12629 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 else
12632#endif
12633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 buf[1] = NUL;
12636 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 rettv->v_type = VAR_STRING;
12638 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012639}
12640
12641/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012642 * "pathshorten()" function
12643 */
12644 static void
12645f_pathshorten(argvars, rettv)
12646 typval_T *argvars;
12647 typval_T *rettv;
12648{
12649 char_u *p;
12650
12651 rettv->v_type = VAR_STRING;
12652 p = get_tv_string_chk(&argvars[0]);
12653 if (p == NULL)
12654 rettv->vval.v_string = NULL;
12655 else
12656 {
12657 p = vim_strsave(p);
12658 rettv->vval.v_string = p;
12659 if (p != NULL)
12660 shorten_dir(p);
12661 }
12662}
12663
12664/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012665 * "prevnonblank()" function
12666 */
12667 static void
12668f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 typval_T *argvars;
12670 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012671{
12672 linenr_T lnum;
12673
12674 lnum = get_tv_lnum(argvars);
12675 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12676 lnum = 0;
12677 else
12678 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12679 --lnum;
12680 rettv->vval.v_number = lnum;
12681}
12682
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012683#ifdef HAVE_STDARG_H
12684/* This dummy va_list is here because:
12685 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12686 * - locally in the function results in a "used before set" warning
12687 * - using va_start() to initialize it gives "function with fixed args" error */
12688static va_list ap;
12689#endif
12690
Bram Moolenaar8c711452005-01-14 21:53:12 +000012691/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012692 * "printf()" function
12693 */
12694 static void
12695f_printf(argvars, rettv)
12696 typval_T *argvars;
12697 typval_T *rettv;
12698{
12699 rettv->v_type = VAR_STRING;
12700 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012701#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012702 {
12703 char_u buf[NUMBUFLEN];
12704 int len;
12705 char_u *s;
12706 int saved_did_emsg = did_emsg;
12707 char *fmt;
12708
12709 /* Get the required length, allocate the buffer and do it for real. */
12710 did_emsg = FALSE;
12711 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012712 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012713 if (!did_emsg)
12714 {
12715 s = alloc(len + 1);
12716 if (s != NULL)
12717 {
12718 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012719 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012720 }
12721 }
12722 did_emsg |= saved_did_emsg;
12723 }
12724#endif
12725}
12726
12727/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012728 * "pumvisible()" function
12729 */
12730/*ARGSUSED*/
12731 static void
12732f_pumvisible(argvars, rettv)
12733 typval_T *argvars;
12734 typval_T *rettv;
12735{
12736 rettv->vval.v_number = 0;
12737#ifdef FEAT_INS_EXPAND
12738 if (pum_visible())
12739 rettv->vval.v_number = 1;
12740#endif
12741}
12742
12743/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012744 * "range()" function
12745 */
12746 static void
12747f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012748 typval_T *argvars;
12749 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012750{
12751 long start;
12752 long end;
12753 long stride = 1;
12754 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012755 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012757 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012758 if (argvars[1].v_type == VAR_UNKNOWN)
12759 {
12760 end = start - 1;
12761 start = 0;
12762 }
12763 else
12764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012765 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012766 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012768 }
12769
12770 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012771 if (error)
12772 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012773 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012774 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012775 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012776 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012777 else
12778 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012779 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012780 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012781 if (list_append_number(rettv->vval.v_list,
12782 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012783 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012784 }
12785}
12786
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012787/*
12788 * "readfile()" function
12789 */
12790 static void
12791f_readfile(argvars, rettv)
12792 typval_T *argvars;
12793 typval_T *rettv;
12794{
12795 int binary = FALSE;
12796 char_u *fname;
12797 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012798 listitem_T *li;
12799#define FREAD_SIZE 200 /* optimized for text lines */
12800 char_u buf[FREAD_SIZE];
12801 int readlen; /* size of last fread() */
12802 int buflen; /* nr of valid chars in buf[] */
12803 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12804 int tolist; /* first byte in buf[] still to be put in list */
12805 int chop; /* how many CR to chop off */
12806 char_u *prev = NULL; /* previously read bytes, if any */
12807 int prevlen = 0; /* length of "prev" if not NULL */
12808 char_u *s;
12809 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012810 long maxline = MAXLNUM;
12811 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012812
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012813 if (argvars[1].v_type != VAR_UNKNOWN)
12814 {
12815 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12816 binary = TRUE;
12817 if (argvars[2].v_type != VAR_UNKNOWN)
12818 maxline = get_tv_number(&argvars[2]);
12819 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012820
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012821 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012822 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012823
12824 /* Always open the file in binary mode, library functions have a mind of
12825 * their own about CR-LF conversion. */
12826 fname = get_tv_string(&argvars[0]);
12827 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12828 {
12829 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12830 return;
12831 }
12832
12833 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012834 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012835 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012836 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012837 buflen = filtd + readlen;
12838 tolist = 0;
12839 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12840 {
12841 if (buf[filtd] == '\n' || readlen <= 0)
12842 {
12843 /* Only when in binary mode add an empty list item when the
12844 * last line ends in a '\n'. */
12845 if (!binary && readlen == 0 && filtd == 0)
12846 break;
12847
12848 /* Found end-of-line or end-of-file: add a text line to the
12849 * list. */
12850 chop = 0;
12851 if (!binary)
12852 while (filtd - chop - 1 >= tolist
12853 && buf[filtd - chop - 1] == '\r')
12854 ++chop;
12855 len = filtd - tolist - chop;
12856 if (prev == NULL)
12857 s = vim_strnsave(buf + tolist, len);
12858 else
12859 {
12860 s = alloc((unsigned)(prevlen + len + 1));
12861 if (s != NULL)
12862 {
12863 mch_memmove(s, prev, prevlen);
12864 vim_free(prev);
12865 prev = NULL;
12866 mch_memmove(s + prevlen, buf + tolist, len);
12867 s[prevlen + len] = NUL;
12868 }
12869 }
12870 tolist = filtd + 1;
12871
12872 li = listitem_alloc();
12873 if (li == NULL)
12874 {
12875 vim_free(s);
12876 break;
12877 }
12878 li->li_tv.v_type = VAR_STRING;
12879 li->li_tv.v_lock = 0;
12880 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012881 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012882
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012883 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012884 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012885 if (readlen <= 0)
12886 break;
12887 }
12888 else if (buf[filtd] == NUL)
12889 buf[filtd] = '\n';
12890 }
12891 if (readlen <= 0)
12892 break;
12893
12894 if (tolist == 0)
12895 {
12896 /* "buf" is full, need to move text to an allocated buffer */
12897 if (prev == NULL)
12898 {
12899 prev = vim_strnsave(buf, buflen);
12900 prevlen = buflen;
12901 }
12902 else
12903 {
12904 s = alloc((unsigned)(prevlen + buflen));
12905 if (s != NULL)
12906 {
12907 mch_memmove(s, prev, prevlen);
12908 mch_memmove(s + prevlen, buf, buflen);
12909 vim_free(prev);
12910 prev = s;
12911 prevlen += buflen;
12912 }
12913 }
12914 filtd = 0;
12915 }
12916 else
12917 {
12918 mch_memmove(buf, buf + tolist, buflen - tolist);
12919 filtd -= tolist;
12920 }
12921 }
12922
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012923 /*
12924 * For a negative line count use only the lines at the end of the file,
12925 * free the rest.
12926 */
12927 if (maxline < 0)
12928 while (cnt > -maxline)
12929 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012930 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012931 --cnt;
12932 }
12933
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012934 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012935 fclose(fd);
12936}
12937
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012938#if defined(FEAT_RELTIME)
12939static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12940
12941/*
12942 * Convert a List to proftime_T.
12943 * Return FAIL when there is something wrong.
12944 */
12945 static int
12946list2proftime(arg, tm)
12947 typval_T *arg;
12948 proftime_T *tm;
12949{
12950 long n1, n2;
12951 int error = FALSE;
12952
12953 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12954 || arg->vval.v_list->lv_len != 2)
12955 return FAIL;
12956 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12957 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12958# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012959 tm->HighPart = n1;
12960 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012961# else
12962 tm->tv_sec = n1;
12963 tm->tv_usec = n2;
12964# endif
12965 return error ? FAIL : OK;
12966}
12967#endif /* FEAT_RELTIME */
12968
12969/*
12970 * "reltime()" function
12971 */
12972 static void
12973f_reltime(argvars, rettv)
12974 typval_T *argvars;
12975 typval_T *rettv;
12976{
12977#ifdef FEAT_RELTIME
12978 proftime_T res;
12979 proftime_T start;
12980
12981 if (argvars[0].v_type == VAR_UNKNOWN)
12982 {
12983 /* No arguments: get current time. */
12984 profile_start(&res);
12985 }
12986 else if (argvars[1].v_type == VAR_UNKNOWN)
12987 {
12988 if (list2proftime(&argvars[0], &res) == FAIL)
12989 return;
12990 profile_end(&res);
12991 }
12992 else
12993 {
12994 /* Two arguments: compute the difference. */
12995 if (list2proftime(&argvars[0], &start) == FAIL
12996 || list2proftime(&argvars[1], &res) == FAIL)
12997 return;
12998 profile_sub(&res, &start);
12999 }
13000
13001 if (rettv_list_alloc(rettv) == OK)
13002 {
13003 long n1, n2;
13004
13005# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013006 n1 = res.HighPart;
13007 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013008# else
13009 n1 = res.tv_sec;
13010 n2 = res.tv_usec;
13011# endif
13012 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13013 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13014 }
13015#endif
13016}
13017
13018/*
13019 * "reltimestr()" function
13020 */
13021 static void
13022f_reltimestr(argvars, rettv)
13023 typval_T *argvars;
13024 typval_T *rettv;
13025{
13026#ifdef FEAT_RELTIME
13027 proftime_T tm;
13028#endif
13029
13030 rettv->v_type = VAR_STRING;
13031 rettv->vval.v_string = NULL;
13032#ifdef FEAT_RELTIME
13033 if (list2proftime(&argvars[0], &tm) == OK)
13034 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13035#endif
13036}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013037
Bram Moolenaar0d660222005-01-07 21:51:51 +000013038#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13039static void make_connection __ARGS((void));
13040static int check_connection __ARGS((void));
13041
13042 static void
13043make_connection()
13044{
13045 if (X_DISPLAY == NULL
13046# ifdef FEAT_GUI
13047 && !gui.in_use
13048# endif
13049 )
13050 {
13051 x_force_connect = TRUE;
13052 setup_term_clip();
13053 x_force_connect = FALSE;
13054 }
13055}
13056
13057 static int
13058check_connection()
13059{
13060 make_connection();
13061 if (X_DISPLAY == NULL)
13062 {
13063 EMSG(_("E240: No connection to Vim server"));
13064 return FAIL;
13065 }
13066 return OK;
13067}
13068#endif
13069
13070#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013071static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013072
13073 static void
13074remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 typval_T *argvars;
13076 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013077 int expr;
13078{
13079 char_u *server_name;
13080 char_u *keys;
13081 char_u *r = NULL;
13082 char_u buf[NUMBUFLEN];
13083# ifdef WIN32
13084 HWND w;
13085# else
13086 Window w;
13087# endif
13088
13089 if (check_restricted() || check_secure())
13090 return;
13091
13092# ifdef FEAT_X11
13093 if (check_connection() == FAIL)
13094 return;
13095# endif
13096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 server_name = get_tv_string_chk(&argvars[0]);
13098 if (server_name == NULL)
13099 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013100 keys = get_tv_string_buf(&argvars[1], buf);
13101# ifdef WIN32
13102 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13103# else
13104 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13105 < 0)
13106# endif
13107 {
13108 if (r != NULL)
13109 EMSG(r); /* sending worked but evaluation failed */
13110 else
13111 EMSG2(_("E241: Unable to send to %s"), server_name);
13112 return;
13113 }
13114
13115 rettv->vval.v_string = r;
13116
13117 if (argvars[2].v_type != VAR_UNKNOWN)
13118 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013120 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013121 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013122
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013123 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013124 v.di_tv.v_type = VAR_STRING;
13125 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013126 idvar = get_tv_string_chk(&argvars[2]);
13127 if (idvar != NULL)
13128 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013130 }
13131}
13132#endif
13133
13134/*
13135 * "remote_expr()" function
13136 */
13137/*ARGSUSED*/
13138 static void
13139f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013140 typval_T *argvars;
13141 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013142{
13143 rettv->v_type = VAR_STRING;
13144 rettv->vval.v_string = NULL;
13145#ifdef FEAT_CLIENTSERVER
13146 remote_common(argvars, rettv, TRUE);
13147#endif
13148}
13149
13150/*
13151 * "remote_foreground()" function
13152 */
13153/*ARGSUSED*/
13154 static void
13155f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013156 typval_T *argvars;
13157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013158{
13159 rettv->vval.v_number = 0;
13160#ifdef FEAT_CLIENTSERVER
13161# ifdef WIN32
13162 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013163 {
13164 char_u *server_name = get_tv_string_chk(&argvars[0]);
13165
13166 if (server_name != NULL)
13167 serverForeground(server_name);
13168 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013169# else
13170 /* Send a foreground() expression to the server. */
13171 argvars[1].v_type = VAR_STRING;
13172 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13173 argvars[2].v_type = VAR_UNKNOWN;
13174 remote_common(argvars, rettv, TRUE);
13175 vim_free(argvars[1].vval.v_string);
13176# endif
13177#endif
13178}
13179
13180/*ARGSUSED*/
13181 static void
13182f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013183 typval_T *argvars;
13184 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013185{
13186#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013188 char_u *s = NULL;
13189# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013190 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013191# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013192 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013193
13194 if (check_restricted() || check_secure())
13195 {
13196 rettv->vval.v_number = -1;
13197 return;
13198 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 serverid = get_tv_string_chk(&argvars[0]);
13200 if (serverid == NULL)
13201 {
13202 rettv->vval.v_number = -1;
13203 return; /* type error; errmsg already given */
13204 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013205# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013206 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013207 if (n == 0)
13208 rettv->vval.v_number = -1;
13209 else
13210 {
13211 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13212 rettv->vval.v_number = (s != NULL);
13213 }
13214# else
13215 rettv->vval.v_number = 0;
13216 if (check_connection() == FAIL)
13217 return;
13218
13219 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013221# endif
13222
13223 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013225 char_u *retvar;
13226
Bram Moolenaar33570922005-01-25 22:26:29 +000013227 v.di_tv.v_type = VAR_STRING;
13228 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013229 retvar = get_tv_string_chk(&argvars[1]);
13230 if (retvar != NULL)
13231 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013233 }
13234#else
13235 rettv->vval.v_number = -1;
13236#endif
13237}
13238
13239/*ARGSUSED*/
13240 static void
13241f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *argvars;
13243 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013244{
13245 char_u *r = NULL;
13246
13247#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013248 char_u *serverid = get_tv_string_chk(&argvars[0]);
13249
13250 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013251 {
13252# ifdef WIN32
13253 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013254 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013255
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013256 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013257 if (n != 0)
13258 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13259 if (r == NULL)
13260# else
13261 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013263# endif
13264 EMSG(_("E277: Unable to read a server reply"));
13265 }
13266#endif
13267 rettv->v_type = VAR_STRING;
13268 rettv->vval.v_string = r;
13269}
13270
13271/*
13272 * "remote_send()" function
13273 */
13274/*ARGSUSED*/
13275 static void
13276f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013277 typval_T *argvars;
13278 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013279{
13280 rettv->v_type = VAR_STRING;
13281 rettv->vval.v_string = NULL;
13282#ifdef FEAT_CLIENTSERVER
13283 remote_common(argvars, rettv, FALSE);
13284#endif
13285}
13286
13287/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013288 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013289 */
13290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013292 typval_T *argvars;
13293 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013294{
Bram Moolenaar33570922005-01-25 22:26:29 +000013295 list_T *l;
13296 listitem_T *item, *item2;
13297 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013298 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013299 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013300 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013301 dict_T *d;
13302 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013303
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013304 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013305 if (argvars[0].v_type == VAR_DICT)
13306 {
13307 if (argvars[2].v_type != VAR_UNKNOWN)
13308 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013309 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013310 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013312 key = get_tv_string_chk(&argvars[1]);
13313 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013315 di = dict_find(d, key, -1);
13316 if (di == NULL)
13317 EMSG2(_(e_dictkey), key);
13318 else
13319 {
13320 *rettv = di->di_tv;
13321 init_tv(&di->di_tv);
13322 dictitem_remove(d, di);
13323 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013324 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013325 }
13326 }
13327 else if (argvars[0].v_type != VAR_LIST)
13328 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013329 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013330 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 int error = FALSE;
13333
13334 idx = get_tv_number_chk(&argvars[1], &error);
13335 if (error)
13336 ; /* type error: do nothing, errmsg already given */
13337 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013338 EMSGN(_(e_listidx), idx);
13339 else
13340 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013341 if (argvars[2].v_type == VAR_UNKNOWN)
13342 {
13343 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013344 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013345 *rettv = item->li_tv;
13346 vim_free(item);
13347 }
13348 else
13349 {
13350 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013351 end = get_tv_number_chk(&argvars[2], &error);
13352 if (error)
13353 ; /* type error: do nothing */
13354 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013355 EMSGN(_(e_listidx), end);
13356 else
13357 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013358 int cnt = 0;
13359
13360 for (li = item; li != NULL; li = li->li_next)
13361 {
13362 ++cnt;
13363 if (li == item2)
13364 break;
13365 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013366 if (li == NULL) /* didn't find "item2" after "item" */
13367 EMSG(_(e_invrange));
13368 else
13369 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013370 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013371 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013372 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013373 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013374 l->lv_first = item;
13375 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013376 item->li_prev = NULL;
13377 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013378 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013379 }
13380 }
13381 }
13382 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013383 }
13384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385}
13386
13387/*
13388 * "rename({from}, {to})" function
13389 */
13390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013391f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013392 typval_T *argvars;
13393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394{
13395 char_u buf[NUMBUFLEN];
13396
13397 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013398 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013400 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13401 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402}
13403
13404/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013405 * "repeat()" function
13406 */
13407/*ARGSUSED*/
13408 static void
13409f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013410 typval_T *argvars;
13411 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013412{
13413 char_u *p;
13414 int n;
13415 int slen;
13416 int len;
13417 char_u *r;
13418 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013419
13420 n = get_tv_number(&argvars[1]);
13421 if (argvars[0].v_type == VAR_LIST)
13422 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013423 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013424 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013425 if (list_extend(rettv->vval.v_list,
13426 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013428 }
13429 else
13430 {
13431 p = get_tv_string(&argvars[0]);
13432 rettv->v_type = VAR_STRING;
13433 rettv->vval.v_string = NULL;
13434
13435 slen = (int)STRLEN(p);
13436 len = slen * n;
13437 if (len <= 0)
13438 return;
13439
13440 r = alloc(len + 1);
13441 if (r != NULL)
13442 {
13443 for (i = 0; i < n; i++)
13444 mch_memmove(r + i * slen, p, (size_t)slen);
13445 r[len] = NUL;
13446 }
13447
13448 rettv->vval.v_string = r;
13449 }
13450}
13451
13452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 * "resolve()" function
13454 */
13455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013456f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013457 typval_T *argvars;
13458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459{
13460 char_u *p;
13461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013462 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463#ifdef FEAT_SHORTCUT
13464 {
13465 char_u *v = NULL;
13466
13467 v = mch_resolve_shortcut(p);
13468 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013469 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013471 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 }
13473#else
13474# ifdef HAVE_READLINK
13475 {
13476 char_u buf[MAXPATHL + 1];
13477 char_u *cpy;
13478 int len;
13479 char_u *remain = NULL;
13480 char_u *q;
13481 int is_relative_to_current = FALSE;
13482 int has_trailing_pathsep = FALSE;
13483 int limit = 100;
13484
13485 p = vim_strsave(p);
13486
13487 if (p[0] == '.' && (vim_ispathsep(p[1])
13488 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13489 is_relative_to_current = TRUE;
13490
13491 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013492 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 has_trailing_pathsep = TRUE;
13494
13495 q = getnextcomp(p);
13496 if (*q != NUL)
13497 {
13498 /* Separate the first path component in "p", and keep the
13499 * remainder (beginning with the path separator). */
13500 remain = vim_strsave(q - 1);
13501 q[-1] = NUL;
13502 }
13503
13504 for (;;)
13505 {
13506 for (;;)
13507 {
13508 len = readlink((char *)p, (char *)buf, MAXPATHL);
13509 if (len <= 0)
13510 break;
13511 buf[len] = NUL;
13512
13513 if (limit-- == 0)
13514 {
13515 vim_free(p);
13516 vim_free(remain);
13517 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 goto fail;
13520 }
13521
13522 /* Ensure that the result will have a trailing path separator
13523 * if the argument has one. */
13524 if (remain == NULL && has_trailing_pathsep)
13525 add_pathsep(buf);
13526
13527 /* Separate the first path component in the link value and
13528 * concatenate the remainders. */
13529 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13530 if (*q != NUL)
13531 {
13532 if (remain == NULL)
13533 remain = vim_strsave(q - 1);
13534 else
13535 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013536 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 if (cpy != NULL)
13538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 vim_free(remain);
13540 remain = cpy;
13541 }
13542 }
13543 q[-1] = NUL;
13544 }
13545
13546 q = gettail(p);
13547 if (q > p && *q == NUL)
13548 {
13549 /* Ignore trailing path separator. */
13550 q[-1] = NUL;
13551 q = gettail(p);
13552 }
13553 if (q > p && !mch_isFullName(buf))
13554 {
13555 /* symlink is relative to directory of argument */
13556 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13557 if (cpy != NULL)
13558 {
13559 STRCPY(cpy, p);
13560 STRCPY(gettail(cpy), buf);
13561 vim_free(p);
13562 p = cpy;
13563 }
13564 }
13565 else
13566 {
13567 vim_free(p);
13568 p = vim_strsave(buf);
13569 }
13570 }
13571
13572 if (remain == NULL)
13573 break;
13574
13575 /* Append the first path component of "remain" to "p". */
13576 q = getnextcomp(remain + 1);
13577 len = q - remain - (*q != NUL);
13578 cpy = vim_strnsave(p, STRLEN(p) + len);
13579 if (cpy != NULL)
13580 {
13581 STRNCAT(cpy, remain, len);
13582 vim_free(p);
13583 p = cpy;
13584 }
13585 /* Shorten "remain". */
13586 if (*q != NUL)
13587 STRCPY(remain, q - 1);
13588 else
13589 {
13590 vim_free(remain);
13591 remain = NULL;
13592 }
13593 }
13594
13595 /* If the result is a relative path name, make it explicitly relative to
13596 * the current directory if and only if the argument had this form. */
13597 if (!vim_ispathsep(*p))
13598 {
13599 if (is_relative_to_current
13600 && *p != NUL
13601 && !(p[0] == '.'
13602 && (p[1] == NUL
13603 || vim_ispathsep(p[1])
13604 || (p[1] == '.'
13605 && (p[2] == NUL
13606 || vim_ispathsep(p[2]))))))
13607 {
13608 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013609 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 if (cpy != NULL)
13611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 vim_free(p);
13613 p = cpy;
13614 }
13615 }
13616 else if (!is_relative_to_current)
13617 {
13618 /* Strip leading "./". */
13619 q = p;
13620 while (q[0] == '.' && vim_ispathsep(q[1]))
13621 q += 2;
13622 if (q > p)
13623 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13624 }
13625 }
13626
13627 /* Ensure that the result will have no trailing path separator
13628 * if the argument had none. But keep "/" or "//". */
13629 if (!has_trailing_pathsep)
13630 {
13631 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013632 if (after_pathsep(p, q))
13633 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 }
13635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 }
13638# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640# endif
13641#endif
13642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644
13645#ifdef HAVE_READLINK
13646fail:
13647#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649}
13650
13651/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013652 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 */
13654 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013655f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013656 typval_T *argvars;
13657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658{
Bram Moolenaar33570922005-01-25 22:26:29 +000013659 list_T *l;
13660 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
Bram Moolenaar0d660222005-01-07 21:51:51 +000013662 rettv->vval.v_number = 0;
13663 if (argvars[0].v_type != VAR_LIST)
13664 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013665 else if ((l = argvars[0].vval.v_list) != NULL
13666 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013667 {
13668 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013669 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013670 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013671 while (li != NULL)
13672 {
13673 ni = li->li_prev;
13674 list_append(l, li);
13675 li = ni;
13676 }
13677 rettv->vval.v_list = l;
13678 rettv->v_type = VAR_LIST;
13679 ++l->lv_refcount;
13680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681}
13682
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013683#define SP_NOMOVE 0x01 /* don't move cursor */
13684#define SP_REPEAT 0x02 /* repeat to find outer pair */
13685#define SP_RETCOUNT 0x04 /* return matchcount */
13686#define SP_SETPCMARK 0x08 /* set previous context mark */
13687#define SP_START 0x10 /* accept match at start position */
13688#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13689#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013690
Bram Moolenaar33570922005-01-25 22:26:29 +000013691static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692
13693/*
13694 * Get flags for a search function.
13695 * Possibly sets "p_ws".
13696 * Returns BACKWARD, FORWARD or zero (for an error).
13697 */
13698 static int
13699get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013701 int *flagsp;
13702{
13703 int dir = FORWARD;
13704 char_u *flags;
13705 char_u nbuf[NUMBUFLEN];
13706 int mask;
13707
13708 if (varp->v_type != VAR_UNKNOWN)
13709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013710 flags = get_tv_string_buf_chk(varp, nbuf);
13711 if (flags == NULL)
13712 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013713 while (*flags != NUL)
13714 {
13715 switch (*flags)
13716 {
13717 case 'b': dir = BACKWARD; break;
13718 case 'w': p_ws = TRUE; break;
13719 case 'W': p_ws = FALSE; break;
13720 default: mask = 0;
13721 if (flagsp != NULL)
13722 switch (*flags)
13723 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013724 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013725 case 'e': mask = SP_END; break;
13726 case 'm': mask = SP_RETCOUNT; break;
13727 case 'n': mask = SP_NOMOVE; break;
13728 case 'p': mask = SP_SUBPAT; break;
13729 case 'r': mask = SP_REPEAT; break;
13730 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013731 }
13732 if (mask == 0)
13733 {
13734 EMSG2(_(e_invarg2), flags);
13735 dir = 0;
13736 }
13737 else
13738 *flagsp |= mask;
13739 }
13740 if (dir == 0)
13741 break;
13742 ++flags;
13743 }
13744 }
13745 return dir;
13746}
13747
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013749 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013751 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013752search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013753 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013754 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013755 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013757 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 char_u *pat;
13759 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013760 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 int save_p_ws = p_ws;
13762 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013763 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013764 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013765 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013766 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013768 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013769 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013770 if (dir == 0)
13771 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013772 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013773 if (flags & SP_START)
13774 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013775 if (flags & SP_END)
13776 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013777
13778 /* Optional extra argument: line number to stop searching. */
13779 if (argvars[1].v_type != VAR_UNKNOWN
13780 && argvars[2].v_type != VAR_UNKNOWN)
13781 {
13782 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13783 if (lnum_stop < 0)
13784 goto theend;
13785 }
13786
Bram Moolenaar231334e2005-07-25 20:46:57 +000013787 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013788 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013789 * Check to make sure only those flags are set.
13790 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13791 * flags cannot be set. Check for that condition also.
13792 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013793 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013794 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013797 goto theend;
13798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013800 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013801 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13802 options, RE_SEARCH, (linenr_T)lnum_stop);
13803 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013805 if (flags & SP_SUBPAT)
13806 retval = subpatnum;
13807 else
13808 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013809 if (flags & SP_SETPCMARK)
13810 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013812 if (match_pos != NULL)
13813 {
13814 /* Store the match cursor position */
13815 match_pos->lnum = pos.lnum;
13816 match_pos->col = pos.col + 1;
13817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 /* "/$" will put the cursor after the end of the line, may need to
13819 * correct that here */
13820 check_cursor();
13821 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013822
13823 /* If 'n' flag is used: restore cursor position. */
13824 if (flags & SP_NOMOVE)
13825 curwin->w_cursor = save_cursor;
13826theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013828
13829 return retval;
13830}
13831
13832/*
13833 * "search()" function
13834 */
13835 static void
13836f_search(argvars, rettv)
13837 typval_T *argvars;
13838 typval_T *rettv;
13839{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013840 int flags = 0;
13841
13842 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843}
13844
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013846 * "searchdecl()" function
13847 */
13848 static void
13849f_searchdecl(argvars, rettv)
13850 typval_T *argvars;
13851 typval_T *rettv;
13852{
13853 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013854 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013855 int error = FALSE;
13856 char_u *name;
13857
13858 rettv->vval.v_number = 1; /* default: FAIL */
13859
13860 name = get_tv_string_chk(&argvars[0]);
13861 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013862 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013863 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013864 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13865 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13866 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013867 if (!error && name != NULL)
13868 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013869 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013870}
13871
13872/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013873 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013875 static int
13876searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013877 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013878 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879{
13880 char_u *spat, *mpat, *epat;
13881 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 int dir;
13884 int flags = 0;
13885 char_u nbuf1[NUMBUFLEN];
13886 char_u nbuf2[NUMBUFLEN];
13887 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013888 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013889 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013892 spat = get_tv_string_chk(&argvars[0]);
13893 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13894 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13895 if (spat == NULL || mpat == NULL || epat == NULL)
13896 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 /* Handle the optional fourth argument: flags */
13899 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013900 if (dir == 0)
13901 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013902
13903 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013904 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13905 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013906 if ((flags & (SP_END | SP_SUBPAT)) != 0
13907 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013908 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013909 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013910 goto theend;
13911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013913 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013914 if (argvars[3].v_type == VAR_UNKNOWN
13915 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 skip = (char_u *)"";
13917 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013920 if (argvars[5].v_type != VAR_UNKNOWN)
13921 {
13922 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13923 if (lnum_stop < 0)
13924 goto theend;
13925 }
13926 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 if (skip == NULL)
13928 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013930 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13931 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013932
13933theend:
13934 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013935
13936 return retval;
13937}
13938
13939/*
13940 * "searchpair()" function
13941 */
13942 static void
13943f_searchpair(argvars, rettv)
13944 typval_T *argvars;
13945 typval_T *rettv;
13946{
13947 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13948}
13949
13950/*
13951 * "searchpairpos()" function
13952 */
13953 static void
13954f_searchpairpos(argvars, rettv)
13955 typval_T *argvars;
13956 typval_T *rettv;
13957{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013958 pos_T match_pos;
13959 int lnum = 0;
13960 int col = 0;
13961
13962 rettv->vval.v_number = 0;
13963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013964 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013965 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013966
13967 if (searchpair_cmn(argvars, &match_pos) > 0)
13968 {
13969 lnum = match_pos.lnum;
13970 col = match_pos.col;
13971 }
13972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013973 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13974 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013975}
13976
13977/*
13978 * Search for a start/middle/end thing.
13979 * Used by searchpair(), see its documentation for the details.
13980 * Returns 0 or -1 for no match,
13981 */
13982 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013983do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013984 char_u *spat; /* start pattern */
13985 char_u *mpat; /* middle pattern */
13986 char_u *epat; /* end pattern */
13987 int dir; /* BACKWARD or FORWARD */
13988 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013989 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013990 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013991 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013992{
13993 char_u *save_cpo;
13994 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13995 long retval = 0;
13996 pos_T pos;
13997 pos_T firstpos;
13998 pos_T foundpos;
13999 pos_T save_cursor;
14000 pos_T save_pos;
14001 int n;
14002 int r;
14003 int nest = 1;
14004 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014005 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014006
14007 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14008 save_cpo = p_cpo;
14009 p_cpo = (char_u *)"";
14010
14011 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14012 * start/middle/end (pat3, for the top pair). */
14013 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14014 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14015 if (pat2 == NULL || pat3 == NULL)
14016 goto theend;
14017 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14018 if (*mpat == NUL)
14019 STRCPY(pat3, pat2);
14020 else
14021 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14022 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014023 if (flags & SP_START)
14024 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014025
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 save_cursor = curwin->w_cursor;
14027 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014028 clearpos(&firstpos);
14029 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 pat = pat3;
14031 for (;;)
14032 {
14033 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014034 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14036 /* didn't find it or found the first match again: FAIL */
14037 break;
14038
14039 if (firstpos.lnum == 0)
14040 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014041 if (equalpos(pos, foundpos))
14042 {
14043 /* Found the same position again. Can happen with a pattern that
14044 * has "\zs" at the end and searching backwards. Advance one
14045 * character and try again. */
14046 if (dir == BACKWARD)
14047 decl(&pos);
14048 else
14049 incl(&pos);
14050 }
14051 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052
14053 /* If the skip pattern matches, ignore this match. */
14054 if (*skip != NUL)
14055 {
14056 save_pos = curwin->w_cursor;
14057 curwin->w_cursor = pos;
14058 r = eval_to_bool(skip, &err, NULL, FALSE);
14059 curwin->w_cursor = save_pos;
14060 if (err)
14061 {
14062 /* Evaluating {skip} caused an error, break here. */
14063 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014064 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 break;
14066 }
14067 if (r)
14068 continue;
14069 }
14070
14071 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14072 {
14073 /* Found end when searching backwards or start when searching
14074 * forward: nested pair. */
14075 ++nest;
14076 pat = pat2; /* nested, don't search for middle */
14077 }
14078 else
14079 {
14080 /* Found end when searching forward or start when searching
14081 * backward: end of (nested) pair; or found middle in outer pair. */
14082 if (--nest == 1)
14083 pat = pat3; /* outer level, search for middle */
14084 }
14085
14086 if (nest == 0)
14087 {
14088 /* Found the match: return matchcount or line number. */
14089 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014090 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014092 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014093 if (flags & SP_SETPCMARK)
14094 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 curwin->w_cursor = pos;
14096 if (!(flags & SP_REPEAT))
14097 break;
14098 nest = 1; /* search for next unmatched */
14099 }
14100 }
14101
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014102 if (match_pos != NULL)
14103 {
14104 /* Store the match cursor position */
14105 match_pos->lnum = curwin->w_cursor.lnum;
14106 match_pos->col = curwin->w_cursor.col + 1;
14107 }
14108
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014110 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 curwin->w_cursor = save_cursor;
14112
14113theend:
14114 vim_free(pat2);
14115 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014117
14118 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119}
14120
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014121/*
14122 * "searchpos()" function
14123 */
14124 static void
14125f_searchpos(argvars, rettv)
14126 typval_T *argvars;
14127 typval_T *rettv;
14128{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014129 pos_T match_pos;
14130 int lnum = 0;
14131 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014132 int n;
14133 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014134
14135 rettv->vval.v_number = 0;
14136
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014137 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014138 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014139
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014140 n = search_cmn(argvars, &match_pos, &flags);
14141 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014142 {
14143 lnum = match_pos.lnum;
14144 col = match_pos.col;
14145 }
14146
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014147 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14148 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014149 if (flags & SP_SUBPAT)
14150 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014151}
14152
14153
Bram Moolenaar0d660222005-01-07 21:51:51 +000014154/*ARGSUSED*/
14155 static void
14156f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014157 typval_T *argvars;
14158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014160#ifdef FEAT_CLIENTSERVER
14161 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 char_u *server = get_tv_string_chk(&argvars[0]);
14163 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 if (server == NULL || reply == NULL)
14167 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014168 if (check_restricted() || check_secure())
14169 return;
14170# ifdef FEAT_X11
14171 if (check_connection() == FAIL)
14172 return;
14173# endif
14174
14175 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014177 EMSG(_("E258: Unable to send to client"));
14178 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014180 rettv->vval.v_number = 0;
14181#else
14182 rettv->vval.v_number = -1;
14183#endif
14184}
14185
14186/*ARGSUSED*/
14187 static void
14188f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 typval_T *argvars;
14190 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014191{
14192 char_u *r = NULL;
14193
14194#ifdef FEAT_CLIENTSERVER
14195# ifdef WIN32
14196 r = serverGetVimNames();
14197# else
14198 make_connection();
14199 if (X_DISPLAY != NULL)
14200 r = serverGetVimNames(X_DISPLAY);
14201# endif
14202#endif
14203 rettv->v_type = VAR_STRING;
14204 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205}
14206
14207/*
14208 * "setbufvar()" function
14209 */
14210/*ARGSUSED*/
14211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014212f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014213 typval_T *argvars;
14214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215{
14216 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 char_u nbuf[NUMBUFLEN];
14221
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 rettv->vval.v_number = 0;
14223
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224 if (check_restricted() || check_secure())
14225 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14227 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 varp = &argvars[2];
14230
14231 if (buf != NULL && varname != NULL && varp != NULL)
14232 {
14233 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235
14236 if (*varname == '&')
14237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 long numval;
14239 char_u *strval;
14240 int error = FALSE;
14241
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 numval = get_tv_number_chk(varp, &error);
14244 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014245 if (!error && strval != NULL)
14246 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 }
14248 else
14249 {
14250 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14251 if (bufvarname != NULL)
14252 {
14253 STRCPY(bufvarname, "b:");
14254 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014255 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 vim_free(bufvarname);
14257 }
14258 }
14259
14260 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263}
14264
14265/*
14266 * "setcmdpos()" function
14267 */
14268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014269f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014270 typval_T *argvars;
14271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 int pos = (int)get_tv_number(&argvars[0]) - 1;
14274
14275 if (pos >= 0)
14276 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277}
14278
14279/*
14280 * "setline()" function
14281 */
14282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014284 typval_T *argvars;
14285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286{
14287 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014288 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014289 list_T *l = NULL;
14290 listitem_T *li = NULL;
14291 long added = 0;
14292 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014294 lnum = get_tv_lnum(&argvars[0]);
14295 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014297 l = argvars[1].vval.v_list;
14298 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014300 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014301 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014302
14303 rettv->vval.v_number = 0; /* OK */
14304 for (;;)
14305 {
14306 if (l != NULL)
14307 {
14308 /* list argument, get next string */
14309 if (li == NULL)
14310 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014312 li = li->li_next;
14313 }
14314
14315 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014317 break;
14318 if (lnum <= curbuf->b_ml.ml_line_count)
14319 {
14320 /* existing line, replace it */
14321 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14322 {
14323 changed_bytes(lnum, 0);
14324 check_cursor_col();
14325 rettv->vval.v_number = 0; /* OK */
14326 }
14327 }
14328 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14329 {
14330 /* lnum is one past the last line, append the line */
14331 ++added;
14332 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14333 rettv->vval.v_number = 0; /* OK */
14334 }
14335
14336 if (l == NULL) /* only one string argument */
14337 break;
14338 ++lnum;
14339 }
14340
14341 if (added > 0)
14342 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343}
14344
14345/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014346 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014347 */
14348/*ARGSUSED*/
14349 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014350set_qf_ll_list(wp, list_arg, action_arg, rettv)
14351 win_T *wp;
14352 typval_T *list_arg;
14353 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014354 typval_T *rettv;
14355{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014356#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014357 char_u *act;
14358 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014359#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014360
Bram Moolenaar2641f772005-03-25 21:58:17 +000014361 rettv->vval.v_number = -1;
14362
14363#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014364 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014365 EMSG(_(e_listreq));
14366 else
14367 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014368 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014369
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014370 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014371 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014372 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 if (act == NULL)
14374 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014375 if (*act == 'a' || *act == 'r')
14376 action = *act;
14377 }
14378
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014379 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014380 rettv->vval.v_number = 0;
14381 }
14382#endif
14383}
14384
14385/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014386 * "setloclist()" function
14387 */
14388/*ARGSUSED*/
14389 static void
14390f_setloclist(argvars, rettv)
14391 typval_T *argvars;
14392 typval_T *rettv;
14393{
14394 win_T *win;
14395
14396 rettv->vval.v_number = -1;
14397
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014398 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014399 if (win != NULL)
14400 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14401}
14402
14403/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014404 * "setpos()" function
14405 */
14406/*ARGSUSED*/
14407 static void
14408f_setpos(argvars, rettv)
14409 typval_T *argvars;
14410 typval_T *rettv;
14411{
14412 pos_T pos;
14413 int fnum;
14414 char_u *name;
14415
14416 name = get_tv_string_chk(argvars);
14417 if (name != NULL)
14418 {
14419 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14420 {
14421 --pos.col;
14422 if (name[0] == '.') /* cursor */
14423 {
14424 if (fnum == curbuf->b_fnum)
14425 {
14426 curwin->w_cursor = pos;
14427 check_cursor();
14428 }
14429 else
14430 EMSG(_(e_invarg));
14431 }
14432 else if (name[0] == '\'') /* mark */
14433 (void)setmark_pos(name[1], &pos, fnum);
14434 else
14435 EMSG(_(e_invarg));
14436 }
14437 }
14438}
14439
14440/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014441 * "setqflist()" function
14442 */
14443/*ARGSUSED*/
14444 static void
14445f_setqflist(argvars, rettv)
14446 typval_T *argvars;
14447 typval_T *rettv;
14448{
14449 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14450}
14451
14452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 * "setreg()" function
14454 */
14455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014457 typval_T *argvars;
14458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459{
14460 int regname;
14461 char_u *strregname;
14462 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014463 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 int append;
14465 char_u yank_type;
14466 long block_len;
14467
14468 block_len = -1;
14469 yank_type = MAUTO;
14470 append = FALSE;
14471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014472 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014475 if (strregname == NULL)
14476 return; /* type error; errmsg already given */
14477 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 if (regname == 0 || regname == '@')
14479 regname = '"';
14480 else if (regname == '=')
14481 return;
14482
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014483 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 stropt = get_tv_string_chk(&argvars[2]);
14486 if (stropt == NULL)
14487 return; /* type error */
14488 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 switch (*stropt)
14490 {
14491 case 'a': case 'A': /* append */
14492 append = TRUE;
14493 break;
14494 case 'v': case 'c': /* character-wise selection */
14495 yank_type = MCHAR;
14496 break;
14497 case 'V': case 'l': /* line-wise selection */
14498 yank_type = MLINE;
14499 break;
14500#ifdef FEAT_VISUAL
14501 case 'b': case Ctrl_V: /* block-wise selection */
14502 yank_type = MBLOCK;
14503 if (VIM_ISDIGIT(stropt[1]))
14504 {
14505 ++stropt;
14506 block_len = getdigits(&stropt) - 1;
14507 --stropt;
14508 }
14509 break;
14510#endif
14511 }
14512 }
14513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014514 strval = get_tv_string_chk(&argvars[1]);
14515 if (strval != NULL)
14516 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519}
14520
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014521/*
14522 * "settabwinvar()" function
14523 */
14524 static void
14525f_settabwinvar(argvars, rettv)
14526 typval_T *argvars;
14527 typval_T *rettv;
14528{
14529 setwinvar(argvars, rettv, 1);
14530}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531
14532/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014533 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014537 typval_T *argvars;
14538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014540 setwinvar(argvars, rettv, 0);
14541}
14542
14543/*
14544 * "setwinvar()" and "settabwinvar()" functions
14545 */
14546 static void
14547setwinvar(argvars, rettv, off)
14548 typval_T *argvars;
14549 typval_T *rettv;
14550 int off;
14551{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 win_T *win;
14553#ifdef FEAT_WINDOWS
14554 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014555 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556#endif
14557 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014558 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014560 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 rettv->vval.v_number = 0;
14563
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 if (check_restricted() || check_secure())
14565 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014566
14567#ifdef FEAT_WINDOWS
14568 if (off == 1)
14569 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14570 else
14571 tp = curtab;
14572#endif
14573 win = find_win_by_nr(&argvars[off], tp);
14574 varname = get_tv_string_chk(&argvars[off + 1]);
14575 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576
14577 if (win != NULL && varname != NULL && varp != NULL)
14578 {
14579#ifdef FEAT_WINDOWS
14580 /* set curwin to be our win, temporarily */
14581 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014582 save_curtab = curtab;
14583 goto_tabpage_tp(tp);
14584 if (!win_valid(win))
14585 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 curwin = win;
14587 curbuf = curwin->w_buffer;
14588#endif
14589
14590 if (*varname == '&')
14591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 long numval;
14593 char_u *strval;
14594 int error = FALSE;
14595
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 numval = get_tv_number_chk(varp, &error);
14598 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 if (!error && strval != NULL)
14600 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 }
14602 else
14603 {
14604 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14605 if (winvarname != NULL)
14606 {
14607 STRCPY(winvarname, "w:");
14608 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014609 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 vim_free(winvarname);
14611 }
14612 }
14613
14614#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014615 /* Restore current tabpage and window, if still valid (autocomands can
14616 * make them invalid). */
14617 if (valid_tabpage(save_curtab))
14618 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 if (win_valid(save_curwin))
14620 {
14621 curwin = save_curwin;
14622 curbuf = curwin->w_buffer;
14623 }
14624#endif
14625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626}
14627
14628/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014629 * "shellescape({string})" function
14630 */
14631 static void
14632f_shellescape(argvars, rettv)
14633 typval_T *argvars;
14634 typval_T *rettv;
14635{
14636 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14637 rettv->v_type = VAR_STRING;
14638}
14639
14640/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 */
14643 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014645 typval_T *argvars;
14646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649
Bram Moolenaar0d660222005-01-07 21:51:51 +000014650 p = get_tv_string(&argvars[0]);
14651 rettv->vval.v_string = vim_strsave(p);
14652 simplify_filename(rettv->vval.v_string); /* simplify in place */
14653 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654}
14655
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656static int
14657#ifdef __BORLANDC__
14658 _RTLENTRYF
14659#endif
14660 item_compare __ARGS((const void *s1, const void *s2));
14661static int
14662#ifdef __BORLANDC__
14663 _RTLENTRYF
14664#endif
14665 item_compare2 __ARGS((const void *s1, const void *s2));
14666
14667static int item_compare_ic;
14668static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670#define ITEM_COMPARE_FAIL 999
14671
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014673 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675 static int
14676#ifdef __BORLANDC__
14677_RTLENTRYF
14678#endif
14679item_compare(s1, s2)
14680 const void *s1;
14681 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683 char_u *p1, *p2;
14684 char_u *tofree1, *tofree2;
14685 int res;
14686 char_u numbuf1[NUMBUFLEN];
14687 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014689 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14690 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691 if (item_compare_ic)
14692 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014694 res = STRCMP(p1, p2);
14695 vim_free(tofree1);
14696 vim_free(tofree2);
14697 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698}
14699
14700 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701#ifdef __BORLANDC__
14702_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704item_compare2(s1, s2)
14705 const void *s1;
14706 const void *s2;
14707{
14708 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014710 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014713 /* shortcut after failure in previous call; compare all items equal */
14714 if (item_compare_func_err)
14715 return 0;
14716
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014717 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14718 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014719 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14720 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014721
14722 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014723 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014724 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014725 clear_tv(&argv[0]);
14726 clear_tv(&argv[1]);
14727
14728 if (res == FAIL)
14729 res = ITEM_COMPARE_FAIL;
14730 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014731 /* return value has wrong type */
14732 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14733 if (item_compare_func_err)
14734 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014735 clear_tv(&rettv);
14736 return res;
14737}
14738
14739/*
14740 * "sort({list})" function
14741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014743f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014744 typval_T *argvars;
14745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746{
Bram Moolenaar33570922005-01-25 22:26:29 +000014747 list_T *l;
14748 listitem_T *li;
14749 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750 long len;
14751 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753 rettv->vval.v_number = 0;
14754 if (argvars[0].v_type != VAR_LIST)
14755 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 else
14757 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014758 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014759 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760 return;
14761 rettv->vval.v_list = l;
14762 rettv->v_type = VAR_LIST;
14763 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764
Bram Moolenaar0d660222005-01-07 21:51:51 +000014765 len = list_len(l);
14766 if (len <= 1)
14767 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768
Bram Moolenaar0d660222005-01-07 21:51:51 +000014769 item_compare_ic = FALSE;
14770 item_compare_func = NULL;
14771 if (argvars[1].v_type != VAR_UNKNOWN)
14772 {
14773 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014775 else
14776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014777 int error = FALSE;
14778
14779 i = get_tv_number_chk(&argvars[1], &error);
14780 if (error)
14781 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014782 if (i == 1)
14783 item_compare_ic = TRUE;
14784 else
14785 item_compare_func = get_tv_string(&argvars[1]);
14786 }
14787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788
Bram Moolenaar0d660222005-01-07 21:51:51 +000014789 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014791 if (ptrs == NULL)
14792 return;
14793 i = 0;
14794 for (li = l->lv_first; li != NULL; li = li->li_next)
14795 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014797 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014798 /* test the compare function */
14799 if (item_compare_func != NULL
14800 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14801 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014802 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014804 {
14805 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014806 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014807 item_compare_func == NULL ? item_compare : item_compare2);
14808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014809 if (!item_compare_func_err)
14810 {
14811 /* Clear the List and append the items in the sorted order. */
14812 l->lv_first = l->lv_last = NULL;
14813 l->lv_len = 0;
14814 for (i = 0; i < len; ++i)
14815 list_append(l, ptrs[i]);
14816 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014817 }
14818
14819 vim_free(ptrs);
14820 }
14821}
14822
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014823/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014824 * "soundfold({word})" function
14825 */
14826 static void
14827f_soundfold(argvars, rettv)
14828 typval_T *argvars;
14829 typval_T *rettv;
14830{
14831 char_u *s;
14832
14833 rettv->v_type = VAR_STRING;
14834 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014835#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014836 rettv->vval.v_string = eval_soundfold(s);
14837#else
14838 rettv->vval.v_string = vim_strsave(s);
14839#endif
14840}
14841
14842/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014843 * "spellbadword()" function
14844 */
14845/* ARGSUSED */
14846 static void
14847f_spellbadword(argvars, rettv)
14848 typval_T *argvars;
14849 typval_T *rettv;
14850{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014851 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014852 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014853 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014854
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014855 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014856 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014857
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014858#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014859 if (argvars[0].v_type == VAR_UNKNOWN)
14860 {
14861 /* Find the start and length of the badly spelled word. */
14862 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14863 if (len != 0)
14864 word = ml_get_cursor();
14865 }
14866 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14867 {
14868 char_u *str = get_tv_string_chk(&argvars[0]);
14869 int capcol = -1;
14870
14871 if (str != NULL)
14872 {
14873 /* Check the argument for spelling. */
14874 while (*str != NUL)
14875 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014876 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014877 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014878 {
14879 word = str;
14880 break;
14881 }
14882 str += len;
14883 }
14884 }
14885 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014886#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014887
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014888 list_append_string(rettv->vval.v_list, word, len);
14889 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014890 attr == HLF_SPB ? "bad" :
14891 attr == HLF_SPR ? "rare" :
14892 attr == HLF_SPL ? "local" :
14893 attr == HLF_SPC ? "caps" :
14894 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014895}
14896
14897/*
14898 * "spellsuggest()" function
14899 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014900/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014901 static void
14902f_spellsuggest(argvars, rettv)
14903 typval_T *argvars;
14904 typval_T *rettv;
14905{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014906#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014907 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014908 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014909 int maxcount;
14910 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014911 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014912 listitem_T *li;
14913 int need_capital = FALSE;
14914#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014915
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014916 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014917 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014918
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014919#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014920 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14921 {
14922 str = get_tv_string(&argvars[0]);
14923 if (argvars[1].v_type != VAR_UNKNOWN)
14924 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014925 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014926 if (maxcount <= 0)
14927 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014928 if (argvars[2].v_type != VAR_UNKNOWN)
14929 {
14930 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14931 if (typeerr)
14932 return;
14933 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014934 }
14935 else
14936 maxcount = 25;
14937
Bram Moolenaar4770d092006-01-12 23:22:24 +000014938 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014939
14940 for (i = 0; i < ga.ga_len; ++i)
14941 {
14942 str = ((char_u **)ga.ga_data)[i];
14943
14944 li = listitem_alloc();
14945 if (li == NULL)
14946 vim_free(str);
14947 else
14948 {
14949 li->li_tv.v_type = VAR_STRING;
14950 li->li_tv.v_lock = 0;
14951 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014952 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014953 }
14954 }
14955 ga_clear(&ga);
14956 }
14957#endif
14958}
14959
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014961f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014962 typval_T *argvars;
14963 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014964{
14965 char_u *str;
14966 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014967 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968 regmatch_T regmatch;
14969 char_u patbuf[NUMBUFLEN];
14970 char_u *save_cpo;
14971 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014973 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014975
14976 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14977 save_cpo = p_cpo;
14978 p_cpo = (char_u *)"";
14979
14980 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014981 if (argvars[1].v_type != VAR_UNKNOWN)
14982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014983 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14984 if (pat == NULL)
14985 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014987 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014988 }
14989 if (pat == NULL || *pat == NUL)
14990 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014991
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014992 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014994 if (typeerr)
14995 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14998 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015001 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015002 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015003 if (*str == NUL)
15004 match = FALSE; /* empty item at the end */
15005 else
15006 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007 if (match)
15008 end = regmatch.startp[0];
15009 else
15010 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015011 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15012 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015013 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015014 if (list_append_string(rettv->vval.v_list, str,
15015 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017 }
15018 if (!match)
15019 break;
15020 /* Advance to just after the match. */
15021 if (regmatch.endp[0] > str)
15022 col = 0;
15023 else
15024 {
15025 /* Don't get stuck at the same match. */
15026#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015027 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015028#else
15029 col = 1;
15030#endif
15031 }
15032 str = regmatch.endp[0];
15033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037
Bram Moolenaar0d660222005-01-07 21:51:51 +000015038 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039}
15040
Bram Moolenaar2c932302006-03-18 21:42:09 +000015041/*
15042 * "str2nr()" function
15043 */
15044 static void
15045f_str2nr(argvars, rettv)
15046 typval_T *argvars;
15047 typval_T *rettv;
15048{
15049 int base = 10;
15050 char_u *p;
15051 long n;
15052
15053 if (argvars[1].v_type != VAR_UNKNOWN)
15054 {
15055 base = get_tv_number(&argvars[1]);
15056 if (base != 8 && base != 10 && base != 16)
15057 {
15058 EMSG(_(e_invarg));
15059 return;
15060 }
15061 }
15062
15063 p = skipwhite(get_tv_string(&argvars[0]));
15064 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15065 rettv->vval.v_number = n;
15066}
15067
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068#ifdef HAVE_STRFTIME
15069/*
15070 * "strftime({format}[, {time}])" function
15071 */
15072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015073f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015074 typval_T *argvars;
15075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076{
15077 char_u result_buf[256];
15078 struct tm *curtime;
15079 time_t seconds;
15080 char_u *p;
15081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015082 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015084 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015085 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086 seconds = time(NULL);
15087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015088 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089 curtime = localtime(&seconds);
15090 /* MSVC returns NULL for an invalid value of seconds. */
15091 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 else
15094 {
15095# ifdef FEAT_MBYTE
15096 vimconv_T conv;
15097 char_u *enc;
15098
15099 conv.vc_type = CONV_NONE;
15100 enc = enc_locale();
15101 convert_setup(&conv, p_enc, enc);
15102 if (conv.vc_type != CONV_NONE)
15103 p = string_convert(&conv, p, NULL);
15104# endif
15105 if (p != NULL)
15106 (void)strftime((char *)result_buf, sizeof(result_buf),
15107 (char *)p, curtime);
15108 else
15109 result_buf[0] = NUL;
15110
15111# ifdef FEAT_MBYTE
15112 if (conv.vc_type != CONV_NONE)
15113 vim_free(p);
15114 convert_setup(&conv, enc, p_enc);
15115 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015116 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 else
15118# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015119 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120
15121# ifdef FEAT_MBYTE
15122 /* Release conversion descriptors */
15123 convert_setup(&conv, NULL, NULL);
15124 vim_free(enc);
15125# endif
15126 }
15127}
15128#endif
15129
15130/*
15131 * "stridx()" function
15132 */
15133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015134f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 typval_T *argvars;
15136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137{
15138 char_u buf[NUMBUFLEN];
15139 char_u *needle;
15140 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015141 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015143 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015145 needle = get_tv_string_chk(&argvars[1]);
15146 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 if (needle == NULL || haystack == NULL)
15149 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150
Bram Moolenaar33570922005-01-25 22:26:29 +000015151 if (argvars[2].v_type != VAR_UNKNOWN)
15152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015153 int error = FALSE;
15154
15155 start_idx = get_tv_number_chk(&argvars[2], &error);
15156 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015157 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015158 if (start_idx >= 0)
15159 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015160 }
15161
15162 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15163 if (pos != NULL)
15164 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165}
15166
15167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015168 * "string()" function
15169 */
15170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015171f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015172 typval_T *argvars;
15173 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015174{
15175 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015176 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015179 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015180 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015181 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182}
15183
15184/*
15185 * "strlen()" function
15186 */
15187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015188f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015189 typval_T *argvars;
15190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015192 rettv->vval.v_number = (varnumber_T)(STRLEN(
15193 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194}
15195
15196/*
15197 * "strpart()" function
15198 */
15199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015200f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015201 typval_T *argvars;
15202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203{
15204 char_u *p;
15205 int n;
15206 int len;
15207 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015208 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015210 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 slen = (int)STRLEN(p);
15212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015213 n = get_tv_number_chk(&argvars[1], &error);
15214 if (error)
15215 len = 0;
15216 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015217 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 else
15219 len = slen - n; /* default len: all bytes that are available. */
15220
15221 /*
15222 * Only return the overlap between the specified part and the actual
15223 * string.
15224 */
15225 if (n < 0)
15226 {
15227 len += n;
15228 n = 0;
15229 }
15230 else if (n > slen)
15231 n = slen;
15232 if (len < 0)
15233 len = 0;
15234 else if (n + len > slen)
15235 len = slen - n;
15236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015237 rettv->v_type = VAR_STRING;
15238 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239}
15240
15241/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242 * "strridx()" function
15243 */
15244 static void
15245f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015246 typval_T *argvars;
15247 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015248{
15249 char_u buf[NUMBUFLEN];
15250 char_u *needle;
15251 char_u *haystack;
15252 char_u *rest;
15253 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015254 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015256 needle = get_tv_string_chk(&argvars[1]);
15257 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015258
15259 rettv->vval.v_number = -1;
15260 if (needle == NULL || haystack == NULL)
15261 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015262
15263 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015264 if (argvars[2].v_type != VAR_UNKNOWN)
15265 {
15266 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015267 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015268 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015269 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015270 }
15271 else
15272 end_idx = haystack_len;
15273
Bram Moolenaar0d660222005-01-07 21:51:51 +000015274 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015275 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015276 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015277 lastmatch = haystack + end_idx;
15278 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015279 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281 for (rest = haystack; *rest != '\0'; ++rest)
15282 {
15283 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015284 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285 break;
15286 lastmatch = rest;
15287 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015288 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015289
15290 if (lastmatch == NULL)
15291 rettv->vval.v_number = -1;
15292 else
15293 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15294}
15295
15296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 * "strtrans()" function
15298 */
15299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015301 typval_T *argvars;
15302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304 rettv->v_type = VAR_STRING;
15305 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306}
15307
15308/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309 * "submatch()" function
15310 */
15311 static void
15312f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015313 typval_T *argvars;
15314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015315{
15316 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015317 rettv->vval.v_string =
15318 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015319}
15320
15321/*
15322 * "substitute()" function
15323 */
15324 static void
15325f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015326 typval_T *argvars;
15327 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328{
15329 char_u patbuf[NUMBUFLEN];
15330 char_u subbuf[NUMBUFLEN];
15331 char_u flagsbuf[NUMBUFLEN];
15332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015333 char_u *str = get_tv_string_chk(&argvars[0]);
15334 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15335 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15336 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15337
Bram Moolenaar0d660222005-01-07 21:51:51 +000015338 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015339 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15340 rettv->vval.v_string = NULL;
15341 else
15342 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343}
15344
15345/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015346 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 */
15348/*ARGSUSED*/
15349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015350f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015351 typval_T *argvars;
15352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353{
15354 int id = 0;
15355#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015356 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 long col;
15358 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015359 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015361 lnum = get_tv_lnum(argvars); /* -1 on type error */
15362 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15363 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015365 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015366 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015367 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368#endif
15369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371}
15372
15373/*
15374 * "synIDattr(id, what [, mode])" function
15375 */
15376/*ARGSUSED*/
15377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015379 typval_T *argvars;
15380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381{
15382 char_u *p = NULL;
15383#ifdef FEAT_SYN_HL
15384 int id;
15385 char_u *what;
15386 char_u *mode;
15387 char_u modebuf[NUMBUFLEN];
15388 int modec;
15389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 id = get_tv_number(&argvars[0]);
15391 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015392 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 modec = TOLOWER_ASC(mode[0]);
15396 if (modec != 't' && modec != 'c'
15397#ifdef FEAT_GUI
15398 && modec != 'g'
15399#endif
15400 )
15401 modec = 0; /* replace invalid with current */
15402 }
15403 else
15404 {
15405#ifdef FEAT_GUI
15406 if (gui.in_use)
15407 modec = 'g';
15408 else
15409#endif
15410 if (t_colors > 1)
15411 modec = 'c';
15412 else
15413 modec = 't';
15414 }
15415
15416
15417 switch (TOLOWER_ASC(what[0]))
15418 {
15419 case 'b':
15420 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15421 p = highlight_color(id, what, modec);
15422 else /* bold */
15423 p = highlight_has_attr(id, HL_BOLD, modec);
15424 break;
15425
15426 case 'f': /* fg[#] */
15427 p = highlight_color(id, what, modec);
15428 break;
15429
15430 case 'i':
15431 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15432 p = highlight_has_attr(id, HL_INVERSE, modec);
15433 else /* italic */
15434 p = highlight_has_attr(id, HL_ITALIC, modec);
15435 break;
15436
15437 case 'n': /* name */
15438 p = get_highlight_name(NULL, id - 1);
15439 break;
15440
15441 case 'r': /* reverse */
15442 p = highlight_has_attr(id, HL_INVERSE, modec);
15443 break;
15444
15445 case 's': /* standout */
15446 p = highlight_has_attr(id, HL_STANDOUT, modec);
15447 break;
15448
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015449 case 'u':
15450 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15451 /* underline */
15452 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15453 else
15454 /* undercurl */
15455 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 break;
15457 }
15458
15459 if (p != NULL)
15460 p = vim_strsave(p);
15461#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 rettv->v_type = VAR_STRING;
15463 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464}
15465
15466/*
15467 * "synIDtrans(id)" function
15468 */
15469/*ARGSUSED*/
15470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015471f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015472 typval_T *argvars;
15473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474{
15475 int id;
15476
15477#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479
15480 if (id > 0)
15481 id = syn_get_final_id(id);
15482 else
15483#endif
15484 id = 0;
15485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487}
15488
15489/*
15490 * "system()" function
15491 */
15492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015494 typval_T *argvars;
15495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015497 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015499 char_u *infile = NULL;
15500 char_u buf[NUMBUFLEN];
15501 int err = FALSE;
15502 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015504 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015505 {
15506 /*
15507 * Write the string to a temp file, to be used for input of the shell
15508 * command.
15509 */
15510 if ((infile = vim_tempname('i')) == NULL)
15511 {
15512 EMSG(_(e_notmp));
15513 return;
15514 }
15515
15516 fd = mch_fopen((char *)infile, WRITEBIN);
15517 if (fd == NULL)
15518 {
15519 EMSG2(_(e_notopen), infile);
15520 goto done;
15521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015522 p = get_tv_string_buf_chk(&argvars[1], buf);
15523 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015524 {
15525 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015526 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015527 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015528 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15529 err = TRUE;
15530 if (fclose(fd) != 0)
15531 err = TRUE;
15532 if (err)
15533 {
15534 EMSG(_("E677: Error writing temp file"));
15535 goto done;
15536 }
15537 }
15538
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015539 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15540 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015541
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542#ifdef USE_CR
15543 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015544 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 {
15546 char_u *s;
15547
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015548 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 {
15550 if (*s == CAR)
15551 *s = NL;
15552 }
15553 }
15554#else
15555# ifdef USE_CRNL
15556 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015557 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 {
15559 char_u *s, *d;
15560
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015561 d = res;
15562 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 {
15564 if (s[0] == CAR && s[1] == NL)
15565 ++s;
15566 *d++ = *s;
15567 }
15568 *d = NUL;
15569 }
15570# endif
15571#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015572
15573done:
15574 if (infile != NULL)
15575 {
15576 mch_remove(infile);
15577 vim_free(infile);
15578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 rettv->v_type = VAR_STRING;
15580 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581}
15582
15583/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015584 * "tabpagebuflist()" function
15585 */
15586/* ARGSUSED */
15587 static void
15588f_tabpagebuflist(argvars, rettv)
15589 typval_T *argvars;
15590 typval_T *rettv;
15591{
15592#ifndef FEAT_WINDOWS
15593 rettv->vval.v_number = 0;
15594#else
15595 tabpage_T *tp;
15596 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015597
15598 if (argvars[0].v_type == VAR_UNKNOWN)
15599 wp = firstwin;
15600 else
15601 {
15602 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15603 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015604 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015605 }
15606 if (wp == NULL)
15607 rettv->vval.v_number = 0;
15608 else
15609 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015610 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015611 rettv->vval.v_number = 0;
15612 else
15613 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015614 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015615 if (list_append_number(rettv->vval.v_list,
15616 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015617 break;
15618 }
15619 }
15620#endif
15621}
15622
15623
15624/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015625 * "tabpagenr()" function
15626 */
15627/* ARGSUSED */
15628 static void
15629f_tabpagenr(argvars, rettv)
15630 typval_T *argvars;
15631 typval_T *rettv;
15632{
15633 int nr = 1;
15634#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015635 char_u *arg;
15636
15637 if (argvars[0].v_type != VAR_UNKNOWN)
15638 {
15639 arg = get_tv_string_chk(&argvars[0]);
15640 nr = 0;
15641 if (arg != NULL)
15642 {
15643 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015644 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015645 else
15646 EMSG2(_(e_invexpr2), arg);
15647 }
15648 }
15649 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015650 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015651#endif
15652 rettv->vval.v_number = nr;
15653}
15654
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015655
15656#ifdef FEAT_WINDOWS
15657static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15658
15659/*
15660 * Common code for tabpagewinnr() and winnr().
15661 */
15662 static int
15663get_winnr(tp, argvar)
15664 tabpage_T *tp;
15665 typval_T *argvar;
15666{
15667 win_T *twin;
15668 int nr = 1;
15669 win_T *wp;
15670 char_u *arg;
15671
15672 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15673 if (argvar->v_type != VAR_UNKNOWN)
15674 {
15675 arg = get_tv_string_chk(argvar);
15676 if (arg == NULL)
15677 nr = 0; /* type error; errmsg already given */
15678 else if (STRCMP(arg, "$") == 0)
15679 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15680 else if (STRCMP(arg, "#") == 0)
15681 {
15682 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15683 if (twin == NULL)
15684 nr = 0;
15685 }
15686 else
15687 {
15688 EMSG2(_(e_invexpr2), arg);
15689 nr = 0;
15690 }
15691 }
15692
15693 if (nr > 0)
15694 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15695 wp != twin; wp = wp->w_next)
15696 ++nr;
15697 return nr;
15698}
15699#endif
15700
15701/*
15702 * "tabpagewinnr()" function
15703 */
15704/* ARGSUSED */
15705 static void
15706f_tabpagewinnr(argvars, rettv)
15707 typval_T *argvars;
15708 typval_T *rettv;
15709{
15710 int nr = 1;
15711#ifdef FEAT_WINDOWS
15712 tabpage_T *tp;
15713
15714 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15715 if (tp == NULL)
15716 nr = 0;
15717 else
15718 nr = get_winnr(tp, &argvars[1]);
15719#endif
15720 rettv->vval.v_number = nr;
15721}
15722
15723
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015724/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015725 * "tagfiles()" function
15726 */
15727/*ARGSUSED*/
15728 static void
15729f_tagfiles(argvars, rettv)
15730 typval_T *argvars;
15731 typval_T *rettv;
15732{
15733 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015734 tagname_T tn;
15735 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015736
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015737 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015738 {
15739 rettv->vval.v_number = 0;
15740 return;
15741 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015742
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015743 for (first = TRUE; ; first = FALSE)
15744 if (get_tagfname(&tn, first, fname) == FAIL
15745 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015746 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015747 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015748}
15749
15750/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015751 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015752 */
15753 static void
15754f_taglist(argvars, rettv)
15755 typval_T *argvars;
15756 typval_T *rettv;
15757{
15758 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015759
15760 tag_pattern = get_tv_string(&argvars[0]);
15761
15762 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015763 if (*tag_pattern == NUL)
15764 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015765
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015766 if (rettv_list_alloc(rettv) == OK)
15767 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015768}
15769
15770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 * "tempname()" function
15772 */
15773/*ARGSUSED*/
15774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015775f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015776 typval_T *argvars;
15777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778{
15779 static int x = 'A';
15780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015781 rettv->v_type = VAR_STRING;
15782 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783
15784 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15785 * names. Skip 'I' and 'O', they are used for shell redirection. */
15786 do
15787 {
15788 if (x == 'Z')
15789 x = '0';
15790 else if (x == '9')
15791 x = 'A';
15792 else
15793 {
15794#ifdef EBCDIC
15795 if (x == 'I')
15796 x = 'J';
15797 else if (x == 'R')
15798 x = 'S';
15799 else
15800#endif
15801 ++x;
15802 }
15803 } while (x == 'I' || x == 'O');
15804}
15805
15806/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015807 * "test(list)" function: Just checking the walls...
15808 */
15809/*ARGSUSED*/
15810 static void
15811f_test(argvars, rettv)
15812 typval_T *argvars;
15813 typval_T *rettv;
15814{
15815 /* Used for unit testing. Change the code below to your liking. */
15816#if 0
15817 listitem_T *li;
15818 list_T *l;
15819 char_u *bad, *good;
15820
15821 if (argvars[0].v_type != VAR_LIST)
15822 return;
15823 l = argvars[0].vval.v_list;
15824 if (l == NULL)
15825 return;
15826 li = l->lv_first;
15827 if (li == NULL)
15828 return;
15829 bad = get_tv_string(&li->li_tv);
15830 li = li->li_next;
15831 if (li == NULL)
15832 return;
15833 good = get_tv_string(&li->li_tv);
15834 rettv->vval.v_number = test_edit_score(bad, good);
15835#endif
15836}
15837
15838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 * "tolower(string)" function
15840 */
15841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015842f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015843 typval_T *argvars;
15844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845{
15846 char_u *p;
15847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015848 p = vim_strsave(get_tv_string(&argvars[0]));
15849 rettv->v_type = VAR_STRING;
15850 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851
15852 if (p != NULL)
15853 while (*p != NUL)
15854 {
15855#ifdef FEAT_MBYTE
15856 int l;
15857
15858 if (enc_utf8)
15859 {
15860 int c, lc;
15861
15862 c = utf_ptr2char(p);
15863 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015864 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 /* TODO: reallocate string when byte count changes. */
15866 if (utf_char2len(lc) == l)
15867 utf_char2bytes(lc, p);
15868 p += l;
15869 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015870 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 p += l; /* skip multi-byte character */
15872 else
15873#endif
15874 {
15875 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15876 ++p;
15877 }
15878 }
15879}
15880
15881/*
15882 * "toupper(string)" function
15883 */
15884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015885f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015886 typval_T *argvars;
15887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015889 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015890 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891}
15892
15893/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015894 * "tr(string, fromstr, tostr)" function
15895 */
15896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015897f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015898 typval_T *argvars;
15899 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015900{
15901 char_u *instr;
15902 char_u *fromstr;
15903 char_u *tostr;
15904 char_u *p;
15905#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015906 int inlen;
15907 int fromlen;
15908 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015909 int idx;
15910 char_u *cpstr;
15911 int cplen;
15912 int first = TRUE;
15913#endif
15914 char_u buf[NUMBUFLEN];
15915 char_u buf2[NUMBUFLEN];
15916 garray_T ga;
15917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015918 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015919 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15920 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015921
15922 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015923 rettv->v_type = VAR_STRING;
15924 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 if (fromstr == NULL || tostr == NULL)
15926 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015927 ga_init2(&ga, (int)sizeof(char), 80);
15928
15929#ifdef FEAT_MBYTE
15930 if (!has_mbyte)
15931#endif
15932 /* not multi-byte: fromstr and tostr must be the same length */
15933 if (STRLEN(fromstr) != STRLEN(tostr))
15934 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015935#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015936error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015937#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015938 EMSG2(_(e_invarg2), fromstr);
15939 ga_clear(&ga);
15940 return;
15941 }
15942
15943 /* fromstr and tostr have to contain the same number of chars */
15944 while (*instr != NUL)
15945 {
15946#ifdef FEAT_MBYTE
15947 if (has_mbyte)
15948 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015949 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015950 cpstr = instr;
15951 cplen = inlen;
15952 idx = 0;
15953 for (p = fromstr; *p != NUL; p += fromlen)
15954 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015955 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015956 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15957 {
15958 for (p = tostr; *p != NUL; p += tolen)
15959 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015960 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015961 if (idx-- == 0)
15962 {
15963 cplen = tolen;
15964 cpstr = p;
15965 break;
15966 }
15967 }
15968 if (*p == NUL) /* tostr is shorter than fromstr */
15969 goto error;
15970 break;
15971 }
15972 ++idx;
15973 }
15974
15975 if (first && cpstr == instr)
15976 {
15977 /* Check that fromstr and tostr have the same number of
15978 * (multi-byte) characters. Done only once when a character
15979 * of instr doesn't appear in fromstr. */
15980 first = FALSE;
15981 for (p = tostr; *p != NUL; p += tolen)
15982 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015983 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015984 --idx;
15985 }
15986 if (idx != 0)
15987 goto error;
15988 }
15989
15990 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015991 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015992 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015993
15994 instr += inlen;
15995 }
15996 else
15997#endif
15998 {
15999 /* When not using multi-byte chars we can do it faster. */
16000 p = vim_strchr(fromstr, *instr);
16001 if (p != NULL)
16002 ga_append(&ga, tostr[p - fromstr]);
16003 else
16004 ga_append(&ga, *instr);
16005 ++instr;
16006 }
16007 }
16008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016010}
16011
16012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 * "type(expr)" function
16014 */
16015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016016f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016017 typval_T *argvars;
16018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016020 int n;
16021
16022 switch (argvars[0].v_type)
16023 {
16024 case VAR_NUMBER: n = 0; break;
16025 case VAR_STRING: n = 1; break;
16026 case VAR_FUNC: n = 2; break;
16027 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016028 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016029 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16030 }
16031 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032}
16033
16034/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016035 * "values(dict)" function
16036 */
16037 static void
16038f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016039 typval_T *argvars;
16040 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016041{
16042 dict_list(argvars, rettv, 1);
16043}
16044
16045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 * "virtcol(string)" function
16047 */
16048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016049f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016050 typval_T *argvars;
16051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052{
16053 colnr_T vcol = 0;
16054 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016055 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016057 fp = var2fpos(&argvars[0], FALSE, &fnum);
16058 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16059 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060 {
16061 getvvcol(curwin, fp, NULL, NULL, &vcol);
16062 ++vcol;
16063 }
16064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016065 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066}
16067
16068/*
16069 * "visualmode()" function
16070 */
16071/*ARGSUSED*/
16072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016073f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016074 typval_T *argvars;
16075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076{
16077#ifdef FEAT_VISUAL
16078 char_u str[2];
16079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016080 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 str[0] = curbuf->b_visual_mode_eval;
16082 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016083 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084
16085 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016086 if ((argvars[0].v_type == VAR_NUMBER
16087 && argvars[0].vval.v_number != 0)
16088 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016089 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 curbuf->b_visual_mode_eval = NUL;
16091#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093#endif
16094}
16095
16096/*
16097 * "winbufnr(nr)" function
16098 */
16099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016100f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016101 typval_T *argvars;
16102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103{
16104 win_T *wp;
16105
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016106 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016108 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016110 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111}
16112
16113/*
16114 * "wincol()" function
16115 */
16116/*ARGSUSED*/
16117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016118f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016119 typval_T *argvars;
16120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121{
16122 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016123 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124}
16125
16126/*
16127 * "winheight(nr)" function
16128 */
16129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016130f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016131 typval_T *argvars;
16132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133{
16134 win_T *wp;
16135
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016136 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016138 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016140 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141}
16142
16143/*
16144 * "winline()" function
16145 */
16146/*ARGSUSED*/
16147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016148f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 typval_T *argvars;
16150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151{
16152 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016153 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154}
16155
16156/*
16157 * "winnr()" function
16158 */
16159/* ARGSUSED */
16160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016161f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016162 typval_T *argvars;
16163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164{
16165 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016166
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016168 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171}
16172
16173/*
16174 * "winrestcmd()" function
16175 */
16176/* ARGSUSED */
16177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016178f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016179 typval_T *argvars;
16180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181{
16182#ifdef FEAT_WINDOWS
16183 win_T *wp;
16184 int winnr = 1;
16185 garray_T ga;
16186 char_u buf[50];
16187
16188 ga_init2(&ga, (int)sizeof(char), 70);
16189 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16190 {
16191 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16192 ga_concat(&ga, buf);
16193# ifdef FEAT_VERTSPLIT
16194 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16195 ga_concat(&ga, buf);
16196# endif
16197 ++winnr;
16198 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016199 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016201 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016203 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206}
16207
16208/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016209 * "winrestview()" function
16210 */
16211/* ARGSUSED */
16212 static void
16213f_winrestview(argvars, rettv)
16214 typval_T *argvars;
16215 typval_T *rettv;
16216{
16217 dict_T *dict;
16218
16219 if (argvars[0].v_type != VAR_DICT
16220 || (dict = argvars[0].vval.v_dict) == NULL)
16221 EMSG(_(e_invarg));
16222 else
16223 {
16224 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16225 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16226#ifdef FEAT_VIRTUALEDIT
16227 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16228#endif
16229 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016230 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016231
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016232 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016233#ifdef FEAT_DIFF
16234 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16235#endif
16236 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16237 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16238
16239 check_cursor();
16240 changed_cline_bef_curs();
16241 invalidate_botline();
16242 redraw_later(VALID);
16243
16244 if (curwin->w_topline == 0)
16245 curwin->w_topline = 1;
16246 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16247 curwin->w_topline = curbuf->b_ml.ml_line_count;
16248#ifdef FEAT_DIFF
16249 check_topfill(curwin, TRUE);
16250#endif
16251 }
16252}
16253
16254/*
16255 * "winsaveview()" function
16256 */
16257/* ARGSUSED */
16258 static void
16259f_winsaveview(argvars, rettv)
16260 typval_T *argvars;
16261 typval_T *rettv;
16262{
16263 dict_T *dict;
16264
16265 dict = dict_alloc();
16266 if (dict == NULL)
16267 return;
16268 rettv->v_type = VAR_DICT;
16269 rettv->vval.v_dict = dict;
16270 ++dict->dv_refcount;
16271
16272 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16273 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16274#ifdef FEAT_VIRTUALEDIT
16275 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16276#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016277 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016278 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16279
16280 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16281#ifdef FEAT_DIFF
16282 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16283#endif
16284 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16285 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16286}
16287
16288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 * "winwidth(nr)" function
16290 */
16291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016292f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016293 typval_T *argvars;
16294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295{
16296 win_T *wp;
16297
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016298 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 else
16302#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016303 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016305 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306#endif
16307}
16308
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016310 * "writefile()" function
16311 */
16312 static void
16313f_writefile(argvars, rettv)
16314 typval_T *argvars;
16315 typval_T *rettv;
16316{
16317 int binary = FALSE;
16318 char_u *fname;
16319 FILE *fd;
16320 listitem_T *li;
16321 char_u *s;
16322 int ret = 0;
16323 int c;
16324
16325 if (argvars[0].v_type != VAR_LIST)
16326 {
16327 EMSG2(_(e_listarg), "writefile()");
16328 return;
16329 }
16330 if (argvars[0].vval.v_list == NULL)
16331 return;
16332
16333 if (argvars[2].v_type != VAR_UNKNOWN
16334 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16335 binary = TRUE;
16336
16337 /* Always open the file in binary mode, library functions have a mind of
16338 * their own about CR-LF conversion. */
16339 fname = get_tv_string(&argvars[1]);
16340 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16341 {
16342 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16343 ret = -1;
16344 }
16345 else
16346 {
16347 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16348 li = li->li_next)
16349 {
16350 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16351 {
16352 if (*s == '\n')
16353 c = putc(NUL, fd);
16354 else
16355 c = putc(*s, fd);
16356 if (c == EOF)
16357 {
16358 ret = -1;
16359 break;
16360 }
16361 }
16362 if (!binary || li->li_next != NULL)
16363 if (putc('\n', fd) == EOF)
16364 {
16365 ret = -1;
16366 break;
16367 }
16368 if (ret < 0)
16369 {
16370 EMSG(_(e_write));
16371 break;
16372 }
16373 }
16374 fclose(fd);
16375 }
16376
16377 rettv->vval.v_number = ret;
16378}
16379
16380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016382 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 */
16384 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016385var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016386 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016388 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016390 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016392 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393
Bram Moolenaara5525202006-03-02 22:52:09 +000016394 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016395 if (varp->v_type == VAR_LIST)
16396 {
16397 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016398 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016399 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016400
16401 l = varp->vval.v_list;
16402 if (l == NULL)
16403 return NULL;
16404
16405 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016406 pos.lnum = list_find_nr(l, 0L, &error);
16407 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016408 return NULL; /* invalid line number */
16409
16410 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016411 pos.col = list_find_nr(l, 1L, &error);
16412 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016413 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016414 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016415 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016416 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016417 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016418 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016419
Bram Moolenaara5525202006-03-02 22:52:09 +000016420#ifdef FEAT_VIRTUALEDIT
16421 /* Get the virtual offset. Defaults to zero. */
16422 pos.coladd = list_find_nr(l, 2L, &error);
16423 if (error)
16424 pos.coladd = 0;
16425#endif
16426
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016427 return &pos;
16428 }
16429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016430 name = get_tv_string_chk(varp);
16431 if (name == NULL)
16432 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433 if (name[0] == '.') /* cursor */
16434 return &curwin->w_cursor;
16435 if (name[0] == '\'') /* mark */
16436 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016437 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16439 return NULL;
16440 return pp;
16441 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016442
16443#ifdef FEAT_VIRTUALEDIT
16444 pos.coladd = 0;
16445#endif
16446
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016447 if (name[0] == 'w' && lnum)
16448 {
16449 pos.col = 0;
16450 if (name[1] == '0') /* "w0": first visible line */
16451 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016452 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016453 pos.lnum = curwin->w_topline;
16454 return &pos;
16455 }
16456 else if (name[1] == '$') /* "w$": last visible line */
16457 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016458 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016459 pos.lnum = curwin->w_botline - 1;
16460 return &pos;
16461 }
16462 }
16463 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 {
16465 if (lnum)
16466 {
16467 pos.lnum = curbuf->b_ml.ml_line_count;
16468 pos.col = 0;
16469 }
16470 else
16471 {
16472 pos.lnum = curwin->w_cursor.lnum;
16473 pos.col = (colnr_T)STRLEN(ml_get_curline());
16474 }
16475 return &pos;
16476 }
16477 return NULL;
16478}
16479
16480/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016481 * Convert list in "arg" into a position and optional file number.
16482 * When "fnump" is NULL there is no file number, only 3 items.
16483 * Note that the column is passed on as-is, the caller may want to decrement
16484 * it to use 1 for the first column.
16485 * Return FAIL when conversion is not possible, doesn't check the position for
16486 * validity.
16487 */
16488 static int
16489list2fpos(arg, posp, fnump)
16490 typval_T *arg;
16491 pos_T *posp;
16492 int *fnump;
16493{
16494 list_T *l = arg->vval.v_list;
16495 long i = 0;
16496 long n;
16497
Bram Moolenaarbde35262006-07-23 20:12:24 +000016498 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16499 * when "fnump" isn't NULL and "coladd" is optional. */
16500 if (arg->v_type != VAR_LIST
16501 || l == NULL
16502 || l->lv_len < (fnump == NULL ? 2 : 3)
16503 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016504 return FAIL;
16505
16506 if (fnump != NULL)
16507 {
16508 n = list_find_nr(l, i++, NULL); /* fnum */
16509 if (n < 0)
16510 return FAIL;
16511 if (n == 0)
16512 n = curbuf->b_fnum; /* current buffer */
16513 *fnump = n;
16514 }
16515
16516 n = list_find_nr(l, i++, NULL); /* lnum */
16517 if (n < 0)
16518 return FAIL;
16519 posp->lnum = n;
16520
16521 n = list_find_nr(l, i++, NULL); /* col */
16522 if (n < 0)
16523 return FAIL;
16524 posp->col = n;
16525
16526#ifdef FEAT_VIRTUALEDIT
16527 n = list_find_nr(l, i, NULL);
16528 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016529 posp->coladd = 0;
16530 else
16531 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016532#endif
16533
16534 return OK;
16535}
16536
16537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 * Get the length of an environment variable name.
16539 * Advance "arg" to the first character after the name.
16540 * Return 0 for error.
16541 */
16542 static int
16543get_env_len(arg)
16544 char_u **arg;
16545{
16546 char_u *p;
16547 int len;
16548
16549 for (p = *arg; vim_isIDc(*p); ++p)
16550 ;
16551 if (p == *arg) /* no name found */
16552 return 0;
16553
16554 len = (int)(p - *arg);
16555 *arg = p;
16556 return len;
16557}
16558
16559/*
16560 * Get the length of the name of a function or internal variable.
16561 * "arg" is advanced to the first non-white character after the name.
16562 * Return 0 if something is wrong.
16563 */
16564 static int
16565get_id_len(arg)
16566 char_u **arg;
16567{
16568 char_u *p;
16569 int len;
16570
16571 /* Find the end of the name. */
16572 for (p = *arg; eval_isnamec(*p); ++p)
16573 ;
16574 if (p == *arg) /* no name found */
16575 return 0;
16576
16577 len = (int)(p - *arg);
16578 *arg = skipwhite(p);
16579
16580 return len;
16581}
16582
16583/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016584 * Get the length of the name of a variable or function.
16585 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016587 * Return -1 if curly braces expansion failed.
16588 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 * If the name contains 'magic' {}'s, expand them and return the
16590 * expanded name in an allocated string via 'alias' - caller must free.
16591 */
16592 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016593get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 char_u **arg;
16595 char_u **alias;
16596 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016597 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598{
16599 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 char_u *p;
16601 char_u *expr_start;
16602 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603
16604 *alias = NULL; /* default to no alias */
16605
16606 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16607 && (*arg)[2] == (int)KE_SNR)
16608 {
16609 /* hard coded <SNR>, already translated */
16610 *arg += 3;
16611 return get_id_len(arg) + 3;
16612 }
16613 len = eval_fname_script(*arg);
16614 if (len > 0)
16615 {
16616 /* literal "<SID>", "s:" or "<SNR>" */
16617 *arg += len;
16618 }
16619
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016623 p = find_name_end(*arg, &expr_start, &expr_end,
16624 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 if (expr_start != NULL)
16626 {
16627 char_u *temp_string;
16628
16629 if (!evaluate)
16630 {
16631 len += (int)(p - *arg);
16632 *arg = skipwhite(p);
16633 return len;
16634 }
16635
16636 /*
16637 * Include any <SID> etc in the expanded string:
16638 * Thus the -len here.
16639 */
16640 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16641 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016642 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 *alias = temp_string;
16644 *arg = skipwhite(p);
16645 return (int)STRLEN(temp_string);
16646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647
16648 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016649 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 EMSG2(_(e_invexpr2), *arg);
16651
16652 return len;
16653}
16654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016655/*
16656 * Find the end of a variable or function name, taking care of magic braces.
16657 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16658 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016659 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016660 * Return a pointer to just after the name. Equal to "arg" if there is no
16661 * valid name.
16662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016664find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 char_u *arg;
16666 char_u **expr_start;
16667 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016668 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670 int mb_nest = 0;
16671 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 char_u *p;
16673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016674 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016676 *expr_start = NULL;
16677 *expr_end = NULL;
16678 }
16679
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016680 /* Quick check for valid starting character. */
16681 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16682 return arg;
16683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016684 for (p = arg; *p != NUL
16685 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016686 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016687 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016688 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016689 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016690 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016691 if (*p == '\'')
16692 {
16693 /* skip over 'string' to avoid counting [ and ] inside it. */
16694 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16695 ;
16696 if (*p == NUL)
16697 break;
16698 }
16699 else if (*p == '"')
16700 {
16701 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16702 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16703 if (*p == '\\' && p[1] != NUL)
16704 ++p;
16705 if (*p == NUL)
16706 break;
16707 }
16708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016711 if (*p == '[')
16712 ++br_nest;
16713 else if (*p == ']')
16714 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016717 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 if (*p == '{')
16720 {
16721 mb_nest++;
16722 if (expr_start != NULL && *expr_start == NULL)
16723 *expr_start = p;
16724 }
16725 else if (*p == '}')
16726 {
16727 mb_nest--;
16728 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16729 *expr_end = p;
16730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 }
16733
16734 return p;
16735}
16736
16737/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016738 * Expands out the 'magic' {}'s in a variable/function name.
16739 * Note that this can call itself recursively, to deal with
16740 * constructs like foo{bar}{baz}{bam}
16741 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16742 * "in_start" ^
16743 * "expr_start" ^
16744 * "expr_end" ^
16745 * "in_end" ^
16746 *
16747 * Returns a new allocated string, which the caller must free.
16748 * Returns NULL for failure.
16749 */
16750 static char_u *
16751make_expanded_name(in_start, expr_start, expr_end, in_end)
16752 char_u *in_start;
16753 char_u *expr_start;
16754 char_u *expr_end;
16755 char_u *in_end;
16756{
16757 char_u c1;
16758 char_u *retval = NULL;
16759 char_u *temp_result;
16760 char_u *nextcmd = NULL;
16761
16762 if (expr_end == NULL || in_end == NULL)
16763 return NULL;
16764 *expr_start = NUL;
16765 *expr_end = NUL;
16766 c1 = *in_end;
16767 *in_end = NUL;
16768
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016769 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016770 if (temp_result != NULL && nextcmd == NULL)
16771 {
16772 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16773 + (in_end - expr_end) + 1));
16774 if (retval != NULL)
16775 {
16776 STRCPY(retval, in_start);
16777 STRCAT(retval, temp_result);
16778 STRCAT(retval, expr_end + 1);
16779 }
16780 }
16781 vim_free(temp_result);
16782
16783 *in_end = c1; /* put char back for error messages */
16784 *expr_start = '{';
16785 *expr_end = '}';
16786
16787 if (retval != NULL)
16788 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016789 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016790 if (expr_start != NULL)
16791 {
16792 /* Further expansion! */
16793 temp_result = make_expanded_name(retval, expr_start,
16794 expr_end, temp_result);
16795 vim_free(retval);
16796 retval = temp_result;
16797 }
16798 }
16799
16800 return retval;
16801}
16802
16803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016805 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 */
16807 static int
16808eval_isnamec(c)
16809 int c;
16810{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016811 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16812}
16813
16814/*
16815 * Return TRUE if character "c" can be used as the first character in a
16816 * variable or function name (excluding '{' and '}').
16817 */
16818 static int
16819eval_isnamec1(c)
16820 int c;
16821{
16822 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823}
16824
16825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 * Set number v: variable to "val".
16827 */
16828 void
16829set_vim_var_nr(idx, val)
16830 int idx;
16831 long val;
16832{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016833 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834}
16835
16836/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016837 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 */
16839 long
16840get_vim_var_nr(idx)
16841 int idx;
16842{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016843 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844}
16845
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016846#if defined(FEAT_AUTOCMD) || defined(PROTO)
16847/*
16848 * Get string v: variable value. Uses a static buffer, can only be used once.
16849 */
16850 char_u *
16851get_vim_var_str(idx)
16852 int idx;
16853{
16854 return get_tv_string(&vimvars[idx].vv_tv);
16855}
16856#endif
16857
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858/*
16859 * Set v:count, v:count1 and v:prevcount.
16860 */
16861 void
16862set_vcount(count, count1)
16863 long count;
16864 long count1;
16865{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016866 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16867 vimvars[VV_COUNT].vv_nr = count;
16868 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869}
16870
16871/*
16872 * Set string v: variable to a copy of "val".
16873 */
16874 void
16875set_vim_var_string(idx, val, len)
16876 int idx;
16877 char_u *val;
16878 int len; /* length of "val" to use or -1 (whole string) */
16879{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016880 /* Need to do this (at least) once, since we can't initialize a union.
16881 * Will always be invoked when "v:progname" is set. */
16882 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16883
Bram Moolenaare9a41262005-01-15 22:18:47 +000016884 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016886 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016888 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016890 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891}
16892
16893/*
16894 * Set v:register if needed.
16895 */
16896 void
16897set_reg_var(c)
16898 int c;
16899{
16900 char_u regname;
16901
16902 if (c == 0 || c == ' ')
16903 regname = '"';
16904 else
16905 regname = c;
16906 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016907 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908 set_vim_var_string(VV_REG, &regname, 1);
16909}
16910
16911/*
16912 * Get or set v:exception. If "oldval" == NULL, return the current value.
16913 * Otherwise, restore the value to "oldval" and return NULL.
16914 * Must always be called in pairs to save and restore v:exception! Does not
16915 * take care of memory allocations.
16916 */
16917 char_u *
16918v_exception(oldval)
16919 char_u *oldval;
16920{
16921 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016922 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923
Bram Moolenaare9a41262005-01-15 22:18:47 +000016924 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 return NULL;
16926}
16927
16928/*
16929 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16930 * Otherwise, restore the value to "oldval" and return NULL.
16931 * Must always be called in pairs to save and restore v:throwpoint! Does not
16932 * take care of memory allocations.
16933 */
16934 char_u *
16935v_throwpoint(oldval)
16936 char_u *oldval;
16937{
16938 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016939 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940
Bram Moolenaare9a41262005-01-15 22:18:47 +000016941 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 return NULL;
16943}
16944
16945#if defined(FEAT_AUTOCMD) || defined(PROTO)
16946/*
16947 * Set v:cmdarg.
16948 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16949 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16950 * Must always be called in pairs!
16951 */
16952 char_u *
16953set_cmdarg(eap, oldarg)
16954 exarg_T *eap;
16955 char_u *oldarg;
16956{
16957 char_u *oldval;
16958 char_u *newval;
16959 unsigned len;
16960
Bram Moolenaare9a41262005-01-15 22:18:47 +000016961 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016962 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016964 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016965 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016966 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 }
16968
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016969 if (eap->force_bin == FORCE_BIN)
16970 len = 6;
16971 else if (eap->force_bin == FORCE_NOBIN)
16972 len = 8;
16973 else
16974 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016975
16976 if (eap->read_edit)
16977 len += 7;
16978
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016979 if (eap->force_ff != 0)
16980 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16981# ifdef FEAT_MBYTE
16982 if (eap->force_enc != 0)
16983 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016984 if (eap->bad_char != 0)
16985 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016986# endif
16987
16988 newval = alloc(len + 1);
16989 if (newval == NULL)
16990 return NULL;
16991
16992 if (eap->force_bin == FORCE_BIN)
16993 sprintf((char *)newval, " ++bin");
16994 else if (eap->force_bin == FORCE_NOBIN)
16995 sprintf((char *)newval, " ++nobin");
16996 else
16997 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016998
16999 if (eap->read_edit)
17000 STRCAT(newval, " ++edit");
17001
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017002 if (eap->force_ff != 0)
17003 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17004 eap->cmd + eap->force_ff);
17005# ifdef FEAT_MBYTE
17006 if (eap->force_enc != 0)
17007 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17008 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017009 if (eap->bad_char != 0)
17010 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17011 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017012# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017013 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017014 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015}
17016#endif
17017
17018/*
17019 * Get the value of internal variable "name".
17020 * Return OK or FAIL.
17021 */
17022 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017023get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 char_u *name;
17025 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017026 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017027 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028{
17029 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017030 typval_T *tv = NULL;
17031 typval_T atv;
17032 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034
17035 /* truncate the name, so that we can use strcmp() */
17036 cc = name[len];
17037 name[len] = NUL;
17038
17039 /*
17040 * Check for "b:changedtick".
17041 */
17042 if (STRCMP(name, "b:changedtick") == 0)
17043 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017044 atv.v_type = VAR_NUMBER;
17045 atv.vval.v_number = curbuf->b_changedtick;
17046 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 }
17048
17049 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050 * Check for user-defined variables.
17051 */
17052 else
17053 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017054 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017056 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 }
17058
Bram Moolenaare9a41262005-01-15 22:18:47 +000017059 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017061 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017062 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 ret = FAIL;
17064 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017065 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017066 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067
17068 name[len] = cc;
17069
17070 return ret;
17071}
17072
17073/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017074 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17075 * Also handle function call with Funcref variable: func(expr)
17076 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17077 */
17078 static int
17079handle_subscript(arg, rettv, evaluate, verbose)
17080 char_u **arg;
17081 typval_T *rettv;
17082 int evaluate; /* do more than finding the end */
17083 int verbose; /* give error messages */
17084{
17085 int ret = OK;
17086 dict_T *selfdict = NULL;
17087 char_u *s;
17088 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017089 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017090
17091 while (ret == OK
17092 && (**arg == '['
17093 || (**arg == '.' && rettv->v_type == VAR_DICT)
17094 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17095 && !vim_iswhite(*(*arg - 1)))
17096 {
17097 if (**arg == '(')
17098 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017099 /* need to copy the funcref so that we can clear rettv */
17100 functv = *rettv;
17101 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017102
17103 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017104 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017105 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017106 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17107 &len, evaluate, selfdict);
17108
17109 /* Clear the funcref afterwards, so that deleting it while
17110 * evaluating the arguments is possible (see test55). */
17111 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017112
17113 /* Stop the expression evaluation when immediately aborting on
17114 * error, or when an interrupt occurred or an exception was thrown
17115 * but not caught. */
17116 if (aborting())
17117 {
17118 if (ret == OK)
17119 clear_tv(rettv);
17120 ret = FAIL;
17121 }
17122 dict_unref(selfdict);
17123 selfdict = NULL;
17124 }
17125 else /* **arg == '[' || **arg == '.' */
17126 {
17127 dict_unref(selfdict);
17128 if (rettv->v_type == VAR_DICT)
17129 {
17130 selfdict = rettv->vval.v_dict;
17131 if (selfdict != NULL)
17132 ++selfdict->dv_refcount;
17133 }
17134 else
17135 selfdict = NULL;
17136 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17137 {
17138 clear_tv(rettv);
17139 ret = FAIL;
17140 }
17141 }
17142 }
17143 dict_unref(selfdict);
17144 return ret;
17145}
17146
17147/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017148 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17149 * value).
17150 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017152alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017153{
Bram Moolenaar33570922005-01-25 22:26:29 +000017154 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017155}
17156
17157/*
17158 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 * The string "s" must have been allocated, it is consumed.
17160 * Return NULL for out of memory, the variable otherwise.
17161 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017162 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017163alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 char_u *s;
17165{
Bram Moolenaar33570922005-01-25 22:26:29 +000017166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168 rettv = alloc_tv();
17169 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017171 rettv->v_type = VAR_STRING;
17172 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 }
17174 else
17175 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177}
17178
17179/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017180 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017182 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017183free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017184 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185{
17186 if (varp != NULL)
17187 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017188 switch (varp->v_type)
17189 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017190 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017191 func_unref(varp->vval.v_string);
17192 /*FALLTHROUGH*/
17193 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017194 vim_free(varp->vval.v_string);
17195 break;
17196 case VAR_LIST:
17197 list_unref(varp->vval.v_list);
17198 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017199 case VAR_DICT:
17200 dict_unref(varp->vval.v_dict);
17201 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017202 case VAR_NUMBER:
17203 case VAR_UNKNOWN:
17204 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017205 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017206 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017207 break;
17208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 vim_free(varp);
17210 }
17211}
17212
17213/*
17214 * Free the memory for a variable value and set the value to NULL or 0.
17215 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017216 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017217clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017218 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219{
17220 if (varp != NULL)
17221 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017222 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017224 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017225 func_unref(varp->vval.v_string);
17226 /*FALLTHROUGH*/
17227 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017228 vim_free(varp->vval.v_string);
17229 varp->vval.v_string = NULL;
17230 break;
17231 case VAR_LIST:
17232 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017233 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017234 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017235 case VAR_DICT:
17236 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017237 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017238 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017239 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017240 varp->vval.v_number = 0;
17241 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017242 case VAR_UNKNOWN:
17243 break;
17244 default:
17245 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017247 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 }
17249}
17250
17251/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017252 * Set the value of a variable to NULL without freeing items.
17253 */
17254 static void
17255init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017256 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017257{
17258 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017259 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017260}
17261
17262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 * Get the number value of a variable.
17264 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017265 * For incompatible types, return 0.
17266 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17267 * caller of incompatible types: it sets *denote to TRUE if "denote"
17268 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 */
17270 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017271get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017272 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017274 int error = FALSE;
17275
17276 return get_tv_number_chk(varp, &error); /* return 0L on error */
17277}
17278
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017279 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017280get_tv_number_chk(varp, denote)
17281 typval_T *varp;
17282 int *denote;
17283{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017284 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017286 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017288 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017289 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017290 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017291 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017292 break;
17293 case VAR_STRING:
17294 if (varp->vval.v_string != NULL)
17295 vim_str2nr(varp->vval.v_string, NULL, NULL,
17296 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017297 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017298 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017299 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017300 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017301 case VAR_DICT:
17302 EMSG(_("E728: Using a Dictionary as a number"));
17303 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017304 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017305 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017308 if (denote == NULL) /* useful for values that must be unsigned */
17309 n = -1;
17310 else
17311 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017312 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313}
17314
17315/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017316 * Get the lnum from the first argument.
17317 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017318 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 */
17320 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017321get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017322 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323{
Bram Moolenaar33570922005-01-25 22:26:29 +000017324 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 linenr_T lnum;
17326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017327 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 if (lnum == 0) /* no valid number, try using line() */
17329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017330 rettv.v_type = VAR_NUMBER;
17331 f_line(argvars, &rettv);
17332 lnum = rettv.vval.v_number;
17333 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 }
17335 return lnum;
17336}
17337
17338/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017339 * Get the lnum from the first argument.
17340 * Also accepts "$", then "buf" is used.
17341 * Returns 0 on error.
17342 */
17343 static linenr_T
17344get_tv_lnum_buf(argvars, buf)
17345 typval_T *argvars;
17346 buf_T *buf;
17347{
17348 if (argvars[0].v_type == VAR_STRING
17349 && argvars[0].vval.v_string != NULL
17350 && argvars[0].vval.v_string[0] == '$'
17351 && buf != NULL)
17352 return buf->b_ml.ml_line_count;
17353 return get_tv_number_chk(&argvars[0], NULL);
17354}
17355
17356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 * Get the string value of a variable.
17358 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017359 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17360 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 * If the String variable has never been set, return an empty string.
17362 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017363 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17364 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 */
17366 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017367get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369{
17370 static char_u mybuf[NUMBUFLEN];
17371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373}
17374
17375 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017376get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017377 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017378 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017380 char_u *res = get_tv_string_buf_chk(varp, buf);
17381
17382 return res != NULL ? res : (char_u *)"";
17383}
17384
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017385 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017386get_tv_string_chk(varp)
17387 typval_T *varp;
17388{
17389 static char_u mybuf[NUMBUFLEN];
17390
17391 return get_tv_string_buf_chk(varp, mybuf);
17392}
17393
17394 static char_u *
17395get_tv_string_buf_chk(varp, buf)
17396 typval_T *varp;
17397 char_u *buf;
17398{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017399 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017401 case VAR_NUMBER:
17402 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17403 return buf;
17404 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017405 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017406 break;
17407 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017408 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017409 break;
17410 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017411 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017412 break;
17413 case VAR_STRING:
17414 if (varp->vval.v_string != NULL)
17415 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017416 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017417 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017418 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017419 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422}
17423
17424/*
17425 * Find variable "name" in the list of variables.
17426 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017427 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017428 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017429 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017431 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017432find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017434 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438
Bram Moolenaara7043832005-01-21 11:56:39 +000017439 ht = find_var_ht(name, &varname);
17440 if (htp != NULL)
17441 *htp = ht;
17442 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017444 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445}
17446
17447/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017448 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017449 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017451 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017452find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017453 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017454 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017455 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017456{
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 hashitem_T *hi;
17458
17459 if (*varname == NUL)
17460 {
17461 /* Must be something like "s:", otherwise "ht" would be NULL. */
17462 switch (varname[-2])
17463 {
17464 case 's': return &SCRIPT_SV(current_SID).sv_var;
17465 case 'g': return &globvars_var;
17466 case 'v': return &vimvars_var;
17467 case 'b': return &curbuf->b_bufvar;
17468 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017469#ifdef FEAT_WINDOWS
17470 case 't': return &curtab->tp_winvar;
17471#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017472 case 'l': return current_funccal == NULL
17473 ? NULL : &current_funccal->l_vars_var;
17474 case 'a': return current_funccal == NULL
17475 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017476 }
17477 return NULL;
17478 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017479
17480 hi = hash_find(ht, varname);
17481 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017482 {
17483 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017484 * worked find the variable again. Don't auto-load a script if it was
17485 * loaded already, otherwise it would be loaded every time when
17486 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017487 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017488 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017489 hi = hash_find(ht, varname);
17490 if (HASHITEM_EMPTY(hi))
17491 return NULL;
17492 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017493 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017494}
17495
17496/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017497 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017498 * Set "varname" to the start of name without ':'.
17499 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017501find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 char_u *name;
17503 char_u **varname;
17504{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017505 hashitem_T *hi;
17506
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 if (name[1] != ':')
17508 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017509 /* The name must not start with a colon or #. */
17510 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 return NULL;
17512 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017513
17514 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017515 hi = hash_find(&compat_hashtab, name);
17516 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017517 return &compat_hashtab;
17518
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017520 return &globvarht; /* global variable */
17521 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 }
17523 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017524 if (*name == 'g') /* global variable */
17525 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017526 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17527 */
17528 if (vim_strchr(name + 2, ':') != NULL
17529 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017530 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017532 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017535#ifdef FEAT_WINDOWS
17536 if (*name == 't') /* tab page variable */
17537 return &curtab->tp_vars.dv_hashtab;
17538#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017539 if (*name == 'v') /* v: variable */
17540 return &vimvarht;
17541 if (*name == 'a' && current_funccal != NULL) /* function argument */
17542 return &current_funccal->l_avars.dv_hashtab;
17543 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17544 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 if (*name == 's' /* script variable */
17546 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17547 return &SCRIPT_VARS(current_SID);
17548 return NULL;
17549}
17550
17551/*
17552 * Get the string value of a (global/local) variable.
17553 * Returns NULL when it doesn't exist.
17554 */
17555 char_u *
17556get_var_value(name)
17557 char_u *name;
17558{
Bram Moolenaar33570922005-01-25 22:26:29 +000017559 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560
Bram Moolenaara7043832005-01-21 11:56:39 +000017561 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 if (v == NULL)
17563 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565}
17566
17567/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017568 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 * sourcing this script and when executing functions defined in the script.
17570 */
17571 void
17572new_script_vars(id)
17573 scid_T id;
17574{
Bram Moolenaara7043832005-01-21 11:56:39 +000017575 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017576 hashtab_T *ht;
17577 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017578
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17580 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017581 /* Re-allocating ga_data means that an ht_array pointing to
17582 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017584 for (i = 1; i <= ga_scripts.ga_len; ++i)
17585 {
17586 ht = &SCRIPT_VARS(i);
17587 if (ht->ht_mask == HT_INIT_SIZE - 1)
17588 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017589 sv = &SCRIPT_SV(i);
17590 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017591 }
17592
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 while (ga_scripts.ga_len < id)
17594 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017595 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17596 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 }
17599 }
17600}
17601
17602/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017603 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17604 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 */
17606 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017607init_var_dict(dict, dict_var)
17608 dict_T *dict;
17609 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610{
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 hash_init(&dict->dv_hashtab);
17612 dict->dv_refcount = 99999;
17613 dict_var->di_tv.vval.v_dict = dict;
17614 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017615 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17617 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618}
17619
17620/*
17621 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 * Frees all allocated variables and the value they contain.
17623 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 */
17625 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017626vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017627 hashtab_T *ht;
17628{
17629 vars_clear_ext(ht, TRUE);
17630}
17631
17632/*
17633 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17634 */
17635 static void
17636vars_clear_ext(ht, free_val)
17637 hashtab_T *ht;
17638 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639{
Bram Moolenaara7043832005-01-21 11:56:39 +000017640 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017641 hashitem_T *hi;
17642 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643
Bram Moolenaar33570922005-01-25 22:26:29 +000017644 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017645 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017646 for (hi = ht->ht_array; todo > 0; ++hi)
17647 {
17648 if (!HASHITEM_EMPTY(hi))
17649 {
17650 --todo;
17651
Bram Moolenaar33570922005-01-25 22:26:29 +000017652 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017653 * ht_array might change then. hash_clear() takes care of it
17654 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017655 v = HI2DI(hi);
17656 if (free_val)
17657 clear_tv(&v->di_tv);
17658 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17659 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017660 }
17661 }
17662 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017663 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664}
17665
Bram Moolenaara7043832005-01-21 11:56:39 +000017666/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017667 * Delete a variable from hashtab "ht" at item "hi".
17668 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017671delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017672 hashtab_T *ht;
17673 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674{
Bram Moolenaar33570922005-01-25 22:26:29 +000017675 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017676
17677 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017678 clear_tv(&di->di_tv);
17679 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680}
17681
17682/*
17683 * List the value of one internal variable.
17684 */
17685 static void
17686list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 char_u *prefix;
17689{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017690 char_u *tofree;
17691 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017692 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017693
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017694 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017695 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017696 s == NULL ? (char_u *)"" : s);
17697 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698}
17699
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 static void
17701list_one_var_a(prefix, name, type, string)
17702 char_u *prefix;
17703 char_u *name;
17704 int type;
17705 char_u *string;
17706{
17707 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17708 if (name != NULL) /* "a:" vars don't have a name stored */
17709 msg_puts(name);
17710 msg_putchar(' ');
17711 msg_advance(22);
17712 if (type == VAR_NUMBER)
17713 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017714 else if (type == VAR_FUNC)
17715 msg_putchar('*');
17716 else if (type == VAR_LIST)
17717 {
17718 msg_putchar('[');
17719 if (*string == '[')
17720 ++string;
17721 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017722 else if (type == VAR_DICT)
17723 {
17724 msg_putchar('{');
17725 if (*string == '{')
17726 ++string;
17727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 else
17729 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017730
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017732
17733 if (type == VAR_FUNC)
17734 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735}
17736
17737/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 * If the variable already exists, the value is updated.
17740 * Otherwise the variable is created.
17741 */
17742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017743set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017745 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017746 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747{
Bram Moolenaar33570922005-01-25 22:26:29 +000017748 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017751 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017754 {
17755 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17756 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17757 ? name[2] : name[0]))
17758 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017759 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017760 return;
17761 }
17762 if (function_exists(name))
17763 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017764 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017765 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017766 return;
17767 }
17768 }
17769
Bram Moolenaara7043832005-01-21 11:56:39 +000017770 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017771 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017772 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017773 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017774 return;
17775 }
17776
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017777 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017780 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017781 if (var_check_ro(v->di_flags, name)
17782 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017783 return;
17784 if (v->di_tv.v_type != tv->v_type
17785 && !((v->di_tv.v_type == VAR_STRING
17786 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017787 && (tv->v_type == VAR_STRING
17788 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017789 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017790 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017791 return;
17792 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017793
17794 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017795 * Handle setting internal v: variables separately: we don't change
17796 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017797 */
17798 if (ht == &vimvarht)
17799 {
17800 if (v->di_tv.v_type == VAR_STRING)
17801 {
17802 vim_free(v->di_tv.vval.v_string);
17803 if (copy || tv->v_type != VAR_STRING)
17804 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17805 else
17806 {
17807 /* Take over the string to avoid an extra alloc/free. */
17808 v->di_tv.vval.v_string = tv->vval.v_string;
17809 tv->vval.v_string = NULL;
17810 }
17811 }
17812 else if (v->di_tv.v_type != VAR_NUMBER)
17813 EMSG2(_(e_intern2), "set_var()");
17814 else
17815 v->di_tv.vval.v_number = get_tv_number(tv);
17816 return;
17817 }
17818
17819 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 }
17821 else /* add a new variable */
17822 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017823 /* Can't add "v:" variable. */
17824 if (ht == &vimvarht)
17825 {
17826 EMSG2(_(e_illvar), name);
17827 return;
17828 }
17829
Bram Moolenaar92124a32005-06-17 22:03:40 +000017830 /* Make sure the variable name is valid. */
17831 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017832 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17833 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017834 {
17835 EMSG2(_(e_illvar), varname);
17836 return;
17837 }
17838
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017839 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17840 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017841 if (v == NULL)
17842 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017843 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017844 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017846 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 return;
17848 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017849 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017852 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017853 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017854 else
17855 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017856 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017857 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017858 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860}
17861
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017862/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017863 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017864 * Also give an error message.
17865 */
17866 static int
17867var_check_ro(flags, name)
17868 int flags;
17869 char_u *name;
17870{
17871 if (flags & DI_FLAGS_RO)
17872 {
17873 EMSG2(_(e_readonlyvar), name);
17874 return TRUE;
17875 }
17876 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17877 {
17878 EMSG2(_(e_readonlysbx), name);
17879 return TRUE;
17880 }
17881 return FALSE;
17882}
17883
17884/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017885 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17886 * Also give an error message.
17887 */
17888 static int
17889var_check_fixed(flags, name)
17890 int flags;
17891 char_u *name;
17892{
17893 if (flags & DI_FLAGS_FIX)
17894 {
17895 EMSG2(_("E795: Cannot delete variable %s"), name);
17896 return TRUE;
17897 }
17898 return FALSE;
17899}
17900
17901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017902 * Return TRUE if typeval "tv" is set to be locked (immutable).
17903 * Also give an error message, using "name".
17904 */
17905 static int
17906tv_check_lock(lock, name)
17907 int lock;
17908 char_u *name;
17909{
17910 if (lock & VAR_LOCKED)
17911 {
17912 EMSG2(_("E741: Value is locked: %s"),
17913 name == NULL ? (char_u *)_("Unknown") : name);
17914 return TRUE;
17915 }
17916 if (lock & VAR_FIXED)
17917 {
17918 EMSG2(_("E742: Cannot change value of %s"),
17919 name == NULL ? (char_u *)_("Unknown") : name);
17920 return TRUE;
17921 }
17922 return FALSE;
17923}
17924
17925/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017927 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017928 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017931copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017932 typval_T *from;
17933 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017935 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017936 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017937 switch (from->v_type)
17938 {
17939 case VAR_NUMBER:
17940 to->vval.v_number = from->vval.v_number;
17941 break;
17942 case VAR_STRING:
17943 case VAR_FUNC:
17944 if (from->vval.v_string == NULL)
17945 to->vval.v_string = NULL;
17946 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017947 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017948 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017949 if (from->v_type == VAR_FUNC)
17950 func_ref(to->vval.v_string);
17951 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017952 break;
17953 case VAR_LIST:
17954 if (from->vval.v_list == NULL)
17955 to->vval.v_list = NULL;
17956 else
17957 {
17958 to->vval.v_list = from->vval.v_list;
17959 ++to->vval.v_list->lv_refcount;
17960 }
17961 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017962 case VAR_DICT:
17963 if (from->vval.v_dict == NULL)
17964 to->vval.v_dict = NULL;
17965 else
17966 {
17967 to->vval.v_dict = from->vval.v_dict;
17968 ++to->vval.v_dict->dv_refcount;
17969 }
17970 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017971 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017972 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017973 break;
17974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975}
17976
17977/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017978 * Make a copy of an item.
17979 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017980 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17981 * reference to an already copied list/dict can be used.
17982 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017983 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017984 static int
17985item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017986 typval_T *from;
17987 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017988 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017989 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017990{
17991 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017992 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017993
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017995 {
17996 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017997 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017998 }
17999 ++recurse;
18000
18001 switch (from->v_type)
18002 {
18003 case VAR_NUMBER:
18004 case VAR_STRING:
18005 case VAR_FUNC:
18006 copy_tv(from, to);
18007 break;
18008 case VAR_LIST:
18009 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018010 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018011 if (from->vval.v_list == NULL)
18012 to->vval.v_list = NULL;
18013 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18014 {
18015 /* use the copy made earlier */
18016 to->vval.v_list = from->vval.v_list->lv_copylist;
18017 ++to->vval.v_list->lv_refcount;
18018 }
18019 else
18020 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18021 if (to->vval.v_list == NULL)
18022 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018023 break;
18024 case VAR_DICT:
18025 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018026 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018027 if (from->vval.v_dict == NULL)
18028 to->vval.v_dict = NULL;
18029 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18030 {
18031 /* use the copy made earlier */
18032 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18033 ++to->vval.v_dict->dv_refcount;
18034 }
18035 else
18036 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18037 if (to->vval.v_dict == NULL)
18038 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018039 break;
18040 default:
18041 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018042 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018043 }
18044 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018045 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018046}
18047
18048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 * ":echo expr1 ..." print each argument separated with a space, add a
18050 * newline at the end.
18051 * ":echon expr1 ..." print each argument plain.
18052 */
18053 void
18054ex_echo(eap)
18055 exarg_T *eap;
18056{
18057 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018058 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018059 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 char_u *p;
18061 int needclr = TRUE;
18062 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018063 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064
18065 if (eap->skip)
18066 ++emsg_skip;
18067 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18068 {
18069 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018070 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 {
18072 /*
18073 * Report the invalid expression unless the expression evaluation
18074 * has been cancelled due to an aborting error, an interrupt, or an
18075 * exception.
18076 */
18077 if (!aborting())
18078 EMSG2(_(e_invexpr2), p);
18079 break;
18080 }
18081 if (!eap->skip)
18082 {
18083 if (atstart)
18084 {
18085 atstart = FALSE;
18086 /* Call msg_start() after eval1(), evaluating the expression
18087 * may cause a message to appear. */
18088 if (eap->cmdidx == CMD_echo)
18089 msg_start();
18090 }
18091 else if (eap->cmdidx == CMD_echo)
18092 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018093 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018094 if (p != NULL)
18095 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018097 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018099 if (*p != TAB && needclr)
18100 {
18101 /* remove any text still there from the command */
18102 msg_clr_eos();
18103 needclr = FALSE;
18104 }
18105 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 }
18107 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018108 {
18109#ifdef FEAT_MBYTE
18110 if (has_mbyte)
18111 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018112 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018113
18114 (void)msg_outtrans_len_attr(p, i, echo_attr);
18115 p += i - 1;
18116 }
18117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018119 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018122 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018124 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 arg = skipwhite(arg);
18126 }
18127 eap->nextcmd = check_nextcmd(arg);
18128
18129 if (eap->skip)
18130 --emsg_skip;
18131 else
18132 {
18133 /* remove text that may still be there from the command */
18134 if (needclr)
18135 msg_clr_eos();
18136 if (eap->cmdidx == CMD_echo)
18137 msg_end();
18138 }
18139}
18140
18141/*
18142 * ":echohl {name}".
18143 */
18144 void
18145ex_echohl(eap)
18146 exarg_T *eap;
18147{
18148 int id;
18149
18150 id = syn_name2id(eap->arg);
18151 if (id == 0)
18152 echo_attr = 0;
18153 else
18154 echo_attr = syn_id2attr(id);
18155}
18156
18157/*
18158 * ":execute expr1 ..." execute the result of an expression.
18159 * ":echomsg expr1 ..." Print a message
18160 * ":echoerr expr1 ..." Print an error
18161 * Each gets spaces around each argument and a newline at the end for
18162 * echo commands
18163 */
18164 void
18165ex_execute(eap)
18166 exarg_T *eap;
18167{
18168 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018169 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 int ret = OK;
18171 char_u *p;
18172 garray_T ga;
18173 int len;
18174 int save_did_emsg;
18175
18176 ga_init2(&ga, 1, 80);
18177
18178 if (eap->skip)
18179 ++emsg_skip;
18180 while (*arg != NUL && *arg != '|' && *arg != '\n')
18181 {
18182 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 {
18185 /*
18186 * Report the invalid expression unless the expression evaluation
18187 * has been cancelled due to an aborting error, an interrupt, or an
18188 * exception.
18189 */
18190 if (!aborting())
18191 EMSG2(_(e_invexpr2), p);
18192 ret = FAIL;
18193 break;
18194 }
18195
18196 if (!eap->skip)
18197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018198 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 len = (int)STRLEN(p);
18200 if (ga_grow(&ga, len + 2) == FAIL)
18201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 ret = FAIL;
18204 break;
18205 }
18206 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 ga.ga_len += len;
18210 }
18211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018212 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 arg = skipwhite(arg);
18214 }
18215
18216 if (ret != FAIL && ga.ga_data != NULL)
18217 {
18218 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018221 out_flush();
18222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 else if (eap->cmdidx == CMD_echoerr)
18224 {
18225 /* We don't want to abort following commands, restore did_emsg. */
18226 save_did_emsg = did_emsg;
18227 EMSG((char_u *)ga.ga_data);
18228 if (!force_abort)
18229 did_emsg = save_did_emsg;
18230 }
18231 else if (eap->cmdidx == CMD_execute)
18232 do_cmdline((char_u *)ga.ga_data,
18233 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18234 }
18235
18236 ga_clear(&ga);
18237
18238 if (eap->skip)
18239 --emsg_skip;
18240
18241 eap->nextcmd = check_nextcmd(arg);
18242}
18243
18244/*
18245 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18246 * "arg" points to the "&" or '+' when called, to "option" when returning.
18247 * Returns NULL when no option name found. Otherwise pointer to the char
18248 * after the option name.
18249 */
18250 static char_u *
18251find_option_end(arg, opt_flags)
18252 char_u **arg;
18253 int *opt_flags;
18254{
18255 char_u *p = *arg;
18256
18257 ++p;
18258 if (*p == 'g' && p[1] == ':')
18259 {
18260 *opt_flags = OPT_GLOBAL;
18261 p += 2;
18262 }
18263 else if (*p == 'l' && p[1] == ':')
18264 {
18265 *opt_flags = OPT_LOCAL;
18266 p += 2;
18267 }
18268 else
18269 *opt_flags = 0;
18270
18271 if (!ASCII_ISALPHA(*p))
18272 return NULL;
18273 *arg = p;
18274
18275 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18276 p += 4; /* termcap option */
18277 else
18278 while (ASCII_ISALPHA(*p))
18279 ++p;
18280 return p;
18281}
18282
18283/*
18284 * ":function"
18285 */
18286 void
18287ex_function(eap)
18288 exarg_T *eap;
18289{
18290 char_u *theline;
18291 int j;
18292 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 char_u *name = NULL;
18295 char_u *p;
18296 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018297 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 garray_T newargs;
18299 garray_T newlines;
18300 int varargs = FALSE;
18301 int mustend = FALSE;
18302 int flags = 0;
18303 ufunc_T *fp;
18304 int indent;
18305 int nesting;
18306 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 dictitem_T *v;
18308 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018309 static int func_nr = 0; /* number for nameless function */
18310 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018311 hashtab_T *ht;
18312 int todo;
18313 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018314 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315
18316 /*
18317 * ":function" without argument: list functions.
18318 */
18319 if (ends_excmd(*eap->arg))
18320 {
18321 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018322 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018323 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018324 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018325 {
18326 if (!HASHITEM_EMPTY(hi))
18327 {
18328 --todo;
18329 fp = HI2UF(hi);
18330 if (!isdigit(*fp->uf_name))
18331 list_func_head(fp, FALSE);
18332 }
18333 }
18334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 eap->nextcmd = check_nextcmd(eap->arg);
18336 return;
18337 }
18338
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018339 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018340 * ":function /pat": list functions matching pattern.
18341 */
18342 if (*eap->arg == '/')
18343 {
18344 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18345 if (!eap->skip)
18346 {
18347 regmatch_T regmatch;
18348
18349 c = *p;
18350 *p = NUL;
18351 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18352 *p = c;
18353 if (regmatch.regprog != NULL)
18354 {
18355 regmatch.rm_ic = p_ic;
18356
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018357 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018358 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18359 {
18360 if (!HASHITEM_EMPTY(hi))
18361 {
18362 --todo;
18363 fp = HI2UF(hi);
18364 if (!isdigit(*fp->uf_name)
18365 && vim_regexec(&regmatch, fp->uf_name, 0))
18366 list_func_head(fp, FALSE);
18367 }
18368 }
18369 }
18370 }
18371 if (*p == '/')
18372 ++p;
18373 eap->nextcmd = check_nextcmd(p);
18374 return;
18375 }
18376
18377 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018378 * Get the function name. There are these situations:
18379 * func normal function name
18380 * "name" == func, "fudi.fd_dict" == NULL
18381 * dict.func new dictionary entry
18382 * "name" == NULL, "fudi.fd_dict" set,
18383 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18384 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018385 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018386 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18387 * dict.func existing dict entry that's not a Funcref
18388 * "name" == NULL, "fudi.fd_dict" set,
18389 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018392 name = trans_function_name(&p, eap->skip, 0, &fudi);
18393 paren = (vim_strchr(p, '(') != NULL);
18394 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 {
18396 /*
18397 * Return on an invalid expression in braces, unless the expression
18398 * evaluation has been cancelled due to an aborting error, an
18399 * interrupt, or an exception.
18400 */
18401 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018403 if (!eap->skip && fudi.fd_newkey != NULL)
18404 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018405 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 else
18409 eap->skip = TRUE;
18410 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018411
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412 /* An error in a function call during evaluation of an expression in magic
18413 * braces should not cause the function not to be defined. */
18414 saved_did_emsg = did_emsg;
18415 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416
18417 /*
18418 * ":function func" with only function name: list function.
18419 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018420 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 {
18422 if (!ends_excmd(*skipwhite(p)))
18423 {
18424 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018425 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426 }
18427 eap->nextcmd = check_nextcmd(p);
18428 if (eap->nextcmd != NULL)
18429 *p = NUL;
18430 if (!eap->skip && !got_int)
18431 {
18432 fp = find_func(name);
18433 if (fp != NULL)
18434 {
18435 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018436 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018438 if (FUNCLINE(fp, j) == NULL)
18439 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 msg_putchar('\n');
18441 msg_outnum((long)(j + 1));
18442 if (j < 9)
18443 msg_putchar(' ');
18444 if (j < 99)
18445 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018446 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 out_flush(); /* show a line at a time */
18448 ui_breakcheck();
18449 }
18450 if (!got_int)
18451 {
18452 msg_putchar('\n');
18453 msg_puts((char_u *)" endfunction");
18454 }
18455 }
18456 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018457 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018459 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 }
18461
18462 /*
18463 * ":function name(arg1, arg2)" Define function.
18464 */
18465 p = skipwhite(p);
18466 if (*p != '(')
18467 {
18468 if (!eap->skip)
18469 {
18470 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018471 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 }
18473 /* attempt to continue by skipping some text */
18474 if (vim_strchr(p, '(') != NULL)
18475 p = vim_strchr(p, '(');
18476 }
18477 p = skipwhite(p + 1);
18478
18479 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18480 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18481
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018482 if (!eap->skip)
18483 {
18484 /* Check the name of the function. */
18485 if (name != NULL)
18486 arg = name;
18487 else
18488 arg = fudi.fd_newkey;
18489 if (arg != NULL)
18490 {
18491 if (*arg == K_SPECIAL)
18492 j = 3;
18493 else
18494 j = 0;
18495 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18496 : eval_isnamec(arg[j])))
18497 ++j;
18498 if (arg[j] != NUL)
18499 emsg_funcname(_(e_invarg2), arg);
18500 }
18501 }
18502
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 /*
18504 * Isolate the arguments: "arg1, arg2, ...)"
18505 */
18506 while (*p != ')')
18507 {
18508 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18509 {
18510 varargs = TRUE;
18511 p += 3;
18512 mustend = TRUE;
18513 }
18514 else
18515 {
18516 arg = p;
18517 while (ASCII_ISALNUM(*p) || *p == '_')
18518 ++p;
18519 if (arg == p || isdigit(*arg)
18520 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18521 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18522 {
18523 if (!eap->skip)
18524 EMSG2(_("E125: Illegal argument: %s"), arg);
18525 break;
18526 }
18527 if (ga_grow(&newargs, 1) == FAIL)
18528 goto erret;
18529 c = *p;
18530 *p = NUL;
18531 arg = vim_strsave(arg);
18532 if (arg == NULL)
18533 goto erret;
18534 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18535 *p = c;
18536 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 if (*p == ',')
18538 ++p;
18539 else
18540 mustend = TRUE;
18541 }
18542 p = skipwhite(p);
18543 if (mustend && *p != ')')
18544 {
18545 if (!eap->skip)
18546 EMSG2(_(e_invarg2), eap->arg);
18547 break;
18548 }
18549 }
18550 ++p; /* skip the ')' */
18551
Bram Moolenaare9a41262005-01-15 22:18:47 +000018552 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 for (;;)
18554 {
18555 p = skipwhite(p);
18556 if (STRNCMP(p, "range", 5) == 0)
18557 {
18558 flags |= FC_RANGE;
18559 p += 5;
18560 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018561 else if (STRNCMP(p, "dict", 4) == 0)
18562 {
18563 flags |= FC_DICT;
18564 p += 4;
18565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 else if (STRNCMP(p, "abort", 5) == 0)
18567 {
18568 flags |= FC_ABORT;
18569 p += 5;
18570 }
18571 else
18572 break;
18573 }
18574
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018575 /* When there is a line break use what follows for the function body.
18576 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18577 if (*p == '\n')
18578 line_arg = p + 1;
18579 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 EMSG(_(e_trailing));
18581
18582 /*
18583 * Read the body of the function, until ":endfunction" is found.
18584 */
18585 if (KeyTyped)
18586 {
18587 /* Check if the function already exists, don't let the user type the
18588 * whole function before telling him it doesn't work! For a script we
18589 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018590 if (!eap->skip && !eap->forceit)
18591 {
18592 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18593 EMSG(_(e_funcdict));
18594 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018595 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018598 if (!eap->skip && did_emsg)
18599 goto erret;
18600
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 msg_putchar('\n'); /* don't overwrite the function name */
18602 cmdline_row = msg_row;
18603 }
18604
18605 indent = 2;
18606 nesting = 0;
18607 for (;;)
18608 {
18609 msg_scroll = TRUE;
18610 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018611 sourcing_lnum_off = sourcing_lnum;
18612
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018613 if (line_arg != NULL)
18614 {
18615 /* Use eap->arg, split up in parts by line breaks. */
18616 theline = line_arg;
18617 p = vim_strchr(theline, '\n');
18618 if (p == NULL)
18619 line_arg += STRLEN(line_arg);
18620 else
18621 {
18622 *p = NUL;
18623 line_arg = p + 1;
18624 }
18625 }
18626 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 theline = getcmdline(':', 0L, indent);
18628 else
18629 theline = eap->getline(':', eap->cookie, indent);
18630 if (KeyTyped)
18631 lines_left = Rows - 1;
18632 if (theline == NULL)
18633 {
18634 EMSG(_("E126: Missing :endfunction"));
18635 goto erret;
18636 }
18637
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018638 /* Detect line continuation: sourcing_lnum increased more than one. */
18639 if (sourcing_lnum > sourcing_lnum_off + 1)
18640 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18641 else
18642 sourcing_lnum_off = 0;
18643
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 if (skip_until != NULL)
18645 {
18646 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18647 * don't check for ":endfunc". */
18648 if (STRCMP(theline, skip_until) == 0)
18649 {
18650 vim_free(skip_until);
18651 skip_until = NULL;
18652 }
18653 }
18654 else
18655 {
18656 /* skip ':' and blanks*/
18657 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18658 ;
18659
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018660 /* Check for "endfunction". */
18661 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018663 if (line_arg == NULL)
18664 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 break;
18666 }
18667
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018668 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 * at "end". */
18670 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18671 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018672 else if (STRNCMP(p, "if", 2) == 0
18673 || STRNCMP(p, "wh", 2) == 0
18674 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 || STRNCMP(p, "try", 3) == 0)
18676 indent += 2;
18677
18678 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018679 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018681 if (*p == '!')
18682 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 p += eval_fname_script(p);
18684 if (ASCII_ISALPHA(*p))
18685 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018686 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 if (*skipwhite(p) == '(')
18688 {
18689 ++nesting;
18690 indent += 2;
18691 }
18692 }
18693 }
18694
18695 /* Check for ":append" or ":insert". */
18696 p = skip_range(p, NULL);
18697 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18698 || (p[0] == 'i'
18699 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18700 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18701 skip_until = vim_strsave((char_u *)".");
18702
18703 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18704 arg = skipwhite(skiptowhite(p));
18705 if (arg[0] == '<' && arg[1] =='<'
18706 && ((p[0] == 'p' && p[1] == 'y'
18707 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18708 || (p[0] == 'p' && p[1] == 'e'
18709 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18710 || (p[0] == 't' && p[1] == 'c'
18711 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18712 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18713 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018714 || (p[0] == 'm' && p[1] == 'z'
18715 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 ))
18717 {
18718 /* ":python <<" continues until a dot, like ":append" */
18719 p = skipwhite(arg + 2);
18720 if (*p == NUL)
18721 skip_until = vim_strsave((char_u *)".");
18722 else
18723 skip_until = vim_strsave(p);
18724 }
18725 }
18726
18727 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018728 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018729 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018730 if (line_arg == NULL)
18731 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018733 }
18734
18735 /* Copy the line to newly allocated memory. get_one_sourceline()
18736 * allocates 250 bytes per line, this saves 80% on average. The cost
18737 * is an extra alloc/free. */
18738 p = vim_strsave(theline);
18739 if (p != NULL)
18740 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018741 if (line_arg == NULL)
18742 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018743 theline = p;
18744 }
18745
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018746 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18747
18748 /* Add NULL lines for continuation lines, so that the line count is
18749 * equal to the index in the growarray. */
18750 while (sourcing_lnum_off-- > 0)
18751 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018752
18753 /* Check for end of eap->arg. */
18754 if (line_arg != NULL && *line_arg == NUL)
18755 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 }
18757
18758 /* Don't define the function when skipping commands or when an error was
18759 * detected. */
18760 if (eap->skip || did_emsg)
18761 goto erret;
18762
18763 /*
18764 * If there are no errors, add the function
18765 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018766 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018767 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018768 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018769 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018770 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018771 emsg_funcname("E707: Function name conflicts with variable: %s",
18772 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018773 goto erret;
18774 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018775
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018776 fp = find_func(name);
18777 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018779 if (!eap->forceit)
18780 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018781 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018782 goto erret;
18783 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018784 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018785 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018786 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018787 name);
18788 goto erret;
18789 }
18790 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018791 ga_clear_strings(&(fp->uf_args));
18792 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018793 vim_free(name);
18794 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 }
18797 else
18798 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018799 char numbuf[20];
18800
18801 fp = NULL;
18802 if (fudi.fd_newkey == NULL && !eap->forceit)
18803 {
18804 EMSG(_(e_funcdict));
18805 goto erret;
18806 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018807 if (fudi.fd_di == NULL)
18808 {
18809 /* Can't add a function to a locked dictionary */
18810 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18811 goto erret;
18812 }
18813 /* Can't change an existing function if it is locked */
18814 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18815 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018816
18817 /* Give the function a sequential number. Can only be used with a
18818 * Funcref! */
18819 vim_free(name);
18820 sprintf(numbuf, "%d", ++func_nr);
18821 name = vim_strsave((char_u *)numbuf);
18822 if (name == NULL)
18823 goto erret;
18824 }
18825
18826 if (fp == NULL)
18827 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018828 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018829 {
18830 int slen, plen;
18831 char_u *scriptname;
18832
18833 /* Check that the autoload name matches the script name. */
18834 j = FAIL;
18835 if (sourcing_name != NULL)
18836 {
18837 scriptname = autoload_name(name);
18838 if (scriptname != NULL)
18839 {
18840 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018841 plen = (int)STRLEN(p);
18842 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018843 if (slen > plen && fnamecmp(p,
18844 sourcing_name + slen - plen) == 0)
18845 j = OK;
18846 vim_free(scriptname);
18847 }
18848 }
18849 if (j == FAIL)
18850 {
18851 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18852 goto erret;
18853 }
18854 }
18855
18856 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 if (fp == NULL)
18858 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018859
18860 if (fudi.fd_dict != NULL)
18861 {
18862 if (fudi.fd_di == NULL)
18863 {
18864 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018865 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018866 if (fudi.fd_di == NULL)
18867 {
18868 vim_free(fp);
18869 goto erret;
18870 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018871 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18872 {
18873 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018874 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018875 goto erret;
18876 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018877 }
18878 else
18879 /* overwrite existing dict entry */
18880 clear_tv(&fudi.fd_di->di_tv);
18881 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018882 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018883 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018884 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018885
18886 /* behave like "dict" was used */
18887 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018888 }
18889
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018891 STRCPY(fp->uf_name, name);
18892 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018894 fp->uf_args = newargs;
18895 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018896#ifdef FEAT_PROFILE
18897 fp->uf_tml_count = NULL;
18898 fp->uf_tml_total = NULL;
18899 fp->uf_tml_self = NULL;
18900 fp->uf_profiling = FALSE;
18901 if (prof_def_func())
18902 func_do_profile(fp);
18903#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018904 fp->uf_varargs = varargs;
18905 fp->uf_flags = flags;
18906 fp->uf_calls = 0;
18907 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018908 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909
18910erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 ga_clear_strings(&newargs);
18912 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018913ret_free:
18914 vim_free(skip_until);
18915 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918}
18919
18920/*
18921 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018922 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018924 * flags:
18925 * TFN_INT: internal function name OK
18926 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 * Advances "pp" to just after the function name (if no error).
18928 */
18929 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018930trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 char_u **pp;
18932 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018933 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018934 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018936 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 char_u *start;
18938 char_u *end;
18939 int lead;
18940 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018943
18944 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018945 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018947
18948 /* Check for hard coded <SNR>: already translated function ID (from a user
18949 * command). */
18950 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18951 && (*pp)[2] == (int)KE_SNR)
18952 {
18953 *pp += 3;
18954 len = get_id_len(pp) + 3;
18955 return vim_strnsave(start, len);
18956 }
18957
18958 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18959 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018961 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018963
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018964 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18965 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018966 if (end == start)
18967 {
18968 if (!skip)
18969 EMSG(_("E129: Function name required"));
18970 goto theend;
18971 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018972 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018973 {
18974 /*
18975 * Report an invalid expression in braces, unless the expression
18976 * evaluation has been cancelled due to an aborting error, an
18977 * interrupt, or an exception.
18978 */
18979 if (!aborting())
18980 {
18981 if (end != NULL)
18982 EMSG2(_(e_invarg2), start);
18983 }
18984 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018985 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018986 goto theend;
18987 }
18988
18989 if (lv.ll_tv != NULL)
18990 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018991 if (fdp != NULL)
18992 {
18993 fdp->fd_dict = lv.ll_dict;
18994 fdp->fd_newkey = lv.ll_newkey;
18995 lv.ll_newkey = NULL;
18996 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018997 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018998 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18999 {
19000 name = vim_strsave(lv.ll_tv->vval.v_string);
19001 *pp = end;
19002 }
19003 else
19004 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019005 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19006 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019007 EMSG(_(e_funcref));
19008 else
19009 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019010 name = NULL;
19011 }
19012 goto theend;
19013 }
19014
19015 if (lv.ll_name == NULL)
19016 {
19017 /* Error found, but continue after the function name. */
19018 *pp = end;
19019 goto theend;
19020 }
19021
19022 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019023 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019024 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019025 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19026 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19027 {
19028 /* When there was "s:" already or the name expanded to get a
19029 * leading "s:" then remove it. */
19030 lv.ll_name += 2;
19031 len -= 2;
19032 lead = 2;
19033 }
19034 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019035 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019036 {
19037 if (lead == 2) /* skip over "s:" */
19038 lv.ll_name += 2;
19039 len = (int)(end - lv.ll_name);
19040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019041
19042 /*
19043 * Copy the function name to allocated memory.
19044 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19045 * Accept <SNR>123_name() outside a script.
19046 */
19047 if (skip)
19048 lead = 0; /* do nothing */
19049 else if (lead > 0)
19050 {
19051 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019052 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19053 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019054 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019055 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019056 if (current_SID <= 0)
19057 {
19058 EMSG(_(e_usingsid));
19059 goto theend;
19060 }
19061 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19062 lead += (int)STRLEN(sid_buf);
19063 }
19064 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019065 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019066 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019067 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019068 goto theend;
19069 }
19070 name = alloc((unsigned)(len + lead + 1));
19071 if (name != NULL)
19072 {
19073 if (lead > 0)
19074 {
19075 name[0] = K_SPECIAL;
19076 name[1] = KS_EXTRA;
19077 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019078 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019079 STRCPY(name + 3, sid_buf);
19080 }
19081 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19082 name[len + lead] = NUL;
19083 }
19084 *pp = end;
19085
19086theend:
19087 clear_lval(&lv);
19088 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089}
19090
19091/*
19092 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19093 * Return 2 if "p" starts with "s:".
19094 * Return 0 otherwise.
19095 */
19096 static int
19097eval_fname_script(p)
19098 char_u *p;
19099{
19100 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19101 || STRNICMP(p + 1, "SNR>", 4) == 0))
19102 return 5;
19103 if (p[0] == 's' && p[1] == ':')
19104 return 2;
19105 return 0;
19106}
19107
19108/*
19109 * Return TRUE if "p" starts with "<SID>" or "s:".
19110 * Only works if eval_fname_script() returned non-zero for "p"!
19111 */
19112 static int
19113eval_fname_sid(p)
19114 char_u *p;
19115{
19116 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19117}
19118
19119/*
19120 * List the head of the function: "name(arg1, arg2)".
19121 */
19122 static void
19123list_func_head(fp, indent)
19124 ufunc_T *fp;
19125 int indent;
19126{
19127 int j;
19128
19129 msg_start();
19130 if (indent)
19131 MSG_PUTS(" ");
19132 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019133 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 {
19135 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019136 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 }
19138 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019139 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019141 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 {
19143 if (j)
19144 MSG_PUTS(", ");
19145 msg_puts(FUNCARG(fp, j));
19146 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019147 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148 {
19149 if (j)
19150 MSG_PUTS(", ");
19151 MSG_PUTS("...");
19152 }
19153 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019154 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019155 if (p_verbose > 0)
19156 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157}
19158
19159/*
19160 * Find a function by name, return pointer to it in ufuncs.
19161 * Return NULL for unknown function.
19162 */
19163 static ufunc_T *
19164find_func(name)
19165 char_u *name;
19166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019167 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019169 hi = hash_find(&func_hashtab, name);
19170 if (!HASHITEM_EMPTY(hi))
19171 return HI2UF(hi);
19172 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173}
19174
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019175#if defined(EXITFREE) || defined(PROTO)
19176 void
19177free_all_functions()
19178{
19179 hashitem_T *hi;
19180
19181 /* Need to start all over every time, because func_free() may change the
19182 * hash table. */
19183 while (func_hashtab.ht_used > 0)
19184 for (hi = func_hashtab.ht_array; ; ++hi)
19185 if (!HASHITEM_EMPTY(hi))
19186 {
19187 func_free(HI2UF(hi));
19188 break;
19189 }
19190}
19191#endif
19192
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019193/*
19194 * Return TRUE if a function "name" exists.
19195 */
19196 static int
19197function_exists(name)
19198 char_u *name;
19199{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019200 char_u *nm = name;
19201 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019202 int n = FALSE;
19203
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019204 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019205 nm = skipwhite(nm);
19206
19207 /* Only accept "funcname", "funcname ", "funcname (..." and
19208 * "funcname(...", not "funcname!...". */
19209 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019210 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019211 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019212 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019213 else
19214 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019215 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019216 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019217 return n;
19218}
19219
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019220/*
19221 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019222 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019223 */
19224 static int
19225builtin_function(name)
19226 char_u *name;
19227{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019228 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19229 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019230}
19231
Bram Moolenaar05159a02005-02-26 23:04:13 +000019232#if defined(FEAT_PROFILE) || defined(PROTO)
19233/*
19234 * Start profiling function "fp".
19235 */
19236 static void
19237func_do_profile(fp)
19238 ufunc_T *fp;
19239{
19240 fp->uf_tm_count = 0;
19241 profile_zero(&fp->uf_tm_self);
19242 profile_zero(&fp->uf_tm_total);
19243 if (fp->uf_tml_count == NULL)
19244 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19245 (sizeof(int) * fp->uf_lines.ga_len));
19246 if (fp->uf_tml_total == NULL)
19247 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19248 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19249 if (fp->uf_tml_self == NULL)
19250 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19251 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19252 fp->uf_tml_idx = -1;
19253 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19254 || fp->uf_tml_self == NULL)
19255 return; /* out of memory */
19256
19257 fp->uf_profiling = TRUE;
19258}
19259
19260/*
19261 * Dump the profiling results for all functions in file "fd".
19262 */
19263 void
19264func_dump_profile(fd)
19265 FILE *fd;
19266{
19267 hashitem_T *hi;
19268 int todo;
19269 ufunc_T *fp;
19270 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019271 ufunc_T **sorttab;
19272 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019273
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019274 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019275 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19276
Bram Moolenaar05159a02005-02-26 23:04:13 +000019277 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19278 {
19279 if (!HASHITEM_EMPTY(hi))
19280 {
19281 --todo;
19282 fp = HI2UF(hi);
19283 if (fp->uf_profiling)
19284 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019285 if (sorttab != NULL)
19286 sorttab[st_len++] = fp;
19287
Bram Moolenaar05159a02005-02-26 23:04:13 +000019288 if (fp->uf_name[0] == K_SPECIAL)
19289 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19290 else
19291 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19292 if (fp->uf_tm_count == 1)
19293 fprintf(fd, "Called 1 time\n");
19294 else
19295 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19296 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19297 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19298 fprintf(fd, "\n");
19299 fprintf(fd, "count total (s) self (s)\n");
19300
19301 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19302 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019303 if (FUNCLINE(fp, i) == NULL)
19304 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019305 prof_func_line(fd, fp->uf_tml_count[i],
19306 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019307 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19308 }
19309 fprintf(fd, "\n");
19310 }
19311 }
19312 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019313
19314 if (sorttab != NULL && st_len > 0)
19315 {
19316 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19317 prof_total_cmp);
19318 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19319 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19320 prof_self_cmp);
19321 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19322 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019323}
Bram Moolenaar73830342005-02-28 22:48:19 +000019324
19325 static void
19326prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19327 FILE *fd;
19328 ufunc_T **sorttab;
19329 int st_len;
19330 char *title;
19331 int prefer_self; /* when equal print only self time */
19332{
19333 int i;
19334 ufunc_T *fp;
19335
19336 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19337 fprintf(fd, "count total (s) self (s) function\n");
19338 for (i = 0; i < 20 && i < st_len; ++i)
19339 {
19340 fp = sorttab[i];
19341 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19342 prefer_self);
19343 if (fp->uf_name[0] == K_SPECIAL)
19344 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19345 else
19346 fprintf(fd, " %s()\n", fp->uf_name);
19347 }
19348 fprintf(fd, "\n");
19349}
19350
19351/*
19352 * Print the count and times for one function or function line.
19353 */
19354 static void
19355prof_func_line(fd, count, total, self, prefer_self)
19356 FILE *fd;
19357 int count;
19358 proftime_T *total;
19359 proftime_T *self;
19360 int prefer_self; /* when equal print only self time */
19361{
19362 if (count > 0)
19363 {
19364 fprintf(fd, "%5d ", count);
19365 if (prefer_self && profile_equal(total, self))
19366 fprintf(fd, " ");
19367 else
19368 fprintf(fd, "%s ", profile_msg(total));
19369 if (!prefer_self && profile_equal(total, self))
19370 fprintf(fd, " ");
19371 else
19372 fprintf(fd, "%s ", profile_msg(self));
19373 }
19374 else
19375 fprintf(fd, " ");
19376}
19377
19378/*
19379 * Compare function for total time sorting.
19380 */
19381 static int
19382#ifdef __BORLANDC__
19383_RTLENTRYF
19384#endif
19385prof_total_cmp(s1, s2)
19386 const void *s1;
19387 const void *s2;
19388{
19389 ufunc_T *p1, *p2;
19390
19391 p1 = *(ufunc_T **)s1;
19392 p2 = *(ufunc_T **)s2;
19393 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19394}
19395
19396/*
19397 * Compare function for self time sorting.
19398 */
19399 static int
19400#ifdef __BORLANDC__
19401_RTLENTRYF
19402#endif
19403prof_self_cmp(s1, s2)
19404 const void *s1;
19405 const void *s2;
19406{
19407 ufunc_T *p1, *p2;
19408
19409 p1 = *(ufunc_T **)s1;
19410 p2 = *(ufunc_T **)s2;
19411 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19412}
19413
Bram Moolenaar05159a02005-02-26 23:04:13 +000019414#endif
19415
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019416/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019417 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019418 * Return TRUE if a package was loaded.
19419 */
19420 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019421script_autoload(name, reload)
19422 char_u *name;
19423 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019424{
19425 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019426 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019427 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019428 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019429
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019430 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019431 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019432 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019433 return FALSE;
19434
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019435 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019436
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019437 /* Find the name in the list of previously loaded package names. Skip
19438 * "autoload/", it's always the same. */
19439 for (i = 0; i < ga_loaded.ga_len; ++i)
19440 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19441 break;
19442 if (!reload && i < ga_loaded.ga_len)
19443 ret = FALSE; /* was loaded already */
19444 else
19445 {
19446 /* Remember the name if it wasn't loaded already. */
19447 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19448 {
19449 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19450 tofree = NULL;
19451 }
19452
19453 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019454 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019455 ret = TRUE;
19456 }
19457
19458 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019459 return ret;
19460}
19461
19462/*
19463 * Return the autoload script name for a function or variable name.
19464 * Returns NULL when out of memory.
19465 */
19466 static char_u *
19467autoload_name(name)
19468 char_u *name;
19469{
19470 char_u *p;
19471 char_u *scriptname;
19472
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019473 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019474 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19475 if (scriptname == NULL)
19476 return FALSE;
19477 STRCPY(scriptname, "autoload/");
19478 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019479 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019480 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019481 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019482 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019483 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019484}
19485
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19487
19488/*
19489 * Function given to ExpandGeneric() to obtain the list of user defined
19490 * function names.
19491 */
19492 char_u *
19493get_user_func_name(xp, idx)
19494 expand_T *xp;
19495 int idx;
19496{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019497 static long_u done;
19498 static hashitem_T *hi;
19499 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500
19501 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019503 done = 0;
19504 hi = func_hashtab.ht_array;
19505 }
19506 if (done < func_hashtab.ht_used)
19507 {
19508 if (done++ > 0)
19509 ++hi;
19510 while (HASHITEM_EMPTY(hi))
19511 ++hi;
19512 fp = HI2UF(hi);
19513
19514 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19515 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516
19517 cat_func_name(IObuff, fp);
19518 if (xp->xp_context != EXPAND_USER_FUNC)
19519 {
19520 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019521 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 STRCAT(IObuff, ")");
19523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 return IObuff;
19525 }
19526 return NULL;
19527}
19528
19529#endif /* FEAT_CMDL_COMPL */
19530
19531/*
19532 * Copy the function name of "fp" to buffer "buf".
19533 * "buf" must be able to hold the function name plus three bytes.
19534 * Takes care of script-local function names.
19535 */
19536 static void
19537cat_func_name(buf, fp)
19538 char_u *buf;
19539 ufunc_T *fp;
19540{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019541 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 {
19543 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019544 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 }
19546 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019547 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548}
19549
19550/*
19551 * ":delfunction {name}"
19552 */
19553 void
19554ex_delfunction(eap)
19555 exarg_T *eap;
19556{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019557 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 char_u *p;
19559 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019560 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561
19562 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019563 name = trans_function_name(&p, eap->skip, 0, &fudi);
19564 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019566 {
19567 if (fudi.fd_dict != NULL && !eap->skip)
19568 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 if (!ends_excmd(*skipwhite(p)))
19572 {
19573 vim_free(name);
19574 EMSG(_(e_trailing));
19575 return;
19576 }
19577 eap->nextcmd = check_nextcmd(p);
19578 if (eap->nextcmd != NULL)
19579 *p = NUL;
19580
19581 if (!eap->skip)
19582 fp = find_func(name);
19583 vim_free(name);
19584
19585 if (!eap->skip)
19586 {
19587 if (fp == NULL)
19588 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019589 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 return;
19591 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019592 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 {
19594 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19595 return;
19596 }
19597
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019598 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019600 /* Delete the dict item that refers to the function, it will
19601 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019602 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019604 else
19605 func_free(fp);
19606 }
19607}
19608
19609/*
19610 * Free a function and remove it from the list of functions.
19611 */
19612 static void
19613func_free(fp)
19614 ufunc_T *fp;
19615{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019616 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019617
19618 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019619 ga_clear_strings(&(fp->uf_args));
19620 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019621#ifdef FEAT_PROFILE
19622 vim_free(fp->uf_tml_count);
19623 vim_free(fp->uf_tml_total);
19624 vim_free(fp->uf_tml_self);
19625#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019626
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019627 /* remove the function from the function hashtable */
19628 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19629 if (HASHITEM_EMPTY(hi))
19630 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019631 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019632 hash_remove(&func_hashtab, hi);
19633
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019634 vim_free(fp);
19635}
19636
19637/*
19638 * Unreference a Function: decrement the reference count and free it when it
19639 * becomes zero. Only for numbered functions.
19640 */
19641 static void
19642func_unref(name)
19643 char_u *name;
19644{
19645 ufunc_T *fp;
19646
19647 if (name != NULL && isdigit(*name))
19648 {
19649 fp = find_func(name);
19650 if (fp == NULL)
19651 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019652 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019653 {
19654 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019655 * when "uf_calls" becomes zero. */
19656 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019657 func_free(fp);
19658 }
19659 }
19660}
19661
19662/*
19663 * Count a reference to a Function.
19664 */
19665 static void
19666func_ref(name)
19667 char_u *name;
19668{
19669 ufunc_T *fp;
19670
19671 if (name != NULL && isdigit(*name))
19672 {
19673 fp = find_func(name);
19674 if (fp == NULL)
19675 EMSG2(_(e_intern2), "func_ref()");
19676 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019677 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 }
19679}
19680
19681/*
19682 * Call a user function.
19683 */
19684 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019685call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 ufunc_T *fp; /* pointer to function */
19687 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019688 typval_T *argvars; /* arguments */
19689 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 linenr_T firstline; /* first line of range */
19691 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019692 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693{
Bram Moolenaar33570922005-01-25 22:26:29 +000019694 char_u *save_sourcing_name;
19695 linenr_T save_sourcing_lnum;
19696 scid_T save_current_SID;
19697 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019698 int save_did_emsg;
19699 static int depth = 0;
19700 dictitem_T *v;
19701 int fixvar_idx = 0; /* index in fixvar[] */
19702 int i;
19703 int ai;
19704 char_u numbuf[NUMBUFLEN];
19705 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019706#ifdef FEAT_PROFILE
19707 proftime_T wait_start;
19708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709
19710 /* If depth of calling is getting too high, don't execute the function */
19711 if (depth >= p_mfd)
19712 {
19713 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 rettv->v_type = VAR_NUMBER;
19715 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 return;
19717 }
19718 ++depth;
19719
19720 line_breakcheck(); /* check for CTRL-C hit */
19721
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019722 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019723 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725 fc.rettv = rettv;
19726 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 fc.linenr = 0;
19728 fc.returned = FALSE;
19729 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019731 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 fc.dbg_tick = debug_tick;
19733
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 /*
19735 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19736 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19737 * each argument variable and saves a lot of time.
19738 */
19739 /*
19740 * Init l: variables.
19741 */
19742 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019743 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019744 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019745 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19746 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019748 name = v->di_key;
19749 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019750 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19751 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19752 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019753 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019754 v->di_tv.vval.v_dict = selfdict;
19755 ++selfdict->dv_refcount;
19756 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019757
Bram Moolenaar33570922005-01-25 22:26:29 +000019758 /*
19759 * Init a: variables.
19760 * Set a:0 to "argcount".
19761 * Set a:000 to a list with room for the "..." arguments.
19762 */
19763 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19764 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019765 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019766 v = &fc.fixvar[fixvar_idx++].var;
19767 STRCPY(v->di_key, "000");
19768 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19769 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19770 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019771 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019772 v->di_tv.vval.v_list = &fc.l_varlist;
19773 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19774 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019775 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019776
19777 /*
19778 * Set a:firstline to "firstline" and a:lastline to "lastline".
19779 * Set a:name to named arguments.
19780 * Set a:N to the "..." arguments.
19781 */
19782 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19783 (varnumber_T)firstline);
19784 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19785 (varnumber_T)lastline);
19786 for (i = 0; i < argcount; ++i)
19787 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019788 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019789 if (ai < 0)
19790 /* named argument a:name */
19791 name = FUNCARG(fp, i);
19792 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019793 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019794 /* "..." argument a:1, a:2, etc. */
19795 sprintf((char *)numbuf, "%d", ai + 1);
19796 name = numbuf;
19797 }
19798 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19799 {
19800 v = &fc.fixvar[fixvar_idx++].var;
19801 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19802 }
19803 else
19804 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019805 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19806 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019807 if (v == NULL)
19808 break;
19809 v->di_flags = DI_FLAGS_RO;
19810 }
19811 STRCPY(v->di_key, name);
19812 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19813
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019814 /* Note: the values are copied directly to avoid alloc/free.
19815 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019817 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019818
19819 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19820 {
19821 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19822 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019823 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019824 }
19825 }
19826
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827 /* Don't redraw while executing the function. */
19828 ++RedrawingDisabled;
19829 save_sourcing_name = sourcing_name;
19830 save_sourcing_lnum = sourcing_lnum;
19831 sourcing_lnum = 1;
19832 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019833 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 if (sourcing_name != NULL)
19835 {
19836 if (save_sourcing_name != NULL
19837 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19838 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19839 else
19840 STRCPY(sourcing_name, "function ");
19841 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19842
19843 if (p_verbose >= 12)
19844 {
19845 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019846 verbose_enter_scroll();
19847
Bram Moolenaar555b2802005-05-19 21:08:39 +000019848 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 if (p_verbose >= 14)
19850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019852 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019853 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854
19855 msg_puts((char_u *)"(");
19856 for (i = 0; i < argcount; ++i)
19857 {
19858 if (i > 0)
19859 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019860 if (argvars[i].v_type == VAR_NUMBER)
19861 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 else
19863 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019864 trunc_string(tv2string(&argvars[i], &tofree,
19865 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019867 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 }
19869 }
19870 msg_puts((char_u *)")");
19871 }
19872 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019873
19874 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 --no_wait_return;
19876 }
19877 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019878#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019879 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019880 {
19881 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19882 func_do_profile(fp);
19883 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019884 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019885 {
19886 ++fp->uf_tm_count;
19887 profile_start(&fp->uf_tm_start);
19888 profile_zero(&fp->uf_tm_children);
19889 }
19890 script_prof_save(&wait_start);
19891 }
19892#endif
19893
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019895 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 save_did_emsg = did_emsg;
19897 did_emsg = FALSE;
19898
19899 /* call do_cmdline() to execute the lines */
19900 do_cmdline(NULL, get_func_line, (void *)&fc,
19901 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19902
19903 --RedrawingDisabled;
19904
19905 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019906 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019908 clear_tv(rettv);
19909 rettv->v_type = VAR_NUMBER;
19910 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 }
19912
Bram Moolenaar05159a02005-02-26 23:04:13 +000019913#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019914 if (do_profiling == PROF_YES && (fp->uf_profiling
19915 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019916 {
19917 profile_end(&fp->uf_tm_start);
19918 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19919 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019920 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019921 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019922 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019923 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19924 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019925 }
19926 }
19927#endif
19928
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 /* when being verbose, mention the return value */
19930 if (p_verbose >= 12)
19931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019933 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019936 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019938 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19939 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019942 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019943 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019944 char_u *tofree;
19945
Bram Moolenaar555b2802005-05-19 21:08:39 +000019946 /* The value may be very long. Skip the middle part, so that we
19947 * have some idea how it starts and ends. smsg() would always
19948 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000019949 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019950 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019951 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019952 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 }
19954 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019955
19956 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 --no_wait_return;
19958 }
19959
19960 vim_free(sourcing_name);
19961 sourcing_name = save_sourcing_name;
19962 sourcing_lnum = save_sourcing_lnum;
19963 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019964#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019965 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019966 script_prof_restore(&wait_start);
19967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968
19969 if (p_verbose >= 12 && sourcing_name != NULL)
19970 {
19971 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019972 verbose_enter_scroll();
19973
Bram Moolenaar555b2802005-05-19 21:08:39 +000019974 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019976
19977 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 --no_wait_return;
19979 }
19980
19981 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019982 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983
Bram Moolenaar33570922005-01-25 22:26:29 +000019984 /* The a: variables typevals were not alloced, only free the allocated
19985 * variables. */
19986 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19987
19988 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 --depth;
19990}
19991
19992/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019993 * Add a number variable "name" to dict "dp" with value "nr".
19994 */
19995 static void
19996add_nr_var(dp, v, name, nr)
19997 dict_T *dp;
19998 dictitem_T *v;
19999 char *name;
20000 varnumber_T nr;
20001{
20002 STRCPY(v->di_key, name);
20003 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20004 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20005 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020006 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020007 v->di_tv.vval.v_number = nr;
20008}
20009
20010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 * ":return [expr]"
20012 */
20013 void
20014ex_return(eap)
20015 exarg_T *eap;
20016{
20017 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020018 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 int returning = FALSE;
20020
20021 if (current_funccal == NULL)
20022 {
20023 EMSG(_("E133: :return not inside a function"));
20024 return;
20025 }
20026
20027 if (eap->skip)
20028 ++emsg_skip;
20029
20030 eap->nextcmd = NULL;
20031 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020032 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 {
20034 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020035 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020037 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 }
20039 /* It's safer to return also on error. */
20040 else if (!eap->skip)
20041 {
20042 /*
20043 * Return unless the expression evaluation has been cancelled due to an
20044 * aborting error, an interrupt, or an exception.
20045 */
20046 if (!aborting())
20047 returning = do_return(eap, FALSE, TRUE, NULL);
20048 }
20049
20050 /* When skipping or the return gets pending, advance to the next command
20051 * in this line (!returning). Otherwise, ignore the rest of the line.
20052 * Following lines will be ignored by get_func_line(). */
20053 if (returning)
20054 eap->nextcmd = NULL;
20055 else if (eap->nextcmd == NULL) /* no argument */
20056 eap->nextcmd = check_nextcmd(arg);
20057
20058 if (eap->skip)
20059 --emsg_skip;
20060}
20061
20062/*
20063 * Return from a function. Possibly makes the return pending. Also called
20064 * for a pending return at the ":endtry" or after returning from an extra
20065 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020067 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 * FALSE when the return gets pending.
20069 */
20070 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 exarg_T *eap;
20073 int reanimate;
20074 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076{
20077 int idx;
20078 struct condstack *cstack = eap->cstack;
20079
20080 if (reanimate)
20081 /* Undo the return. */
20082 current_funccal->returned = FALSE;
20083
20084 /*
20085 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20086 * not in its finally clause (which then is to be executed next) is found.
20087 * In this case, make the ":return" pending for execution at the ":endtry".
20088 * Otherwise, return normally.
20089 */
20090 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20091 if (idx >= 0)
20092 {
20093 cstack->cs_pending[idx] = CSTP_RETURN;
20094
20095 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 /* A pending return again gets pending. "rettv" points to an
20097 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020099 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 else
20101 {
20102 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020103 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020105 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 {
20109 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020110 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 else
20113 EMSG(_(e_outofmem));
20114 }
20115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020116 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117
20118 if (reanimate)
20119 {
20120 /* The pending return value could be overwritten by a ":return"
20121 * without argument in a finally clause; reset the default
20122 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020123 current_funccal->rettv->v_type = VAR_NUMBER;
20124 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 }
20126 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020127 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 }
20129 else
20130 {
20131 current_funccal->returned = TRUE;
20132
20133 /* If the return is carried out now, store the return value. For
20134 * a return immediately after reanimation, the value is already
20135 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020136 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020139 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 }
20143 }
20144
20145 return idx < 0;
20146}
20147
20148/*
20149 * Free the variable with a pending return value.
20150 */
20151 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020152discard_pending_return(rettv)
20153 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154{
Bram Moolenaar33570922005-01-25 22:26:29 +000020155 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156}
20157
20158/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020159 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 * is an allocated string. Used by report_pending() for verbose messages.
20161 */
20162 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163get_return_cmd(rettv)
20164 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020166 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020167 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020168 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020170 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020171 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020172 if (s == NULL)
20173 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020174
20175 STRCPY(IObuff, ":return ");
20176 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20177 if (STRLEN(s) + 8 >= IOSIZE)
20178 STRCPY(IObuff + IOSIZE - 4, "...");
20179 vim_free(tofree);
20180 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181}
20182
20183/*
20184 * Get next function line.
20185 * Called by do_cmdline() to get the next line.
20186 * Returns allocated string, or NULL for end of function.
20187 */
20188/* ARGSUSED */
20189 char_u *
20190get_func_line(c, cookie, indent)
20191 int c; /* not used */
20192 void *cookie;
20193 int indent; /* not used */
20194{
Bram Moolenaar33570922005-01-25 22:26:29 +000020195 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020196 ufunc_T *fp = fcp->func;
20197 char_u *retval;
20198 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199
20200 /* If breakpoints have been added/deleted need to check for it. */
20201 if (fcp->dbg_tick != debug_tick)
20202 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020203 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 sourcing_lnum);
20205 fcp->dbg_tick = debug_tick;
20206 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020207#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020208 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020209 func_line_end(cookie);
20210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211
Bram Moolenaar05159a02005-02-26 23:04:13 +000020212 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020213 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20214 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 retval = NULL;
20216 else
20217 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020218 /* Skip NULL lines (continuation lines). */
20219 while (fcp->linenr < gap->ga_len
20220 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20221 ++fcp->linenr;
20222 if (fcp->linenr >= gap->ga_len)
20223 retval = NULL;
20224 else
20225 {
20226 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20227 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020228#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020229 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020230 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020231#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 }
20234
20235 /* Did we encounter a breakpoint? */
20236 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20237 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020238 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020240 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 sourcing_lnum);
20242 fcp->dbg_tick = debug_tick;
20243 }
20244
20245 return retval;
20246}
20247
Bram Moolenaar05159a02005-02-26 23:04:13 +000020248#if defined(FEAT_PROFILE) || defined(PROTO)
20249/*
20250 * Called when starting to read a function line.
20251 * "sourcing_lnum" must be correct!
20252 * When skipping lines it may not actually be executed, but we won't find out
20253 * until later and we need to store the time now.
20254 */
20255 void
20256func_line_start(cookie)
20257 void *cookie;
20258{
20259 funccall_T *fcp = (funccall_T *)cookie;
20260 ufunc_T *fp = fcp->func;
20261
20262 if (fp->uf_profiling && sourcing_lnum >= 1
20263 && sourcing_lnum <= fp->uf_lines.ga_len)
20264 {
20265 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020266 /* Skip continuation lines. */
20267 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20268 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020269 fp->uf_tml_execed = FALSE;
20270 profile_start(&fp->uf_tml_start);
20271 profile_zero(&fp->uf_tml_children);
20272 profile_get_wait(&fp->uf_tml_wait);
20273 }
20274}
20275
20276/*
20277 * Called when actually executing a function line.
20278 */
20279 void
20280func_line_exec(cookie)
20281 void *cookie;
20282{
20283 funccall_T *fcp = (funccall_T *)cookie;
20284 ufunc_T *fp = fcp->func;
20285
20286 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20287 fp->uf_tml_execed = TRUE;
20288}
20289
20290/*
20291 * Called when done with a function line.
20292 */
20293 void
20294func_line_end(cookie)
20295 void *cookie;
20296{
20297 funccall_T *fcp = (funccall_T *)cookie;
20298 ufunc_T *fp = fcp->func;
20299
20300 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20301 {
20302 if (fp->uf_tml_execed)
20303 {
20304 ++fp->uf_tml_count[fp->uf_tml_idx];
20305 profile_end(&fp->uf_tml_start);
20306 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020307 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020308 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20309 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020310 }
20311 fp->uf_tml_idx = -1;
20312 }
20313}
20314#endif
20315
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316/*
20317 * Return TRUE if the currently active function should be ended, because a
20318 * return was encountered or an error occured. Used inside a ":while".
20319 */
20320 int
20321func_has_ended(cookie)
20322 void *cookie;
20323{
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325
20326 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20327 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020328 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 || fcp->returned);
20330}
20331
20332/*
20333 * return TRUE if cookie indicates a function which "abort"s on errors.
20334 */
20335 int
20336func_has_abort(cookie)
20337 void *cookie;
20338{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020339 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340}
20341
20342#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20343typedef enum
20344{
20345 VAR_FLAVOUR_DEFAULT,
20346 VAR_FLAVOUR_SESSION,
20347 VAR_FLAVOUR_VIMINFO
20348} var_flavour_T;
20349
20350static var_flavour_T var_flavour __ARGS((char_u *varname));
20351
20352 static var_flavour_T
20353var_flavour(varname)
20354 char_u *varname;
20355{
20356 char_u *p = varname;
20357
20358 if (ASCII_ISUPPER(*p))
20359 {
20360 while (*(++p))
20361 if (ASCII_ISLOWER(*p))
20362 return VAR_FLAVOUR_SESSION;
20363 return VAR_FLAVOUR_VIMINFO;
20364 }
20365 else
20366 return VAR_FLAVOUR_DEFAULT;
20367}
20368#endif
20369
20370#if defined(FEAT_VIMINFO) || defined(PROTO)
20371/*
20372 * Restore global vars that start with a capital from the viminfo file
20373 */
20374 int
20375read_viminfo_varlist(virp, writing)
20376 vir_T *virp;
20377 int writing;
20378{
20379 char_u *tab;
20380 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382
20383 if (!writing && (find_viminfo_parameter('!') != NULL))
20384 {
20385 tab = vim_strchr(virp->vir_line + 1, '\t');
20386 if (tab != NULL)
20387 {
20388 *tab++ = '\0'; /* isolate the variable name */
20389 if (*tab == 'S') /* string var */
20390 is_string = TRUE;
20391
20392 tab = vim_strchr(tab, '\t');
20393 if (tab != NULL)
20394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 if (is_string)
20396 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020397 tv.v_type = VAR_STRING;
20398 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 }
20401 else
20402 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020403 tv.v_type = VAR_NUMBER;
20404 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020406 set_var(virp->vir_line + 1, &tv, FALSE);
20407 if (is_string)
20408 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 }
20410 }
20411 }
20412
20413 return viminfo_readline(virp);
20414}
20415
20416/*
20417 * Write global vars that start with a capital to the viminfo file
20418 */
20419 void
20420write_viminfo_varlist(fp)
20421 FILE *fp;
20422{
Bram Moolenaar33570922005-01-25 22:26:29 +000020423 hashitem_T *hi;
20424 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020425 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020426 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020427 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020428 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020429 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430
20431 if (find_viminfo_parameter('!') == NULL)
20432 return;
20433
20434 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020436 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020437 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020441 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020442 this_var = HI2DI(hi);
20443 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020444 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020445 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020446 {
20447 case VAR_STRING: s = "STR"; break;
20448 case VAR_NUMBER: s = "NUM"; break;
20449 default: continue;
20450 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020451 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020452 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020453 if (p != NULL)
20454 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020455 vim_free(tofree);
20456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 }
20458 }
20459}
20460#endif
20461
20462#if defined(FEAT_SESSION) || defined(PROTO)
20463 int
20464store_session_globals(fd)
20465 FILE *fd;
20466{
Bram Moolenaar33570922005-01-25 22:26:29 +000020467 hashitem_T *hi;
20468 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020469 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 char_u *p, *t;
20471
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020472 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020475 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020477 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020478 this_var = HI2DI(hi);
20479 if ((this_var->di_tv.v_type == VAR_NUMBER
20480 || this_var->di_tv.v_type == VAR_STRING)
20481 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020482 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020483 /* Escape special characters with a backslash. Turn a LF and
20484 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020486 (char_u *)"\\\"\n\r");
20487 if (p == NULL) /* out of memory */
20488 break;
20489 for (t = p; *t != NUL; ++t)
20490 if (*t == '\n')
20491 *t = 'n';
20492 else if (*t == '\r')
20493 *t = 'r';
20494 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020495 this_var->di_key,
20496 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20497 : ' ',
20498 p,
20499 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20500 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020501 || put_eol(fd) == FAIL)
20502 {
20503 vim_free(p);
20504 return FAIL;
20505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 vim_free(p);
20507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 }
20509 }
20510 return OK;
20511}
20512#endif
20513
Bram Moolenaar661b1822005-07-28 22:36:45 +000020514/*
20515 * Display script name where an item was last set.
20516 * Should only be invoked when 'verbose' is non-zero.
20517 */
20518 void
20519last_set_msg(scriptID)
20520 scid_T scriptID;
20521{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020522 char_u *p;
20523
Bram Moolenaar661b1822005-07-28 22:36:45 +000020524 if (scriptID != 0)
20525 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020526 p = home_replace_save(NULL, get_scriptname(scriptID));
20527 if (p != NULL)
20528 {
20529 verbose_enter();
20530 MSG_PUTS(_("\n\tLast set from "));
20531 MSG_PUTS(p);
20532 vim_free(p);
20533 verbose_leave();
20534 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020535 }
20536}
20537
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538#endif /* FEAT_EVAL */
20539
20540#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20541
20542
20543#ifdef WIN3264
20544/*
20545 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20546 */
20547static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20548static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20549static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20550
20551/*
20552 * Get the short pathname of a file.
20553 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20554 */
20555 static int
20556get_short_pathname(fnamep, bufp, fnamelen)
20557 char_u **fnamep;
20558 char_u **bufp;
20559 int *fnamelen;
20560{
20561 int l,len;
20562 char_u *newbuf;
20563
20564 len = *fnamelen;
20565
20566 l = GetShortPathName(*fnamep, *fnamep, len);
20567 if (l > len - 1)
20568 {
20569 /* If that doesn't work (not enough space), then save the string
20570 * and try again with a new buffer big enough
20571 */
20572 newbuf = vim_strnsave(*fnamep, l);
20573 if (newbuf == NULL)
20574 return 0;
20575
20576 vim_free(*bufp);
20577 *fnamep = *bufp = newbuf;
20578
20579 l = GetShortPathName(*fnamep,*fnamep,l+1);
20580
20581 /* Really should always succeed, as the buffer is big enough */
20582 }
20583
20584 *fnamelen = l;
20585 return 1;
20586}
20587
20588/*
20589 * Create a short path name. Returns the length of the buffer it needs.
20590 * Doesn't copy over the end of the buffer passed in.
20591 */
20592 static int
20593shortpath_for_invalid_fname(fname, bufp, fnamelen)
20594 char_u **fname;
20595 char_u **bufp;
20596 int *fnamelen;
20597{
20598 char_u *s, *p, *pbuf2, *pbuf3;
20599 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020600 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601
20602 /* Make a copy */
20603 len2 = *fnamelen;
20604 pbuf2 = vim_strnsave(*fname, len2);
20605 pbuf3 = NULL;
20606
20607 s = pbuf2 + len2 - 1; /* Find the end */
20608 slen = 1;
20609 plen = len2;
20610
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020611 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 {
20613 --s;
20614 ++slen;
20615 --plen;
20616 }
20617
20618 do
20619 {
20620 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020621 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622 {
20623 --s;
20624 ++slen;
20625 --plen;
20626 }
20627 if (s <= pbuf2)
20628 break;
20629
20630 /* Remeber the character that is about to be blatted */
20631 ch = *s;
20632 *s = 0; /* get_short_pathname requires a null-terminated string */
20633
20634 /* Try it in situ */
20635 p = pbuf2;
20636 if (!get_short_pathname(&p, &pbuf3, &plen))
20637 {
20638 vim_free(pbuf2);
20639 return -1;
20640 }
20641 *s = ch; /* Preserve the string */
20642 } while (plen == 0);
20643
20644 if (plen > 0)
20645 {
20646 /* Remeber the length of the new string. */
20647 *fnamelen = len = plen + slen;
20648 vim_free(*bufp);
20649 if (len > len2)
20650 {
20651 /* If there's not enough space in the currently allocated string,
20652 * then copy it to a buffer big enough.
20653 */
20654 *fname= *bufp = vim_strnsave(p, len);
20655 if (*fname == NULL)
20656 return -1;
20657 }
20658 else
20659 {
20660 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20661 *fname = *bufp = pbuf2;
20662 if (p != pbuf2)
20663 strncpy(*fname, p, plen);
20664 pbuf2 = NULL;
20665 }
20666 /* Concat the next bit */
20667 strncpy(*fname + plen, s, slen);
20668 (*fname)[len] = '\0';
20669 }
20670 vim_free(pbuf3);
20671 vim_free(pbuf2);
20672 return 0;
20673}
20674
20675/*
20676 * Get a pathname for a partial path.
20677 */
20678 static int
20679shortpath_for_partial(fnamep, bufp, fnamelen)
20680 char_u **fnamep;
20681 char_u **bufp;
20682 int *fnamelen;
20683{
20684 int sepcount, len, tflen;
20685 char_u *p;
20686 char_u *pbuf, *tfname;
20687 int hasTilde;
20688
20689 /* Count up the path seperators from the RHS.. so we know which part
20690 * of the path to return.
20691 */
20692 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020693 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 if (vim_ispathsep(*p))
20695 ++sepcount;
20696
20697 /* Need full path first (use expand_env() to remove a "~/") */
20698 hasTilde = (**fnamep == '~');
20699 if (hasTilde)
20700 pbuf = tfname = expand_env_save(*fnamep);
20701 else
20702 pbuf = tfname = FullName_save(*fnamep, FALSE);
20703
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020704 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705
20706 if (!get_short_pathname(&tfname, &pbuf, &len))
20707 return -1;
20708
20709 if (len == 0)
20710 {
20711 /* Don't have a valid filename, so shorten the rest of the
20712 * path if we can. This CAN give us invalid 8.3 filenames, but
20713 * there's not a lot of point in guessing what it might be.
20714 */
20715 len = tflen;
20716 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20717 return -1;
20718 }
20719
20720 /* Count the paths backward to find the beginning of the desired string. */
20721 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020722 {
20723#ifdef FEAT_MBYTE
20724 if (has_mbyte)
20725 p -= mb_head_off(tfname, p);
20726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 if (vim_ispathsep(*p))
20728 {
20729 if (sepcount == 0 || (hasTilde && sepcount == 1))
20730 break;
20731 else
20732 sepcount --;
20733 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 if (hasTilde)
20736 {
20737 --p;
20738 if (p >= tfname)
20739 *p = '~';
20740 else
20741 return -1;
20742 }
20743 else
20744 ++p;
20745
20746 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20747 vim_free(*bufp);
20748 *fnamelen = (int)STRLEN(p);
20749 *bufp = pbuf;
20750 *fnamep = p;
20751
20752 return 0;
20753}
20754#endif /* WIN3264 */
20755
20756/*
20757 * Adjust a filename, according to a string of modifiers.
20758 * *fnamep must be NUL terminated when called. When returning, the length is
20759 * determined by *fnamelen.
20760 * Returns valid flags.
20761 * When there is an error, *fnamep is set to NULL.
20762 */
20763 int
20764modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20765 char_u *src; /* string with modifiers */
20766 int *usedlen; /* characters after src that are used */
20767 char_u **fnamep; /* file name so far */
20768 char_u **bufp; /* buffer for allocated file name or NULL */
20769 int *fnamelen; /* length of fnamep */
20770{
20771 int valid = 0;
20772 char_u *tail;
20773 char_u *s, *p, *pbuf;
20774 char_u dirname[MAXPATHL];
20775 int c;
20776 int has_fullname = 0;
20777#ifdef WIN3264
20778 int has_shortname = 0;
20779#endif
20780
20781repeat:
20782 /* ":p" - full path/file_name */
20783 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20784 {
20785 has_fullname = 1;
20786
20787 valid |= VALID_PATH;
20788 *usedlen += 2;
20789
20790 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20791 if ((*fnamep)[0] == '~'
20792#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20793 && ((*fnamep)[1] == '/'
20794# ifdef BACKSLASH_IN_FILENAME
20795 || (*fnamep)[1] == '\\'
20796# endif
20797 || (*fnamep)[1] == NUL)
20798
20799#endif
20800 )
20801 {
20802 *fnamep = expand_env_save(*fnamep);
20803 vim_free(*bufp); /* free any allocated file name */
20804 *bufp = *fnamep;
20805 if (*fnamep == NULL)
20806 return -1;
20807 }
20808
20809 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020810 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 {
20812 if (vim_ispathsep(*p)
20813 && p[1] == '.'
20814 && (p[2] == NUL
20815 || vim_ispathsep(p[2])
20816 || (p[2] == '.'
20817 && (p[3] == NUL || vim_ispathsep(p[3])))))
20818 break;
20819 }
20820
20821 /* FullName_save() is slow, don't use it when not needed. */
20822 if (*p != NUL || !vim_isAbsName(*fnamep))
20823 {
20824 *fnamep = FullName_save(*fnamep, *p != NUL);
20825 vim_free(*bufp); /* free any allocated file name */
20826 *bufp = *fnamep;
20827 if (*fnamep == NULL)
20828 return -1;
20829 }
20830
20831 /* Append a path separator to a directory. */
20832 if (mch_isdir(*fnamep))
20833 {
20834 /* Make room for one or two extra characters. */
20835 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20836 vim_free(*bufp); /* free any allocated file name */
20837 *bufp = *fnamep;
20838 if (*fnamep == NULL)
20839 return -1;
20840 add_pathsep(*fnamep);
20841 }
20842 }
20843
20844 /* ":." - path relative to the current directory */
20845 /* ":~" - path relative to the home directory */
20846 /* ":8" - shortname path - postponed till after */
20847 while (src[*usedlen] == ':'
20848 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20849 {
20850 *usedlen += 2;
20851 if (c == '8')
20852 {
20853#ifdef WIN3264
20854 has_shortname = 1; /* Postpone this. */
20855#endif
20856 continue;
20857 }
20858 pbuf = NULL;
20859 /* Need full path first (use expand_env() to remove a "~/") */
20860 if (!has_fullname)
20861 {
20862 if (c == '.' && **fnamep == '~')
20863 p = pbuf = expand_env_save(*fnamep);
20864 else
20865 p = pbuf = FullName_save(*fnamep, FALSE);
20866 }
20867 else
20868 p = *fnamep;
20869
20870 has_fullname = 0;
20871
20872 if (p != NULL)
20873 {
20874 if (c == '.')
20875 {
20876 mch_dirname(dirname, MAXPATHL);
20877 s = shorten_fname(p, dirname);
20878 if (s != NULL)
20879 {
20880 *fnamep = s;
20881 if (pbuf != NULL)
20882 {
20883 vim_free(*bufp); /* free any allocated file name */
20884 *bufp = pbuf;
20885 pbuf = NULL;
20886 }
20887 }
20888 }
20889 else
20890 {
20891 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20892 /* Only replace it when it starts with '~' */
20893 if (*dirname == '~')
20894 {
20895 s = vim_strsave(dirname);
20896 if (s != NULL)
20897 {
20898 *fnamep = s;
20899 vim_free(*bufp);
20900 *bufp = s;
20901 }
20902 }
20903 }
20904 vim_free(pbuf);
20905 }
20906 }
20907
20908 tail = gettail(*fnamep);
20909 *fnamelen = (int)STRLEN(*fnamep);
20910
20911 /* ":h" - head, remove "/file_name", can be repeated */
20912 /* Don't remove the first "/" or "c:\" */
20913 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20914 {
20915 valid |= VALID_HEAD;
20916 *usedlen += 2;
20917 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020918 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 --tail;
20920 *fnamelen = (int)(tail - *fnamep);
20921#ifdef VMS
20922 if (*fnamelen > 0)
20923 *fnamelen += 1; /* the path separator is part of the path */
20924#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020925 while (tail > s && !after_pathsep(s, tail))
20926 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 }
20928
20929 /* ":8" - shortname */
20930 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20931 {
20932 *usedlen += 2;
20933#ifdef WIN3264
20934 has_shortname = 1;
20935#endif
20936 }
20937
20938#ifdef WIN3264
20939 /* Check shortname after we have done 'heads' and before we do 'tails'
20940 */
20941 if (has_shortname)
20942 {
20943 pbuf = NULL;
20944 /* Copy the string if it is shortened by :h */
20945 if (*fnamelen < (int)STRLEN(*fnamep))
20946 {
20947 p = vim_strnsave(*fnamep, *fnamelen);
20948 if (p == 0)
20949 return -1;
20950 vim_free(*bufp);
20951 *bufp = *fnamep = p;
20952 }
20953
20954 /* Split into two implementations - makes it easier. First is where
20955 * there isn't a full name already, second is where there is.
20956 */
20957 if (!has_fullname && !vim_isAbsName(*fnamep))
20958 {
20959 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20960 return -1;
20961 }
20962 else
20963 {
20964 int l;
20965
20966 /* Simple case, already have the full-name
20967 * Nearly always shorter, so try first time. */
20968 l = *fnamelen;
20969 if (!get_short_pathname(fnamep, bufp, &l))
20970 return -1;
20971
20972 if (l == 0)
20973 {
20974 /* Couldn't find the filename.. search the paths.
20975 */
20976 l = *fnamelen;
20977 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20978 return -1;
20979 }
20980 *fnamelen = l;
20981 }
20982 }
20983#endif /* WIN3264 */
20984
20985 /* ":t" - tail, just the basename */
20986 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20987 {
20988 *usedlen += 2;
20989 *fnamelen -= (int)(tail - *fnamep);
20990 *fnamep = tail;
20991 }
20992
20993 /* ":e" - extension, can be repeated */
20994 /* ":r" - root, without extension, can be repeated */
20995 while (src[*usedlen] == ':'
20996 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20997 {
20998 /* find a '.' in the tail:
20999 * - for second :e: before the current fname
21000 * - otherwise: The last '.'
21001 */
21002 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21003 s = *fnamep - 2;
21004 else
21005 s = *fnamep + *fnamelen - 1;
21006 for ( ; s > tail; --s)
21007 if (s[0] == '.')
21008 break;
21009 if (src[*usedlen + 1] == 'e') /* :e */
21010 {
21011 if (s > tail)
21012 {
21013 *fnamelen += (int)(*fnamep - (s + 1));
21014 *fnamep = s + 1;
21015#ifdef VMS
21016 /* cut version from the extension */
21017 s = *fnamep + *fnamelen - 1;
21018 for ( ; s > *fnamep; --s)
21019 if (s[0] == ';')
21020 break;
21021 if (s > *fnamep)
21022 *fnamelen = s - *fnamep;
21023#endif
21024 }
21025 else if (*fnamep <= tail)
21026 *fnamelen = 0;
21027 }
21028 else /* :r */
21029 {
21030 if (s > tail) /* remove one extension */
21031 *fnamelen = (int)(s - *fnamep);
21032 }
21033 *usedlen += 2;
21034 }
21035
21036 /* ":s?pat?foo?" - substitute */
21037 /* ":gs?pat?foo?" - global substitute */
21038 if (src[*usedlen] == ':'
21039 && (src[*usedlen + 1] == 's'
21040 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21041 {
21042 char_u *str;
21043 char_u *pat;
21044 char_u *sub;
21045 int sep;
21046 char_u *flags;
21047 int didit = FALSE;
21048
21049 flags = (char_u *)"";
21050 s = src + *usedlen + 2;
21051 if (src[*usedlen + 1] == 'g')
21052 {
21053 flags = (char_u *)"g";
21054 ++s;
21055 }
21056
21057 sep = *s++;
21058 if (sep)
21059 {
21060 /* find end of pattern */
21061 p = vim_strchr(s, sep);
21062 if (p != NULL)
21063 {
21064 pat = vim_strnsave(s, (int)(p - s));
21065 if (pat != NULL)
21066 {
21067 s = p + 1;
21068 /* find end of substitution */
21069 p = vim_strchr(s, sep);
21070 if (p != NULL)
21071 {
21072 sub = vim_strnsave(s, (int)(p - s));
21073 str = vim_strnsave(*fnamep, *fnamelen);
21074 if (sub != NULL && str != NULL)
21075 {
21076 *usedlen = (int)(p + 1 - src);
21077 s = do_string_sub(str, pat, sub, flags);
21078 if (s != NULL)
21079 {
21080 *fnamep = s;
21081 *fnamelen = (int)STRLEN(s);
21082 vim_free(*bufp);
21083 *bufp = s;
21084 didit = TRUE;
21085 }
21086 }
21087 vim_free(sub);
21088 vim_free(str);
21089 }
21090 vim_free(pat);
21091 }
21092 }
21093 /* after using ":s", repeat all the modifiers */
21094 if (didit)
21095 goto repeat;
21096 }
21097 }
21098
21099 return valid;
21100}
21101
21102/*
21103 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21104 * "flags" can be "g" to do a global substitute.
21105 * Returns an allocated string, NULL for error.
21106 */
21107 char_u *
21108do_string_sub(str, pat, sub, flags)
21109 char_u *str;
21110 char_u *pat;
21111 char_u *sub;
21112 char_u *flags;
21113{
21114 int sublen;
21115 regmatch_T regmatch;
21116 int i;
21117 int do_all;
21118 char_u *tail;
21119 garray_T ga;
21120 char_u *ret;
21121 char_u *save_cpo;
21122
21123 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21124 save_cpo = p_cpo;
21125 p_cpo = (char_u *)"";
21126
21127 ga_init2(&ga, 1, 200);
21128
21129 do_all = (flags[0] == 'g');
21130
21131 regmatch.rm_ic = p_ic;
21132 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21133 if (regmatch.regprog != NULL)
21134 {
21135 tail = str;
21136 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21137 {
21138 /*
21139 * Get some space for a temporary buffer to do the substitution
21140 * into. It will contain:
21141 * - The text up to where the match is.
21142 * - The substituted text.
21143 * - The text after the match.
21144 */
21145 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21146 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21147 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21148 {
21149 ga_clear(&ga);
21150 break;
21151 }
21152
21153 /* copy the text up to where the match is */
21154 i = (int)(regmatch.startp[0] - tail);
21155 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21156 /* add the substituted text */
21157 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21158 + ga.ga_len + i, TRUE, TRUE, FALSE);
21159 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 /* avoid getting stuck on a match with an empty string */
21161 if (tail == regmatch.endp[0])
21162 {
21163 if (*tail == NUL)
21164 break;
21165 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21166 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 }
21168 else
21169 {
21170 tail = regmatch.endp[0];
21171 if (*tail == NUL)
21172 break;
21173 }
21174 if (!do_all)
21175 break;
21176 }
21177
21178 if (ga.ga_data != NULL)
21179 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21180
21181 vim_free(regmatch.regprog);
21182 }
21183
21184 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21185 ga_clear(&ga);
21186 p_cpo = save_cpo;
21187
21188 return ret;
21189}
21190
21191#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */