blob: 3369789b316939a32edadb301c4c9cbde3aa5d24 [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
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000201/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000202static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
203
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000204/* list heads for garbage collection */
205static dict_T *first_dict = NULL; /* list of all dicts */
206static list_T *first_list = NULL; /* list of all lists */
207
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000208/* From user function to hashitem and back. */
209static ufunc_T dumuf;
210#define UF2HIKEY(fp) ((fp)->uf_name)
211#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
212#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
213
214#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
215#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar33570922005-01-25 22:26:29 +0000217#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
218#define VAR_SHORT_LEN 20 /* short variable name length */
219#define FIXVAR_CNT 12 /* number of fixed variables */
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000222typedef struct funccall_S funccall_T;
223
224struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 ufunc_T *func; /* function being called */
227 int linenr; /* next line to be executed */
228 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000229 struct /* fixed variables for arguments */
230 {
231 dictitem_T var; /* variable (without room for name) */
232 char_u room[VAR_SHORT_LEN]; /* room for the name */
233 } fixvar[FIXVAR_CNT];
234 dict_T l_vars; /* l: local function variables */
235 dictitem_T l_vars_var; /* variable for l: scope */
236 dict_T l_avars; /* a: argument variables */
237 dictitem_T l_avars_var; /* variable for a: scope */
238 list_T l_varlist; /* list for a:000 */
239 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
240 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 linenr_T breakpoint; /* next line with breakpoint or zero */
242 int dbg_tick; /* debug_tick when breakpoint was set */
243 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000244#ifdef FEAT_PROFILE
245 proftime_T prof_child; /* time spent in a child */
246#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000247 funccall_T *caller; /* calling function or NULL */
248};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251 * Info used by a ":for" loop.
252 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000253typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000254{
255 int fi_semicolon; /* TRUE if ending in '; var]' */
256 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000257 listwatch_T fi_lw; /* keep an eye on the item used. */
258 list_T *fi_list; /* list being used */
259} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000262 * Struct used by trans_function_name()
263 */
264typedef struct
265{
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000267 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 dictitem_T *fd_di; /* Dictionary item used */
269} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270
Bram Moolenaara7043832005-01-21 11:56:39 +0000271
272/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000273 * Array to hold the value of v: variables.
274 * The value is in a dictitem, so that it can also be used in the v: scope.
275 * The reason to use this table anyway is for very quick access to the
276 * variables with the VV_ defines.
277 */
278#include "version.h"
279
280/* values for vv_flags: */
281#define VV_COMPAT 1 /* compatible, also used without "v:" */
282#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000283#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000285#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000286
287static struct vimvar
288{
289 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 dictitem_T vv_di; /* value and name for key */
291 char vv_filler[16]; /* space for LONGEST name below!!! */
292 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
293} vimvars[VV_LEN] =
294{
295 /*
296 * The order here must match the VV_ defines in vim.h!
297 * Initializing a union does not work, leave tv.vval empty to get zero's.
298 */
299 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
300 {VV_NAME("count1", VAR_NUMBER), VV_RO},
301 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
302 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
303 {VV_NAME("warningmsg", VAR_STRING), 0},
304 {VV_NAME("statusmsg", VAR_STRING), 0},
305 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
307 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
309 {VV_NAME("termresponse", VAR_STRING), VV_RO},
310 {VV_NAME("fname", VAR_STRING), VV_RO},
311 {VV_NAME("lang", VAR_STRING), VV_RO},
312 {VV_NAME("lc_time", VAR_STRING), VV_RO},
313 {VV_NAME("ctype", VAR_STRING), VV_RO},
314 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
315 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
316 {VV_NAME("fname_in", VAR_STRING), VV_RO},
317 {VV_NAME("fname_out", VAR_STRING), VV_RO},
318 {VV_NAME("fname_new", VAR_STRING), VV_RO},
319 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
320 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
321 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
324 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
325 {VV_NAME("progname", VAR_STRING), VV_RO},
326 {VV_NAME("servername", VAR_STRING), VV_RO},
327 {VV_NAME("dying", VAR_NUMBER), VV_RO},
328 {VV_NAME("exception", VAR_STRING), VV_RO},
329 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
330 {VV_NAME("register", VAR_STRING), VV_RO},
331 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
332 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000333 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
334 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000335 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000336 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
337 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000338 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
341 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000343 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000344 {VV_NAME("swapname", VAR_STRING), VV_RO},
345 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000346 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000347 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000348};
349
350/* shorthand */
351#define vv_type vv_di.di_tv.v_type
352#define vv_nr vv_di.di_tv.vval.v_number
353#define vv_str vv_di.di_tv.vval.v_string
354#define vv_tv vv_di.di_tv
355
356/*
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
359 */
360static dict_T vimvardict;
361static dictitem_T vimvars_var;
362#define vimvarht vimvardict.dv_hashtab
363
Bram Moolenaara40058a2005-07-11 22:42:07 +0000364static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
365static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
366#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
368#endif
369static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
370static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
371static char_u *skip_var_one __ARGS((char_u *arg));
372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
373static void list_glob_vars __ARGS((void));
374static void list_buf_vars __ARGS((void));
375static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
377static void list_tab_vars __ARGS((void));
378#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000380static void list_script_vars __ARGS((void));
381static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
438static void dict_free __ARGS((dict_T *d));
439static dictitem_T *dictitem_alloc __ARGS((char_u *key));
440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
442static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int dict_add __ARGS((dict_T *d, dictitem_T *item));
445static long dict_len __ARGS((dict_T *d));
446static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
450static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static char_u *string_quote __ARGS((char_u *str, int function));
452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
454static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
455static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
456static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000457static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000480static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
489static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000502static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000516static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000518static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000524static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000532static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000533static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000536static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000557static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000563static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000579static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585#ifdef vim_mkdir
586static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
587#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000591static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000593static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000594static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000596static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000597static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000610static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000612static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000619static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000620static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000621static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000623static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000627static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000628static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000631static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632#ifdef HAVE_STRFTIME
633static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
634#endif
635static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000647static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000648static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000649static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000650static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000651static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000653static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000667static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000670static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000672static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
673static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static int get_env_len __ARGS((char_u **arg));
675static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000676static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000677static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
678#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
679#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
680 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000681static 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 +0000682static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000683static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000684static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
685static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static typval_T *alloc_tv __ARGS((void));
687static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void init_tv __ARGS((typval_T *varp));
689static long get_tv_number __ARGS((typval_T *varp));
690static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000691static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static char_u *get_tv_string __ARGS((typval_T *varp));
693static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000694static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000696static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
698static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
699static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
700static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
701static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
702static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
703static int var_check_ro __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 Moolenaar9ef486d2005-01-17 22:23:00 +00002263 char_u *tofree = NULL;
2264 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 {
2272 p = tofree = concat_str(s, p);
2273 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 Moolenaar9ef486d2005-01-17 22:23:00 +00002281 vim_free(tofree);
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 Moolenaar2e6aff32005-01-31 19:25:36 +00003367 if (var_check_ro(HI2DI(hi)->di_flags, name))
3368 return FAIL;
3369 delete_var(ht, hi);
3370 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003373 if (forceit)
3374 return OK;
3375 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 return FAIL;
3377}
3378
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003379/*
3380 * Lock or unlock variable indicated by "lp".
3381 * "deep" is the levels to go (-1 for unlimited);
3382 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3383 */
3384 static int
3385do_lock_var(lp, name_end, deep, lock)
3386 lval_T *lp;
3387 char_u *name_end;
3388 int deep;
3389 int lock;
3390{
3391 int ret = OK;
3392 int cc;
3393 dictitem_T *di;
3394
3395 if (deep == 0) /* nothing to do */
3396 return OK;
3397
3398 if (lp->ll_tv == NULL)
3399 {
3400 cc = *name_end;
3401 *name_end = NUL;
3402
3403 /* Normal name or expanded name. */
3404 if (check_changedtick(lp->ll_name))
3405 ret = FAIL;
3406 else
3407 {
3408 di = find_var(lp->ll_name, NULL);
3409 if (di == NULL)
3410 ret = FAIL;
3411 else
3412 {
3413 if (lock)
3414 di->di_flags |= DI_FLAGS_LOCK;
3415 else
3416 di->di_flags &= ~DI_FLAGS_LOCK;
3417 item_lock(&di->di_tv, deep, lock);
3418 }
3419 }
3420 *name_end = cc;
3421 }
3422 else if (lp->ll_range)
3423 {
3424 listitem_T *li = lp->ll_li;
3425
3426 /* (un)lock a range of List items. */
3427 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3428 {
3429 item_lock(&li->li_tv, deep, lock);
3430 li = li->li_next;
3431 ++lp->ll_n1;
3432 }
3433 }
3434 else if (lp->ll_list != NULL)
3435 /* (un)lock a List item. */
3436 item_lock(&lp->ll_li->li_tv, deep, lock);
3437 else
3438 /* un(lock) a Dictionary item. */
3439 item_lock(&lp->ll_di->di_tv, deep, lock);
3440
3441 return ret;
3442}
3443
3444/*
3445 * Lock or unlock an item. "deep" is nr of levels to go.
3446 */
3447 static void
3448item_lock(tv, deep, lock)
3449 typval_T *tv;
3450 int deep;
3451 int lock;
3452{
3453 static int recurse = 0;
3454 list_T *l;
3455 listitem_T *li;
3456 dict_T *d;
3457 hashitem_T *hi;
3458 int todo;
3459
3460 if (recurse >= DICT_MAXNEST)
3461 {
3462 EMSG(_("E743: variable nested too deep for (un)lock"));
3463 return;
3464 }
3465 if (deep == 0)
3466 return;
3467 ++recurse;
3468
3469 /* lock/unlock the item itself */
3470 if (lock)
3471 tv->v_lock |= VAR_LOCKED;
3472 else
3473 tv->v_lock &= ~VAR_LOCKED;
3474
3475 switch (tv->v_type)
3476 {
3477 case VAR_LIST:
3478 if ((l = tv->vval.v_list) != NULL)
3479 {
3480 if (lock)
3481 l->lv_lock |= VAR_LOCKED;
3482 else
3483 l->lv_lock &= ~VAR_LOCKED;
3484 if (deep < 0 || deep > 1)
3485 /* recursive: lock/unlock the items the List contains */
3486 for (li = l->lv_first; li != NULL; li = li->li_next)
3487 item_lock(&li->li_tv, deep - 1, lock);
3488 }
3489 break;
3490 case VAR_DICT:
3491 if ((d = tv->vval.v_dict) != NULL)
3492 {
3493 if (lock)
3494 d->dv_lock |= VAR_LOCKED;
3495 else
3496 d->dv_lock &= ~VAR_LOCKED;
3497 if (deep < 0 || deep > 1)
3498 {
3499 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003501 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3502 {
3503 if (!HASHITEM_EMPTY(hi))
3504 {
3505 --todo;
3506 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3507 }
3508 }
3509 }
3510 }
3511 }
3512 --recurse;
3513}
3514
Bram Moolenaara40058a2005-07-11 22:42:07 +00003515/*
3516 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3517 * it refers to a List or Dictionary that is locked.
3518 */
3519 static int
3520tv_islocked(tv)
3521 typval_T *tv;
3522{
3523 return (tv->v_lock & VAR_LOCKED)
3524 || (tv->v_type == VAR_LIST
3525 && tv->vval.v_list != NULL
3526 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3527 || (tv->v_type == VAR_DICT
3528 && tv->vval.v_dict != NULL
3529 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3530}
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3533/*
3534 * Delete all "menutrans_" variables.
3535 */
3536 void
3537del_menutrans_vars()
3538{
Bram Moolenaar33570922005-01-25 22:26:29 +00003539 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003540 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003543 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003544 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 {
3546 if (!HASHITEM_EMPTY(hi))
3547 {
3548 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003549 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3550 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003551 }
3552 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003553 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554}
3555#endif
3556
3557#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3558
3559/*
3560 * Local string buffer for the next two functions to store a variable name
3561 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3562 * get_user_var_name().
3563 */
3564
3565static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3566
3567static char_u *varnamebuf = NULL;
3568static int varnamebuflen = 0;
3569
3570/*
3571 * Function to concatenate a prefix and a variable name.
3572 */
3573 static char_u *
3574cat_prefix_varname(prefix, name)
3575 int prefix;
3576 char_u *name;
3577{
3578 int len;
3579
3580 len = (int)STRLEN(name) + 3;
3581 if (len > varnamebuflen)
3582 {
3583 vim_free(varnamebuf);
3584 len += 10; /* some additional space */
3585 varnamebuf = alloc(len);
3586 if (varnamebuf == NULL)
3587 {
3588 varnamebuflen = 0;
3589 return NULL;
3590 }
3591 varnamebuflen = len;
3592 }
3593 *varnamebuf = prefix;
3594 varnamebuf[1] = ':';
3595 STRCPY(varnamebuf + 2, name);
3596 return varnamebuf;
3597}
3598
3599/*
3600 * Function given to ExpandGeneric() to obtain the list of user defined
3601 * (global/buffer/window/built-in) variable names.
3602 */
3603/*ARGSUSED*/
3604 char_u *
3605get_user_var_name(xp, idx)
3606 expand_T *xp;
3607 int idx;
3608{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003609 static long_u gdone;
3610 static long_u bdone;
3611 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003612#ifdef FEAT_WINDOWS
3613 static long_u tdone;
3614#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003615 static int vidx;
3616 static hashitem_T *hi;
3617 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
3619 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003620 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003621 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003622#ifdef FEAT_WINDOWS
3623 tdone = 0;
3624#endif
3625 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003626
3627 /* Global variables */
3628 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003630 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003631 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003632 else
3633 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003634 while (HASHITEM_EMPTY(hi))
3635 ++hi;
3636 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3637 return cat_prefix_varname('g', hi->hi_key);
3638 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003640
3641 /* b: variables */
3642 ht = &curbuf->b_vars.dv_hashtab;
3643 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003645 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003647 else
3648 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 while (HASHITEM_EMPTY(hi))
3650 ++hi;
3651 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return (char_u *)"b:changedtick";
3657 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003658
3659 /* w: variables */
3660 ht = &curwin->w_vars.dv_hashtab;
3661 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003663 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003665 else
3666 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 while (HASHITEM_EMPTY(hi))
3668 ++hi;
3669 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003671
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003672#ifdef FEAT_WINDOWS
3673 /* t: variables */
3674 ht = &curtab->tp_vars.dv_hashtab;
3675 if (tdone < ht->ht_used)
3676 {
3677 if (tdone++ == 0)
3678 hi = ht->ht_array;
3679 else
3680 ++hi;
3681 while (HASHITEM_EMPTY(hi))
3682 ++hi;
3683 return cat_prefix_varname('t', hi->hi_key);
3684 }
3685#endif
3686
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 /* v: variables */
3688 if (vidx < VV_LEN)
3689 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 vim_free(varnamebuf);
3692 varnamebuf = NULL;
3693 varnamebuflen = 0;
3694 return NULL;
3695}
3696
3697#endif /* FEAT_CMDL_COMPL */
3698
3699/*
3700 * types for expressions.
3701 */
3702typedef enum
3703{
3704 TYPE_UNKNOWN = 0
3705 , TYPE_EQUAL /* == */
3706 , TYPE_NEQUAL /* != */
3707 , TYPE_GREATER /* > */
3708 , TYPE_GEQUAL /* >= */
3709 , TYPE_SMALLER /* < */
3710 , TYPE_SEQUAL /* <= */
3711 , TYPE_MATCH /* =~ */
3712 , TYPE_NOMATCH /* !~ */
3713} exptype_T;
3714
3715/*
3716 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3719 */
3720
3721/*
3722 * Handle zero level expression.
3723 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003725 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 * Return OK or FAIL.
3727 */
3728 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 char_u **nextcmd;
3733 int evaluate;
3734{
3735 int ret;
3736 char_u *p;
3737
3738 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003739 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (ret == FAIL || !ends_excmd(*p))
3741 {
3742 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 /*
3745 * Report the invalid expression unless the expression evaluation has
3746 * been cancelled due to an aborting error, an interrupt, or an
3747 * exception.
3748 */
3749 if (!aborting())
3750 EMSG2(_(e_invexpr2), arg);
3751 ret = FAIL;
3752 }
3753 if (nextcmd != NULL)
3754 *nextcmd = check_nextcmd(p);
3755
3756 return ret;
3757}
3758
3759/*
3760 * Handle top level expression:
3761 * expr1 ? expr0 : expr0
3762 *
3763 * "arg" must point to the first non-white of the expression.
3764 * "arg" is advanced to the next non-white after the recognized expression.
3765 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003766 * Note: "rettv.v_lock" is not set.
3767 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 * Return OK or FAIL.
3769 */
3770 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 int evaluate;
3775{
3776 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003777 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
3779 /*
3780 * Get the first variable.
3781 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003782 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return FAIL;
3784
3785 if ((*arg)[0] == '?')
3786 {
3787 result = FALSE;
3788 if (evaluate)
3789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003790 int error = FALSE;
3791
3792 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003795 if (error)
3796 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798
3799 /*
3800 * Get the second variable.
3801 */
3802 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805
3806 /*
3807 * Check for the ":".
3808 */
3809 if ((*arg)[0] != ':')
3810 {
3811 EMSG(_("E109: Missing ':' after '?'"));
3812 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 return FAIL;
3815 }
3816
3817 /*
3818 * Get the third variable.
3819 */
3820 *arg = skipwhite(*arg + 1);
3821 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3822 {
3823 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 return FAIL;
3826 }
3827 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003828 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830
3831 return OK;
3832}
3833
3834/*
3835 * Handle first level expression:
3836 * expr2 || expr2 || expr2 logical OR
3837 *
3838 * "arg" must point to the first non-white of the expression.
3839 * "arg" is advanced to the next non-white after the recognized expression.
3840 *
3841 * Return OK or FAIL.
3842 */
3843 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003844eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 int evaluate;
3848{
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 long result;
3851 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003852 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
3854 /*
3855 * Get the first variable.
3856 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 return FAIL;
3859
3860 /*
3861 * Repeat until there is no following "||".
3862 */
3863 first = TRUE;
3864 result = FALSE;
3865 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3866 {
3867 if (evaluate && first)
3868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003871 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003872 if (error)
3873 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 first = FALSE;
3875 }
3876
3877 /*
3878 * Get the second variable.
3879 */
3880 *arg = skipwhite(*arg + 2);
3881 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3882 return FAIL;
3883
3884 /*
3885 * Compute the result.
3886 */
3887 if (evaluate && !result)
3888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003889 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003892 if (error)
3893 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895 if (evaluate)
3896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003897 rettv->v_type = VAR_NUMBER;
3898 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 }
3901
3902 return OK;
3903}
3904
3905/*
3906 * Handle second level expression:
3907 * expr3 && expr3 && expr3 logical AND
3908 *
3909 * "arg" must point to the first non-white of the expression.
3910 * "arg" is advanced to the next non-white after the recognized expression.
3911 *
3912 * Return OK or FAIL.
3913 */
3914 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 int evaluate;
3919{
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 long result;
3922 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003923 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 /*
3926 * Get the first variable.
3927 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003928 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 return FAIL;
3930
3931 /*
3932 * Repeat until there is no following "&&".
3933 */
3934 first = TRUE;
3935 result = TRUE;
3936 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3937 {
3938 if (evaluate && first)
3939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003940 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (error)
3944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 first = FALSE;
3946 }
3947
3948 /*
3949 * Get the second variable.
3950 */
3951 *arg = skipwhite(*arg + 2);
3952 if (eval4(arg, &var2, evaluate && result) == FAIL)
3953 return FAIL;
3954
3955 /*
3956 * Compute the result.
3957 */
3958 if (evaluate && result)
3959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003960 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003962 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003963 if (error)
3964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 if (evaluate)
3967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 rettv->v_type = VAR_NUMBER;
3969 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 }
3972
3973 return OK;
3974}
3975
3976/*
3977 * Handle third level expression:
3978 * var1 == var2
3979 * var1 =~ var2
3980 * var1 != var2
3981 * var1 !~ var2
3982 * var1 > var2
3983 * var1 >= var2
3984 * var1 < var2
3985 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003986 * var1 is var2
3987 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 *
3989 * "arg" must point to the first non-white of the expression.
3990 * "arg" is advanced to the next non-white after the recognized expression.
3991 *
3992 * Return OK or FAIL.
3993 */
3994 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 int evaluate;
3999{
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 char_u *p;
4002 int i;
4003 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004004 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 int len = 2;
4006 long n1, n2;
4007 char_u *s1, *s2;
4008 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4009 regmatch_T regmatch;
4010 int ic;
4011 char_u *save_cpo;
4012
4013 /*
4014 * Get the first variable.
4015 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 return FAIL;
4018
4019 p = *arg;
4020 switch (p[0])
4021 {
4022 case '=': if (p[1] == '=')
4023 type = TYPE_EQUAL;
4024 else if (p[1] == '~')
4025 type = TYPE_MATCH;
4026 break;
4027 case '!': if (p[1] == '=')
4028 type = TYPE_NEQUAL;
4029 else if (p[1] == '~')
4030 type = TYPE_NOMATCH;
4031 break;
4032 case '>': if (p[1] != '=')
4033 {
4034 type = TYPE_GREATER;
4035 len = 1;
4036 }
4037 else
4038 type = TYPE_GEQUAL;
4039 break;
4040 case '<': if (p[1] != '=')
4041 {
4042 type = TYPE_SMALLER;
4043 len = 1;
4044 }
4045 else
4046 type = TYPE_SEQUAL;
4047 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004048 case 'i': if (p[1] == 's')
4049 {
4050 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4051 len = 5;
4052 if (!vim_isIDc(p[len]))
4053 {
4054 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4055 type_is = TRUE;
4056 }
4057 }
4058 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060
4061 /*
4062 * If there is a comparitive operator, use it.
4063 */
4064 if (type != TYPE_UNKNOWN)
4065 {
4066 /* extra question mark appended: ignore case */
4067 if (p[len] == '?')
4068 {
4069 ic = TRUE;
4070 ++len;
4071 }
4072 /* extra '#' appended: match case */
4073 else if (p[len] == '#')
4074 {
4075 ic = FALSE;
4076 ++len;
4077 }
4078 /* nothing appened: use 'ignorecase' */
4079 else
4080 ic = p_ic;
4081
4082 /*
4083 * Get the second variable.
4084 */
4085 *arg = skipwhite(p + len);
4086 if (eval5(arg, &var2, evaluate) == FAIL)
4087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return FAIL;
4090 }
4091
4092 if (evaluate)
4093 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004094 if (type_is && rettv->v_type != var2.v_type)
4095 {
4096 /* For "is" a different type always means FALSE, for "notis"
4097 * it means TRUE. */
4098 n1 = (type == TYPE_NEQUAL);
4099 }
4100 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4101 {
4102 if (type_is)
4103 {
4104 n1 = (rettv->v_type == var2.v_type
4105 && rettv->vval.v_list == var2.vval.v_list);
4106 if (type == TYPE_NEQUAL)
4107 n1 = !n1;
4108 }
4109 else if (rettv->v_type != var2.v_type
4110 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4111 {
4112 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004113 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004114 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004115 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004116 clear_tv(rettv);
4117 clear_tv(&var2);
4118 return FAIL;
4119 }
4120 else
4121 {
4122 /* Compare two Lists for being equal or unequal. */
4123 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4124 if (type == TYPE_NEQUAL)
4125 n1 = !n1;
4126 }
4127 }
4128
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004129 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4130 {
4131 if (type_is)
4132 {
4133 n1 = (rettv->v_type == var2.v_type
4134 && rettv->vval.v_dict == var2.vval.v_dict);
4135 if (type == TYPE_NEQUAL)
4136 n1 = !n1;
4137 }
4138 else if (rettv->v_type != var2.v_type
4139 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4140 {
4141 if (rettv->v_type != var2.v_type)
4142 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4143 else
4144 EMSG(_("E736: Invalid operation for Dictionary"));
4145 clear_tv(rettv);
4146 clear_tv(&var2);
4147 return FAIL;
4148 }
4149 else
4150 {
4151 /* Compare two Dictionaries for being equal or unequal. */
4152 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4153 if (type == TYPE_NEQUAL)
4154 n1 = !n1;
4155 }
4156 }
4157
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004158 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4159 {
4160 if (rettv->v_type != var2.v_type
4161 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4162 {
4163 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004164 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004165 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 clear_tv(rettv);
4168 clear_tv(&var2);
4169 return FAIL;
4170 }
4171 else
4172 {
4173 /* Compare two Funcrefs for being equal or unequal. */
4174 if (rettv->vval.v_string == NULL
4175 || var2.vval.v_string == NULL)
4176 n1 = FALSE;
4177 else
4178 n1 = STRCMP(rettv->vval.v_string,
4179 var2.vval.v_string) == 0;
4180 if (type == TYPE_NEQUAL)
4181 n1 = !n1;
4182 }
4183 }
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /*
4186 * If one of the two variables is a number, compare as a number.
4187 * When using "=~" or "!~", always compare as string.
4188 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004189 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 n1 = get_tv_number(rettv);
4193 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 switch (type)
4195 {
4196 case TYPE_EQUAL: n1 = (n1 == n2); break;
4197 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4198 case TYPE_GREATER: n1 = (n1 > n2); break;
4199 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4200 case TYPE_SMALLER: n1 = (n1 < n2); break;
4201 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4202 case TYPE_UNKNOWN:
4203 case TYPE_MATCH:
4204 case TYPE_NOMATCH: break; /* avoid gcc warning */
4205 }
4206 }
4207 else
4208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 s1 = get_tv_string_buf(rettv, buf1);
4210 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4212 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4213 else
4214 i = 0;
4215 n1 = FALSE;
4216 switch (type)
4217 {
4218 case TYPE_EQUAL: n1 = (i == 0); break;
4219 case TYPE_NEQUAL: n1 = (i != 0); break;
4220 case TYPE_GREATER: n1 = (i > 0); break;
4221 case TYPE_GEQUAL: n1 = (i >= 0); break;
4222 case TYPE_SMALLER: n1 = (i < 0); break;
4223 case TYPE_SEQUAL: n1 = (i <= 0); break;
4224
4225 case TYPE_MATCH:
4226 case TYPE_NOMATCH:
4227 /* avoid 'l' flag in 'cpoptions' */
4228 save_cpo = p_cpo;
4229 p_cpo = (char_u *)"";
4230 regmatch.regprog = vim_regcomp(s2,
4231 RE_MAGIC + RE_STRING);
4232 regmatch.rm_ic = ic;
4233 if (regmatch.regprog != NULL)
4234 {
4235 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4236 vim_free(regmatch.regprog);
4237 if (type == TYPE_NOMATCH)
4238 n1 = !n1;
4239 }
4240 p_cpo = save_cpo;
4241 break;
4242
4243 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4244 }
4245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(rettv);
4247 clear_tv(&var2);
4248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252
4253 return OK;
4254}
4255
4256/*
4257 * Handle fourth level expression:
4258 * + number addition
4259 * - number subtraction
4260 * . string concatenation
4261 *
4262 * "arg" must point to the first non-white of the expression.
4263 * "arg" is advanced to the next non-white after the recognized expression.
4264 *
4265 * Return OK or FAIL.
4266 */
4267 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 int evaluate;
4272{
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T var2;
4274 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 int op;
4276 long n1, n2;
4277 char_u *s1, *s2;
4278 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4279 char_u *p;
4280
4281 /*
4282 * Get the first variable.
4283 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return FAIL;
4286
4287 /*
4288 * Repeat computing, until no '+', '-' or '.' is following.
4289 */
4290 for (;;)
4291 {
4292 op = **arg;
4293 if (op != '+' && op != '-' && op != '.')
4294 break;
4295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 if (op != '+' || rettv->v_type != VAR_LIST)
4297 {
4298 /* For "list + ...", an illegal use of the first operand as
4299 * a number cannot be determined before evaluating the 2nd
4300 * operand: if this is also a list, all is ok.
4301 * For "something . ...", "something - ..." or "non-list + ...",
4302 * we know that the first operand needs to be a string or number
4303 * without evaluating the 2nd operand. So check before to avoid
4304 * side effects after an error. */
4305 if (evaluate && get_tv_string_chk(rettv) == NULL)
4306 {
4307 clear_tv(rettv);
4308 return FAIL;
4309 }
4310 }
4311
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 /*
4313 * Get the second variable.
4314 */
4315 *arg = skipwhite(*arg + 1);
4316 if (eval6(arg, &var2, evaluate) == FAIL)
4317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return FAIL;
4320 }
4321
4322 if (evaluate)
4323 {
4324 /*
4325 * Compute the result.
4326 */
4327 if (op == '.')
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4330 s2 = get_tv_string_buf_chk(&var2, buf2);
4331 if (s2 == NULL) /* type error ? */
4332 {
4333 clear_tv(rettv);
4334 clear_tv(&var2);
4335 return FAIL;
4336 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004337 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 clear_tv(rettv);
4339 rettv->v_type = VAR_STRING;
4340 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004342 else if (op == '+' && rettv->v_type == VAR_LIST
4343 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004344 {
4345 /* concatenate Lists */
4346 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4347 &var3) == FAIL)
4348 {
4349 clear_tv(rettv);
4350 clear_tv(&var2);
4351 return FAIL;
4352 }
4353 clear_tv(rettv);
4354 *rettv = var3;
4355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 else
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 int error = FALSE;
4359
4360 n1 = get_tv_number_chk(rettv, &error);
4361 if (error)
4362 {
4363 /* This can only happen for "list + non-list".
4364 * For "non-list + ..." or "something - ...", we returned
4365 * before evaluating the 2nd operand. */
4366 clear_tv(rettv);
4367 return FAIL;
4368 }
4369 n2 = get_tv_number_chk(&var2, &error);
4370 if (error)
4371 {
4372 clear_tv(rettv);
4373 clear_tv(&var2);
4374 return FAIL;
4375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (op == '+')
4378 n1 = n1 + n2;
4379 else
4380 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381 rettv->v_type = VAR_NUMBER;
4382 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386 }
4387 return OK;
4388}
4389
4390/*
4391 * Handle fifth level expression:
4392 * * number multiplication
4393 * / number division
4394 * % number modulo
4395 *
4396 * "arg" must point to the first non-white of the expression.
4397 * "arg" is advanced to the next non-white after the recognized expression.
4398 *
4399 * Return OK or FAIL.
4400 */
4401 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 int evaluate;
4406{
Bram Moolenaar33570922005-01-25 22:26:29 +00004407 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 int op;
4409 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 /*
4413 * Get the first variable.
4414 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 return FAIL;
4417
4418 /*
4419 * Repeat computing, until no '*', '/' or '%' is following.
4420 */
4421 for (;;)
4422 {
4423 op = **arg;
4424 if (op != '*' && op != '/' && op != '%')
4425 break;
4426
4427 if (evaluate)
4428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004429 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004431 if (error)
4432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 else
4435 n1 = 0;
4436
4437 /*
4438 * Get the second variable.
4439 */
4440 *arg = skipwhite(*arg + 1);
4441 if (eval7(arg, &var2, evaluate) == FAIL)
4442 return FAIL;
4443
4444 if (evaluate)
4445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004446 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004447 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004448 if (error)
4449 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 /*
4452 * Compute the result.
4453 */
4454 if (op == '*')
4455 n1 = n1 * n2;
4456 else if (op == '/')
4457 {
4458 if (n2 == 0) /* give an error message? */
4459 n1 = 0x7fffffffL;
4460 else
4461 n1 = n1 / n2;
4462 }
4463 else
4464 {
4465 if (n2 == 0) /* give an error message? */
4466 n1 = 0;
4467 else
4468 n1 = n1 % n2;
4469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv->v_type = VAR_NUMBER;
4471 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 }
4474
4475 return OK;
4476}
4477
4478/*
4479 * Handle sixth level expression:
4480 * number number constant
4481 * "string" string contstant
4482 * 'string' literal string contstant
4483 * &option-name option value
4484 * @r register contents
4485 * identifier variable value
4486 * function() function call
4487 * $VAR environment variable
4488 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004489 * [expr, expr] List
4490 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 *
4492 * Also handle:
4493 * ! in front logical NOT
4494 * - in front unary minus
4495 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 * trailing [] subscript in String or List
4497 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 *
4499 * "arg" must point to the first non-white of the expression.
4500 * "arg" is advanced to the next non-white after the recognized expression.
4501 *
4502 * Return OK or FAIL.
4503 */
4504 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 int evaluate;
4509{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 long n;
4511 int len;
4512 char_u *s;
4513 int val;
4514 char_u *start_leader, *end_leader;
4515 int ret = OK;
4516 char_u *alias;
4517
4518 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004520 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 /*
4525 * Skip '!' and '-' characters. They are handled later.
4526 */
4527 start_leader = *arg;
4528 while (**arg == '!' || **arg == '-' || **arg == '+')
4529 *arg = skipwhite(*arg + 1);
4530 end_leader = *arg;
4531
4532 switch (**arg)
4533 {
4534 /*
4535 * Number constant.
4536 */
4537 case '0':
4538 case '1':
4539 case '2':
4540 case '3':
4541 case '4':
4542 case '5':
4543 case '6':
4544 case '7':
4545 case '8':
4546 case '9':
4547 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4548 *arg += len;
4549 if (evaluate)
4550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004551 rettv->v_type = VAR_NUMBER;
4552 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554 break;
4555
4556 /*
4557 * String constant: "string".
4558 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004559 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 break;
4561
4562 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 break;
4567
4568 /*
4569 * List: [expr, expr]
4570 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004571 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 break;
4573
4574 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 * Dictionary: {key: val, key: val}
4576 */
4577 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4578 break;
4579
4580 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004581 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004583 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 break;
4585
4586 /*
4587 * Environment variable: $VAR.
4588 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004589 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 break;
4591
4592 /*
4593 * Register contents: @r.
4594 */
4595 case '@': ++*arg;
4596 if (evaluate)
4597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004599 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 if (**arg != NUL)
4602 ++*arg;
4603 break;
4604
4605 /*
4606 * nested expression: (expression).
4607 */
4608 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (**arg == ')')
4611 ++*arg;
4612 else if (ret == OK)
4613 {
4614 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 ret = FAIL;
4617 }
4618 break;
4619
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 break;
4622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004623
4624 if (ret == NOTDONE)
4625 {
4626 /*
4627 * Must be a variable or function name.
4628 * Can also be a curly-braces kind of name: {expr}.
4629 */
4630 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004631 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004632 if (alias != NULL)
4633 s = alias;
4634
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004635 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636 ret = FAIL;
4637 else
4638 {
4639 if (**arg == '(') /* recursive! */
4640 {
4641 /* If "s" is the name of a variable of type VAR_FUNC
4642 * use its contents. */
4643 s = deref_func_name(s, &len);
4644
4645 /* Invoke the function. */
4646 ret = get_func_tv(s, len, rettv, arg,
4647 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004648 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649 /* Stop the expression evaluation when immediately
4650 * aborting on error, or when an interrupt occurred or
4651 * an exception was thrown but not caught. */
4652 if (aborting())
4653 {
4654 if (ret == OK)
4655 clear_tv(rettv);
4656 ret = FAIL;
4657 }
4658 }
4659 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004660 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004661 else
4662 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 }
4664
4665 if (alias != NULL)
4666 vim_free(alias);
4667 }
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 *arg = skipwhite(*arg);
4670
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004671 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4672 * expr(expr). */
4673 if (ret == OK)
4674 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 /*
4677 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4678 */
4679 if (ret == OK && evaluate && end_leader > start_leader)
4680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 int error = FALSE;
4682
4683 val = get_tv_number_chk(rettv, &error);
4684 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004686 clear_tv(rettv);
4687 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 else
4690 {
4691 while (end_leader > start_leader)
4692 {
4693 --end_leader;
4694 if (*end_leader == '!')
4695 val = !val;
4696 else if (*end_leader == '-')
4697 val = -val;
4698 }
4699 clear_tv(rettv);
4700 rettv->v_type = VAR_NUMBER;
4701 rettv->vval.v_number = val;
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704
4705 return ret;
4706}
4707
4708/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004709 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4710 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4712 */
4713 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004714eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004716 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004718 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719{
4720 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004721 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004723 long len = -1;
4724 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004728 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004730 if (verbose)
4731 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 return FAIL;
4733 }
4734
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737 /*
4738 * dict.name
4739 */
4740 key = *arg + 1;
4741 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4742 ;
4743 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 }
4747 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 /*
4750 * something[idx]
4751 *
4752 * Get the (first) variable from inside the [].
4753 */
4754 *arg = skipwhite(*arg + 1);
4755 if (**arg == ':')
4756 empty1 = TRUE;
4757 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4758 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4760 {
4761 /* not a number or string */
4762 clear_tv(&var1);
4763 return FAIL;
4764 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004765
4766 /*
4767 * Get the second variable from inside the [:].
4768 */
4769 if (**arg == ':')
4770 {
4771 range = TRUE;
4772 *arg = skipwhite(*arg + 1);
4773 if (**arg == ']')
4774 empty2 = TRUE;
4775 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 if (!empty1)
4778 clear_tv(&var1);
4779 return FAIL;
4780 }
4781 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4782 {
4783 /* not a number or string */
4784 if (!empty1)
4785 clear_tv(&var1);
4786 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004787 return FAIL;
4788 }
4789 }
4790
4791 /* Check for the ']'. */
4792 if (**arg != ']')
4793 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004794 if (verbose)
4795 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 clear_tv(&var1);
4797 if (range)
4798 clear_tv(&var2);
4799 return FAIL;
4800 }
4801 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 }
4803
4804 if (evaluate)
4805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806 n1 = 0;
4807 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 n1 = get_tv_number(&var1);
4810 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 }
4812 if (range)
4813 {
4814 if (empty2)
4815 n2 = -1;
4816 else
4817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004818 n2 = get_tv_number(&var2);
4819 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 }
4821 }
4822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 {
4825 case VAR_NUMBER:
4826 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828 len = (long)STRLEN(s);
4829 if (range)
4830 {
4831 /* The resulting variable is a substring. If the indexes
4832 * are out of range the result is empty. */
4833 if (n1 < 0)
4834 {
4835 n1 = len + n1;
4836 if (n1 < 0)
4837 n1 = 0;
4838 }
4839 if (n2 < 0)
4840 n2 = len + n2;
4841 else if (n2 >= len)
4842 n2 = len;
4843 if (n1 >= len || n2 < 0 || n1 > n2)
4844 s = NULL;
4845 else
4846 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4847 }
4848 else
4849 {
4850 /* The resulting variable is a string of a single
4851 * character. If the index is too big or negative the
4852 * result is empty. */
4853 if (n1 >= len || n1 < 0)
4854 s = NULL;
4855 else
4856 s = vim_strnsave(s + n1, 1);
4857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858 clear_tv(rettv);
4859 rettv->v_type = VAR_STRING;
4860 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 break;
4862
4863 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 if (n1 < 0)
4866 n1 = len + n1;
4867 if (!empty1 && (n1 < 0 || n1 >= len))
4868 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004869 /* For a range we allow invalid values and return an empty
4870 * list. A list index out of range is an error. */
4871 if (!range)
4872 {
4873 if (verbose)
4874 EMSGN(_(e_listidx), n1);
4875 return FAIL;
4876 }
4877 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878 }
4879 if (range)
4880 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004881 list_T *l;
4882 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883
4884 if (n2 < 0)
4885 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004886 else if (n2 >= len)
4887 n2 = len - 1;
4888 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004889 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004890 l = list_alloc();
4891 if (l == NULL)
4892 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 n1 <= n2; ++n1)
4895 {
4896 if (list_append_tv(l, &item->li_tv) == FAIL)
4897 {
4898 list_free(l);
4899 return FAIL;
4900 }
4901 item = item->li_next;
4902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 clear_tv(rettv);
4904 rettv->v_type = VAR_LIST;
4905 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004906 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004907 }
4908 else
4909 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004910 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
4912 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 }
4914 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915
4916 case VAR_DICT:
4917 if (range)
4918 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004919 if (verbose)
4920 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 if (len == -1)
4922 clear_tv(&var1);
4923 return FAIL;
4924 }
4925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004926 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927
4928 if (len == -1)
4929 {
4930 key = get_tv_string(&var1);
4931 if (*key == NUL)
4932 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004933 if (verbose)
4934 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935 clear_tv(&var1);
4936 return FAIL;
4937 }
4938 }
4939
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004940 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004941
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004942 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004943 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 if (len == -1)
4945 clear_tv(&var1);
4946 if (item == NULL)
4947 return FAIL;
4948
4949 copy_tv(&item->di_tv, &var1);
4950 clear_tv(rettv);
4951 *rettv = var1;
4952 }
4953 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954 }
4955 }
4956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 return OK;
4958}
4959
4960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 * Get an option value.
4962 * "arg" points to the '&' or '+' before the option name.
4963 * "arg" is advanced to character after the option name.
4964 * Return OK or FAIL.
4965 */
4966 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004967get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004969 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 int evaluate;
4971{
4972 char_u *option_end;
4973 long numval;
4974 char_u *stringval;
4975 int opt_type;
4976 int c;
4977 int working = (**arg == '+'); /* has("+option") */
4978 int ret = OK;
4979 int opt_flags;
4980
4981 /*
4982 * Isolate the option name and find its value.
4983 */
4984 option_end = find_option_end(arg, &opt_flags);
4985 if (option_end == NULL)
4986 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 EMSG2(_("E112: Option name missing: %s"), *arg);
4989 return FAIL;
4990 }
4991
4992 if (!evaluate)
4993 {
4994 *arg = option_end;
4995 return OK;
4996 }
4997
4998 c = *option_end;
4999 *option_end = NUL;
5000 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
5003 if (opt_type == -3) /* invalid name */
5004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 EMSG2(_("E113: Unknown option: %s"), *arg);
5007 ret = FAIL;
5008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005009 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 if (opt_type == -2) /* hidden string option */
5012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005013 rettv->v_type = VAR_STRING;
5014 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016 else if (opt_type == -1) /* hidden number option */
5017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 else if (opt_type == 1) /* number option */
5022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 rettv->v_type = VAR_NUMBER;
5024 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 else /* string option */
5027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 rettv->v_type = VAR_STRING;
5029 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 }
5032 else if (working && (opt_type == -2 || opt_type == -1))
5033 ret = FAIL;
5034
5035 *option_end = c; /* put back for error messages */
5036 *arg = option_end;
5037
5038 return ret;
5039}
5040
5041/*
5042 * Allocate a variable for a string constant.
5043 * Return OK or FAIL.
5044 */
5045 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 int evaluate;
5050{
5051 char_u *p;
5052 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 int extra = 0;
5054
5055 /*
5056 * Find the end of the string, skipping backslashed characters.
5057 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 if (*p == '\\' && p[1] != NUL)
5061 {
5062 ++p;
5063 /* A "\<x>" form occupies at least 4 characters, and produces up
5064 * to 6 characters: reserve space for 2 extra */
5065 if (*p == '<')
5066 extra += 2;
5067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069
5070 if (*p != '"')
5071 {
5072 EMSG2(_("E114: Missing quote: %s"), *arg);
5073 return FAIL;
5074 }
5075
5076 /* If only parsing, set *arg and return here */
5077 if (!evaluate)
5078 {
5079 *arg = p + 1;
5080 return OK;
5081 }
5082
5083 /*
5084 * Copy the string into allocated memory, handling backslashed
5085 * characters.
5086 */
5087 name = alloc((unsigned)(p - *arg + extra));
5088 if (name == NULL)
5089 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 rettv->v_type = VAR_STRING;
5091 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 if (*p == '\\')
5096 {
5097 switch (*++p)
5098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 case 'b': *name++ = BS; ++p; break;
5100 case 'e': *name++ = ESC; ++p; break;
5101 case 'f': *name++ = FF; ++p; break;
5102 case 'n': *name++ = NL; ++p; break;
5103 case 'r': *name++ = CAR; ++p; break;
5104 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
5106 case 'X': /* hex: "\x1", "\x12" */
5107 case 'x':
5108 case 'u': /* Unicode: "\u0023" */
5109 case 'U':
5110 if (vim_isxdigit(p[1]))
5111 {
5112 int n, nr;
5113 int c = toupper(*p);
5114
5115 if (c == 'X')
5116 n = 2;
5117 else
5118 n = 4;
5119 nr = 0;
5120 while (--n >= 0 && vim_isxdigit(p[1]))
5121 {
5122 ++p;
5123 nr = (nr << 4) + hex2nr(*p);
5124 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#ifdef FEAT_MBYTE
5127 /* For "\u" store the number according to
5128 * 'encoding'. */
5129 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 else
5132#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136
5137 /* octal: "\1", "\12", "\123" */
5138 case '0':
5139 case '1':
5140 case '2':
5141 case '3':
5142 case '4':
5143 case '5':
5144 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 case '7': *name = *p++ - '0';
5146 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 *name = (*name << 3) + *p++ - '0';
5149 if (*p >= '0' && *p <= '7')
5150 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154
5155 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (extra != 0)
5158 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 break;
5161 }
5162 /* FALLTHROUGH */
5163
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 break;
5166 }
5167 }
5168 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 *arg = p + 1;
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 return OK;
5176}
5177
5178/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * Return OK or FAIL.
5181 */
5182 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 int evaluate;
5187{
5188 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005189 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005190 int reduce = 0;
5191
5192 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005194 */
5195 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5196 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005198 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005200 break;
5201 ++reduce;
5202 ++p;
5203 }
5204 }
5205
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005207 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005209 return FAIL;
5210 }
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005213 if (!evaluate)
5214 {
5215 *arg = p + 1;
5216 return OK;
5217 }
5218
5219 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005221 */
5222 str = alloc((unsigned)((p - *arg) - reduce));
5223 if (str == NULL)
5224 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 rettv->v_type = VAR_STRING;
5226 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005227
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005229 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005231 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005233 break;
5234 ++p;
5235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005237 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005239 *arg = p + 1;
5240
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 return OK;
5242}
5243
5244/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245 * Allocate a variable for a List and fill it from "*arg".
5246 * Return OK or FAIL.
5247 */
5248 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005249get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005251 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 int evaluate;
5253{
Bram Moolenaar33570922005-01-25 22:26:29 +00005254 list_T *l = NULL;
5255 typval_T tv;
5256 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257
5258 if (evaluate)
5259 {
5260 l = list_alloc();
5261 if (l == NULL)
5262 return FAIL;
5263 }
5264
5265 *arg = skipwhite(*arg + 1);
5266 while (**arg != ']' && **arg != NUL)
5267 {
5268 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5269 goto failret;
5270 if (evaluate)
5271 {
5272 item = listitem_alloc();
5273 if (item != NULL)
5274 {
5275 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005276 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 list_append(l, item);
5278 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005279 else
5280 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 }
5282
5283 if (**arg == ']')
5284 break;
5285 if (**arg != ',')
5286 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 goto failret;
5289 }
5290 *arg = skipwhite(*arg + 1);
5291 }
5292
5293 if (**arg != ']')
5294 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296failret:
5297 if (evaluate)
5298 list_free(l);
5299 return FAIL;
5300 }
5301
5302 *arg = skipwhite(*arg + 1);
5303 if (evaluate)
5304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 rettv->v_type = VAR_LIST;
5306 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 ++l->lv_refcount;
5308 }
5309
5310 return OK;
5311}
5312
5313/*
5314 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005315 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005317 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318list_alloc()
5319{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005320 list_T *l;
5321
5322 l = (list_T *)alloc_clear(sizeof(list_T));
5323 if (l != NULL)
5324 {
5325 /* Prepend the list to the list of lists for garbage collection. */
5326 if (first_list != NULL)
5327 first_list->lv_used_prev = l;
5328 l->lv_used_prev = NULL;
5329 l->lv_used_next = first_list;
5330 first_list = l;
5331 }
5332 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333}
5334
5335/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005336 * Allocate an empty list for a return value.
5337 * Returns OK or FAIL.
5338 */
5339 static int
5340rettv_list_alloc(rettv)
5341 typval_T *rettv;
5342{
5343 list_T *l = list_alloc();
5344
5345 if (l == NULL)
5346 return FAIL;
5347
5348 rettv->vval.v_list = l;
5349 rettv->v_type = VAR_LIST;
5350 ++l->lv_refcount;
5351 return OK;
5352}
5353
5354/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 * Unreference a list: decrement the reference count and free it when it
5356 * becomes zero.
5357 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005358 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005360 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5363 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364}
5365
5366/*
5367 * Free a list, including all items it points to.
5368 * Ignores the reference count.
5369 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005370 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005372 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373{
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375
Bram Moolenaard9fba312005-06-26 22:34:35 +00005376 /* Avoid that recursive reference to the list frees us again. */
5377 l->lv_refcount = DEL_REFCOUNT;
5378
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 Moolenaar49cd9572005-01-03 21:06:01 +00005391 listitem_free(item);
5392 }
5393 vim_free(l);
5394}
5395
5396/*
5397 * Allocate a list item.
5398 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400listitem_alloc()
5401{
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403}
5404
5405/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005406 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 */
5408 static void
5409listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005412 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 vim_free(item);
5414}
5415
5416/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005417 * Remove a list item from a List and free it. Also clears the value.
5418 */
5419 static void
5420listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 list_T *l;
5422 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005423{
5424 list_remove(l, item, item);
5425 listitem_free(item);
5426}
5427
5428/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 * Get the number of items in a list.
5430 */
5431 static long
5432list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005433 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 if (l == NULL)
5436 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005437 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438}
5439
5440/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005441 * Return TRUE when two lists have exactly the same values.
5442 */
5443 static int
5444list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005445 list_T *l1;
5446 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005447 int ic; /* ignore case for strings */
5448{
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005450
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005451 if (list_len(l1) != list_len(l2))
5452 return FALSE;
5453
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005454 for (item1 = l1->lv_first, item2 = l2->lv_first;
5455 item1 != NULL && item2 != NULL;
5456 item1 = item1->li_next, item2 = item2->li_next)
5457 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5458 return FALSE;
5459 return item1 == NULL && item2 == NULL;
5460}
5461
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005462#if defined(FEAT_PYTHON) || defined(PROTO)
5463/*
5464 * Return the dictitem that an entry in a hashtable points to.
5465 */
5466 dictitem_T *
5467dict_lookup(hi)
5468 hashitem_T *hi;
5469{
5470 return HI2DI(hi);
5471}
5472#endif
5473
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005474/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005475 * Return TRUE when two dictionaries have exactly the same key/values.
5476 */
5477 static int
5478dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005479 dict_T *d1;
5480 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005481 int ic; /* ignore case for strings */
5482{
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 hashitem_T *hi;
5484 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005485 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005486
5487 if (dict_len(d1) != dict_len(d2))
5488 return FALSE;
5489
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005490 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005492 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005493 if (!HASHITEM_EMPTY(hi))
5494 {
5495 item2 = dict_find(d2, hi->hi_key, -1);
5496 if (item2 == NULL)
5497 return FALSE;
5498 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5499 return FALSE;
5500 --todo;
5501 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005502 }
5503 return TRUE;
5504}
5505
5506/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005507 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005508 * Compares the items just like "==" would compare them, but strings and
5509 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005510 */
5511 static int
5512tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005513 typval_T *tv1;
5514 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005515 int ic; /* ignore case */
5516{
5517 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005518 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005519
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005520 if (tv1->v_type != tv2->v_type)
5521 return FALSE;
5522
5523 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005524 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005525 case VAR_LIST:
5526 /* recursive! */
5527 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5528
5529 case VAR_DICT:
5530 /* recursive! */
5531 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5532
5533 case VAR_FUNC:
5534 return (tv1->vval.v_string != NULL
5535 && tv2->vval.v_string != NULL
5536 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5537
5538 case VAR_NUMBER:
5539 return tv1->vval.v_number == tv2->vval.v_number;
5540
5541 case VAR_STRING:
5542 s1 = get_tv_string_buf(tv1, buf1);
5543 s2 = get_tv_string_buf(tv2, buf2);
5544 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005545 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005546
5547 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548 return TRUE;
5549}
5550
5551/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 * Locate item with index "n" in list "l" and return it.
5553 * A negative index is counted from the end; -1 is the last item.
5554 * Returns NULL when "n" is out of range.
5555 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005556 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005558 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 long n;
5560{
Bram Moolenaar33570922005-01-25 22:26:29 +00005561 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 long idx;
5563
5564 if (l == NULL)
5565 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005566
5567 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005569 n = l->lv_len + n;
5570
5571 /* Check for index out of range. */
5572 if (n < 0 || n >= l->lv_len)
5573 return NULL;
5574
5575 /* When there is a cached index may start search from there. */
5576 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005578 if (n < l->lv_idx / 2)
5579 {
5580 /* closest to the start of the list */
5581 item = l->lv_first;
5582 idx = 0;
5583 }
5584 else if (n > (l->lv_idx + l->lv_len) / 2)
5585 {
5586 /* closest to the end of the list */
5587 item = l->lv_last;
5588 idx = l->lv_len - 1;
5589 }
5590 else
5591 {
5592 /* closest to the cached index */
5593 item = l->lv_idx_item;
5594 idx = l->lv_idx;
5595 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 }
5597 else
5598 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005599 if (n < l->lv_len / 2)
5600 {
5601 /* closest to the start of the list */
5602 item = l->lv_first;
5603 idx = 0;
5604 }
5605 else
5606 {
5607 /* closest to the end of the list */
5608 item = l->lv_last;
5609 idx = l->lv_len - 1;
5610 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005612
5613 while (n > idx)
5614 {
5615 /* search forward */
5616 item = item->li_next;
5617 ++idx;
5618 }
5619 while (n < idx)
5620 {
5621 /* search backward */
5622 item = item->li_prev;
5623 --idx;
5624 }
5625
5626 /* cache the used index */
5627 l->lv_idx = idx;
5628 l->lv_idx_item = item;
5629
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 return item;
5631}
5632
5633/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005634 * Get list item "l[idx]" as a number.
5635 */
5636 static long
5637list_find_nr(l, idx, errorp)
5638 list_T *l;
5639 long idx;
5640 int *errorp; /* set to TRUE when something wrong */
5641{
5642 listitem_T *li;
5643
5644 li = list_find(l, idx);
5645 if (li == NULL)
5646 {
5647 if (errorp != NULL)
5648 *errorp = TRUE;
5649 return -1L;
5650 }
5651 return get_tv_number_chk(&li->li_tv, errorp);
5652}
5653
5654/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005655 * Locate "item" list "l" and return its index.
5656 * Returns -1 when "item" is not in the list.
5657 */
5658 static long
5659list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005660 list_T *l;
5661 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005662{
5663 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005664 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005665
5666 if (l == NULL)
5667 return -1;
5668 idx = 0;
5669 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5670 ++idx;
5671 if (li == NULL)
5672 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005673 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005674}
5675
5676/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 * Append item "item" to the end of list "l".
5678 */
5679 static void
5680list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005681 list_T *l;
5682 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683{
5684 if (l->lv_last == NULL)
5685 {
5686 /* empty list */
5687 l->lv_first = item;
5688 l->lv_last = item;
5689 item->li_prev = NULL;
5690 }
5691 else
5692 {
5693 l->lv_last->li_next = item;
5694 item->li_prev = l->lv_last;
5695 l->lv_last = item;
5696 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005697 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 item->li_next = NULL;
5699}
5700
5701/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005703 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 */
5705 static int
5706list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005707 list_T *l;
5708 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005710 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005711
Bram Moolenaar05159a02005-02-26 23:04:13 +00005712 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005714 copy_tv(tv, &li->li_tv);
5715 list_append(l, li);
5716 return OK;
5717}
5718
5719/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005720 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005721 * Return FAIL when out of memory.
5722 */
5723 int
5724list_append_dict(list, dict)
5725 list_T *list;
5726 dict_T *dict;
5727{
5728 listitem_T *li = listitem_alloc();
5729
5730 if (li == NULL)
5731 return FAIL;
5732 li->li_tv.v_type = VAR_DICT;
5733 li->li_tv.v_lock = 0;
5734 li->li_tv.vval.v_dict = dict;
5735 list_append(list, li);
5736 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 return OK;
5738}
5739
5740/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005741 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005742 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005743 * Returns FAIL when out of memory.
5744 */
5745 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005746list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005747 list_T *l;
5748 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005749 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005750{
5751 listitem_T *li = listitem_alloc();
5752
5753 if (li == NULL)
5754 return FAIL;
5755 list_append(l, li);
5756 li->li_tv.v_type = VAR_STRING;
5757 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005758 if (str == NULL)
5759 li->li_tv.vval.v_string = NULL;
5760 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005761 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005762 return FAIL;
5763 return OK;
5764}
5765
5766/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005767 * Append "n" to list "l".
5768 * Returns FAIL when out of memory.
5769 */
5770 static int
5771list_append_number(l, n)
5772 list_T *l;
5773 varnumber_T n;
5774{
5775 listitem_T *li;
5776
5777 li = listitem_alloc();
5778 if (li == NULL)
5779 return FAIL;
5780 li->li_tv.v_type = VAR_NUMBER;
5781 li->li_tv.v_lock = 0;
5782 li->li_tv.vval.v_number = n;
5783 list_append(l, li);
5784 return OK;
5785}
5786
5787/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005789 * If "item" is NULL append at the end.
5790 * Return FAIL when out of memory.
5791 */
5792 static int
5793list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005794 list_T *l;
5795 typval_T *tv;
5796 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005797{
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005799
5800 if (ni == NULL)
5801 return FAIL;
5802 copy_tv(tv, &ni->li_tv);
5803 if (item == NULL)
5804 /* Append new item at end of list. */
5805 list_append(l, ni);
5806 else
5807 {
5808 /* Insert new item before existing item. */
5809 ni->li_prev = item->li_prev;
5810 ni->li_next = item;
5811 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005812 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005813 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005814 ++l->lv_idx;
5815 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005816 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005817 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005818 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005819 l->lv_idx_item = NULL;
5820 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005821 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005822 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005823 }
5824 return OK;
5825}
5826
5827/*
5828 * Extend "l1" with "l2".
5829 * If "bef" is NULL append at the end, otherwise insert before this item.
5830 * Returns FAIL when out of memory.
5831 */
5832 static int
5833list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 list_T *l1;
5835 list_T *l2;
5836 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005837{
Bram Moolenaar33570922005-01-25 22:26:29 +00005838 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005839
5840 for (item = l2->lv_first; item != NULL; item = item->li_next)
5841 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5842 return FAIL;
5843 return OK;
5844}
5845
5846/*
5847 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5848 * Return FAIL when out of memory.
5849 */
5850 static int
5851list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005852 list_T *l1;
5853 list_T *l2;
5854 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005855{
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005857
5858 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005859 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005860 if (l == NULL)
5861 return FAIL;
5862 tv->v_type = VAR_LIST;
5863 tv->vval.v_list = l;
5864
5865 /* append all items from the second list */
5866 return list_extend(l, l2, NULL);
5867}
5868
5869/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005870 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005872 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 * Returns NULL when out of memory.
5874 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005876list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880{
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 list_T *copy;
5882 listitem_T *item;
5883 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884
5885 if (orig == NULL)
5886 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887
5888 copy = list_alloc();
5889 if (copy != NULL)
5890 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005891 if (copyID != 0)
5892 {
5893 /* Do this before adding the items, because one of the items may
5894 * refer back to this list. */
5895 orig->lv_copyID = copyID;
5896 orig->lv_copylist = copy;
5897 }
5898 for (item = orig->lv_first; item != NULL && !got_int;
5899 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 {
5901 ni = listitem_alloc();
5902 if (ni == NULL)
5903 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005904 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005905 {
5906 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5907 {
5908 vim_free(ni);
5909 break;
5910 }
5911 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005913 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 list_append(copy, ni);
5915 }
5916 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005917 if (item != NULL)
5918 {
5919 list_unref(copy);
5920 copy = NULL;
5921 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 }
5923
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 return copy;
5925}
5926
5927/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005929 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005932list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 list_T *l;
5934 listitem_T *item;
5935 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005936{
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005939 /* notify watchers */
5940 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005942 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005943 list_fix_watch(l, ip);
5944 if (ip == item2)
5945 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005947
5948 if (item2->li_next == NULL)
5949 l->lv_last = item->li_prev;
5950 else
5951 item2->li_next->li_prev = item->li_prev;
5952 if (item->li_prev == NULL)
5953 l->lv_first = item2->li_next;
5954 else
5955 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005956 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957}
5958
5959/*
5960 * Return an allocated string with the string representation of a list.
5961 * May return NULL.
5962 */
5963 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005964list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005966 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967{
5968 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969
5970 if (tv->vval.v_list == NULL)
5971 return NULL;
5972 ga_init2(&ga, (int)sizeof(char), 80);
5973 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005974 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 {
5976 vim_free(ga.ga_data);
5977 return NULL;
5978 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 ga_append(&ga, ']');
5980 ga_append(&ga, NUL);
5981 return (char_u *)ga.ga_data;
5982}
5983
5984/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 * Join list "l" into a string in "*gap", using separator "sep".
5986 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005987 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005988 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005989 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005990list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 char_u *sep;
5994 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005995 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996{
5997 int first = TRUE;
5998 char_u *tofree;
5999 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006001 char_u *s;
6002
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006003 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006004 {
6005 if (first)
6006 first = FALSE;
6007 else
6008 ga_concat(gap, sep);
6009
6010 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006011 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006012 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006013 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006014 if (s != NULL)
6015 ga_concat(gap, s);
6016 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006017 if (s == NULL)
6018 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006019 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006020 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006021}
6022
6023/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024 * Garbage collection for lists and dictionaries.
6025 *
6026 * We use reference counts to be able to free most items right away when they
6027 * are no longer used. But for composite items it's possible that it becomes
6028 * unused while the reference count is > 0: When there is a recursive
6029 * reference. Example:
6030 * :let l = [1, 2, 3]
6031 * :let d = {9: l}
6032 * :let l[1] = d
6033 *
6034 * Since this is quite unusual we handle this with garbage collection: every
6035 * once in a while find out which lists and dicts are not referenced from any
6036 * variable.
6037 *
6038 * Here is a good reference text about garbage collection (refers to Python
6039 * but it applies to all reference-counting mechanisms):
6040 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042
6043/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006044 * Do garbage collection for lists and dicts.
6045 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006046 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 int
6048garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006049{
6050 dict_T *dd;
6051 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006052 int copyID = ++current_copyID;
6053 buf_T *buf;
6054 win_T *wp;
6055 int i;
6056 funccall_T *fc;
6057 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006058#ifdef FEAT_WINDOWS
6059 tabpage_T *tp;
6060#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006061
6062 /*
6063 * 1. Go through all accessible variables and mark all lists and dicts
6064 * with copyID.
6065 */
6066 /* script-local variables */
6067 for (i = 1; i <= ga_scripts.ga_len; ++i)
6068 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6069
6070 /* buffer-local variables */
6071 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6072 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6073
6074 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006075 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006076 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6077
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006078#ifdef FEAT_WINDOWS
6079 /* tabpage-local variables */
6080 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6081 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6082#endif
6083
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006084 /* global variables */
6085 set_ref_in_ht(&globvarht, copyID);
6086
6087 /* function-local variables */
6088 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6089 {
6090 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6091 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6092 }
6093
6094 /*
6095 * 2. Go through the list of dicts and free items without the copyID.
6096 */
6097 for (dd = first_dict; dd != NULL; )
6098 if (dd->dv_copyID != copyID)
6099 {
6100 dict_free(dd);
6101 did_free = TRUE;
6102
6103 /* restart, next dict may also have been freed */
6104 dd = first_dict;
6105 }
6106 else
6107 dd = dd->dv_used_next;
6108
6109 /*
6110 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006111 * But don't free a list that has a watcher (used in a for loop), these
6112 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 */
6114 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006115 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116 {
6117 list_free(ll);
6118 did_free = TRUE;
6119
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006120 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 ll = first_list;
6122 }
6123 else
6124 ll = ll->lv_used_next;
6125
6126 return did_free;
6127}
6128
6129/*
6130 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6131 */
6132 static void
6133set_ref_in_ht(ht, copyID)
6134 hashtab_T *ht;
6135 int copyID;
6136{
6137 int todo;
6138 hashitem_T *hi;
6139
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006140 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006141 for (hi = ht->ht_array; todo > 0; ++hi)
6142 if (!HASHITEM_EMPTY(hi))
6143 {
6144 --todo;
6145 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6146 }
6147}
6148
6149/*
6150 * Mark all lists and dicts referenced through list "l" with "copyID".
6151 */
6152 static void
6153set_ref_in_list(l, copyID)
6154 list_T *l;
6155 int copyID;
6156{
6157 listitem_T *li;
6158
6159 for (li = l->lv_first; li != NULL; li = li->li_next)
6160 set_ref_in_item(&li->li_tv, copyID);
6161}
6162
6163/*
6164 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6165 */
6166 static void
6167set_ref_in_item(tv, copyID)
6168 typval_T *tv;
6169 int copyID;
6170{
6171 dict_T *dd;
6172 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006173
6174 switch (tv->v_type)
6175 {
6176 case VAR_DICT:
6177 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006178 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006179 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006180 /* Didn't see this dict yet. */
6181 dd->dv_copyID = copyID;
6182 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006183 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006184 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006185
6186 case VAR_LIST:
6187 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006188 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006189 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006190 /* Didn't see this list yet. */
6191 ll->lv_copyID = copyID;
6192 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006193 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006195 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006196 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006197}
6198
6199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006200 * Allocate an empty header for a dictionary.
6201 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006202 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006203dict_alloc()
6204{
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006206
Bram Moolenaar33570922005-01-25 22:26:29 +00006207 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006208 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006209 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006210 /* Add the list to the hashtable for garbage collection. */
6211 if (first_dict != NULL)
6212 first_dict->dv_used_prev = d;
6213 d->dv_used_next = first_dict;
6214 d->dv_used_prev = NULL;
6215
Bram Moolenaar33570922005-01-25 22:26:29 +00006216 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006217 d->dv_lock = 0;
6218 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006219 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006222}
6223
6224/*
6225 * Unreference a Dictionary: decrement the reference count and free it when it
6226 * becomes zero.
6227 */
6228 static void
6229dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006231{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006232 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6233 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006234}
6235
6236/*
6237 * Free a Dictionary, including all items it contains.
6238 * Ignores the reference count.
6239 */
6240 static void
6241dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006242 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006243{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006244 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006246 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006247
Bram Moolenaard9fba312005-06-26 22:34:35 +00006248 /* Avoid that recursive reference to the dict frees us again. */
6249 d->dv_refcount = DEL_REFCOUNT;
6250
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006251 /* Remove the dict from the list of dicts for garbage collection. */
6252 if (d->dv_used_prev == NULL)
6253 first_dict = d->dv_used_next;
6254 else
6255 d->dv_used_prev->dv_used_next = d->dv_used_next;
6256 if (d->dv_used_next != NULL)
6257 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6258
6259 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006260 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006261 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006264 if (!HASHITEM_EMPTY(hi))
6265 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006266 /* Remove the item before deleting it, just in case there is
6267 * something recursive causing trouble. */
6268 di = HI2DI(hi);
6269 hash_remove(&d->dv_hashtab, hi);
6270 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006271 --todo;
6272 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006273 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006275 vim_free(d);
6276}
6277
6278/*
6279 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006280 * The "key" is copied to the new item.
6281 * Note that the value of the item "di_tv" still needs to be initialized!
6282 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285dictitem_alloc(key)
6286 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006287{
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006290 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006291 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006293 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006294 di->di_flags = 0;
6295 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006297}
6298
6299/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 * Make a copy of a Dictionary item.
6301 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305{
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006308 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6309 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006310 if (di != NULL)
6311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006312 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006313 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006314 copy_tv(&org->di_tv, &di->di_tv);
6315 }
6316 return di;
6317}
6318
6319/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320 * Remove item "item" from Dictionary "dict" and free it.
6321 */
6322 static void
6323dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006324 dict_T *dict;
6325 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326{
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330 if (HASHITEM_EMPTY(hi))
6331 EMSG2(_(e_intern2), "dictitem_remove()");
6332 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334 dictitem_free(item);
6335}
6336
6337/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 * Free a dict item. Also clears the value.
6339 */
6340 static void
6341dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006343{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344 clear_tv(&item->di_tv);
6345 vim_free(item);
6346}
6347
6348/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006349 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6350 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 * Returns NULL when out of memory.
6353 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006355dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359{
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 dict_T *copy;
6361 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006362 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006364
6365 if (orig == NULL)
6366 return NULL;
6367
6368 copy = dict_alloc();
6369 if (copy != NULL)
6370 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006371 if (copyID != 0)
6372 {
6373 orig->dv_copyID = copyID;
6374 orig->dv_copydict = copy;
6375 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006376 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006377 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006380 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006381 --todo;
6382
6383 di = dictitem_alloc(hi->hi_key);
6384 if (di == NULL)
6385 break;
6386 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006387 {
6388 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6389 copyID) == FAIL)
6390 {
6391 vim_free(di);
6392 break;
6393 }
6394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006395 else
6396 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6397 if (dict_add(copy, di) == FAIL)
6398 {
6399 dictitem_free(di);
6400 break;
6401 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006403 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404
Bram Moolenaare9a41262005-01-15 22:18:47 +00006405 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006406 if (todo > 0)
6407 {
6408 dict_unref(copy);
6409 copy = NULL;
6410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006411 }
6412
6413 return copy;
6414}
6415
6416/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006418 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006419 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006420 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 dict_T *d;
6423 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424{
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426}
6427
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006429 * Add a number or string entry to dictionary "d".
6430 * When "str" is NULL use number "nr", otherwise use "str".
6431 * Returns FAIL when out of memory and when key already exists.
6432 */
6433 int
6434dict_add_nr_str(d, key, nr, str)
6435 dict_T *d;
6436 char *key;
6437 long nr;
6438 char_u *str;
6439{
6440 dictitem_T *item;
6441
6442 item = dictitem_alloc((char_u *)key);
6443 if (item == NULL)
6444 return FAIL;
6445 item->di_tv.v_lock = 0;
6446 if (str == NULL)
6447 {
6448 item->di_tv.v_type = VAR_NUMBER;
6449 item->di_tv.vval.v_number = nr;
6450 }
6451 else
6452 {
6453 item->di_tv.v_type = VAR_STRING;
6454 item->di_tv.vval.v_string = vim_strsave(str);
6455 }
6456 if (dict_add(d, item) == FAIL)
6457 {
6458 dictitem_free(item);
6459 return FAIL;
6460 }
6461 return OK;
6462}
6463
6464/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006465 * Get the number of items in a Dictionary.
6466 */
6467 static long
6468dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006470{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006471 if (d == NULL)
6472 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006473 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474}
6475
6476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477 * Find item "key[len]" in Dictionary "d".
6478 * If "len" is negative use strlen(key).
6479 * Returns NULL when not found.
6480 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006481 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006482dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006483 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 char_u *key;
6485 int len;
6486{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006487#define AKEYLEN 200
6488 char_u buf[AKEYLEN];
6489 char_u *akey;
6490 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006492
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006493 if (len < 0)
6494 akey = key;
6495 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006496 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006497 tofree = akey = vim_strnsave(key, len);
6498 if (akey == NULL)
6499 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006500 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 else
6502 {
6503 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006504 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006505 akey = buf;
6506 }
6507
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006509 vim_free(tofree);
6510 if (HASHITEM_EMPTY(hi))
6511 return NULL;
6512 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006513}
6514
6515/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006516 * Get a string item from a dictionary.
6517 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006518 * Returns NULL if the entry doesn't exist or out of memory.
6519 */
6520 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006521get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006522 dict_T *d;
6523 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006524 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006525{
6526 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006527 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006528
6529 di = dict_find(d, key, -1);
6530 if (di == NULL)
6531 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006532 s = get_tv_string(&di->di_tv);
6533 if (save && s != NULL)
6534 s = vim_strsave(s);
6535 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006536}
6537
6538/*
6539 * Get a number item from a dictionary.
6540 * Returns 0 if the entry doesn't exist or out of memory.
6541 */
6542 long
6543get_dict_number(d, key)
6544 dict_T *d;
6545 char_u *key;
6546{
6547 dictitem_T *di;
6548
6549 di = dict_find(d, key, -1);
6550 if (di == NULL)
6551 return 0;
6552 return get_tv_number(&di->di_tv);
6553}
6554
6555/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556 * Return an allocated string with the string representation of a Dictionary.
6557 * May return NULL.
6558 */
6559 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006560dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006562 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563{
6564 garray_T ga;
6565 int first = TRUE;
6566 char_u *tofree;
6567 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006569 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006570 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006571 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006573 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006574 return NULL;
6575 ga_init2(&ga, (int)sizeof(char), 80);
6576 ga_append(&ga, '{');
6577
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006578 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006579 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006581 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006583 --todo;
6584
6585 if (first)
6586 first = FALSE;
6587 else
6588 ga_concat(&ga, (char_u *)", ");
6589
6590 tofree = string_quote(hi->hi_key, FALSE);
6591 if (tofree != NULL)
6592 {
6593 ga_concat(&ga, tofree);
6594 vim_free(tofree);
6595 }
6596 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006597 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006598 if (s != NULL)
6599 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006600 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 if (s == NULL)
6602 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006604 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605 if (todo > 0)
6606 {
6607 vim_free(ga.ga_data);
6608 return NULL;
6609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006610
6611 ga_append(&ga, '}');
6612 ga_append(&ga, NUL);
6613 return (char_u *)ga.ga_data;
6614}
6615
6616/*
6617 * Allocate a variable for a Dictionary and fill it from "*arg".
6618 * Return OK or FAIL. Returns NOTDONE for {expr}.
6619 */
6620 static int
6621get_dict_tv(arg, rettv, evaluate)
6622 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006624 int evaluate;
6625{
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 dict_T *d = NULL;
6627 typval_T tvkey;
6628 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006630 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006631 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006632 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006633
6634 /*
6635 * First check if it's not a curly-braces thing: {expr}.
6636 * Must do this without evaluating, otherwise a function may be called
6637 * twice. Unfortunately this means we need to call eval1() twice for the
6638 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006640 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006641 if (*start != '}')
6642 {
6643 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6644 return FAIL;
6645 if (*start == '}')
6646 return NOTDONE;
6647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648
6649 if (evaluate)
6650 {
6651 d = dict_alloc();
6652 if (d == NULL)
6653 return FAIL;
6654 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006655 tvkey.v_type = VAR_UNKNOWN;
6656 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006657
6658 *arg = skipwhite(*arg + 1);
6659 while (**arg != '}' && **arg != NUL)
6660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006662 goto failret;
6663 if (**arg != ':')
6664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006665 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006666 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006667 goto failret;
6668 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006669 key = get_tv_string_buf_chk(&tvkey, buf);
6670 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006672 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6673 if (key != NULL)
6674 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006675 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006676 goto failret;
6677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678
6679 *arg = skipwhite(*arg + 1);
6680 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6681 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006682 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 goto failret;
6684 }
6685 if (evaluate)
6686 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006687 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006688 if (item != NULL)
6689 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006690 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006691 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692 clear_tv(&tv);
6693 goto failret;
6694 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006695 item = dictitem_alloc(key);
6696 clear_tv(&tvkey);
6697 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006699 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006700 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006701 if (dict_add(d, item) == FAIL)
6702 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006703 }
6704 }
6705
6706 if (**arg == '}')
6707 break;
6708 if (**arg != ',')
6709 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006710 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711 goto failret;
6712 }
6713 *arg = skipwhite(*arg + 1);
6714 }
6715
6716 if (**arg != '}')
6717 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006718 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719failret:
6720 if (evaluate)
6721 dict_free(d);
6722 return FAIL;
6723 }
6724
6725 *arg = skipwhite(*arg + 1);
6726 if (evaluate)
6727 {
6728 rettv->v_type = VAR_DICT;
6729 rettv->vval.v_dict = d;
6730 ++d->dv_refcount;
6731 }
6732
6733 return OK;
6734}
6735
Bram Moolenaar8c711452005-01-14 21:53:12 +00006736/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006737 * Return a string with the string representation of a variable.
6738 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006739 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006740 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006741 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006742 * May return NULL;
6743 */
6744 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006745echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006747 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006748 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006749 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006751 static int recurse = 0;
6752 char_u *r = NULL;
6753
Bram Moolenaar33570922005-01-25 22:26:29 +00006754 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006755 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006756 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006757 *tofree = NULL;
6758 return NULL;
6759 }
6760 ++recurse;
6761
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762 switch (tv->v_type)
6763 {
6764 case VAR_FUNC:
6765 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006766 r = tv->vval.v_string;
6767 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006768
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006769 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006770 if (tv->vval.v_list == NULL)
6771 {
6772 *tofree = NULL;
6773 r = NULL;
6774 }
6775 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6776 {
6777 *tofree = NULL;
6778 r = (char_u *)"[...]";
6779 }
6780 else
6781 {
6782 tv->vval.v_list->lv_copyID = copyID;
6783 *tofree = list2string(tv, copyID);
6784 r = *tofree;
6785 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006786 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006787
Bram Moolenaar8c711452005-01-14 21:53:12 +00006788 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006789 if (tv->vval.v_dict == NULL)
6790 {
6791 *tofree = NULL;
6792 r = NULL;
6793 }
6794 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6795 {
6796 *tofree = NULL;
6797 r = (char_u *)"{...}";
6798 }
6799 else
6800 {
6801 tv->vval.v_dict->dv_copyID = copyID;
6802 *tofree = dict2string(tv, copyID);
6803 r = *tofree;
6804 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006806
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 case VAR_STRING:
6808 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 *tofree = NULL;
6810 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006811 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006813 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006815 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006817
6818 --recurse;
6819 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820}
6821
6822/*
6823 * Return a string with the string representation of a variable.
6824 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6825 * "numbuf" is used for a number.
6826 * Puts quotes around strings, so that they can be parsed back by eval().
6827 * May return NULL;
6828 */
6829 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006830tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006831 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 char_u **tofree;
6833 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006834 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835{
6836 switch (tv->v_type)
6837 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006838 case VAR_FUNC:
6839 *tofree = string_quote(tv->vval.v_string, TRUE);
6840 return *tofree;
6841 case VAR_STRING:
6842 *tofree = string_quote(tv->vval.v_string, FALSE);
6843 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006845 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006846 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006847 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006848 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006849 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006850 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006851 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006852}
6853
6854/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 * Return string "str" in ' quotes, doubling ' characters.
6856 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006857 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006858 */
6859 static char_u *
6860string_quote(str, function)
6861 char_u *str;
6862 int function;
6863{
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006865 char_u *p, *r, *s;
6866
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 len = (function ? 13 : 3);
6868 if (str != NULL)
6869 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006870 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006871 for (p = str; *p != NUL; mb_ptr_adv(p))
6872 if (*p == '\'')
6873 ++len;
6874 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875 s = r = alloc(len);
6876 if (r != NULL)
6877 {
6878 if (function)
6879 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006881 r += 10;
6882 }
6883 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006884 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006885 if (str != NULL)
6886 for (p = str; *p != NUL; )
6887 {
6888 if (*p == '\'')
6889 *r++ = '\'';
6890 MB_COPY_CHAR(p, r);
6891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006892 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006893 if (function)
6894 *r++ = ')';
6895 *r++ = NUL;
6896 }
6897 return s;
6898}
6899
6900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 * Get the value of an environment variable.
6902 * "arg" is pointing to the '$'. It is advanced to after the name.
6903 * If the environment variable was not set, silently assume it is empty.
6904 * Always return OK.
6905 */
6906 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006907get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 int evaluate;
6911{
6912 char_u *string = NULL;
6913 int len;
6914 int cc;
6915 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006916 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917
6918 ++*arg;
6919 name = *arg;
6920 len = get_env_len(arg);
6921 if (evaluate)
6922 {
6923 if (len != 0)
6924 {
6925 cc = name[len];
6926 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006927 /* first try vim_getenv(), fast for normal environment vars */
6928 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006930 {
6931 if (!mustfree)
6932 string = vim_strsave(string);
6933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 else
6935 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006936 if (mustfree)
6937 vim_free(string);
6938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 /* next try expanding things like $VIM and ${HOME} */
6940 string = expand_env_save(name - 1);
6941 if (string != NULL && *string == '$')
6942 {
6943 vim_free(string);
6944 string = NULL;
6945 }
6946 }
6947 name[len] = cc;
6948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006949 rettv->v_type = VAR_STRING;
6950 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952
6953 return OK;
6954}
6955
6956/*
6957 * Array with names and number of arguments of all internal functions
6958 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6959 */
6960static struct fst
6961{
6962 char *f_name; /* function name */
6963 char f_min_argc; /* minimal number of arguments */
6964 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006965 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006966 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967} functions[] =
6968{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006969 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {"append", 2, 2, f_append},
6971 {"argc", 0, 0, f_argc},
6972 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006973 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006975 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {"bufexists", 1, 1, f_bufexists},
6977 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6978 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6979 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6980 {"buflisted", 1, 1, f_buflisted},
6981 {"bufloaded", 1, 1, f_bufloaded},
6982 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006983 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {"bufwinnr", 1, 1, f_bufwinnr},
6985 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006986 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006988 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {"char2nr", 1, 1, f_char2nr},
6990 {"cindent", 1, 1, f_cindent},
6991 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006992#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006993 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006994 {"complete_add", 1, 1, f_complete_add},
6995 {"complete_check", 0, 0, f_complete_check},
6996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006998 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006999 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007001 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007002 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"delete", 1, 1, f_delete},
7004 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007005 {"diff_filler", 1, 1, f_diff_filler},
7006 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007007 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007009 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {"eventhandler", 0, 0, f_eventhandler},
7011 {"executable", 1, 1, f_executable},
7012 {"exists", 1, 1, f_exists},
7013 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007014 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007015 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7017 {"filereadable", 1, 1, f_filereadable},
7018 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007019 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007020 {"finddir", 1, 3, f_finddir},
7021 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {"fnamemodify", 2, 2, f_fnamemodify},
7023 {"foldclosed", 1, 1, f_foldclosed},
7024 {"foldclosedend", 1, 1, f_foldclosedend},
7025 {"foldlevel", 1, 1, f_foldlevel},
7026 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007027 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007029 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007031 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007032 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {"getbufvar", 2, 2, f_getbufvar},
7034 {"getchar", 0, 1, f_getchar},
7035 {"getcharmod", 0, 0, f_getcharmod},
7036 {"getcmdline", 0, 0, f_getcmdline},
7037 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007038 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007040 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007041 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"getfsize", 1, 1, f_getfsize},
7043 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007044 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007045 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007046 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007047 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007048 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007049 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007051 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"getwinposx", 0, 0, f_getwinposx},
7053 {"getwinposy", 0, 0, f_getwinposy},
7054 {"getwinvar", 2, 2, f_getwinvar},
7055 {"glob", 1, 1, f_glob},
7056 {"globpath", 2, 2, f_globpath},
7057 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007058 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007059 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7061 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7062 {"histadd", 2, 2, f_histadd},
7063 {"histdel", 1, 2, f_histdel},
7064 {"histget", 1, 2, f_histget},
7065 {"histnr", 1, 1, f_histnr},
7066 {"hlID", 1, 1, f_hlID},
7067 {"hlexists", 1, 1, f_hlexists},
7068 {"hostname", 0, 0, f_hostname},
7069 {"iconv", 3, 3, f_iconv},
7070 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007071 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007072 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007074 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {"inputrestore", 0, 0, f_inputrestore},
7076 {"inputsave", 0, 0, f_inputsave},
7077 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007078 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007080 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007082 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"libcall", 3, 3, f_libcall},
7087 {"libcallnr", 3, 3, f_libcallnr},
7088 {"line", 1, 1, f_line},
7089 {"line2byte", 1, 1, f_line2byte},
7090 {"lispindent", 1, 1, f_lispindent},
7091 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007092 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007093 {"maparg", 1, 3, f_maparg},
7094 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007095 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007096 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007097 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007098 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007099 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007100 {"max", 1, 1, f_max},
7101 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007102#ifdef vim_mkdir
7103 {"mkdir", 1, 3, f_mkdir},
7104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {"mode", 0, 0, f_mode},
7106 {"nextnonblank", 1, 1, f_nextnonblank},
7107 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007108 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007110 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007111 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007113 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007114 {"reltime", 0, 2, f_reltime},
7115 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"remote_expr", 2, 3, f_remote_expr},
7117 {"remote_foreground", 1, 1, f_remote_foreground},
7118 {"remote_peek", 1, 2, f_remote_peek},
7119 {"remote_read", 1, 1, f_remote_read},
7120 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007121 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007123 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007125 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007126 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007127 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007128 {"searchpair", 3, 6, f_searchpair},
7129 {"searchpairpos", 3, 6, f_searchpairpos},
7130 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {"server2client", 2, 2, f_server2client},
7132 {"serverlist", 0, 0, f_serverlist},
7133 {"setbufvar", 3, 3, f_setbufvar},
7134 {"setcmdpos", 1, 1, f_setcmdpos},
7135 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007136 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007137 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007138 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007140 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {"setwinvar", 3, 3, f_setwinvar},
7142 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007143 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007144 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007145 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007146 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007147 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007148 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#ifdef HAVE_STRFTIME
7150 {"strftime", 1, 2, f_strftime},
7151#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007153 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {"strlen", 1, 1, f_strlen},
7155 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007156 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {"strtrans", 1, 1, f_strtrans},
7158 {"submatch", 1, 1, f_submatch},
7159 {"substitute", 4, 4, f_substitute},
7160 {"synID", 3, 3, f_synID},
7161 {"synIDattr", 2, 3, f_synIDattr},
7162 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007163 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007165 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007167 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007168 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007170 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {"tolower", 1, 1, f_tolower},
7172 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007173 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {"virtcol", 1, 1, f_virtcol},
7177 {"visualmode", 0, 1, f_visualmode},
7178 {"winbufnr", 1, 1, f_winbufnr},
7179 {"wincol", 0, 0, f_wincol},
7180 {"winheight", 1, 1, f_winheight},
7181 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007182 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007184 {"winrestview", 1, 1, f_winrestview},
7185 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007187 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188};
7189
7190#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7191
7192/*
7193 * Function given to ExpandGeneric() to obtain the list of internal
7194 * or user defined function names.
7195 */
7196 char_u *
7197get_function_name(xp, idx)
7198 expand_T *xp;
7199 int idx;
7200{
7201 static int intidx = -1;
7202 char_u *name;
7203
7204 if (idx == 0)
7205 intidx = -1;
7206 if (intidx < 0)
7207 {
7208 name = get_user_func_name(xp, idx);
7209 if (name != NULL)
7210 return name;
7211 }
7212 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7213 {
7214 STRCPY(IObuff, functions[intidx].f_name);
7215 STRCAT(IObuff, "(");
7216 if (functions[intidx].f_max_argc == 0)
7217 STRCAT(IObuff, ")");
7218 return IObuff;
7219 }
7220
7221 return NULL;
7222}
7223
7224/*
7225 * Function given to ExpandGeneric() to obtain the list of internal or
7226 * user defined variable or function names.
7227 */
7228/*ARGSUSED*/
7229 char_u *
7230get_expr_name(xp, idx)
7231 expand_T *xp;
7232 int idx;
7233{
7234 static int intidx = -1;
7235 char_u *name;
7236
7237 if (idx == 0)
7238 intidx = -1;
7239 if (intidx < 0)
7240 {
7241 name = get_function_name(xp, idx);
7242 if (name != NULL)
7243 return name;
7244 }
7245 return get_user_var_name(xp, ++intidx);
7246}
7247
7248#endif /* FEAT_CMDL_COMPL */
7249
7250/*
7251 * Find internal function in table above.
7252 * Return index, or -1 if not found
7253 */
7254 static int
7255find_internal_func(name)
7256 char_u *name; /* name of the function */
7257{
7258 int first = 0;
7259 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7260 int cmp;
7261 int x;
7262
7263 /*
7264 * Find the function name in the table. Binary search.
7265 */
7266 while (first <= last)
7267 {
7268 x = first + ((unsigned)(last - first) >> 1);
7269 cmp = STRCMP(name, functions[x].f_name);
7270 if (cmp < 0)
7271 last = x - 1;
7272 else if (cmp > 0)
7273 first = x + 1;
7274 else
7275 return x;
7276 }
7277 return -1;
7278}
7279
7280/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007281 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7282 * name it contains, otherwise return "name".
7283 */
7284 static char_u *
7285deref_func_name(name, lenp)
7286 char_u *name;
7287 int *lenp;
7288{
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007290 int cc;
7291
7292 cc = name[*lenp];
7293 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007294 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007295 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007299 {
7300 *lenp = 0;
7301 return (char_u *)""; /* just in case */
7302 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007305 }
7306
7307 return name;
7308}
7309
7310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 * Allocate a variable for the result of a function.
7312 * Return OK or FAIL.
7313 */
7314 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7316 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 char_u *name; /* name of the function */
7318 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 char_u **arg; /* argument, pointing to the '(' */
7321 linenr_T firstline; /* first line of range */
7322 linenr_T lastline; /* last line of range */
7323 int *doesrange; /* return: function handled range */
7324 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
7327 char_u *argp;
7328 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007329 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 int argcount = 0; /* number of arguments found */
7331
7332 /*
7333 * Get the arguments.
7334 */
7335 argp = *arg;
7336 while (argcount < MAX_FUNC_ARGS)
7337 {
7338 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7339 if (*argp == ')' || *argp == ',' || *argp == NUL)
7340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7342 {
7343 ret = FAIL;
7344 break;
7345 }
7346 ++argcount;
7347 if (*argp != ',')
7348 break;
7349 }
7350 if (*argp == ')')
7351 ++argp;
7352 else
7353 ret = FAIL;
7354
7355 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007356 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007357 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 {
7360 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007363 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
7366 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007367 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
7369 *arg = skipwhite(argp);
7370 return ret;
7371}
7372
7373
7374/*
7375 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007376 * Return OK when the function can't be called, FAIL otherwise.
7377 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 */
7379 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007380call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 char_u *name; /* name of the function */
7383 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007386 typval_T *argvars; /* vars for arguments, must have "argcount"
7387 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 linenr_T firstline; /* first line of range */
7389 linenr_T lastline; /* last line of range */
7390 int *doesrange; /* return: function handled range */
7391 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395#define ERROR_UNKNOWN 0
7396#define ERROR_TOOMANY 1
7397#define ERROR_TOOFEW 2
7398#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399#define ERROR_DICT 4
7400#define ERROR_NONE 5
7401#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int error = ERROR_NONE;
7403 int i;
7404 int llen;
7405 ufunc_T *fp;
7406 int cc;
7407#define FLEN_FIXED 40
7408 char_u fname_buf[FLEN_FIXED + 1];
7409 char_u *fname;
7410
7411 /*
7412 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7413 * Change <SNR>123_name() to K_SNR 123_name().
7414 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7415 */
7416 cc = name[len];
7417 name[len] = NUL;
7418 llen = eval_fname_script(name);
7419 if (llen > 0)
7420 {
7421 fname_buf[0] = K_SPECIAL;
7422 fname_buf[1] = KS_EXTRA;
7423 fname_buf[2] = (int)KE_SNR;
7424 i = 3;
7425 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7426 {
7427 if (current_SID <= 0)
7428 error = ERROR_SCRIPT;
7429 else
7430 {
7431 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7432 i = (int)STRLEN(fname_buf);
7433 }
7434 }
7435 if (i + STRLEN(name + llen) < FLEN_FIXED)
7436 {
7437 STRCPY(fname_buf + i, name + llen);
7438 fname = fname_buf;
7439 }
7440 else
7441 {
7442 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7443 if (fname == NULL)
7444 error = ERROR_OTHER;
7445 else
7446 {
7447 mch_memmove(fname, fname_buf, (size_t)i);
7448 STRCPY(fname + i, name + llen);
7449 }
7450 }
7451 }
7452 else
7453 fname = name;
7454
7455 *doesrange = FALSE;
7456
7457
7458 /* execute the function if no errors detected and executing */
7459 if (evaluate && error == ERROR_NONE)
7460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007461 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 error = ERROR_UNKNOWN;
7463
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007464 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
7466 /*
7467 * User defined function.
7468 */
7469 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007472 /* Trigger FuncUndefined event, may load the function. */
7473 if (fp == NULL
7474 && apply_autocmds(EVENT_FUNCUNDEFINED,
7475 fname, fname, TRUE, NULL)
7476 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007478 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 fp = find_func(fname);
7480 }
7481#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007482 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007483 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007484 {
7485 /* loaded a package, search for the function again */
7486 fp = find_func(fname);
7487 }
7488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 if (fp != NULL)
7490 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007491 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007493 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007495 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007497 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007498 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 else
7500 {
7501 /*
7502 * Call the user function.
7503 * Save and restore search patterns, script variables and
7504 * redo buffer.
7505 */
7506 save_search_patterns();
7507 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007508 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007511 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7512 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7513 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007514 /* Function was unreferenced while being used, free it
7515 * now. */
7516 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 restoreRedobuff();
7518 restore_search_patterns();
7519 error = ERROR_NONE;
7520 }
7521 }
7522 }
7523 else
7524 {
7525 /*
7526 * Find the function name in the table, call its implementation.
7527 */
7528 i = find_internal_func(fname);
7529 if (i >= 0)
7530 {
7531 if (argcount < functions[i].f_min_argc)
7532 error = ERROR_TOOFEW;
7533 else if (argcount > functions[i].f_max_argc)
7534 error = ERROR_TOOMANY;
7535 else
7536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007537 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 error = ERROR_NONE;
7540 }
7541 }
7542 }
7543 /*
7544 * The function call (or "FuncUndefined" autocommand sequence) might
7545 * have been aborted by an error, an interrupt, or an explicitly thrown
7546 * exception that has not been caught so far. This situation can be
7547 * tested for by calling aborting(). For an error in an internal
7548 * function or for the "E132" error in call_user_func(), however, the
7549 * throw point at which the "force_abort" flag (temporarily reset by
7550 * emsg()) is normally updated has not been reached yet. We need to
7551 * update that flag first to make aborting() reliable.
7552 */
7553 update_force_abort();
7554 }
7555 if (error == ERROR_NONE)
7556 ret = OK;
7557
7558 /*
7559 * Report an error unless the argument evaluation or function call has been
7560 * cancelled due to an aborting error, an interrupt, or an exception.
7561 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 if (!aborting())
7563 {
7564 switch (error)
7565 {
7566 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007567 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 break;
7569 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007570 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 break;
7572 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007573 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 name);
7575 break;
7576 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007577 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 name);
7579 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007580 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007581 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007582 name);
7583 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 }
7585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
7587 name[len] = cc;
7588 if (fname != name && fname != fname_buf)
7589 vim_free(fname);
7590
7591 return ret;
7592}
7593
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007594/*
7595 * Give an error message with a function name. Handle <SNR> things.
7596 */
7597 static void
7598emsg_funcname(msg, name)
7599 char *msg;
7600 char_u *name;
7601{
7602 char_u *p;
7603
7604 if (*name == K_SPECIAL)
7605 p = concat_str((char_u *)"<SNR>", name + 3);
7606 else
7607 p = name;
7608 EMSG2(_(msg), p);
7609 if (p != name)
7610 vim_free(p);
7611}
7612
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613/*********************************************
7614 * Implementation of the built-in functions
7615 */
7616
7617/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007618 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 */
7620 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007621f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 typval_T *argvars;
7623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007630 if ((l = argvars[0].vval.v_list) != NULL
7631 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7632 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007633 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 }
7635 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007636 EMSG(_(e_listreq));
7637}
7638
7639/*
7640 * "append(lnum, string/list)" function
7641 */
7642 static void
7643f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 typval_T *argvars;
7645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007646{
7647 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007648 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 list_T *l = NULL;
7650 listitem_T *li = NULL;
7651 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007652 long added = 0;
7653
Bram Moolenaar0d660222005-01-07 21:51:51 +00007654 lnum = get_tv_lnum(argvars);
7655 if (lnum >= 0
7656 && lnum <= curbuf->b_ml.ml_line_count
7657 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007659 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007660 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 l = argvars[1].vval.v_list;
7662 if (l == NULL)
7663 return;
7664 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007666 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007667 for (;;)
7668 {
7669 if (l == NULL)
7670 tv = &argvars[1]; /* append a string */
7671 else if (li == NULL)
7672 break; /* end of list */
7673 else
7674 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007675 line = get_tv_string_chk(tv);
7676 if (line == NULL) /* type error */
7677 {
7678 rettv->vval.v_number = 1; /* Failed */
7679 break;
7680 }
7681 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007682 ++added;
7683 if (l == NULL)
7684 break;
7685 li = li->li_next;
7686 }
7687
7688 appended_lines_mark(lnum, added);
7689 if (curwin->w_cursor.lnum > lnum)
7690 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007692 else
7693 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694}
7695
7696/*
7697 * "argc()" function
7698 */
7699/* ARGSUSED */
7700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 typval_T *argvars;
7703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007705 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706}
7707
7708/*
7709 * "argidx()" function
7710 */
7711/* ARGSUSED */
7712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 typval_T *argvars;
7715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718}
7719
7720/*
7721 * "argv(nr)" function
7722 */
7723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 typval_T *argvars;
7726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 int idx;
7729
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007730 if (argvars[0].v_type != VAR_UNKNOWN)
7731 {
7732 idx = get_tv_number_chk(&argvars[0], NULL);
7733 if (idx >= 0 && idx < ARGCOUNT)
7734 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7735 else
7736 rettv->vval.v_string = NULL;
7737 rettv->v_type = VAR_STRING;
7738 }
7739 else if (rettv_list_alloc(rettv) == OK)
7740 for (idx = 0; idx < ARGCOUNT; ++idx)
7741 list_append_string(rettv->vval.v_list,
7742 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745/*
7746 * "browse(save, title, initdir, default)" function
7747 */
7748/* ARGSUSED */
7749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 typval_T *argvars;
7752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754#ifdef FEAT_BROWSE
7755 int save;
7756 char_u *title;
7757 char_u *initdir;
7758 char_u *defname;
7759 char_u buf[NUMBUFLEN];
7760 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007761 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007763 save = get_tv_number_chk(&argvars[0], &error);
7764 title = get_tv_string_chk(&argvars[1]);
7765 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7766 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007768 if (error || title == NULL || initdir == NULL || defname == NULL)
7769 rettv->vval.v_string = NULL;
7770 else
7771 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007772 do_browse(save ? BROWSE_SAVE : 0,
7773 title, defname, NULL, initdir, NULL, curbuf);
7774#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007776#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007778}
7779
7780/*
7781 * "browsedir(title, initdir)" function
7782 */
7783/* ARGSUSED */
7784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 typval_T *argvars;
7787 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007788{
7789#ifdef FEAT_BROWSE
7790 char_u *title;
7791 char_u *initdir;
7792 char_u buf[NUMBUFLEN];
7793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007794 title = get_tv_string_chk(&argvars[0]);
7795 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007797 if (title == NULL || initdir == NULL)
7798 rettv->vval.v_string = NULL;
7799 else
7800 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007801 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007803 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806}
7807
Bram Moolenaar33570922005-01-25 22:26:29 +00007808static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007809
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810/*
7811 * Find a buffer by number or exact name.
7812 */
7813 static buf_T *
7814find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007819 if (avar->v_type == VAR_NUMBER)
7820 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007821 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007824 if (buf == NULL)
7825 {
7826 /* No full path name match, try a match with a URL or a "nofile"
7827 * buffer, these don't use the full path. */
7828 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7829 if (buf->b_fname != NULL
7830 && (path_with_url(buf->b_fname)
7831#ifdef FEAT_QUICKFIX
7832 || bt_nofile(buf)
7833#endif
7834 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007836 break;
7837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
7839 return buf;
7840}
7841
7842/*
7843 * "bufexists(expr)" function
7844 */
7845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 typval_T *argvars;
7848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851}
7852
7853/*
7854 * "buflisted(expr)" function
7855 */
7856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 typval_T *argvars;
7859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 buf_T *buf;
7862
7863 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865}
7866
7867/*
7868 * "bufloaded(expr)" function
7869 */
7870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 typval_T *argvars;
7873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 buf_T *buf;
7876
7877 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879}
7880
Bram Moolenaar33570922005-01-25 22:26:29 +00007881static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007882
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883/*
7884 * Get buffer by number or pattern.
7885 */
7886 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 int save_magic;
7892 char_u *save_cpo;
7893 buf_T *buf;
7894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895 if (tv->v_type == VAR_NUMBER)
7896 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007897 if (tv->v_type != VAR_STRING)
7898 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (name == NULL || *name == NUL)
7900 return curbuf;
7901 if (name[0] == '$' && name[1] == NUL)
7902 return lastbuf;
7903
7904 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7905 save_magic = p_magic;
7906 p_magic = TRUE;
7907 save_cpo = p_cpo;
7908 p_cpo = (char_u *)"";
7909
7910 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7911 TRUE, FALSE));
7912
7913 p_magic = save_magic;
7914 p_cpo = save_cpo;
7915
7916 /* If not found, try expanding the name, like done for bufexists(). */
7917 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
7920 return buf;
7921}
7922
7923/*
7924 * "bufname(expr)" function
7925 */
7926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 typval_T *argvars;
7929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 buf_T *buf;
7932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007933 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 buf = get_buf_tv(&argvars[0]);
7936 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007938 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 --emsg_off;
7942}
7943
7944/*
7945 * "bufnr(expr)" function
7946 */
7947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007953 int error = FALSE;
7954 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007956 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007959 --emsg_off;
7960
7961 /* If the buffer isn't found and the second argument is not zero create a
7962 * new buffer. */
7963 if (buf == NULL
7964 && argvars[1].v_type != VAR_UNKNOWN
7965 && get_tv_number_chk(&argvars[1], &error) != 0
7966 && !error
7967 && (name = get_tv_string_chk(&argvars[0])) != NULL
7968 && !error)
7969 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7970
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975}
7976
7977/*
7978 * "bufwinnr(nr)" function
7979 */
7980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 typval_T *argvars;
7983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985#ifdef FEAT_WINDOWS
7986 win_T *wp;
7987 int winnr = 0;
7988#endif
7989 buf_T *buf;
7990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007991 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994#ifdef FEAT_WINDOWS
7995 for (wp = firstwin; wp; wp = wp->w_next)
7996 {
7997 ++winnr;
7998 if (wp->w_buffer == buf)
7999 break;
8000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008001 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004#endif
8005 --emsg_off;
8006}
8007
8008/*
8009 * "byte2line(byte)" function
8010 */
8011/*ARGSUSED*/
8012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 typval_T *argvars;
8015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019#else
8020 long boff = 0;
8021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008022 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008026 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 (linenr_T)0, &boff);
8028#endif
8029}
8030
8031/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008032 * "byteidx()" function
8033 */
8034/*ARGSUSED*/
8035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008037 typval_T *argvars;
8038 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008039{
8040#ifdef FEAT_MBYTE
8041 char_u *t;
8042#endif
8043 char_u *str;
8044 long idx;
8045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 str = get_tv_string_chk(&argvars[0]);
8047 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008049 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008050 return;
8051
8052#ifdef FEAT_MBYTE
8053 t = str;
8054 for ( ; idx > 0; idx--)
8055 {
8056 if (*t == NUL) /* EOL reached */
8057 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008058 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008059 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008060 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008061#else
8062 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008064#endif
8065}
8066
8067/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008068 * "call(func, arglist)" function
8069 */
8070 static void
8071f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 typval_T *argvars;
8073 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008074{
8075 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008076 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008077 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008078 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008079 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008081
8082 rettv->vval.v_number = 0;
8083 if (argvars[1].v_type != VAR_LIST)
8084 {
8085 EMSG(_(e_listreq));
8086 return;
8087 }
8088 if (argvars[1].vval.v_list == NULL)
8089 return;
8090
8091 if (argvars[0].v_type == VAR_FUNC)
8092 func = argvars[0].vval.v_string;
8093 else
8094 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008095 if (*func == NUL)
8096 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008097
Bram Moolenaare9a41262005-01-15 22:18:47 +00008098 if (argvars[2].v_type != VAR_UNKNOWN)
8099 {
8100 if (argvars[2].v_type != VAR_DICT)
8101 {
8102 EMSG(_(e_dictreq));
8103 return;
8104 }
8105 selfdict = argvars[2].vval.v_dict;
8106 }
8107
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008108 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8109 item = item->li_next)
8110 {
8111 if (argc == MAX_FUNC_ARGS)
8112 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008113 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008114 break;
8115 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008116 /* Make a copy of each argument. This is needed to be able to set
8117 * v_lock to VAR_FIXED in the copy without changing the original list.
8118 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008119 copy_tv(&item->li_tv, &argv[argc++]);
8120 }
8121
8122 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008123 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008124 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8125 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126
8127 /* Free the arguments. */
8128 while (argc > 0)
8129 clear_tv(&argv[--argc]);
8130}
8131
8132/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008133 * "changenr()" function
8134 */
8135/*ARGSUSED*/
8136 static void
8137f_changenr(argvars, rettv)
8138 typval_T *argvars;
8139 typval_T *rettv;
8140{
8141 rettv->vval.v_number = curbuf->b_u_seq_cur;
8142}
8143
8144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 * "char2nr(string)" function
8146 */
8147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008148f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 typval_T *argvars;
8150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152#ifdef FEAT_MBYTE
8153 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008154 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 else
8156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158}
8159
8160/*
8161 * "cindent(lnum)" function
8162 */
8163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008165 typval_T *argvars;
8166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167{
8168#ifdef FEAT_CINDENT
8169 pos_T pos;
8170 linenr_T lnum;
8171
8172 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8175 {
8176 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008177 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 curwin->w_cursor = pos;
8179 }
8180 else
8181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183}
8184
8185/*
8186 * "col(string)" function
8187 */
8188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008189f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008190 typval_T *argvars;
8191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 colnr_T col = 0;
8194 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008195 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008197 fp = var2fpos(&argvars[0], FALSE, &fnum);
8198 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
8200 if (fp->col == MAXCOL)
8201 {
8202 /* '> can be MAXCOL, get the length of the line then */
8203 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008204 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 else
8206 col = MAXCOL;
8207 }
8208 else
8209 {
8210 col = fp->col + 1;
8211#ifdef FEAT_VIRTUALEDIT
8212 /* col(".") when the cursor is on the NUL at the end of the line
8213 * because of "coladd" can be seen as an extra column. */
8214 if (virtual_active() && fp == &curwin->w_cursor)
8215 {
8216 char_u *p = ml_get_cursor();
8217
8218 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8219 curwin->w_virtcol - curwin->w_cursor.coladd))
8220 {
8221# ifdef FEAT_MBYTE
8222 int l;
8223
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008224 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 col += l;
8226# else
8227 if (*p != NUL && p[1] == NUL)
8228 ++col;
8229# endif
8230 }
8231 }
8232#endif
8233 }
8234 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236}
8237
Bram Moolenaar572cb562005-08-05 21:35:02 +00008238#if defined(FEAT_INS_EXPAND)
8239/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008240 * "complete()" function
8241 */
8242/*ARGSUSED*/
8243 static void
8244f_complete(argvars, rettv)
8245 typval_T *argvars;
8246 typval_T *rettv;
8247{
8248 int startcol;
8249
8250 if ((State & INSERT) == 0)
8251 {
8252 EMSG(_("E785: complete() can only be used in Insert mode"));
8253 return;
8254 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008255
8256 /* Check for undo allowed here, because if something was already inserted
8257 * the line was already saved for undo and this check isn't done. */
8258 if (!undo_allowed())
8259 return;
8260
Bram Moolenaarade00832006-03-10 21:46:58 +00008261 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8262 {
8263 EMSG(_(e_invarg));
8264 return;
8265 }
8266
8267 startcol = get_tv_number_chk(&argvars[0], NULL);
8268 if (startcol <= 0)
8269 return;
8270
8271 set_completion(startcol - 1, argvars[1].vval.v_list);
8272}
8273
8274/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008275 * "complete_add()" function
8276 */
8277/*ARGSUSED*/
8278 static void
8279f_complete_add(argvars, rettv)
8280 typval_T *argvars;
8281 typval_T *rettv;
8282{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008283 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008284}
8285
8286/*
8287 * "complete_check()" function
8288 */
8289/*ARGSUSED*/
8290 static void
8291f_complete_check(argvars, rettv)
8292 typval_T *argvars;
8293 typval_T *rettv;
8294{
8295 int saved = RedrawingDisabled;
8296
8297 RedrawingDisabled = 0;
8298 ins_compl_check_keys(0);
8299 rettv->vval.v_number = compl_interrupted;
8300 RedrawingDisabled = saved;
8301}
8302#endif
8303
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304/*
8305 * "confirm(message, buttons[, default [, type]])" function
8306 */
8307/*ARGSUSED*/
8308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 typval_T *argvars;
8311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
8313#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8314 char_u *message;
8315 char_u *buttons = NULL;
8316 char_u buf[NUMBUFLEN];
8317 char_u buf2[NUMBUFLEN];
8318 int def = 1;
8319 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008320 char_u *typestr;
8321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008323 message = get_tv_string_chk(&argvars[0]);
8324 if (message == NULL)
8325 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008326 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008328 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8329 if (buttons == NULL)
8330 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008333 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008334 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008336 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8337 if (typestr == NULL)
8338 error = TRUE;
8339 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008341 switch (TOUPPER_ASC(*typestr))
8342 {
8343 case 'E': type = VIM_ERROR; break;
8344 case 'Q': type = VIM_QUESTION; break;
8345 case 'I': type = VIM_INFO; break;
8346 case 'W': type = VIM_WARNING; break;
8347 case 'G': type = VIM_GENERIC; break;
8348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 }
8350 }
8351 }
8352 }
8353
8354 if (buttons == NULL || *buttons == NUL)
8355 buttons = (char_u *)_("&Ok");
8356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008357 if (error)
8358 rettv->vval.v_number = 0;
8359 else
8360 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 def, NULL);
8362#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#endif
8365}
8366
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008367/*
8368 * "copy()" function
8369 */
8370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *argvars;
8373 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008374{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008375 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008376}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377
8378/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008379 * "count()" function
8380 */
8381 static void
8382f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008383 typval_T *argvars;
8384 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008385{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008386 long n = 0;
8387 int ic = FALSE;
8388
Bram Moolenaare9a41262005-01-15 22:18:47 +00008389 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008390 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 listitem_T *li;
8392 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008393 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008394
Bram Moolenaare9a41262005-01-15 22:18:47 +00008395 if ((l = argvars[0].vval.v_list) != NULL)
8396 {
8397 li = l->lv_first;
8398 if (argvars[2].v_type != VAR_UNKNOWN)
8399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008400 int error = FALSE;
8401
8402 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008403 if (argvars[3].v_type != VAR_UNKNOWN)
8404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008405 idx = get_tv_number_chk(&argvars[3], &error);
8406 if (!error)
8407 {
8408 li = list_find(l, idx);
8409 if (li == NULL)
8410 EMSGN(_(e_listidx), idx);
8411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008413 if (error)
8414 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415 }
8416
8417 for ( ; li != NULL; li = li->li_next)
8418 if (tv_equal(&li->li_tv, &argvars[1], ic))
8419 ++n;
8420 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422 else if (argvars[0].v_type == VAR_DICT)
8423 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008424 int todo;
8425 dict_T *d;
8426 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008428 if ((d = argvars[0].vval.v_dict) != NULL)
8429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008430 int error = FALSE;
8431
Bram Moolenaare9a41262005-01-15 22:18:47 +00008432 if (argvars[2].v_type != VAR_UNKNOWN)
8433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008434 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008435 if (argvars[3].v_type != VAR_UNKNOWN)
8436 EMSG(_(e_invarg));
8437 }
8438
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008439 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008441 {
8442 if (!HASHITEM_EMPTY(hi))
8443 {
8444 --todo;
8445 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8446 ++n;
8447 }
8448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008449 }
8450 }
8451 else
8452 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008453 rettv->vval.v_number = n;
8454}
8455
8456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8458 *
8459 * Checks the existence of a cscope connection.
8460 */
8461/*ARGSUSED*/
8462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008463f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 typval_T *argvars;
8465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
8467#ifdef FEAT_CSCOPE
8468 int num = 0;
8469 char_u *dbpath = NULL;
8470 char_u *prepend = NULL;
8471 char_u buf[NUMBUFLEN];
8472
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008473 if (argvars[0].v_type != VAR_UNKNOWN
8474 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 num = (int)get_tv_number(&argvars[0]);
8477 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008478 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008479 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 }
8481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008482 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485#endif
8486}
8487
8488/*
8489 * "cursor(lnum, col)" function
8490 *
8491 * Moves the cursor to the specified line and column
8492 */
8493/*ARGSUSED*/
8494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008495f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 typval_T *argvars;
8497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
8499 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008500#ifdef FEAT_VIRTUALEDIT
8501 long coladd = 0;
8502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
Bram Moolenaara5525202006-03-02 22:52:09 +00008504 if (argvars[1].v_type == VAR_UNKNOWN)
8505 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008506 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008507
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008508 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008509 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008510 line = pos.lnum;
8511 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008512#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008514#endif
8515 }
8516 else
8517 {
8518 line = get_tv_lnum(argvars);
8519 col = get_tv_number_chk(&argvars[1], NULL);
8520#ifdef FEAT_VIRTUALEDIT
8521 if (argvars[2].v_type != VAR_UNKNOWN)
8522 coladd = get_tv_number_chk(&argvars[2], NULL);
8523#endif
8524 }
8525 if (line < 0 || col < 0
8526#ifdef FEAT_VIRTUALEDIT
8527 || coladd < 0
8528#endif
8529 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008530 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 if (line > 0)
8532 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 if (col > 0)
8534 curwin->w_cursor.col = col - 1;
8535#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008536 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537#endif
8538
8539 /* Make sure the cursor is in a valid position. */
8540 check_cursor();
8541#ifdef FEAT_MBYTE
8542 /* Correct cursor for multi-byte character. */
8543 if (has_mbyte)
8544 mb_adjust_cursor();
8545#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008546
8547 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548}
8549
8550/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 */
8553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T *argvars;
8556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008558 int noref = 0;
8559
8560 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008562 if (noref < 0 || noref > 1)
8563 EMSG(_(e_invarg));
8564 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008565 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566}
8567
8568/*
8569 * "delete()" function
8570 */
8571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 typval_T *argvars;
8574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580}
8581
8582/*
8583 * "did_filetype()" function
8584 */
8585/*ARGSUSED*/
8586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 typval_T *argvars;
8589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#endif
8596}
8597
8598/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008599 * "diff_filler()" function
8600 */
8601/*ARGSUSED*/
8602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *argvars;
8605 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008606{
8607#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008609#endif
8610}
8611
8612/*
8613 * "diff_hlID()" function
8614 */
8615/*ARGSUSED*/
8616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *argvars;
8619 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008620{
8621#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008623 static linenr_T prev_lnum = 0;
8624 static int changedtick = 0;
8625 static int fnum = 0;
8626 static int change_start = 0;
8627 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008628 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008629 int filler_lines;
8630 int col;
8631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008632 if (lnum < 0) /* ignore type error in {lnum} arg */
8633 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008634 if (lnum != prev_lnum
8635 || changedtick != curbuf->b_changedtick
8636 || fnum != curbuf->b_fnum)
8637 {
8638 /* New line, buffer, change: need to get the values. */
8639 filler_lines = diff_check(curwin, lnum);
8640 if (filler_lines < 0)
8641 {
8642 if (filler_lines == -1)
8643 {
8644 change_start = MAXCOL;
8645 change_end = -1;
8646 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8647 hlID = HLF_ADD; /* added line */
8648 else
8649 hlID = HLF_CHD; /* changed line */
8650 }
8651 else
8652 hlID = HLF_ADD; /* added line */
8653 }
8654 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008655 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008656 prev_lnum = lnum;
8657 changedtick = curbuf->b_changedtick;
8658 fnum = curbuf->b_fnum;
8659 }
8660
8661 if (hlID == HLF_CHD || hlID == HLF_TXD)
8662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008664 if (col >= change_start && col <= change_end)
8665 hlID = HLF_TXD; /* changed text */
8666 else
8667 hlID = HLF_CHD; /* changed line */
8668 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008669 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008670#endif
8671}
8672
8673/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008674 * "empty({expr})" function
8675 */
8676 static void
8677f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 typval_T *argvars;
8679 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008680{
8681 int n;
8682
8683 switch (argvars[0].v_type)
8684 {
8685 case VAR_STRING:
8686 case VAR_FUNC:
8687 n = argvars[0].vval.v_string == NULL
8688 || *argvars[0].vval.v_string == NUL;
8689 break;
8690 case VAR_NUMBER:
8691 n = argvars[0].vval.v_number == 0;
8692 break;
8693 case VAR_LIST:
8694 n = argvars[0].vval.v_list == NULL
8695 || argvars[0].vval.v_list->lv_first == NULL;
8696 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008697 case VAR_DICT:
8698 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008699 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008700 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008701 default:
8702 EMSG2(_(e_intern2), "f_empty()");
8703 n = 0;
8704 }
8705
8706 rettv->vval.v_number = n;
8707}
8708
8709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 * "escape({string}, {chars})" function
8711 */
8712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 typval_T *argvars;
8715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716{
8717 char_u buf[NUMBUFLEN];
8718
Bram Moolenaar758711c2005-02-02 23:11:38 +00008719 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8720 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722}
8723
8724/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008725 * "eval()" function
8726 */
8727/*ARGSUSED*/
8728 static void
8729f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *argvars;
8731 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008732{
8733 char_u *s;
8734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008735 s = get_tv_string_chk(&argvars[0]);
8736 if (s != NULL)
8737 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8740 {
8741 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008742 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008743 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008744 else if (*s != NUL)
8745 EMSG(_(e_trailing));
8746}
8747
8748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 * "eventhandler()" function
8750 */
8751/*ARGSUSED*/
8752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T *argvars;
8755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758}
8759
8760/*
8761 * "executable()" function
8762 */
8763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008764f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008765 typval_T *argvars;
8766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770
8771/*
8772 * "exists()" function
8773 */
8774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008775f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 typval_T *argvars;
8777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 char_u *p;
8780 char_u *name;
8781 int n = FALSE;
8782 int len = 0;
8783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 if (*p == '$') /* environment variable */
8786 {
8787 /* first try "normal" environment variables (fast) */
8788 if (mch_getenv(p + 1) != NULL)
8789 n = TRUE;
8790 else
8791 {
8792 /* try expanding things like $VIM and ${HOME} */
8793 p = expand_env_save(p);
8794 if (p != NULL && *p != '$')
8795 n = TRUE;
8796 vim_free(p);
8797 }
8798 }
8799 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008802 if (*skipwhite(p) != NUL)
8803 n = FALSE; /* trailing garbage */
8804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 else if (*p == '*') /* internal or user defined function */
8806 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008807 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 }
8809 else if (*p == ':')
8810 {
8811 n = cmd_exists(p + 1);
8812 }
8813 else if (*p == '#')
8814 {
8815#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008816 if (p[1] == '#')
8817 n = autocmd_supported(p + 2);
8818 else
8819 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820#endif
8821 }
8822 else /* internal variable */
8823 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008824 char_u *tofree;
8825 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008827 /* get_name_len() takes care of expanding curly braces */
8828 name = p;
8829 len = get_name_len(&p, &tofree, TRUE, FALSE);
8830 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008832 if (tofree != NULL)
8833 name = tofree;
8834 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8835 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008837 /* handle d.key, l[idx], f(expr) */
8838 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8839 if (n)
8840 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 }
8842 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008843 if (*p != NUL)
8844 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008846 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 }
8848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850}
8851
8852/*
8853 * "expand()" function
8854 */
8855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 typval_T *argvars;
8858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 char_u *s;
8861 int len;
8862 char_u *errormsg;
8863 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8864 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008865 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 rettv->v_type = VAR_STRING;
8868 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (*s == '%' || *s == '#' || *s == '<')
8870 {
8871 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 --emsg_off;
8874 }
8875 else
8876 {
8877 /* When the optional second argument is non-zero, don't remove matches
8878 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008879 if (argvars[1].v_type != VAR_UNKNOWN
8880 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 if (!error)
8883 {
8884 ExpandInit(&xpc);
8885 xpc.xp_context = EXPAND_FILES;
8886 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 }
8888 else
8889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
8891}
8892
8893/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008894 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008895 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008896 */
8897 static void
8898f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008899 typval_T *argvars;
8900 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008901{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008902 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008903 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008904 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008905 list_T *l1, *l2;
8906 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008907 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008908 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008909
Bram Moolenaare9a41262005-01-15 22:18:47 +00008910 l1 = argvars[0].vval.v_list;
8911 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008912 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8913 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008914 {
8915 if (argvars[2].v_type != VAR_UNKNOWN)
8916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 before = get_tv_number_chk(&argvars[2], &error);
8918 if (error)
8919 return; /* type error; errmsg already given */
8920
Bram Moolenaar758711c2005-02-02 23:11:38 +00008921 if (before == l1->lv_len)
8922 item = NULL;
8923 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008924 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008925 item = list_find(l1, before);
8926 if (item == NULL)
8927 {
8928 EMSGN(_(e_listidx), before);
8929 return;
8930 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008931 }
8932 }
8933 else
8934 item = NULL;
8935 list_extend(l1, l2, item);
8936
Bram Moolenaare9a41262005-01-15 22:18:47 +00008937 copy_tv(&argvars[0], rettv);
8938 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008940 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008942 dict_T *d1, *d2;
8943 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 char_u *action;
8945 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008947 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948
8949 d1 = argvars[0].vval.v_dict;
8950 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008951 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8952 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008953 {
8954 /* Check the third argument. */
8955 if (argvars[2].v_type != VAR_UNKNOWN)
8956 {
8957 static char *(av[]) = {"keep", "force", "error"};
8958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008959 action = get_tv_string_chk(&argvars[2]);
8960 if (action == NULL)
8961 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 for (i = 0; i < 3; ++i)
8963 if (STRCMP(action, av[i]) == 0)
8964 break;
8965 if (i == 3)
8966 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008967 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008968 return;
8969 }
8970 }
8971 else
8972 action = (char_u *)"force";
8973
8974 /* Go over all entries in the second dict and add them to the
8975 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008976 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008978 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008979 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008980 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008981 --todo;
8982 di1 = dict_find(d1, hi2->hi_key, -1);
8983 if (di1 == NULL)
8984 {
8985 di1 = dictitem_copy(HI2DI(hi2));
8986 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8987 dictitem_free(di1);
8988 }
8989 else if (*action == 'e')
8990 {
8991 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8992 break;
8993 }
8994 else if (*action == 'f')
8995 {
8996 clear_tv(&di1->di_tv);
8997 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8998 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 }
9000 }
9001
Bram Moolenaare9a41262005-01-15 22:18:47 +00009002 copy_tv(&argvars[0], rettv);
9003 }
9004 }
9005 else
9006 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007}
9008
9009/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009010 * "feedkeys()" function
9011 */
9012/*ARGSUSED*/
9013 static void
9014f_feedkeys(argvars, rettv)
9015 typval_T *argvars;
9016 typval_T *rettv;
9017{
9018 int remap = TRUE;
9019 char_u *keys, *flags;
9020 char_u nbuf[NUMBUFLEN];
9021 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009022 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009023
9024 rettv->vval.v_number = 0;
9025 keys = get_tv_string(&argvars[0]);
9026 if (*keys != NUL)
9027 {
9028 if (argvars[1].v_type != VAR_UNKNOWN)
9029 {
9030 flags = get_tv_string_buf(&argvars[1], nbuf);
9031 for ( ; *flags != NUL; ++flags)
9032 {
9033 switch (*flags)
9034 {
9035 case 'n': remap = FALSE; break;
9036 case 'm': remap = TRUE; break;
9037 case 't': typed = TRUE; break;
9038 }
9039 }
9040 }
9041
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009042 /* Need to escape K_SPECIAL and CSI before putting the string in the
9043 * typeahead buffer. */
9044 keys_esc = vim_strsave_escape_csi(keys);
9045 if (keys_esc != NULL)
9046 {
9047 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009048 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009049 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009050 if (vgetc_busy)
9051 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009052 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009053 }
9054}
9055
9056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 * "filereadable()" function
9058 */
9059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009061 typval_T *argvars;
9062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 FILE *fd;
9065 char_u *p;
9066 int n;
9067
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9070 {
9071 n = TRUE;
9072 fclose(fd);
9073 }
9074 else
9075 n = FALSE;
9076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078}
9079
9080/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009081 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 * rights to write into.
9083 */
9084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 typval_T *argvars;
9087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009089 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090}
9091
Bram Moolenaar33570922005-01-25 22:26:29 +00009092static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009093
9094 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009095findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009098 int dir;
9099{
9100#ifdef FEAT_SEARCHPATH
9101 char_u *fname;
9102 char_u *fresult = NULL;
9103 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9104 char_u *p;
9105 char_u pathbuf[NUMBUFLEN];
9106 int count = 1;
9107 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009108 int error = FALSE;
9109#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009110
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009111 rettv->vval.v_string = NULL;
9112 rettv->v_type = VAR_STRING;
9113
9114#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009116
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009117 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9120 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009121 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009122 else
9123 {
9124 if (*p != NUL)
9125 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009128 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009129 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009130 }
9131
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009132 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9133 error = TRUE;
9134
9135 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009137 do
9138 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009139 if (rettv->v_type == VAR_STRING)
9140 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 fresult = find_file_in_path_option(first ? fname : NULL,
9142 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009143 0, first, path, dir, NULL,
9144 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009146
9147 if (fresult != NULL && rettv->v_type == VAR_LIST)
9148 list_append_string(rettv->vval.v_list, fresult, -1);
9149
9150 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009152
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009153 if (rettv->v_type == VAR_STRING)
9154 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009155#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009156}
9157
Bram Moolenaar33570922005-01-25 22:26:29 +00009158static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9159static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009160
9161/*
9162 * Implementation of map() and filter().
9163 */
9164 static void
9165filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 typval_T *argvars;
9167 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009168 int map;
9169{
9170 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009171 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009172 listitem_T *li, *nli;
9173 list_T *l = NULL;
9174 dictitem_T *di;
9175 hashtab_T *ht;
9176 hashitem_T *hi;
9177 dict_T *d = NULL;
9178 typval_T save_val;
9179 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009180 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009181 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009182 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009183 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009184
9185 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009186 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009187 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009188 if ((l = argvars[0].vval.v_list) == NULL
9189 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009190 return;
9191 }
9192 else if (argvars[0].v_type == VAR_DICT)
9193 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009194 if ((d = argvars[0].vval.v_dict) == NULL
9195 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009196 return;
9197 }
9198 else
9199 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009200 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009201 return;
9202 }
9203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 expr = get_tv_string_buf_chk(&argvars[1], buf);
9205 /* On type errors, the preceding call has already displayed an error
9206 * message. Avoid a misleading error message for an empty string that
9207 * was not passed as argument. */
9208 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009210 prepare_vimvar(VV_VAL, &save_val);
9211 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009212
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009213 /* We reset "did_emsg" to be able to detect whether an error
9214 * occurred during evaluation of the expression. */
9215 save_did_emsg = did_emsg;
9216 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009218 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 prepare_vimvar(VV_KEY, &save_key);
9221 vimvars[VV_KEY].vv_type = VAR_STRING;
9222
9223 ht = &d->dv_hashtab;
9224 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009225 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009226 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 if (!HASHITEM_EMPTY(hi))
9229 {
9230 --todo;
9231 di = HI2DI(hi);
9232 if (tv_check_lock(di->di_tv.v_lock, msg))
9233 break;
9234 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009235 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009236 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 break;
9238 if (!map && rem)
9239 dictitem_remove(d, di);
9240 clear_tv(&vimvars[VV_KEY].vv_tv);
9241 }
9242 }
9243 hash_unlock(ht);
9244
9245 restore_vimvar(VV_KEY, &save_key);
9246 }
9247 else
9248 {
9249 for (li = l->lv_first; li != NULL; li = nli)
9250 {
9251 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009252 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009254 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009255 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009256 break;
9257 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009259 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009260 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009262 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009263
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009264 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009265 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009266
9267 copy_tv(&argvars[0], rettv);
9268}
9269
9270 static int
9271filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009272 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009273 char_u *expr;
9274 int map;
9275 int *remp;
9276{
Bram Moolenaar33570922005-01-25 22:26:29 +00009277 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009278 char_u *s;
9279
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009281 s = expr;
9282 if (eval1(&s, &rettv, TRUE) == FAIL)
9283 return FAIL;
9284 if (*s != NUL) /* check for trailing chars after expr */
9285 {
9286 EMSG2(_(e_invexpr2), s);
9287 return FAIL;
9288 }
9289 if (map)
9290 {
9291 /* map(): replace the list item value */
9292 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009293 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 *tv = rettv;
9295 }
9296 else
9297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009298 int error = FALSE;
9299
Bram Moolenaare9a41262005-01-15 22:18:47 +00009300 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009302 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009303 /* On type error, nothing has been removed; return FAIL to stop the
9304 * loop. The error message was given by get_tv_number_chk(). */
9305 if (error)
9306 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009308 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009310}
9311
9312/*
9313 * "filter()" function
9314 */
9315 static void
9316f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 typval_T *argvars;
9318 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009319{
9320 filter_map(argvars, rettv, FALSE);
9321}
9322
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009323/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009324 * "finddir({fname}[, {path}[, {count}]])" function
9325 */
9326 static void
9327f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009328 typval_T *argvars;
9329 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009330{
9331 findfilendir(argvars, rettv, TRUE);
9332}
9333
9334/*
9335 * "findfile({fname}[, {path}[, {count}]])" function
9336 */
9337 static void
9338f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009339 typval_T *argvars;
9340 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009341{
9342 findfilendir(argvars, rettv, FALSE);
9343}
9344
9345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 * "fnamemodify({fname}, {mods})" function
9347 */
9348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009350 typval_T *argvars;
9351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353 char_u *fname;
9354 char_u *mods;
9355 int usedlen = 0;
9356 int len;
9357 char_u *fbuf = NULL;
9358 char_u buf[NUMBUFLEN];
9359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 fname = get_tv_string_chk(&argvars[0]);
9361 mods = get_tv_string_buf_chk(&argvars[1], buf);
9362 if (fname == NULL || mods == NULL)
9363 fname = NULL;
9364 else
9365 {
9366 len = (int)STRLEN(fname);
9367 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 vim_free(fbuf);
9376}
9377
Bram Moolenaar33570922005-01-25 22:26:29 +00009378static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379
9380/*
9381 * "foldclosed()" function
9382 */
9383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009384foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009385 typval_T *argvars;
9386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 int end;
9388{
9389#ifdef FEAT_FOLDING
9390 linenr_T lnum;
9391 linenr_T first, last;
9392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9395 {
9396 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9397 {
9398 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 return;
9403 }
9404 }
9405#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407}
9408
9409/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009410 * "foldclosed()" function
9411 */
9412 static void
9413f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009414 typval_T *argvars;
9415 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009416{
9417 foldclosed_both(argvars, rettv, FALSE);
9418}
9419
9420/*
9421 * "foldclosedend()" function
9422 */
9423 static void
9424f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009425 typval_T *argvars;
9426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427{
9428 foldclosed_both(argvars, rettv, TRUE);
9429}
9430
9431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 * "foldlevel()" function
9433 */
9434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439#ifdef FEAT_FOLDING
9440 linenr_T lnum;
9441
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 else
9446#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448}
9449
9450/*
9451 * "foldtext()" function
9452 */
9453/*ARGSUSED*/
9454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009456 typval_T *argvars;
9457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
9459#ifdef FEAT_FOLDING
9460 linenr_T lnum;
9461 char_u *s;
9462 char_u *r;
9463 int len;
9464 char *txt;
9465#endif
9466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467 rettv->v_type = VAR_STRING;
9468 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009470 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9471 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9472 <= curbuf->b_ml.ml_line_count
9473 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 {
9475 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009476 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9477 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 {
9479 if (!linewhite(lnum))
9480 break;
9481 ++lnum;
9482 }
9483
9484 /* Find interesting text in this line. */
9485 s = skipwhite(ml_get(lnum));
9486 /* skip C comment-start */
9487 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009490 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009491 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009492 {
9493 s = skipwhite(ml_get(lnum + 1));
9494 if (*s == '*')
9495 s = skipwhite(s + 1);
9496 }
9497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 txt = _("+-%s%3ld lines: ");
9499 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009500 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 + 20 /* for %3ld */
9502 + STRLEN(s))); /* concatenated */
9503 if (r != NULL)
9504 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009505 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9506 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9507 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 len = (int)STRLEN(r);
9509 STRCAT(r, s);
9510 /* remove 'foldmarker' and 'commentstring' */
9511 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 }
9514 }
9515#endif
9516}
9517
9518/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009519 * "foldtextresult(lnum)" function
9520 */
9521/*ARGSUSED*/
9522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009524 typval_T *argvars;
9525 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009526{
9527#ifdef FEAT_FOLDING
9528 linenr_T lnum;
9529 char_u *text;
9530 char_u buf[51];
9531 foldinfo_T foldinfo;
9532 int fold_count;
9533#endif
9534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->v_type = VAR_STRING;
9536 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009537#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 /* treat illegal types and illegal string values for {lnum} the same */
9540 if (lnum < 0)
9541 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009542 fold_count = foldedCount(curwin, lnum, &foldinfo);
9543 if (fold_count > 0)
9544 {
9545 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9546 &foldinfo, buf);
9547 if (text == buf)
9548 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009550 }
9551#endif
9552}
9553
9554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 * "foreground()" function
9556 */
9557/*ARGSUSED*/
9558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009560 typval_T *argvars;
9561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#ifdef FEAT_GUI
9565 if (gui.in_use)
9566 gui_mch_set_foreground();
9567#else
9568# ifdef WIN32
9569 win32_set_foreground();
9570# endif
9571#endif
9572}
9573
9574/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009575 * "function()" function
9576 */
9577/*ARGSUSED*/
9578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009580 typval_T *argvars;
9581 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582{
9583 char_u *s;
9584
Bram Moolenaara7043832005-01-21 11:56:39 +00009585 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009587 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009588 EMSG2(_(e_invarg2), s);
9589 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009590 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 else
9592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->vval.v_string = vim_strsave(s);
9594 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009595 }
9596}
9597
9598/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009599 * "garbagecollect()" function
9600 */
9601/*ARGSUSED*/
9602 static void
9603f_garbagecollect(argvars, rettv)
9604 typval_T *argvars;
9605 typval_T *rettv;
9606{
9607 garbage_collect();
9608}
9609
9610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009611 * "get()" function
9612 */
9613 static void
9614f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009615 typval_T *argvars;
9616 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009617{
Bram Moolenaar33570922005-01-25 22:26:29 +00009618 listitem_T *li;
9619 list_T *l;
9620 dictitem_T *di;
9621 dict_T *d;
9622 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009623
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009625 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009626 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009628 int error = FALSE;
9629
9630 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9631 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009633 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009634 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 else if (argvars[0].v_type == VAR_DICT)
9636 {
9637 if ((d = argvars[0].vval.v_dict) != NULL)
9638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009639 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009640 if (di != NULL)
9641 tv = &di->di_tv;
9642 }
9643 }
9644 else
9645 EMSG2(_(e_listdictarg), "get()");
9646
9647 if (tv == NULL)
9648 {
9649 if (argvars[2].v_type == VAR_UNKNOWN)
9650 rettv->vval.v_number = 0;
9651 else
9652 copy_tv(&argvars[2], rettv);
9653 }
9654 else
9655 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009656}
9657
Bram Moolenaar342337a2005-07-21 21:11:17 +00009658static 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 +00009659
9660/*
9661 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009662 * Return a range (from start to end) of lines in rettv from the specified
9663 * buffer.
9664 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009665 */
9666 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009667get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009668 buf_T *buf;
9669 linenr_T start;
9670 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009671 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009672 typval_T *rettv;
9673{
9674 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009675
Bram Moolenaar342337a2005-07-21 21:11:17 +00009676 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009677 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009678 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009679 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009680 }
9681 else
9682 rettv->vval.v_number = 0;
9683
9684 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9685 return;
9686
9687 if (!retlist)
9688 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009689 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9690 p = ml_get_buf(buf, start, FALSE);
9691 else
9692 p = (char_u *)"";
9693
9694 rettv->v_type = VAR_STRING;
9695 rettv->vval.v_string = vim_strsave(p);
9696 }
9697 else
9698 {
9699 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009700 return;
9701
9702 if (start < 1)
9703 start = 1;
9704 if (end > buf->b_ml.ml_line_count)
9705 end = buf->b_ml.ml_line_count;
9706 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009707 if (list_append_string(rettv->vval.v_list,
9708 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009709 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009710 }
9711}
9712
9713/*
9714 * "getbufline()" function
9715 */
9716 static void
9717f_getbufline(argvars, rettv)
9718 typval_T *argvars;
9719 typval_T *rettv;
9720{
9721 linenr_T lnum;
9722 linenr_T end;
9723 buf_T *buf;
9724
9725 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9726 ++emsg_off;
9727 buf = get_buf_tv(&argvars[0]);
9728 --emsg_off;
9729
Bram Moolenaar661b1822005-07-28 22:36:45 +00009730 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009731 if (argvars[2].v_type == VAR_UNKNOWN)
9732 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009733 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009734 end = get_tv_lnum_buf(&argvars[2], buf);
9735
Bram Moolenaar342337a2005-07-21 21:11:17 +00009736 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009737}
9738
Bram Moolenaar0d660222005-01-07 21:51:51 +00009739/*
9740 * "getbufvar()" function
9741 */
9742 static void
9743f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009744 typval_T *argvars;
9745 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009746{
9747 buf_T *buf;
9748 buf_T *save_curbuf;
9749 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009750 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009752 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9753 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009754 ++emsg_off;
9755 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009756
9757 rettv->v_type = VAR_STRING;
9758 rettv->vval.v_string = NULL;
9759
9760 if (buf != NULL && varname != NULL)
9761 {
9762 if (*varname == '&') /* buffer-local-option */
9763 {
9764 /* set curbuf to be our buf, temporarily */
9765 save_curbuf = curbuf;
9766 curbuf = buf;
9767
9768 get_option_tv(&varname, rettv, TRUE);
9769
9770 /* restore previous notion of curbuf */
9771 curbuf = save_curbuf;
9772 }
9773 else
9774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 if (*varname == NUL)
9776 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9777 * scope prefix before the NUL byte is required by
9778 * find_var_in_ht(). */
9779 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009780 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009781 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009782 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009784 }
9785 }
9786
9787 --emsg_off;
9788}
9789
9790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 * "getchar()" function
9792 */
9793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 typval_T *argvars;
9796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009799 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009801 /* Position the cursor. Needed after a message that ends in a space. */
9802 windgoto(msg_row, msg_col);
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 ++no_mapping;
9805 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009806 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 /* getchar(): blocking wait. */
9808 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 /* getchar(1): only check if char avail */
9811 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 else if (error || vpeekc() == NUL)
9813 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 n = 0;
9815 else
9816 /* getchar(0) and char avail: return char */
9817 n = safe_vgetc();
9818 --no_mapping;
9819 --allow_keys;
9820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 if (IS_SPECIAL(n) || mod_mask != 0)
9823 {
9824 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9825 int i = 0;
9826
9827 /* Turn a special key into three bytes, plus modifier. */
9828 if (mod_mask != 0)
9829 {
9830 temp[i++] = K_SPECIAL;
9831 temp[i++] = KS_MODIFIER;
9832 temp[i++] = mod_mask;
9833 }
9834 if (IS_SPECIAL(n))
9835 {
9836 temp[i++] = K_SPECIAL;
9837 temp[i++] = K_SECOND(n);
9838 temp[i++] = K_THIRD(n);
9839 }
9840#ifdef FEAT_MBYTE
9841 else if (has_mbyte)
9842 i += (*mb_char2bytes)(n, temp + i);
9843#endif
9844 else
9845 temp[i++] = n;
9846 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847 rettv->v_type = VAR_STRING;
9848 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 }
9850}
9851
9852/*
9853 * "getcharmod()" function
9854 */
9855/*ARGSUSED*/
9856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862}
9863
9864/*
9865 * "getcmdline()" function
9866 */
9867/*ARGSUSED*/
9868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009870 typval_T *argvars;
9871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873 rettv->v_type = VAR_STRING;
9874 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875}
9876
9877/*
9878 * "getcmdpos()" function
9879 */
9880/*ARGSUSED*/
9881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009883 typval_T *argvars;
9884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887}
9888
9889/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009890 * "getcmdtype()" function
9891 */
9892/*ARGSUSED*/
9893 static void
9894f_getcmdtype(argvars, rettv)
9895 typval_T *argvars;
9896 typval_T *rettv;
9897{
9898 rettv->v_type = VAR_STRING;
9899 rettv->vval.v_string = alloc(2);
9900 if (rettv->vval.v_string != NULL)
9901 {
9902 rettv->vval.v_string[0] = get_cmdline_type();
9903 rettv->vval.v_string[1] = NUL;
9904 }
9905}
9906
9907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 * "getcwd()" function
9909 */
9910/*ARGSUSED*/
9911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 typval_T *argvars;
9914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 char_u cwd[MAXPATHL];
9917
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009920 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 else
9922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009925 if (rettv->vval.v_string != NULL)
9926 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#endif
9928 }
9929}
9930
9931/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009932 * "getfontname()" function
9933 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009934/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009937 typval_T *argvars;
9938 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->v_type = VAR_STRING;
9941 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009942#ifdef FEAT_GUI
9943 if (gui.in_use)
9944 {
9945 GuiFont font;
9946 char_u *name = NULL;
9947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009948 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009949 {
9950 /* Get the "Normal" font. Either the name saved by
9951 * hl_set_font_name() or from the font ID. */
9952 font = gui.norm_font;
9953 name = hl_get_font_name();
9954 }
9955 else
9956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009958 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9959 return;
9960 font = gui_mch_get_font(name, FALSE);
9961 if (font == NOFONT)
9962 return; /* Invalid font name, return empty string. */
9963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009965 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009966 gui_mch_free_font(font);
9967 }
9968#endif
9969}
9970
9971/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009972 * "getfperm({fname})" function
9973 */
9974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *argvars;
9977 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009978{
9979 char_u *fname;
9980 struct stat st;
9981 char_u *perm = NULL;
9982 char_u flags[] = "rwx";
9983 int i;
9984
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009986
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009988 if (mch_stat((char *)fname, &st) >= 0)
9989 {
9990 perm = vim_strsave((char_u *)"---------");
9991 if (perm != NULL)
9992 {
9993 for (i = 0; i < 9; i++)
9994 {
9995 if (st.st_mode & (1 << (8 - i)))
9996 perm[i] = flags[i % 3];
9997 }
9998 }
9999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010001}
10002
10003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 * "getfsize({fname})" function
10005 */
10006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010008 typval_T *argvars;
10009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010{
10011 char_u *fname;
10012 struct stat st;
10013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017
10018 if (mch_stat((char *)fname, &st) >= 0)
10019 {
10020 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027}
10028
10029/*
10030 * "getftime({fname})" function
10031 */
10032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010034 typval_T *argvars;
10035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 char_u *fname;
10038 struct stat st;
10039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
10042 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010049 * "getftype({fname})" function
10050 */
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010055{
10056 char_u *fname;
10057 struct stat st;
10058 char_u *type = NULL;
10059 char *t;
10060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010064 if (mch_lstat((char *)fname, &st) >= 0)
10065 {
10066#ifdef S_ISREG
10067 if (S_ISREG(st.st_mode))
10068 t = "file";
10069 else if (S_ISDIR(st.st_mode))
10070 t = "dir";
10071# ifdef S_ISLNK
10072 else if (S_ISLNK(st.st_mode))
10073 t = "link";
10074# endif
10075# ifdef S_ISBLK
10076 else if (S_ISBLK(st.st_mode))
10077 t = "bdev";
10078# endif
10079# ifdef S_ISCHR
10080 else if (S_ISCHR(st.st_mode))
10081 t = "cdev";
10082# endif
10083# ifdef S_ISFIFO
10084 else if (S_ISFIFO(st.st_mode))
10085 t = "fifo";
10086# endif
10087# ifdef S_ISSOCK
10088 else if (S_ISSOCK(st.st_mode))
10089 t = "fifo";
10090# endif
10091 else
10092 t = "other";
10093#else
10094# ifdef S_IFMT
10095 switch (st.st_mode & S_IFMT)
10096 {
10097 case S_IFREG: t = "file"; break;
10098 case S_IFDIR: t = "dir"; break;
10099# ifdef S_IFLNK
10100 case S_IFLNK: t = "link"; break;
10101# endif
10102# ifdef S_IFBLK
10103 case S_IFBLK: t = "bdev"; break;
10104# endif
10105# ifdef S_IFCHR
10106 case S_IFCHR: t = "cdev"; break;
10107# endif
10108# ifdef S_IFIFO
10109 case S_IFIFO: t = "fifo"; break;
10110# endif
10111# ifdef S_IFSOCK
10112 case S_IFSOCK: t = "socket"; break;
10113# endif
10114 default: t = "other";
10115 }
10116# else
10117 if (mch_isdir(fname))
10118 t = "dir";
10119 else
10120 t = "file";
10121# endif
10122#endif
10123 type = vim_strsave((char_u *)t);
10124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010126}
10127
10128/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010129 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010130 */
10131 static void
10132f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010133 typval_T *argvars;
10134 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010135{
10136 linenr_T lnum;
10137 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010138 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010139
10140 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010141 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010142 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010143 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010144 retlist = FALSE;
10145 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010146 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010147 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010148 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010149 retlist = TRUE;
10150 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010151
Bram Moolenaar342337a2005-07-21 21:11:17 +000010152 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010153}
10154
10155/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010156 * "getpos(string)" function
10157 */
10158 static void
10159f_getpos(argvars, rettv)
10160 typval_T *argvars;
10161 typval_T *rettv;
10162{
10163 pos_T *fp;
10164 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010165 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010166
10167 if (rettv_list_alloc(rettv) == OK)
10168 {
10169 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010170 fp = var2fpos(&argvars[0], TRUE, &fnum);
10171 if (fnum != -1)
10172 list_append_number(l, (varnumber_T)fnum);
10173 else
10174 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010175 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10176 : (varnumber_T)0);
10177 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10178 : (varnumber_T)0);
10179 list_append_number(l,
10180#ifdef FEAT_VIRTUALEDIT
10181 (fp != NULL) ? (varnumber_T)fp->coladd :
10182#endif
10183 (varnumber_T)0);
10184 }
10185 else
10186 rettv->vval.v_number = FALSE;
10187}
10188
10189/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010190 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010191 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010192/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010193 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010194f_getqflist(argvars, rettv)
10195 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010196 typval_T *rettv;
10197{
10198#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010199 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010200#endif
10201
10202 rettv->vval.v_number = FALSE;
10203#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010204 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010205 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010206 wp = NULL;
10207 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10208 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010209 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010210 if (wp == NULL)
10211 return;
10212 }
10213
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010214 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010215 }
10216#endif
10217}
10218
10219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 * "getreg()" function
10221 */
10222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 char_u *strregname;
10228 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010229 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010232 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 strregname = get_tv_string_chk(&argvars[0]);
10235 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010236 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 regname = (strregname == NULL ? '"' : *strregname);
10242 if (regname == 0)
10243 regname = '"';
10244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010246 rettv->vval.v_string = error ? NULL :
10247 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248}
10249
10250/*
10251 * "getregtype()" function
10252 */
10253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010254f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010255 typval_T *argvars;
10256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257{
10258 char_u *strregname;
10259 int regname;
10260 char_u buf[NUMBUFLEN + 2];
10261 long reglen = 0;
10262
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010263 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010264 {
10265 strregname = get_tv_string_chk(&argvars[0]);
10266 if (strregname == NULL) /* type error; errmsg already given */
10267 {
10268 rettv->v_type = VAR_STRING;
10269 rettv->vval.v_string = NULL;
10270 return;
10271 }
10272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 else
10274 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276
10277 regname = (strregname == NULL ? '"' : *strregname);
10278 if (regname == 0)
10279 regname = '"';
10280
10281 buf[0] = NUL;
10282 buf[1] = NUL;
10283 switch (get_reg_type(regname, &reglen))
10284 {
10285 case MLINE: buf[0] = 'V'; break;
10286 case MCHAR: buf[0] = 'v'; break;
10287#ifdef FEAT_VISUAL
10288 case MBLOCK:
10289 buf[0] = Ctrl_V;
10290 sprintf((char *)buf + 1, "%ld", reglen + 1);
10291 break;
10292#endif
10293 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->v_type = VAR_STRING;
10295 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296}
10297
10298/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010299 * "gettabwinvar()" function
10300 */
10301 static void
10302f_gettabwinvar(argvars, rettv)
10303 typval_T *argvars;
10304 typval_T *rettv;
10305{
10306 getwinvar(argvars, rettv, 1);
10307}
10308
10309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 * "getwinposx()" function
10311 */
10312/*ARGSUSED*/
10313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010314f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 typval_T *argvars;
10316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319#ifdef FEAT_GUI
10320 if (gui.in_use)
10321 {
10322 int x, y;
10323
10324 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 }
10327#endif
10328}
10329
10330/*
10331 * "getwinposy()" function
10332 */
10333/*ARGSUSED*/
10334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 typval_T *argvars;
10337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#ifdef FEAT_GUI
10341 if (gui.in_use)
10342 {
10343 int x, y;
10344
10345 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 }
10348#endif
10349}
10350
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010351/*
10352 * Find window specifed by "vp" in tabpage "tp".
10353 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010354 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010355find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010356 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010357 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010358{
10359#ifdef FEAT_WINDOWS
10360 win_T *wp;
10361#endif
10362 int nr;
10363
10364 nr = get_tv_number_chk(vp, NULL);
10365
10366#ifdef FEAT_WINDOWS
10367 if (nr < 0)
10368 return NULL;
10369 if (nr == 0)
10370 return curwin;
10371
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010372 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10373 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010374 if (--nr <= 0)
10375 break;
10376 return wp;
10377#else
10378 if (nr == 0 || nr == 1)
10379 return curwin;
10380 return NULL;
10381#endif
10382}
10383
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384/*
10385 * "getwinvar()" function
10386 */
10387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010388f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010389 typval_T *argvars;
10390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010392 getwinvar(argvars, rettv, 0);
10393}
10394
10395/*
10396 * getwinvar() and gettabwinvar()
10397 */
10398 static void
10399getwinvar(argvars, rettv, off)
10400 typval_T *argvars;
10401 typval_T *rettv;
10402 int off; /* 1 for gettabwinvar() */
10403{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 win_T *win, *oldcurwin;
10405 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010407 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010409#ifdef FEAT_WINDOWS
10410 if (off == 1)
10411 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10412 else
10413 tp = curtab;
10414#endif
10415 win = find_win_by_nr(&argvars[off], tp);
10416 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419 rettv->v_type = VAR_STRING;
10420 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421
10422 if (win != NULL && varname != NULL)
10423 {
10424 if (*varname == '&') /* window-local-option */
10425 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010426 /* Set curwin to be our win, temporarily. Also set curbuf, so
10427 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 oldcurwin = curwin;
10429 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010430 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
10434 /* restore previous notion of curwin */
10435 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010436 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 }
10438 else
10439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 if (*varname == NUL)
10441 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10442 * scope prefix before the NUL byte is required by
10443 * find_var_in_ht(). */
10444 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010446 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010448 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 }
10450 }
10451
10452 --emsg_off;
10453}
10454
10455/*
10456 * "glob()" function
10457 */
10458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010460 typval_T *argvars;
10461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 expand_T xpc;
10464
10465 ExpandInit(&xpc);
10466 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 rettv->v_type = VAR_STRING;
10468 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470}
10471
10472/*
10473 * "globpath()" function
10474 */
10475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010477 typval_T *argvars;
10478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479{
10480 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 if (file == NULL)
10485 rettv->vval.v_string = NULL;
10486 else
10487 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488}
10489
10490/*
10491 * "has()" function
10492 */
10493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 typval_T *argvars;
10496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498 int i;
10499 char_u *name;
10500 int n = FALSE;
10501 static char *(has_list[]) =
10502 {
10503#ifdef AMIGA
10504 "amiga",
10505# ifdef FEAT_ARP
10506 "arp",
10507# endif
10508#endif
10509#ifdef __BEOS__
10510 "beos",
10511#endif
10512#ifdef MSDOS
10513# ifdef DJGPP
10514 "dos32",
10515# else
10516 "dos16",
10517# endif
10518#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010519#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 "mac",
10521#endif
10522#if defined(MACOS_X_UNIX)
10523 "macunix",
10524#endif
10525#ifdef OS2
10526 "os2",
10527#endif
10528#ifdef __QNX__
10529 "qnx",
10530#endif
10531#ifdef RISCOS
10532 "riscos",
10533#endif
10534#ifdef UNIX
10535 "unix",
10536#endif
10537#ifdef VMS
10538 "vms",
10539#endif
10540#ifdef WIN16
10541 "win16",
10542#endif
10543#ifdef WIN32
10544 "win32",
10545#endif
10546#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10547 "win32unix",
10548#endif
10549#ifdef WIN64
10550 "win64",
10551#endif
10552#ifdef EBCDIC
10553 "ebcdic",
10554#endif
10555#ifndef CASE_INSENSITIVE_FILENAME
10556 "fname_case",
10557#endif
10558#ifdef FEAT_ARABIC
10559 "arabic",
10560#endif
10561#ifdef FEAT_AUTOCMD
10562 "autocmd",
10563#endif
10564#ifdef FEAT_BEVAL
10565 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010566# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10567 "balloon_multiline",
10568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569#endif
10570#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10571 "builtin_terms",
10572# ifdef ALL_BUILTIN_TCAPS
10573 "all_builtin_terms",
10574# endif
10575#endif
10576#ifdef FEAT_BYTEOFF
10577 "byte_offset",
10578#endif
10579#ifdef FEAT_CINDENT
10580 "cindent",
10581#endif
10582#ifdef FEAT_CLIENTSERVER
10583 "clientserver",
10584#endif
10585#ifdef FEAT_CLIPBOARD
10586 "clipboard",
10587#endif
10588#ifdef FEAT_CMDL_COMPL
10589 "cmdline_compl",
10590#endif
10591#ifdef FEAT_CMDHIST
10592 "cmdline_hist",
10593#endif
10594#ifdef FEAT_COMMENTS
10595 "comments",
10596#endif
10597#ifdef FEAT_CRYPT
10598 "cryptv",
10599#endif
10600#ifdef FEAT_CSCOPE
10601 "cscope",
10602#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010603#ifdef CURSOR_SHAPE
10604 "cursorshape",
10605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606#ifdef DEBUG
10607 "debug",
10608#endif
10609#ifdef FEAT_CON_DIALOG
10610 "dialog_con",
10611#endif
10612#ifdef FEAT_GUI_DIALOG
10613 "dialog_gui",
10614#endif
10615#ifdef FEAT_DIFF
10616 "diff",
10617#endif
10618#ifdef FEAT_DIGRAPHS
10619 "digraphs",
10620#endif
10621#ifdef FEAT_DND
10622 "dnd",
10623#endif
10624#ifdef FEAT_EMACS_TAGS
10625 "emacs_tags",
10626#endif
10627 "eval", /* always present, of course! */
10628#ifdef FEAT_EX_EXTRA
10629 "ex_extra",
10630#endif
10631#ifdef FEAT_SEARCH_EXTRA
10632 "extra_search",
10633#endif
10634#ifdef FEAT_FKMAP
10635 "farsi",
10636#endif
10637#ifdef FEAT_SEARCHPATH
10638 "file_in_path",
10639#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010640#if defined(UNIX) && !defined(USE_SYSTEM)
10641 "filterpipe",
10642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643#ifdef FEAT_FIND_ID
10644 "find_in_path",
10645#endif
10646#ifdef FEAT_FOLDING
10647 "folding",
10648#endif
10649#ifdef FEAT_FOOTER
10650 "footer",
10651#endif
10652#if !defined(USE_SYSTEM) && defined(UNIX)
10653 "fork",
10654#endif
10655#ifdef FEAT_GETTEXT
10656 "gettext",
10657#endif
10658#ifdef FEAT_GUI
10659 "gui",
10660#endif
10661#ifdef FEAT_GUI_ATHENA
10662# ifdef FEAT_GUI_NEXTAW
10663 "gui_neXtaw",
10664# else
10665 "gui_athena",
10666# endif
10667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668#ifdef FEAT_GUI_GTK
10669 "gui_gtk",
10670# ifdef HAVE_GTK2
10671 "gui_gtk2",
10672# endif
10673#endif
10674#ifdef FEAT_GUI_MAC
10675 "gui_mac",
10676#endif
10677#ifdef FEAT_GUI_MOTIF
10678 "gui_motif",
10679#endif
10680#ifdef FEAT_GUI_PHOTON
10681 "gui_photon",
10682#endif
10683#ifdef FEAT_GUI_W16
10684 "gui_win16",
10685#endif
10686#ifdef FEAT_GUI_W32
10687 "gui_win32",
10688#endif
10689#ifdef FEAT_HANGULIN
10690 "hangul_input",
10691#endif
10692#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10693 "iconv",
10694#endif
10695#ifdef FEAT_INS_EXPAND
10696 "insert_expand",
10697#endif
10698#ifdef FEAT_JUMPLIST
10699 "jumplist",
10700#endif
10701#ifdef FEAT_KEYMAP
10702 "keymap",
10703#endif
10704#ifdef FEAT_LANGMAP
10705 "langmap",
10706#endif
10707#ifdef FEAT_LIBCALL
10708 "libcall",
10709#endif
10710#ifdef FEAT_LINEBREAK
10711 "linebreak",
10712#endif
10713#ifdef FEAT_LISP
10714 "lispindent",
10715#endif
10716#ifdef FEAT_LISTCMDS
10717 "listcmds",
10718#endif
10719#ifdef FEAT_LOCALMAP
10720 "localmap",
10721#endif
10722#ifdef FEAT_MENU
10723 "menu",
10724#endif
10725#ifdef FEAT_SESSION
10726 "mksession",
10727#endif
10728#ifdef FEAT_MODIFY_FNAME
10729 "modify_fname",
10730#endif
10731#ifdef FEAT_MOUSE
10732 "mouse",
10733#endif
10734#ifdef FEAT_MOUSESHAPE
10735 "mouseshape",
10736#endif
10737#if defined(UNIX) || defined(VMS)
10738# ifdef FEAT_MOUSE_DEC
10739 "mouse_dec",
10740# endif
10741# ifdef FEAT_MOUSE_GPM
10742 "mouse_gpm",
10743# endif
10744# ifdef FEAT_MOUSE_JSB
10745 "mouse_jsbterm",
10746# endif
10747# ifdef FEAT_MOUSE_NET
10748 "mouse_netterm",
10749# endif
10750# ifdef FEAT_MOUSE_PTERM
10751 "mouse_pterm",
10752# endif
10753# ifdef FEAT_MOUSE_XTERM
10754 "mouse_xterm",
10755# endif
10756#endif
10757#ifdef FEAT_MBYTE
10758 "multi_byte",
10759#endif
10760#ifdef FEAT_MBYTE_IME
10761 "multi_byte_ime",
10762#endif
10763#ifdef FEAT_MULTI_LANG
10764 "multi_lang",
10765#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010766#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010767#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010768 "mzscheme",
10769#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771#ifdef FEAT_OLE
10772 "ole",
10773#endif
10774#ifdef FEAT_OSFILETYPE
10775 "osfiletype",
10776#endif
10777#ifdef FEAT_PATH_EXTRA
10778 "path_extra",
10779#endif
10780#ifdef FEAT_PERL
10781#ifndef DYNAMIC_PERL
10782 "perl",
10783#endif
10784#endif
10785#ifdef FEAT_PYTHON
10786#ifndef DYNAMIC_PYTHON
10787 "python",
10788#endif
10789#endif
10790#ifdef FEAT_POSTSCRIPT
10791 "postscript",
10792#endif
10793#ifdef FEAT_PRINTER
10794 "printer",
10795#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010796#ifdef FEAT_PROFILE
10797 "profile",
10798#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010799#ifdef FEAT_RELTIME
10800 "reltime",
10801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802#ifdef FEAT_QUICKFIX
10803 "quickfix",
10804#endif
10805#ifdef FEAT_RIGHTLEFT
10806 "rightleft",
10807#endif
10808#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10809 "ruby",
10810#endif
10811#ifdef FEAT_SCROLLBIND
10812 "scrollbind",
10813#endif
10814#ifdef FEAT_CMDL_INFO
10815 "showcmd",
10816 "cmdline_info",
10817#endif
10818#ifdef FEAT_SIGNS
10819 "signs",
10820#endif
10821#ifdef FEAT_SMARTINDENT
10822 "smartindent",
10823#endif
10824#ifdef FEAT_SNIFF
10825 "sniff",
10826#endif
10827#ifdef FEAT_STL_OPT
10828 "statusline",
10829#endif
10830#ifdef FEAT_SUN_WORKSHOP
10831 "sun_workshop",
10832#endif
10833#ifdef FEAT_NETBEANS_INTG
10834 "netbeans_intg",
10835#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010836#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010837 "spell",
10838#endif
10839#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 "syntax",
10841#endif
10842#if defined(USE_SYSTEM) || !defined(UNIX)
10843 "system",
10844#endif
10845#ifdef FEAT_TAG_BINS
10846 "tag_binary",
10847#endif
10848#ifdef FEAT_TAG_OLDSTATIC
10849 "tag_old_static",
10850#endif
10851#ifdef FEAT_TAG_ANYWHITE
10852 "tag_any_white",
10853#endif
10854#ifdef FEAT_TCL
10855# ifndef DYNAMIC_TCL
10856 "tcl",
10857# endif
10858#endif
10859#ifdef TERMINFO
10860 "terminfo",
10861#endif
10862#ifdef FEAT_TERMRESPONSE
10863 "termresponse",
10864#endif
10865#ifdef FEAT_TEXTOBJ
10866 "textobjects",
10867#endif
10868#ifdef HAVE_TGETENT
10869 "tgetent",
10870#endif
10871#ifdef FEAT_TITLE
10872 "title",
10873#endif
10874#ifdef FEAT_TOOLBAR
10875 "toolbar",
10876#endif
10877#ifdef FEAT_USR_CMDS
10878 "user-commands", /* was accidentally included in 5.4 */
10879 "user_commands",
10880#endif
10881#ifdef FEAT_VIMINFO
10882 "viminfo",
10883#endif
10884#ifdef FEAT_VERTSPLIT
10885 "vertsplit",
10886#endif
10887#ifdef FEAT_VIRTUALEDIT
10888 "virtualedit",
10889#endif
10890#ifdef FEAT_VISUAL
10891 "visual",
10892#endif
10893#ifdef FEAT_VISUALEXTRA
10894 "visualextra",
10895#endif
10896#ifdef FEAT_VREPLACE
10897 "vreplace",
10898#endif
10899#ifdef FEAT_WILDIGN
10900 "wildignore",
10901#endif
10902#ifdef FEAT_WILDMENU
10903 "wildmenu",
10904#endif
10905#ifdef FEAT_WINDOWS
10906 "windows",
10907#endif
10908#ifdef FEAT_WAK
10909 "winaltkeys",
10910#endif
10911#ifdef FEAT_WRITEBACKUP
10912 "writebackup",
10913#endif
10914#ifdef FEAT_XIM
10915 "xim",
10916#endif
10917#ifdef FEAT_XFONTSET
10918 "xfontset",
10919#endif
10920#ifdef USE_XSMP
10921 "xsmp",
10922#endif
10923#ifdef USE_XSMP_INTERACT
10924 "xsmp_interact",
10925#endif
10926#ifdef FEAT_XCLIPBOARD
10927 "xterm_clipboard",
10928#endif
10929#ifdef FEAT_XTERM_SAVE
10930 "xterm_save",
10931#endif
10932#if defined(UNIX) && defined(FEAT_X11)
10933 "X11",
10934#endif
10935 NULL
10936 };
10937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 for (i = 0; has_list[i] != NULL; ++i)
10940 if (STRICMP(name, has_list[i]) == 0)
10941 {
10942 n = TRUE;
10943 break;
10944 }
10945
10946 if (n == FALSE)
10947 {
10948 if (STRNICMP(name, "patch", 5) == 0)
10949 n = has_patch(atoi((char *)name + 5));
10950 else if (STRICMP(name, "vim_starting") == 0)
10951 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010952#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10953 else if (STRICMP(name, "balloon_multiline") == 0)
10954 n = multiline_balloon_available();
10955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956#ifdef DYNAMIC_TCL
10957 else if (STRICMP(name, "tcl") == 0)
10958 n = tcl_enabled(FALSE);
10959#endif
10960#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10961 else if (STRICMP(name, "iconv") == 0)
10962 n = iconv_enabled(FALSE);
10963#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010964#ifdef DYNAMIC_MZSCHEME
10965 else if (STRICMP(name, "mzscheme") == 0)
10966 n = mzscheme_enabled(FALSE);
10967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968#ifdef DYNAMIC_RUBY
10969 else if (STRICMP(name, "ruby") == 0)
10970 n = ruby_enabled(FALSE);
10971#endif
10972#ifdef DYNAMIC_PYTHON
10973 else if (STRICMP(name, "python") == 0)
10974 n = python_enabled(FALSE);
10975#endif
10976#ifdef DYNAMIC_PERL
10977 else if (STRICMP(name, "perl") == 0)
10978 n = perl_enabled(FALSE);
10979#endif
10980#ifdef FEAT_GUI
10981 else if (STRICMP(name, "gui_running") == 0)
10982 n = (gui.in_use || gui.starting);
10983# ifdef FEAT_GUI_W32
10984 else if (STRICMP(name, "gui_win32s") == 0)
10985 n = gui_is_win32s();
10986# endif
10987# ifdef FEAT_BROWSE
10988 else if (STRICMP(name, "browse") == 0)
10989 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10990# endif
10991#endif
10992#ifdef FEAT_SYN_HL
10993 else if (STRICMP(name, "syntax_items") == 0)
10994 n = syntax_present(curbuf);
10995#endif
10996#if defined(WIN3264)
10997 else if (STRICMP(name, "win95") == 0)
10998 n = mch_windows95();
10999#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011000#ifdef FEAT_NETBEANS_INTG
11001 else if (STRICMP(name, "netbeans_enabled") == 0)
11002 n = usingNetbeans;
11003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 }
11005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007}
11008
11009/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 * "has_key()" function
11011 */
11012 static void
11013f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016{
11017 rettv->vval.v_number = 0;
11018 if (argvars[0].v_type != VAR_DICT)
11019 {
11020 EMSG(_(e_dictreq));
11021 return;
11022 }
11023 if (argvars[0].vval.v_dict == NULL)
11024 return;
11025
11026 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011027 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011028}
11029
11030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 * "hasmapto()" function
11032 */
11033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 typval_T *argvars;
11036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
11038 char_u *name;
11039 char_u *mode;
11040 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011041 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011044 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 mode = (char_u *)"nvo";
11046 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011049 if (argvars[2].v_type != VAR_UNKNOWN)
11050 abbr = get_tv_number(&argvars[2]);
11051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052
Bram Moolenaar2c932302006-03-18 21:42:09 +000011053 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057}
11058
11059/*
11060 * "histadd()" function
11061 */
11062/*ARGSUSED*/
11063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064f_histadd(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#ifdef FEAT_CMDHIST
11069 int histype;
11070 char_u *str;
11071 char_u buf[NUMBUFLEN];
11072#endif
11073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 if (check_restricted() || check_secure())
11076 return;
11077#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11079 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 if (histype >= 0)
11081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 if (*str != NUL)
11084 {
11085 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 return;
11088 }
11089 }
11090#endif
11091}
11092
11093/*
11094 * "histdel()" function
11095 */
11096/*ARGSUSED*/
11097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101{
11102#ifdef FEAT_CMDHIST
11103 int n;
11104 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11108 if (str == NULL)
11109 n = 0;
11110 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011113 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 else
11118 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011119 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 get_tv_string_buf(&argvars[1], buf));
11121 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124#endif
11125}
11126
11127/*
11128 * "histget()" function
11129 */
11130/*ARGSUSED*/
11131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 typval_T *argvars;
11134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135{
11136#ifdef FEAT_CMDHIST
11137 int type;
11138 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11142 if (str == NULL)
11143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 {
11146 type = get_histtype(str);
11147 if (argvars[1].v_type == VAR_UNKNOWN)
11148 idx = get_history_idx(type);
11149 else
11150 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11151 /* -1 on type error */
11152 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158}
11159
11160/*
11161 * "histnr()" function
11162 */
11163/*ARGSUSED*/
11164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011166 typval_T *argvars;
11167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168{
11169 int i;
11170
11171#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 char_u *history = get_tv_string_chk(&argvars[0]);
11173
11174 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 if (i >= HIST_CMD && i < HIST_COUNT)
11176 i = get_history_idx(i);
11177 else
11178#endif
11179 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181}
11182
11183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 * "highlightID(name)" function
11185 */
11186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T *argvars;
11189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192}
11193
11194/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011195 * "highlight_exists()" function
11196 */
11197 static void
11198f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011199 typval_T *argvars;
11200 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011201{
11202 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11203}
11204
11205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 * "hostname()" function
11207 */
11208/*ARGSUSED*/
11209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *argvars;
11212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
11214 char_u hostname[256];
11215
11216 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 rettv->v_type = VAR_STRING;
11218 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219}
11220
11221/*
11222 * iconv() function
11223 */
11224/*ARGSUSED*/
11225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 typval_T *argvars;
11228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229{
11230#ifdef FEAT_MBYTE
11231 char_u buf1[NUMBUFLEN];
11232 char_u buf2[NUMBUFLEN];
11233 char_u *from, *to, *str;
11234 vimconv_T vimconv;
11235#endif
11236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237 rettv->v_type = VAR_STRING;
11238 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239
11240#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 str = get_tv_string(&argvars[0]);
11242 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11243 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 vimconv.vc_type = CONV_NONE;
11245 convert_setup(&vimconv, from, to);
11246
11247 /* If the encodings are equal, no conversion needed. */
11248 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252
11253 convert_setup(&vimconv, NULL, NULL);
11254 vim_free(from);
11255 vim_free(to);
11256#endif
11257}
11258
11259/*
11260 * "indent()" function
11261 */
11262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011264 typval_T *argvars;
11265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266{
11267 linenr_T lnum;
11268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274}
11275
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011276/*
11277 * "index()" function
11278 */
11279 static void
11280f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011283{
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 list_T *l;
11285 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011286 long idx = 0;
11287 int ic = FALSE;
11288
11289 rettv->vval.v_number = -1;
11290 if (argvars[0].v_type != VAR_LIST)
11291 {
11292 EMSG(_(e_listreq));
11293 return;
11294 }
11295 l = argvars[0].vval.v_list;
11296 if (l != NULL)
11297 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011298 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011299 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 int error = FALSE;
11302
Bram Moolenaar758711c2005-02-02 23:11:38 +000011303 /* Start at specified item. Use the cached index that list_find()
11304 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011306 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011307 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 ic = get_tv_number_chk(&argvars[3], &error);
11309 if (error)
11310 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011311 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011312
Bram Moolenaar758711c2005-02-02 23:11:38 +000011313 for ( ; item != NULL; item = item->li_next, ++idx)
11314 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011315 {
11316 rettv->vval.v_number = idx;
11317 break;
11318 }
11319 }
11320}
11321
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322static int inputsecret_flag = 0;
11323
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011324static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11325
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011327 * This function is used by f_input() and f_inputdialog() functions. The third
11328 * argument to f_input() specifies the type of completion to use at the
11329 * prompt. The third argument to f_inputdialog() specifies the value to return
11330 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 */
11332 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011333get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011334 typval_T *argvars;
11335 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011336 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 char_u *p = NULL;
11340 int c;
11341 char_u buf[NUMBUFLEN];
11342 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011344 int xp_type = EXPAND_NOTHING;
11345 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348
11349#ifdef NO_CONSOLE_INPUT
11350 /* While starting up, there is no place to enter text. */
11351 if (no_console_input())
11352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 return;
11355 }
11356#endif
11357
11358 cmd_silent = FALSE; /* Want to see the prompt. */
11359 if (prompt != NULL)
11360 {
11361 /* Only the part of the message after the last NL is considered as
11362 * prompt for the command line */
11363 p = vim_strrchr(prompt, '\n');
11364 if (p == NULL)
11365 p = prompt;
11366 else
11367 {
11368 ++p;
11369 c = *p;
11370 *p = NUL;
11371 msg_start();
11372 msg_clr_eos();
11373 msg_puts_attr(prompt, echo_attr);
11374 msg_didout = FALSE;
11375 msg_starthere();
11376 *p = c;
11377 }
11378 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 if (argvars[1].v_type != VAR_UNKNOWN)
11381 {
11382 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11383 if (defstr != NULL)
11384 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011386 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011387 {
11388 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011389 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011390 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011391
Bram Moolenaar4463f292005-09-25 22:20:24 +000011392 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011393
Bram Moolenaar4463f292005-09-25 22:20:24 +000011394 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11395 if (xp_name == NULL)
11396 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011397
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011398 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011399
Bram Moolenaar4463f292005-09-25 22:20:24 +000011400 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11401 &xp_arg) == FAIL)
11402 return;
11403 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011404 }
11405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011406 if (defstr != NULL)
11407 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011408 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11409 xp_type, xp_arg);
11410
11411 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 /* since the user typed this, no need to wait for return */
11414 need_wait_return = FALSE;
11415 msg_didout = FALSE;
11416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 cmd_silent = cmd_silent_save;
11418}
11419
11420/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011421 * "input()" function
11422 * Also handles inputsecret() when inputsecret is set.
11423 */
11424 static void
11425f_input(argvars, rettv)
11426 typval_T *argvars;
11427 typval_T *rettv;
11428{
11429 get_user_input(argvars, rettv, FALSE);
11430}
11431
11432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 * "inputdialog()" function
11434 */
11435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *argvars;
11438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
11440#if defined(FEAT_GUI_TEXTDIALOG)
11441 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11442 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11443 {
11444 char_u *message;
11445 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 message = get_tv_string_chk(&argvars[0]);
11449 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011450 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011451 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 else
11453 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011454 if (message != NULL && defstr != NULL
11455 && do_dialog(VIM_QUESTION, NULL, message,
11456 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 else
11459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 if (message != NULL && defstr != NULL
11461 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011462 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463 rettv->vval.v_string = vim_strsave(
11464 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 }
11470 else
11471#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011472 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473}
11474
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011475/*
11476 * "inputlist()" function
11477 */
11478 static void
11479f_inputlist(argvars, rettv)
11480 typval_T *argvars;
11481 typval_T *rettv;
11482{
11483 listitem_T *li;
11484 int selected;
11485 int mouse_used;
11486
11487 rettv->vval.v_number = 0;
11488#ifdef NO_CONSOLE_INPUT
11489 /* While starting up, there is no place to enter text. */
11490 if (no_console_input())
11491 return;
11492#endif
11493 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11494 {
11495 EMSG2(_(e_listarg), "inputlist()");
11496 return;
11497 }
11498
11499 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011500 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011501 lines_left = Rows; /* avoid more prompt */
11502 msg_scroll = TRUE;
11503 msg_clr_eos();
11504
11505 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11506 {
11507 msg_puts(get_tv_string(&li->li_tv));
11508 msg_putchar('\n');
11509 }
11510
11511 /* Ask for choice. */
11512 selected = prompt_for_number(&mouse_used);
11513 if (mouse_used)
11514 selected -= lines_left;
11515
11516 rettv->vval.v_number = selected;
11517}
11518
11519
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11521
11522/*
11523 * "inputrestore()" function
11524 */
11525/*ARGSUSED*/
11526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *argvars;
11529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 if (ga_userinput.ga_len > 0)
11532 {
11533 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11535 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 }
11538 else if (p_verbose > 1)
11539 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011540 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 }
11543}
11544
11545/*
11546 * "inputsave()" function
11547 */
11548/*ARGSUSED*/
11549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011551 typval_T *argvars;
11552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553{
11554 /* Add an entry to the stack of typehead storage. */
11555 if (ga_grow(&ga_userinput, 1) == OK)
11556 {
11557 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11558 + ga_userinput.ga_len);
11559 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 }
11562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564}
11565
11566/*
11567 * "inputsecret()" function
11568 */
11569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
11574 ++cmdline_star;
11575 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 --cmdline_star;
11578 --inputsecret_flag;
11579}
11580
11581/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011582 * "insert()" function
11583 */
11584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 typval_T *argvars;
11587 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011588{
11589 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011590 listitem_T *item;
11591 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011594 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011595 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011596 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011597 else if ((l = argvars[0].vval.v_list) != NULL
11598 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011599 {
11600 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011601 before = get_tv_number_chk(&argvars[2], &error);
11602 if (error)
11603 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604
Bram Moolenaar758711c2005-02-02 23:11:38 +000011605 if (before == l->lv_len)
11606 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011607 else
11608 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011609 item = list_find(l, before);
11610 if (item == NULL)
11611 {
11612 EMSGN(_(e_listidx), before);
11613 l = NULL;
11614 }
11615 }
11616 if (l != NULL)
11617 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011618 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011619 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011620 }
11621 }
11622}
11623
11624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 * "isdirectory()" function
11626 */
11627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011629 typval_T *argvars;
11630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633}
11634
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011635/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011636 * "islocked()" function
11637 */
11638 static void
11639f_islocked(argvars, rettv)
11640 typval_T *argvars;
11641 typval_T *rettv;
11642{
11643 lval_T lv;
11644 char_u *end;
11645 dictitem_T *di;
11646
11647 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011648 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11649 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011650 if (end != NULL && lv.ll_name != NULL)
11651 {
11652 if (*end != NUL)
11653 EMSG(_(e_trailing));
11654 else
11655 {
11656 if (lv.ll_tv == NULL)
11657 {
11658 if (check_changedtick(lv.ll_name))
11659 rettv->vval.v_number = 1; /* always locked */
11660 else
11661 {
11662 di = find_var(lv.ll_name, NULL);
11663 if (di != NULL)
11664 {
11665 /* Consider a variable locked when:
11666 * 1. the variable itself is locked
11667 * 2. the value of the variable is locked.
11668 * 3. the List or Dict value is locked.
11669 */
11670 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11671 || tv_islocked(&di->di_tv));
11672 }
11673 }
11674 }
11675 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011676 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011677 else if (lv.ll_newkey != NULL)
11678 EMSG2(_(e_dictkey), lv.ll_newkey);
11679 else if (lv.ll_list != NULL)
11680 /* List item. */
11681 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11682 else
11683 /* Dictionary item. */
11684 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11685 }
11686 }
11687
11688 clear_lval(&lv);
11689}
11690
Bram Moolenaar33570922005-01-25 22:26:29 +000011691static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011692
11693/*
11694 * Turn a dict into a list:
11695 * "what" == 0: list of keys
11696 * "what" == 1: list of values
11697 * "what" == 2: list of items
11698 */
11699 static void
11700dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011701 typval_T *argvars;
11702 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011703 int what;
11704{
Bram Moolenaar33570922005-01-25 22:26:29 +000011705 list_T *l2;
11706 dictitem_T *di;
11707 hashitem_T *hi;
11708 listitem_T *li;
11709 listitem_T *li2;
11710 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011711 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011712
11713 rettv->vval.v_number = 0;
11714 if (argvars[0].v_type != VAR_DICT)
11715 {
11716 EMSG(_(e_dictreq));
11717 return;
11718 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011719 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011720 return;
11721
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011722 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011723 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011724
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011725 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011726 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011727 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011728 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011729 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011730 --todo;
11731 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011732
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011733 li = listitem_alloc();
11734 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011735 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011736 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011737
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011738 if (what == 0)
11739 {
11740 /* keys() */
11741 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011742 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011743 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11744 }
11745 else if (what == 1)
11746 {
11747 /* values() */
11748 copy_tv(&di->di_tv, &li->li_tv);
11749 }
11750 else
11751 {
11752 /* items() */
11753 l2 = list_alloc();
11754 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011755 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011756 li->li_tv.vval.v_list = l2;
11757 if (l2 == NULL)
11758 break;
11759 ++l2->lv_refcount;
11760
11761 li2 = listitem_alloc();
11762 if (li2 == NULL)
11763 break;
11764 list_append(l2, li2);
11765 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011766 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011767 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11768
11769 li2 = listitem_alloc();
11770 if (li2 == NULL)
11771 break;
11772 list_append(l2, li2);
11773 copy_tv(&di->di_tv, &li2->li_tv);
11774 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011775 }
11776 }
11777}
11778
11779/*
11780 * "items(dict)" function
11781 */
11782 static void
11783f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *argvars;
11785 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011786{
11787 dict_list(argvars, rettv, 2);
11788}
11789
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011791 * "join()" function
11792 */
11793 static void
11794f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011795 typval_T *argvars;
11796 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011797{
11798 garray_T ga;
11799 char_u *sep;
11800
11801 rettv->vval.v_number = 0;
11802 if (argvars[0].v_type != VAR_LIST)
11803 {
11804 EMSG(_(e_listreq));
11805 return;
11806 }
11807 if (argvars[0].vval.v_list == NULL)
11808 return;
11809 if (argvars[1].v_type == VAR_UNKNOWN)
11810 sep = (char_u *)" ";
11811 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011813
11814 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815
11816 if (sep != NULL)
11817 {
11818 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011819 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 ga_append(&ga, NUL);
11821 rettv->vval.v_string = (char_u *)ga.ga_data;
11822 }
11823 else
11824 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825}
11826
11827/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011828 * "keys()" function
11829 */
11830 static void
11831f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011832 typval_T *argvars;
11833 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011834{
11835 dict_list(argvars, rettv, 0);
11836}
11837
11838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 * "last_buffer_nr()" function.
11840 */
11841/*ARGSUSED*/
11842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011844 typval_T *argvars;
11845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846{
11847 int n = 0;
11848 buf_T *buf;
11849
11850 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11851 if (n < buf->b_fnum)
11852 n = buf->b_fnum;
11853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011855}
11856
11857/*
11858 * "len()" function
11859 */
11860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011862 typval_T *argvars;
11863 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011864{
11865 switch (argvars[0].v_type)
11866 {
11867 case VAR_STRING:
11868 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->vval.v_number = (varnumber_T)STRLEN(
11870 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011871 break;
11872 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011874 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011875 case VAR_DICT:
11876 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11877 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011878 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011879 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011880 break;
11881 }
11882}
11883
Bram Moolenaar33570922005-01-25 22:26:29 +000011884static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011885
11886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011888 typval_T *argvars;
11889 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011890 int type;
11891{
11892#ifdef FEAT_LIBCALL
11893 char_u *string_in;
11894 char_u **string_result;
11895 int nr_result;
11896#endif
11897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011899 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011903
11904 if (check_restricted() || check_secure())
11905 return;
11906
11907#ifdef FEAT_LIBCALL
11908 /* The first two args must be strings, otherwise its meaningless */
11909 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11910 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011911 string_in = NULL;
11912 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011913 string_in = argvars[2].vval.v_string;
11914 if (type == VAR_NUMBER)
11915 string_result = NULL;
11916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011917 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011918 if (mch_libcall(argvars[0].vval.v_string,
11919 argvars[1].vval.v_string,
11920 string_in,
11921 argvars[2].vval.v_number,
11922 string_result,
11923 &nr_result) == OK
11924 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011925 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011926 }
11927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928}
11929
11930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011931 * "libcall()" function
11932 */
11933 static void
11934f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 typval_T *argvars;
11936 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011937{
11938 libcall_common(argvars, rettv, VAR_STRING);
11939}
11940
11941/*
11942 * "libcallnr()" function
11943 */
11944 static void
11945f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011946 typval_T *argvars;
11947 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948{
11949 libcall_common(argvars, rettv, VAR_NUMBER);
11950}
11951
11952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 * "line(string)" function
11954 */
11955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011957 typval_T *argvars;
11958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959{
11960 linenr_T lnum = 0;
11961 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011962 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011964 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 if (fp != NULL)
11966 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968}
11969
11970/*
11971 * "line2byte(lnum)" function
11972 */
11973/*ARGSUSED*/
11974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *argvars;
11977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
11979#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981#else
11982 linenr_T lnum;
11983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11989 if (rettv->vval.v_number >= 0)
11990 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991#endif
11992}
11993
11994/*
11995 * "lispindent(lnum)" function
11996 */
11997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *argvars;
12000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001{
12002#ifdef FEAT_LISP
12003 pos_T pos;
12004 linenr_T lnum;
12005
12006 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12009 {
12010 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 curwin->w_cursor = pos;
12013 }
12014 else
12015#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017}
12018
12019/*
12020 * "localtime()" function
12021 */
12022/*ARGSUSED*/
12023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012024f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012025 typval_T *argvars;
12026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029}
12030
Bram Moolenaar33570922005-01-25 22:26:29 +000012031static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
12033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012035 typval_T *argvars;
12036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 int exact;
12038{
12039 char_u *keys;
12040 char_u *which;
12041 char_u buf[NUMBUFLEN];
12042 char_u *keys_buf = NULL;
12043 char_u *rhs;
12044 int mode;
12045 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012046 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047
12048 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 rettv->v_type = VAR_STRING;
12050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 if (*keys == NUL)
12054 return;
12055
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012056 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012059 if (argvars[2].v_type != VAR_UNKNOWN)
12060 abbr = get_tv_number(&argvars[2]);
12061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 else
12063 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012064 if (which == NULL)
12065 return;
12066
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 mode = get_map_mode(&which, 0);
12068
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012069 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012070 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 vim_free(keys_buf);
12072 if (rhs != NULL)
12073 {
12074 ga_init(&ga);
12075 ga.ga_itemsize = 1;
12076 ga.ga_growsize = 40;
12077
12078 while (*rhs != NUL)
12079 ga_concat(&ga, str2special(&rhs, FALSE));
12080
12081 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 }
12084}
12085
12086/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012087 * "map()" function
12088 */
12089 static void
12090f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012091 typval_T *argvars;
12092 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012093{
12094 filter_map(argvars, rettv, TRUE);
12095}
12096
12097/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012098 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 */
12100 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012101f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012102 typval_T *argvars;
12103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106}
12107
12108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012109 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110 */
12111 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012112f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012113 typval_T *argvars;
12114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012116 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117}
12118
Bram Moolenaar33570922005-01-25 22:26:29 +000012119static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120
12121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012123 typval_T *argvars;
12124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 int type;
12126{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012127 char_u *str = NULL;
12128 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 char_u *pat;
12130 regmatch_T regmatch;
12131 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012132 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133 char_u *save_cpo;
12134 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012135 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012136 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012137 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 list_T *l = NULL;
12139 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012140 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012141 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142
12143 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12144 save_cpo = p_cpo;
12145 p_cpo = (char_u *)"";
12146
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012147 rettv->vval.v_number = -1;
12148 if (type == 3)
12149 {
12150 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012151 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012152 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012153 }
12154 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 rettv->v_type = VAR_STRING;
12157 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012160 if (argvars[0].v_type == VAR_LIST)
12161 {
12162 if ((l = argvars[0].vval.v_list) == NULL)
12163 goto theend;
12164 li = l->lv_first;
12165 }
12166 else
12167 expr = str = get_tv_string(&argvars[0]);
12168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012169 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12170 if (pat == NULL)
12171 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012172
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012173 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 int error = FALSE;
12176
12177 start = get_tv_number_chk(&argvars[2], &error);
12178 if (error)
12179 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012180 if (l != NULL)
12181 {
12182 li = list_find(l, start);
12183 if (li == NULL)
12184 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012185 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012186 }
12187 else
12188 {
12189 if (start < 0)
12190 start = 0;
12191 if (start > (long)STRLEN(str))
12192 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012193 /* When "count" argument is there ignore matches before "start",
12194 * otherwise skip part of the string. Differs when pattern is "^"
12195 * or "\<". */
12196 if (argvars[3].v_type != VAR_UNKNOWN)
12197 startcol = start;
12198 else
12199 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012200 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012201
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012202 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012203 nth = get_tv_number_chk(&argvars[3], &error);
12204 if (error)
12205 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 }
12207
12208 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12209 if (regmatch.regprog != NULL)
12210 {
12211 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012212
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012213 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012214 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012215 if (l != NULL)
12216 {
12217 if (li == NULL)
12218 {
12219 match = FALSE;
12220 break;
12221 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012222 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012223 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012224 if (str == NULL)
12225 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012226 }
12227
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012228 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012229
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012230 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012231 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012232 if (l == NULL && !match)
12233 break;
12234
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012235 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012236 if (l != NULL)
12237 {
12238 li = li->li_next;
12239 ++idx;
12240 }
12241 else
12242 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012243#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012244 startcol = (colnr_T)(regmatch.startp[0]
12245 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012246#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012247 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012248#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012249 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012250 }
12251
12252 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012254 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012256 int i;
12257
12258 /* return list with matched string and submatches */
12259 for (i = 0; i < NSUBEXP; ++i)
12260 {
12261 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012262 {
12263 if (list_append_string(rettv->vval.v_list,
12264 (char_u *)"", 0) == FAIL)
12265 break;
12266 }
12267 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012268 regmatch.startp[i],
12269 (int)(regmatch.endp[i] - regmatch.startp[i]))
12270 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012271 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012272 }
12273 }
12274 else if (type == 2)
12275 {
12276 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012277 if (l != NULL)
12278 copy_tv(&li->li_tv, rettv);
12279 else
12280 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012282 }
12283 else if (l != NULL)
12284 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 else
12286 {
12287 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 (varnumber_T)(regmatch.startp[0] - str);
12290 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012293 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 }
12295 }
12296 vim_free(regmatch.regprog);
12297 }
12298
12299theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012300 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 p_cpo = save_cpo;
12302}
12303
12304/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012305 * "match()" function
12306 */
12307 static void
12308f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012309 typval_T *argvars;
12310 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012311{
12312 find_some_match(argvars, rettv, 1);
12313}
12314
12315/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012316 * "matcharg()" function
12317 */
12318 static void
12319f_matcharg(argvars, rettv)
12320 typval_T *argvars;
12321 typval_T *rettv;
12322{
12323 if (rettv_list_alloc(rettv) == OK)
12324 {
12325#ifdef FEAT_SEARCH_EXTRA
12326 int mi = get_tv_number(&argvars[0]);
12327
12328 if (mi >= 1 && mi <= 3)
12329 {
12330 list_append_string(rettv->vval.v_list,
12331 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12332 list_append_string(rettv->vval.v_list,
12333 curwin->w_match_pat[mi - 1], -1);
12334 }
12335#endif
12336 }
12337}
12338
12339/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012340 * "matchend()" function
12341 */
12342 static void
12343f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012344 typval_T *argvars;
12345 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012346{
12347 find_some_match(argvars, rettv, 0);
12348}
12349
12350/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012351 * "matchlist()" function
12352 */
12353 static void
12354f_matchlist(argvars, rettv)
12355 typval_T *argvars;
12356 typval_T *rettv;
12357{
12358 find_some_match(argvars, rettv, 3);
12359}
12360
12361/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012362 * "matchstr()" function
12363 */
12364 static void
12365f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 typval_T *argvars;
12367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012368{
12369 find_some_match(argvars, rettv, 2);
12370}
12371
Bram Moolenaar33570922005-01-25 22:26:29 +000012372static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012373
12374 static void
12375max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012376 typval_T *argvars;
12377 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012378 int domax;
12379{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012380 long n = 0;
12381 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012382 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012383
12384 if (argvars[0].v_type == VAR_LIST)
12385 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012386 list_T *l;
12387 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012388
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012389 l = argvars[0].vval.v_list;
12390 if (l != NULL)
12391 {
12392 li = l->lv_first;
12393 if (li != NULL)
12394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012395 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012396 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012397 {
12398 li = li->li_next;
12399 if (li == NULL)
12400 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012402 if (domax ? i > n : i < n)
12403 n = i;
12404 }
12405 }
12406 }
12407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012408 else if (argvars[0].v_type == VAR_DICT)
12409 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012410 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012411 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012412 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012413 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012414
12415 d = argvars[0].vval.v_dict;
12416 if (d != NULL)
12417 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012418 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012419 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012421 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012422 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012423 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012424 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012425 if (first)
12426 {
12427 n = i;
12428 first = FALSE;
12429 }
12430 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431 n = i;
12432 }
12433 }
12434 }
12435 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012436 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012437 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012438 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012439}
12440
12441/*
12442 * "max()" function
12443 */
12444 static void
12445f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012446 typval_T *argvars;
12447 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012448{
12449 max_min(argvars, rettv, TRUE);
12450}
12451
12452/*
12453 * "min()" function
12454 */
12455 static void
12456f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012457 typval_T *argvars;
12458 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012459{
12460 max_min(argvars, rettv, FALSE);
12461}
12462
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012463static int mkdir_recurse __ARGS((char_u *dir, int prot));
12464
12465/*
12466 * Create the directory in which "dir" is located, and higher levels when
12467 * needed.
12468 */
12469 static int
12470mkdir_recurse(dir, prot)
12471 char_u *dir;
12472 int prot;
12473{
12474 char_u *p;
12475 char_u *updir;
12476 int r = FAIL;
12477
12478 /* Get end of directory name in "dir".
12479 * We're done when it's "/" or "c:/". */
12480 p = gettail_sep(dir);
12481 if (p <= get_past_head(dir))
12482 return OK;
12483
12484 /* If the directory exists we're done. Otherwise: create it.*/
12485 updir = vim_strnsave(dir, (int)(p - dir));
12486 if (updir == NULL)
12487 return FAIL;
12488 if (mch_isdir(updir))
12489 r = OK;
12490 else if (mkdir_recurse(updir, prot) == OK)
12491 r = vim_mkdir_emsg(updir, prot);
12492 vim_free(updir);
12493 return r;
12494}
12495
12496#ifdef vim_mkdir
12497/*
12498 * "mkdir()" function
12499 */
12500 static void
12501f_mkdir(argvars, rettv)
12502 typval_T *argvars;
12503 typval_T *rettv;
12504{
12505 char_u *dir;
12506 char_u buf[NUMBUFLEN];
12507 int prot = 0755;
12508
12509 rettv->vval.v_number = FAIL;
12510 if (check_restricted() || check_secure())
12511 return;
12512
12513 dir = get_tv_string_buf(&argvars[0], buf);
12514 if (argvars[1].v_type != VAR_UNKNOWN)
12515 {
12516 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 prot = get_tv_number_chk(&argvars[2], NULL);
12518 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012519 mkdir_recurse(dir, prot);
12520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012521 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012522}
12523#endif
12524
Bram Moolenaar0d660222005-01-07 21:51:51 +000012525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 * "mode()" function
12527 */
12528/*ARGSUSED*/
12529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012530f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 typval_T *argvars;
12532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533{
12534 char_u buf[2];
12535
12536#ifdef FEAT_VISUAL
12537 if (VIsual_active)
12538 {
12539 if (VIsual_select)
12540 buf[0] = VIsual_mode + 's' - 'v';
12541 else
12542 buf[0] = VIsual_mode;
12543 }
12544 else
12545#endif
12546 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12547 buf[0] = 'r';
12548 else if (State & INSERT)
12549 {
12550 if (State & REPLACE_FLAG)
12551 buf[0] = 'R';
12552 else
12553 buf[0] = 'i';
12554 }
12555 else if (State & CMDLINE)
12556 buf[0] = 'c';
12557 else
12558 buf[0] = 'n';
12559
12560 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 rettv->vval.v_string = vim_strsave(buf);
12562 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563}
12564
12565/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566 * "nextnonblank()" function
12567 */
12568 static void
12569f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012570 typval_T *argvars;
12571 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012572{
12573 linenr_T lnum;
12574
12575 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578 {
12579 lnum = 0;
12580 break;
12581 }
12582 if (*skipwhite(ml_get(lnum)) != NUL)
12583 break;
12584 }
12585 rettv->vval.v_number = lnum;
12586}
12587
12588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589 * "nr2char()" function
12590 */
12591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 typval_T *argvars;
12594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595{
12596 char_u buf[NUMBUFLEN];
12597
12598#ifdef FEAT_MBYTE
12599 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012600 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601 else
12602#endif
12603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 buf[1] = NUL;
12606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012607 rettv->v_type = VAR_STRING;
12608 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012609}
12610
12611/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012612 * "pathshorten()" function
12613 */
12614 static void
12615f_pathshorten(argvars, rettv)
12616 typval_T *argvars;
12617 typval_T *rettv;
12618{
12619 char_u *p;
12620
12621 rettv->v_type = VAR_STRING;
12622 p = get_tv_string_chk(&argvars[0]);
12623 if (p == NULL)
12624 rettv->vval.v_string = NULL;
12625 else
12626 {
12627 p = vim_strsave(p);
12628 rettv->vval.v_string = p;
12629 if (p != NULL)
12630 shorten_dir(p);
12631 }
12632}
12633
12634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012635 * "prevnonblank()" function
12636 */
12637 static void
12638f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 typval_T *argvars;
12640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641{
12642 linenr_T lnum;
12643
12644 lnum = get_tv_lnum(argvars);
12645 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12646 lnum = 0;
12647 else
12648 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12649 --lnum;
12650 rettv->vval.v_number = lnum;
12651}
12652
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012653#ifdef HAVE_STDARG_H
12654/* This dummy va_list is here because:
12655 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12656 * - locally in the function results in a "used before set" warning
12657 * - using va_start() to initialize it gives "function with fixed args" error */
12658static va_list ap;
12659#endif
12660
Bram Moolenaar8c711452005-01-14 21:53:12 +000012661/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012662 * "printf()" function
12663 */
12664 static void
12665f_printf(argvars, rettv)
12666 typval_T *argvars;
12667 typval_T *rettv;
12668{
12669 rettv->v_type = VAR_STRING;
12670 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012671#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012672 {
12673 char_u buf[NUMBUFLEN];
12674 int len;
12675 char_u *s;
12676 int saved_did_emsg = did_emsg;
12677 char *fmt;
12678
12679 /* Get the required length, allocate the buffer and do it for real. */
12680 did_emsg = FALSE;
12681 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012682 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012683 if (!did_emsg)
12684 {
12685 s = alloc(len + 1);
12686 if (s != NULL)
12687 {
12688 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012689 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012690 }
12691 }
12692 did_emsg |= saved_did_emsg;
12693 }
12694#endif
12695}
12696
12697/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012698 * "pumvisible()" function
12699 */
12700/*ARGSUSED*/
12701 static void
12702f_pumvisible(argvars, rettv)
12703 typval_T *argvars;
12704 typval_T *rettv;
12705{
12706 rettv->vval.v_number = 0;
12707#ifdef FEAT_INS_EXPAND
12708 if (pum_visible())
12709 rettv->vval.v_number = 1;
12710#endif
12711}
12712
12713/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012714 * "range()" function
12715 */
12716 static void
12717f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012718 typval_T *argvars;
12719 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012720{
12721 long start;
12722 long end;
12723 long stride = 1;
12724 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012728 if (argvars[1].v_type == VAR_UNKNOWN)
12729 {
12730 end = start - 1;
12731 start = 0;
12732 }
12733 else
12734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012736 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012738 }
12739
12740 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741 if (error)
12742 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012743 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012744 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012745 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012746 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012747 else
12748 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012749 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012750 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012751 if (list_append_number(rettv->vval.v_list,
12752 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012753 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012754 }
12755}
12756
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012757/*
12758 * "readfile()" function
12759 */
12760 static void
12761f_readfile(argvars, rettv)
12762 typval_T *argvars;
12763 typval_T *rettv;
12764{
12765 int binary = FALSE;
12766 char_u *fname;
12767 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012768 listitem_T *li;
12769#define FREAD_SIZE 200 /* optimized for text lines */
12770 char_u buf[FREAD_SIZE];
12771 int readlen; /* size of last fread() */
12772 int buflen; /* nr of valid chars in buf[] */
12773 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12774 int tolist; /* first byte in buf[] still to be put in list */
12775 int chop; /* how many CR to chop off */
12776 char_u *prev = NULL; /* previously read bytes, if any */
12777 int prevlen = 0; /* length of "prev" if not NULL */
12778 char_u *s;
12779 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012780 long maxline = MAXLNUM;
12781 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012782
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012783 if (argvars[1].v_type != VAR_UNKNOWN)
12784 {
12785 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12786 binary = TRUE;
12787 if (argvars[2].v_type != VAR_UNKNOWN)
12788 maxline = get_tv_number(&argvars[2]);
12789 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012790
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012791 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012792 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012793
12794 /* Always open the file in binary mode, library functions have a mind of
12795 * their own about CR-LF conversion. */
12796 fname = get_tv_string(&argvars[0]);
12797 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12798 {
12799 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12800 return;
12801 }
12802
12803 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012804 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012805 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012806 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012807 buflen = filtd + readlen;
12808 tolist = 0;
12809 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12810 {
12811 if (buf[filtd] == '\n' || readlen <= 0)
12812 {
12813 /* Only when in binary mode add an empty list item when the
12814 * last line ends in a '\n'. */
12815 if (!binary && readlen == 0 && filtd == 0)
12816 break;
12817
12818 /* Found end-of-line or end-of-file: add a text line to the
12819 * list. */
12820 chop = 0;
12821 if (!binary)
12822 while (filtd - chop - 1 >= tolist
12823 && buf[filtd - chop - 1] == '\r')
12824 ++chop;
12825 len = filtd - tolist - chop;
12826 if (prev == NULL)
12827 s = vim_strnsave(buf + tolist, len);
12828 else
12829 {
12830 s = alloc((unsigned)(prevlen + len + 1));
12831 if (s != NULL)
12832 {
12833 mch_memmove(s, prev, prevlen);
12834 vim_free(prev);
12835 prev = NULL;
12836 mch_memmove(s + prevlen, buf + tolist, len);
12837 s[prevlen + len] = NUL;
12838 }
12839 }
12840 tolist = filtd + 1;
12841
12842 li = listitem_alloc();
12843 if (li == NULL)
12844 {
12845 vim_free(s);
12846 break;
12847 }
12848 li->li_tv.v_type = VAR_STRING;
12849 li->li_tv.v_lock = 0;
12850 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012851 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012852
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012853 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012854 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012855 if (readlen <= 0)
12856 break;
12857 }
12858 else if (buf[filtd] == NUL)
12859 buf[filtd] = '\n';
12860 }
12861 if (readlen <= 0)
12862 break;
12863
12864 if (tolist == 0)
12865 {
12866 /* "buf" is full, need to move text to an allocated buffer */
12867 if (prev == NULL)
12868 {
12869 prev = vim_strnsave(buf, buflen);
12870 prevlen = buflen;
12871 }
12872 else
12873 {
12874 s = alloc((unsigned)(prevlen + buflen));
12875 if (s != NULL)
12876 {
12877 mch_memmove(s, prev, prevlen);
12878 mch_memmove(s + prevlen, buf, buflen);
12879 vim_free(prev);
12880 prev = s;
12881 prevlen += buflen;
12882 }
12883 }
12884 filtd = 0;
12885 }
12886 else
12887 {
12888 mch_memmove(buf, buf + tolist, buflen - tolist);
12889 filtd -= tolist;
12890 }
12891 }
12892
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012893 /*
12894 * For a negative line count use only the lines at the end of the file,
12895 * free the rest.
12896 */
12897 if (maxline < 0)
12898 while (cnt > -maxline)
12899 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012900 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012901 --cnt;
12902 }
12903
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012904 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012905 fclose(fd);
12906}
12907
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012908#if defined(FEAT_RELTIME)
12909static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12910
12911/*
12912 * Convert a List to proftime_T.
12913 * Return FAIL when there is something wrong.
12914 */
12915 static int
12916list2proftime(arg, tm)
12917 typval_T *arg;
12918 proftime_T *tm;
12919{
12920 long n1, n2;
12921 int error = FALSE;
12922
12923 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12924 || arg->vval.v_list->lv_len != 2)
12925 return FAIL;
12926 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12927 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12928# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012929 tm->HighPart = n1;
12930 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012931# else
12932 tm->tv_sec = n1;
12933 tm->tv_usec = n2;
12934# endif
12935 return error ? FAIL : OK;
12936}
12937#endif /* FEAT_RELTIME */
12938
12939/*
12940 * "reltime()" function
12941 */
12942 static void
12943f_reltime(argvars, rettv)
12944 typval_T *argvars;
12945 typval_T *rettv;
12946{
12947#ifdef FEAT_RELTIME
12948 proftime_T res;
12949 proftime_T start;
12950
12951 if (argvars[0].v_type == VAR_UNKNOWN)
12952 {
12953 /* No arguments: get current time. */
12954 profile_start(&res);
12955 }
12956 else if (argvars[1].v_type == VAR_UNKNOWN)
12957 {
12958 if (list2proftime(&argvars[0], &res) == FAIL)
12959 return;
12960 profile_end(&res);
12961 }
12962 else
12963 {
12964 /* Two arguments: compute the difference. */
12965 if (list2proftime(&argvars[0], &start) == FAIL
12966 || list2proftime(&argvars[1], &res) == FAIL)
12967 return;
12968 profile_sub(&res, &start);
12969 }
12970
12971 if (rettv_list_alloc(rettv) == OK)
12972 {
12973 long n1, n2;
12974
12975# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012976 n1 = res.HighPart;
12977 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012978# else
12979 n1 = res.tv_sec;
12980 n2 = res.tv_usec;
12981# endif
12982 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12983 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12984 }
12985#endif
12986}
12987
12988/*
12989 * "reltimestr()" function
12990 */
12991 static void
12992f_reltimestr(argvars, rettv)
12993 typval_T *argvars;
12994 typval_T *rettv;
12995{
12996#ifdef FEAT_RELTIME
12997 proftime_T tm;
12998#endif
12999
13000 rettv->v_type = VAR_STRING;
13001 rettv->vval.v_string = NULL;
13002#ifdef FEAT_RELTIME
13003 if (list2proftime(&argvars[0], &tm) == OK)
13004 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13005#endif
13006}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013007
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13009static void make_connection __ARGS((void));
13010static int check_connection __ARGS((void));
13011
13012 static void
13013make_connection()
13014{
13015 if (X_DISPLAY == NULL
13016# ifdef FEAT_GUI
13017 && !gui.in_use
13018# endif
13019 )
13020 {
13021 x_force_connect = TRUE;
13022 setup_term_clip();
13023 x_force_connect = FALSE;
13024 }
13025}
13026
13027 static int
13028check_connection()
13029{
13030 make_connection();
13031 if (X_DISPLAY == NULL)
13032 {
13033 EMSG(_("E240: No connection to Vim server"));
13034 return FAIL;
13035 }
13036 return OK;
13037}
13038#endif
13039
13040#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013041static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013042
13043 static void
13044remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013047 int expr;
13048{
13049 char_u *server_name;
13050 char_u *keys;
13051 char_u *r = NULL;
13052 char_u buf[NUMBUFLEN];
13053# ifdef WIN32
13054 HWND w;
13055# else
13056 Window w;
13057# endif
13058
13059 if (check_restricted() || check_secure())
13060 return;
13061
13062# ifdef FEAT_X11
13063 if (check_connection() == FAIL)
13064 return;
13065# endif
13066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013067 server_name = get_tv_string_chk(&argvars[0]);
13068 if (server_name == NULL)
13069 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013070 keys = get_tv_string_buf(&argvars[1], buf);
13071# ifdef WIN32
13072 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13073# else
13074 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13075 < 0)
13076# endif
13077 {
13078 if (r != NULL)
13079 EMSG(r); /* sending worked but evaluation failed */
13080 else
13081 EMSG2(_("E241: Unable to send to %s"), server_name);
13082 return;
13083 }
13084
13085 rettv->vval.v_string = r;
13086
13087 if (argvars[2].v_type != VAR_UNKNOWN)
13088 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013089 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013090 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013091 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013092
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013093 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013094 v.di_tv.v_type = VAR_STRING;
13095 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013096 idvar = get_tv_string_chk(&argvars[2]);
13097 if (idvar != NULL)
13098 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013099 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013100 }
13101}
13102#endif
13103
13104/*
13105 * "remote_expr()" function
13106 */
13107/*ARGSUSED*/
13108 static void
13109f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 typval_T *argvars;
13111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013112{
13113 rettv->v_type = VAR_STRING;
13114 rettv->vval.v_string = NULL;
13115#ifdef FEAT_CLIENTSERVER
13116 remote_common(argvars, rettv, TRUE);
13117#endif
13118}
13119
13120/*
13121 * "remote_foreground()" function
13122 */
13123/*ARGSUSED*/
13124 static void
13125f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013126 typval_T *argvars;
13127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013128{
13129 rettv->vval.v_number = 0;
13130#ifdef FEAT_CLIENTSERVER
13131# ifdef WIN32
13132 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013133 {
13134 char_u *server_name = get_tv_string_chk(&argvars[0]);
13135
13136 if (server_name != NULL)
13137 serverForeground(server_name);
13138 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139# else
13140 /* Send a foreground() expression to the server. */
13141 argvars[1].v_type = VAR_STRING;
13142 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13143 argvars[2].v_type = VAR_UNKNOWN;
13144 remote_common(argvars, rettv, TRUE);
13145 vim_free(argvars[1].vval.v_string);
13146# endif
13147#endif
13148}
13149
13150/*ARGSUSED*/
13151 static void
13152f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013153 typval_T *argvars;
13154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013155{
13156#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013157 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013158 char_u *s = NULL;
13159# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013160 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013161# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013162 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013163
13164 if (check_restricted() || check_secure())
13165 {
13166 rettv->vval.v_number = -1;
13167 return;
13168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013169 serverid = get_tv_string_chk(&argvars[0]);
13170 if (serverid == NULL)
13171 {
13172 rettv->vval.v_number = -1;
13173 return; /* type error; errmsg already given */
13174 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013175# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013176 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013177 if (n == 0)
13178 rettv->vval.v_number = -1;
13179 else
13180 {
13181 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13182 rettv->vval.v_number = (s != NULL);
13183 }
13184# else
13185 rettv->vval.v_number = 0;
13186 if (check_connection() == FAIL)
13187 return;
13188
13189 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013190 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013191# endif
13192
13193 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013195 char_u *retvar;
13196
Bram Moolenaar33570922005-01-25 22:26:29 +000013197 v.di_tv.v_type = VAR_STRING;
13198 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 retvar = get_tv_string_chk(&argvars[1]);
13200 if (retvar != NULL)
13201 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013202 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013203 }
13204#else
13205 rettv->vval.v_number = -1;
13206#endif
13207}
13208
13209/*ARGSUSED*/
13210 static void
13211f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013212 typval_T *argvars;
13213 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013214{
13215 char_u *r = NULL;
13216
13217#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013218 char_u *serverid = get_tv_string_chk(&argvars[0]);
13219
13220 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013221 {
13222# ifdef WIN32
13223 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013224 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013225
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013226 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013227 if (n != 0)
13228 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13229 if (r == NULL)
13230# else
13231 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013233# endif
13234 EMSG(_("E277: Unable to read a server reply"));
13235 }
13236#endif
13237 rettv->v_type = VAR_STRING;
13238 rettv->vval.v_string = r;
13239}
13240
13241/*
13242 * "remote_send()" function
13243 */
13244/*ARGSUSED*/
13245 static void
13246f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013247 typval_T *argvars;
13248 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013249{
13250 rettv->v_type = VAR_STRING;
13251 rettv->vval.v_string = NULL;
13252#ifdef FEAT_CLIENTSERVER
13253 remote_common(argvars, rettv, FALSE);
13254#endif
13255}
13256
13257/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013258 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013259 */
13260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013262 typval_T *argvars;
13263 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013264{
Bram Moolenaar33570922005-01-25 22:26:29 +000013265 list_T *l;
13266 listitem_T *item, *item2;
13267 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013268 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013269 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013270 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013271 dict_T *d;
13272 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013273
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013274 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013275 if (argvars[0].v_type == VAR_DICT)
13276 {
13277 if (argvars[2].v_type != VAR_UNKNOWN)
13278 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013279 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013280 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282 key = get_tv_string_chk(&argvars[1]);
13283 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 di = dict_find(d, key, -1);
13286 if (di == NULL)
13287 EMSG2(_(e_dictkey), key);
13288 else
13289 {
13290 *rettv = di->di_tv;
13291 init_tv(&di->di_tv);
13292 dictitem_remove(d, di);
13293 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013295 }
13296 }
13297 else if (argvars[0].v_type != VAR_LIST)
13298 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013299 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013300 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 int error = FALSE;
13303
13304 idx = get_tv_number_chk(&argvars[1], &error);
13305 if (error)
13306 ; /* type error: do nothing, errmsg already given */
13307 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308 EMSGN(_(e_listidx), idx);
13309 else
13310 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013311 if (argvars[2].v_type == VAR_UNKNOWN)
13312 {
13313 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013314 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013315 *rettv = item->li_tv;
13316 vim_free(item);
13317 }
13318 else
13319 {
13320 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 end = get_tv_number_chk(&argvars[2], &error);
13322 if (error)
13323 ; /* type error: do nothing */
13324 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013325 EMSGN(_(e_listidx), end);
13326 else
13327 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013328 int cnt = 0;
13329
13330 for (li = item; li != NULL; li = li->li_next)
13331 {
13332 ++cnt;
13333 if (li == item2)
13334 break;
13335 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013336 if (li == NULL) /* didn't find "item2" after "item" */
13337 EMSG(_(e_invrange));
13338 else
13339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013340 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013341 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013342 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013343 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013344 l->lv_first = item;
13345 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013346 item->li_prev = NULL;
13347 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013348 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013349 }
13350 }
13351 }
13352 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013353 }
13354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355}
13356
13357/*
13358 * "rename({from}, {to})" function
13359 */
13360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364{
13365 char_u buf[NUMBUFLEN];
13366
13367 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13371 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372}
13373
13374/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013375 * "repeat()" function
13376 */
13377/*ARGSUSED*/
13378 static void
13379f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013380 typval_T *argvars;
13381 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013382{
13383 char_u *p;
13384 int n;
13385 int slen;
13386 int len;
13387 char_u *r;
13388 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013389
13390 n = get_tv_number(&argvars[1]);
13391 if (argvars[0].v_type == VAR_LIST)
13392 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013393 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013394 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013395 if (list_extend(rettv->vval.v_list,
13396 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013397 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013398 }
13399 else
13400 {
13401 p = get_tv_string(&argvars[0]);
13402 rettv->v_type = VAR_STRING;
13403 rettv->vval.v_string = NULL;
13404
13405 slen = (int)STRLEN(p);
13406 len = slen * n;
13407 if (len <= 0)
13408 return;
13409
13410 r = alloc(len + 1);
13411 if (r != NULL)
13412 {
13413 for (i = 0; i < n; i++)
13414 mch_memmove(r + i * slen, p, (size_t)slen);
13415 r[len] = NUL;
13416 }
13417
13418 rettv->vval.v_string = r;
13419 }
13420}
13421
13422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 * "resolve()" function
13424 */
13425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013426f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013427 typval_T *argvars;
13428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429{
13430 char_u *p;
13431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013432 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433#ifdef FEAT_SHORTCUT
13434 {
13435 char_u *v = NULL;
13436
13437 v = mch_resolve_shortcut(p);
13438 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 }
13443#else
13444# ifdef HAVE_READLINK
13445 {
13446 char_u buf[MAXPATHL + 1];
13447 char_u *cpy;
13448 int len;
13449 char_u *remain = NULL;
13450 char_u *q;
13451 int is_relative_to_current = FALSE;
13452 int has_trailing_pathsep = FALSE;
13453 int limit = 100;
13454
13455 p = vim_strsave(p);
13456
13457 if (p[0] == '.' && (vim_ispathsep(p[1])
13458 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13459 is_relative_to_current = TRUE;
13460
13461 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013462 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 has_trailing_pathsep = TRUE;
13464
13465 q = getnextcomp(p);
13466 if (*q != NUL)
13467 {
13468 /* Separate the first path component in "p", and keep the
13469 * remainder (beginning with the path separator). */
13470 remain = vim_strsave(q - 1);
13471 q[-1] = NUL;
13472 }
13473
13474 for (;;)
13475 {
13476 for (;;)
13477 {
13478 len = readlink((char *)p, (char *)buf, MAXPATHL);
13479 if (len <= 0)
13480 break;
13481 buf[len] = NUL;
13482
13483 if (limit-- == 0)
13484 {
13485 vim_free(p);
13486 vim_free(remain);
13487 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 goto fail;
13490 }
13491
13492 /* Ensure that the result will have a trailing path separator
13493 * if the argument has one. */
13494 if (remain == NULL && has_trailing_pathsep)
13495 add_pathsep(buf);
13496
13497 /* Separate the first path component in the link value and
13498 * concatenate the remainders. */
13499 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13500 if (*q != NUL)
13501 {
13502 if (remain == NULL)
13503 remain = vim_strsave(q - 1);
13504 else
13505 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013506 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 if (cpy != NULL)
13508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 vim_free(remain);
13510 remain = cpy;
13511 }
13512 }
13513 q[-1] = NUL;
13514 }
13515
13516 q = gettail(p);
13517 if (q > p && *q == NUL)
13518 {
13519 /* Ignore trailing path separator. */
13520 q[-1] = NUL;
13521 q = gettail(p);
13522 }
13523 if (q > p && !mch_isFullName(buf))
13524 {
13525 /* symlink is relative to directory of argument */
13526 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13527 if (cpy != NULL)
13528 {
13529 STRCPY(cpy, p);
13530 STRCPY(gettail(cpy), buf);
13531 vim_free(p);
13532 p = cpy;
13533 }
13534 }
13535 else
13536 {
13537 vim_free(p);
13538 p = vim_strsave(buf);
13539 }
13540 }
13541
13542 if (remain == NULL)
13543 break;
13544
13545 /* Append the first path component of "remain" to "p". */
13546 q = getnextcomp(remain + 1);
13547 len = q - remain - (*q != NUL);
13548 cpy = vim_strnsave(p, STRLEN(p) + len);
13549 if (cpy != NULL)
13550 {
13551 STRNCAT(cpy, remain, len);
13552 vim_free(p);
13553 p = cpy;
13554 }
13555 /* Shorten "remain". */
13556 if (*q != NUL)
13557 STRCPY(remain, q - 1);
13558 else
13559 {
13560 vim_free(remain);
13561 remain = NULL;
13562 }
13563 }
13564
13565 /* If the result is a relative path name, make it explicitly relative to
13566 * the current directory if and only if the argument had this form. */
13567 if (!vim_ispathsep(*p))
13568 {
13569 if (is_relative_to_current
13570 && *p != NUL
13571 && !(p[0] == '.'
13572 && (p[1] == NUL
13573 || vim_ispathsep(p[1])
13574 || (p[1] == '.'
13575 && (p[2] == NUL
13576 || vim_ispathsep(p[2]))))))
13577 {
13578 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013579 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 if (cpy != NULL)
13581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 vim_free(p);
13583 p = cpy;
13584 }
13585 }
13586 else if (!is_relative_to_current)
13587 {
13588 /* Strip leading "./". */
13589 q = p;
13590 while (q[0] == '.' && vim_ispathsep(q[1]))
13591 q += 2;
13592 if (q > p)
13593 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13594 }
13595 }
13596
13597 /* Ensure that the result will have no trailing path separator
13598 * if the argument had none. But keep "/" or "//". */
13599 if (!has_trailing_pathsep)
13600 {
13601 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013602 if (after_pathsep(p, q))
13603 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 }
13605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 }
13608# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610# endif
13611#endif
13612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614
13615#ifdef HAVE_READLINK
13616fail:
13617#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619}
13620
13621/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013622 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 */
13624 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *argvars;
13627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 list_T *l;
13630 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631
Bram Moolenaar0d660222005-01-07 21:51:51 +000013632 rettv->vval.v_number = 0;
13633 if (argvars[0].v_type != VAR_LIST)
13634 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013635 else if ((l = argvars[0].vval.v_list) != NULL
13636 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013637 {
13638 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013639 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013640 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641 while (li != NULL)
13642 {
13643 ni = li->li_prev;
13644 list_append(l, li);
13645 li = ni;
13646 }
13647 rettv->vval.v_list = l;
13648 rettv->v_type = VAR_LIST;
13649 ++l->lv_refcount;
13650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651}
13652
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013653#define SP_NOMOVE 0x01 /* don't move cursor */
13654#define SP_REPEAT 0x02 /* repeat to find outer pair */
13655#define SP_RETCOUNT 0x04 /* return matchcount */
13656#define SP_SETPCMARK 0x08 /* set previous context mark */
13657#define SP_START 0x10 /* accept match at start position */
13658#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13659#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013660
Bram Moolenaar33570922005-01-25 22:26:29 +000013661static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013662
13663/*
13664 * Get flags for a search function.
13665 * Possibly sets "p_ws".
13666 * Returns BACKWARD, FORWARD or zero (for an error).
13667 */
13668 static int
13669get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013670 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013671 int *flagsp;
13672{
13673 int dir = FORWARD;
13674 char_u *flags;
13675 char_u nbuf[NUMBUFLEN];
13676 int mask;
13677
13678 if (varp->v_type != VAR_UNKNOWN)
13679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 flags = get_tv_string_buf_chk(varp, nbuf);
13681 if (flags == NULL)
13682 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013683 while (*flags != NUL)
13684 {
13685 switch (*flags)
13686 {
13687 case 'b': dir = BACKWARD; break;
13688 case 'w': p_ws = TRUE; break;
13689 case 'W': p_ws = FALSE; break;
13690 default: mask = 0;
13691 if (flagsp != NULL)
13692 switch (*flags)
13693 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013694 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013695 case 'e': mask = SP_END; break;
13696 case 'm': mask = SP_RETCOUNT; break;
13697 case 'n': mask = SP_NOMOVE; break;
13698 case 'p': mask = SP_SUBPAT; break;
13699 case 'r': mask = SP_REPEAT; break;
13700 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013701 }
13702 if (mask == 0)
13703 {
13704 EMSG2(_(e_invarg2), flags);
13705 dir = 0;
13706 }
13707 else
13708 *flagsp |= mask;
13709 }
13710 if (dir == 0)
13711 break;
13712 ++flags;
13713 }
13714 }
13715 return dir;
13716}
13717
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013719 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013721 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013722search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013723 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013724 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013725 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013727 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 char_u *pat;
13729 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013730 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 int save_p_ws = p_ws;
13732 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013733 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013734 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013735 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013736 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013739 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013740 if (dir == 0)
13741 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013742 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013743 if (flags & SP_START)
13744 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013745 if (flags & SP_END)
13746 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013747
13748 /* Optional extra argument: line number to stop searching. */
13749 if (argvars[1].v_type != VAR_UNKNOWN
13750 && argvars[2].v_type != VAR_UNKNOWN)
13751 {
13752 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13753 if (lnum_stop < 0)
13754 goto theend;
13755 }
13756
Bram Moolenaar231334e2005-07-25 20:46:57 +000013757 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013758 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013759 * Check to make sure only those flags are set.
13760 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13761 * flags cannot be set. Check for that condition also.
13762 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013763 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013764 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013766 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013767 goto theend;
13768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013770 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013771 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13772 options, RE_SEARCH, (linenr_T)lnum_stop);
13773 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013775 if (flags & SP_SUBPAT)
13776 retval = subpatnum;
13777 else
13778 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013779 if (flags & SP_SETPCMARK)
13780 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013782 if (match_pos != NULL)
13783 {
13784 /* Store the match cursor position */
13785 match_pos->lnum = pos.lnum;
13786 match_pos->col = pos.col + 1;
13787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 /* "/$" will put the cursor after the end of the line, may need to
13789 * correct that here */
13790 check_cursor();
13791 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013792
13793 /* If 'n' flag is used: restore cursor position. */
13794 if (flags & SP_NOMOVE)
13795 curwin->w_cursor = save_cursor;
13796theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013798
13799 return retval;
13800}
13801
13802/*
13803 * "search()" function
13804 */
13805 static void
13806f_search(argvars, rettv)
13807 typval_T *argvars;
13808 typval_T *rettv;
13809{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013810 int flags = 0;
13811
13812 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813}
13814
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013816 * "searchdecl()" function
13817 */
13818 static void
13819f_searchdecl(argvars, rettv)
13820 typval_T *argvars;
13821 typval_T *rettv;
13822{
13823 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013824 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013825 int error = FALSE;
13826 char_u *name;
13827
13828 rettv->vval.v_number = 1; /* default: FAIL */
13829
13830 name = get_tv_string_chk(&argvars[0]);
13831 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013832 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013833 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013834 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13835 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13836 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013837 if (!error && name != NULL)
13838 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013839 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013840}
13841
13842/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013843 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013845 static int
13846searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013847 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013848 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849{
13850 char_u *spat, *mpat, *epat;
13851 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 int dir;
13854 int flags = 0;
13855 char_u nbuf1[NUMBUFLEN];
13856 char_u nbuf2[NUMBUFLEN];
13857 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013858 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013859 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 spat = get_tv_string_chk(&argvars[0]);
13863 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13864 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13865 if (spat == NULL || mpat == NULL || epat == NULL)
13866 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 /* Handle the optional fourth argument: flags */
13869 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013870 if (dir == 0)
13871 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013872
13873 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013874 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13875 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013876 if ((flags & (SP_END | SP_SUBPAT)) != 0
13877 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013878 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013879 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013880 goto theend;
13881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013883 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013884 if (argvars[3].v_type == VAR_UNKNOWN
13885 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 skip = (char_u *)"";
13887 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013890 if (argvars[5].v_type != VAR_UNKNOWN)
13891 {
13892 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13893 if (lnum_stop < 0)
13894 goto theend;
13895 }
13896 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 if (skip == NULL)
13898 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013900 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13901 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013902
13903theend:
13904 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013905
13906 return retval;
13907}
13908
13909/*
13910 * "searchpair()" function
13911 */
13912 static void
13913f_searchpair(argvars, rettv)
13914 typval_T *argvars;
13915 typval_T *rettv;
13916{
13917 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13918}
13919
13920/*
13921 * "searchpairpos()" function
13922 */
13923 static void
13924f_searchpairpos(argvars, rettv)
13925 typval_T *argvars;
13926 typval_T *rettv;
13927{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013928 pos_T match_pos;
13929 int lnum = 0;
13930 int col = 0;
13931
13932 rettv->vval.v_number = 0;
13933
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013934 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013935 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013936
13937 if (searchpair_cmn(argvars, &match_pos) > 0)
13938 {
13939 lnum = match_pos.lnum;
13940 col = match_pos.col;
13941 }
13942
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013943 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13944 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013945}
13946
13947/*
13948 * Search for a start/middle/end thing.
13949 * Used by searchpair(), see its documentation for the details.
13950 * Returns 0 or -1 for no match,
13951 */
13952 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013953do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013954 char_u *spat; /* start pattern */
13955 char_u *mpat; /* middle pattern */
13956 char_u *epat; /* end pattern */
13957 int dir; /* BACKWARD or FORWARD */
13958 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013959 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013960 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013961 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013962{
13963 char_u *save_cpo;
13964 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13965 long retval = 0;
13966 pos_T pos;
13967 pos_T firstpos;
13968 pos_T foundpos;
13969 pos_T save_cursor;
13970 pos_T save_pos;
13971 int n;
13972 int r;
13973 int nest = 1;
13974 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013975 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013976
13977 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13978 save_cpo = p_cpo;
13979 p_cpo = (char_u *)"";
13980
13981 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13982 * start/middle/end (pat3, for the top pair). */
13983 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13984 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13985 if (pat2 == NULL || pat3 == NULL)
13986 goto theend;
13987 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13988 if (*mpat == NUL)
13989 STRCPY(pat3, pat2);
13990 else
13991 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13992 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013993 if (flags & SP_START)
13994 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013995
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 save_cursor = curwin->w_cursor;
13997 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013998 clearpos(&firstpos);
13999 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 pat = pat3;
14001 for (;;)
14002 {
14003 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014004 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14006 /* didn't find it or found the first match again: FAIL */
14007 break;
14008
14009 if (firstpos.lnum == 0)
14010 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014011 if (equalpos(pos, foundpos))
14012 {
14013 /* Found the same position again. Can happen with a pattern that
14014 * has "\zs" at the end and searching backwards. Advance one
14015 * character and try again. */
14016 if (dir == BACKWARD)
14017 decl(&pos);
14018 else
14019 incl(&pos);
14020 }
14021 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022
14023 /* If the skip pattern matches, ignore this match. */
14024 if (*skip != NUL)
14025 {
14026 save_pos = curwin->w_cursor;
14027 curwin->w_cursor = pos;
14028 r = eval_to_bool(skip, &err, NULL, FALSE);
14029 curwin->w_cursor = save_pos;
14030 if (err)
14031 {
14032 /* Evaluating {skip} caused an error, break here. */
14033 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014034 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 break;
14036 }
14037 if (r)
14038 continue;
14039 }
14040
14041 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14042 {
14043 /* Found end when searching backwards or start when searching
14044 * forward: nested pair. */
14045 ++nest;
14046 pat = pat2; /* nested, don't search for middle */
14047 }
14048 else
14049 {
14050 /* Found end when searching forward or start when searching
14051 * backward: end of (nested) pair; or found middle in outer pair. */
14052 if (--nest == 1)
14053 pat = pat3; /* outer level, search for middle */
14054 }
14055
14056 if (nest == 0)
14057 {
14058 /* Found the match: return matchcount or line number. */
14059 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014060 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014062 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014063 if (flags & SP_SETPCMARK)
14064 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 curwin->w_cursor = pos;
14066 if (!(flags & SP_REPEAT))
14067 break;
14068 nest = 1; /* search for next unmatched */
14069 }
14070 }
14071
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014072 if (match_pos != NULL)
14073 {
14074 /* Store the match cursor position */
14075 match_pos->lnum = curwin->w_cursor.lnum;
14076 match_pos->col = curwin->w_cursor.col + 1;
14077 }
14078
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014080 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 curwin->w_cursor = save_cursor;
14082
14083theend:
14084 vim_free(pat2);
14085 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014087
14088 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089}
14090
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014091/*
14092 * "searchpos()" function
14093 */
14094 static void
14095f_searchpos(argvars, rettv)
14096 typval_T *argvars;
14097 typval_T *rettv;
14098{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014099 pos_T match_pos;
14100 int lnum = 0;
14101 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014102 int n;
14103 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014104
14105 rettv->vval.v_number = 0;
14106
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014107 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014108 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014109
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014110 n = search_cmn(argvars, &match_pos, &flags);
14111 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014112 {
14113 lnum = match_pos.lnum;
14114 col = match_pos.col;
14115 }
14116
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014117 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14118 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014119 if (flags & SP_SUBPAT)
14120 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014121}
14122
14123
Bram Moolenaar0d660222005-01-07 21:51:51 +000014124/*ARGSUSED*/
14125 static void
14126f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014127 typval_T *argvars;
14128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014130#ifdef FEAT_CLIENTSERVER
14131 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 char_u *server = get_tv_string_chk(&argvars[0]);
14133 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134
Bram Moolenaar0d660222005-01-07 21:51:51 +000014135 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 if (server == NULL || reply == NULL)
14137 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014138 if (check_restricted() || check_secure())
14139 return;
14140# ifdef FEAT_X11
14141 if (check_connection() == FAIL)
14142 return;
14143# endif
14144
14145 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147 EMSG(_("E258: Unable to send to client"));
14148 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014150 rettv->vval.v_number = 0;
14151#else
14152 rettv->vval.v_number = -1;
14153#endif
14154}
14155
14156/*ARGSUSED*/
14157 static void
14158f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014159 typval_T *argvars;
14160 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014161{
14162 char_u *r = NULL;
14163
14164#ifdef FEAT_CLIENTSERVER
14165# ifdef WIN32
14166 r = serverGetVimNames();
14167# else
14168 make_connection();
14169 if (X_DISPLAY != NULL)
14170 r = serverGetVimNames(X_DISPLAY);
14171# endif
14172#endif
14173 rettv->v_type = VAR_STRING;
14174 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175}
14176
14177/*
14178 * "setbufvar()" function
14179 */
14180/*ARGSUSED*/
14181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 typval_T *argvars;
14184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185{
14186 buf_T *buf;
14187#ifdef FEAT_AUTOCMD
14188 aco_save_T aco;
14189#else
14190 buf_T *save_curbuf;
14191#endif
14192 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 char_u nbuf[NUMBUFLEN];
14195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 rettv->vval.v_number = 0;
14197
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 if (check_restricted() || check_secure())
14199 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14201 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014202 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 varp = &argvars[2];
14204
14205 if (buf != NULL && varname != NULL && varp != NULL)
14206 {
14207 /* set curbuf to be our buf, temporarily */
14208#ifdef FEAT_AUTOCMD
14209 aucmd_prepbuf(&aco, buf);
14210#else
14211 save_curbuf = curbuf;
14212 curbuf = buf;
14213#endif
14214
14215 if (*varname == '&')
14216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014217 long numval;
14218 char_u *strval;
14219 int error = FALSE;
14220
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 numval = get_tv_number_chk(varp, &error);
14223 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 if (!error && strval != NULL)
14225 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 }
14227 else
14228 {
14229 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14230 if (bufvarname != NULL)
14231 {
14232 STRCPY(bufvarname, "b:");
14233 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014234 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 vim_free(bufvarname);
14236 }
14237 }
14238
14239 /* reset notion of buffer */
14240#ifdef FEAT_AUTOCMD
14241 aucmd_restbuf(&aco);
14242#else
14243 curbuf = save_curbuf;
14244#endif
14245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246}
14247
14248/*
14249 * "setcmdpos()" function
14250 */
14251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014253 typval_T *argvars;
14254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 int pos = (int)get_tv_number(&argvars[0]) - 1;
14257
14258 if (pos >= 0)
14259 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260}
14261
14262/*
14263 * "setline()" function
14264 */
14265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014267 typval_T *argvars;
14268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269{
14270 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014271 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014272 list_T *l = NULL;
14273 listitem_T *li = NULL;
14274 long added = 0;
14275 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014277 lnum = get_tv_lnum(&argvars[0]);
14278 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014280 l = argvars[1].vval.v_list;
14281 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014283 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014284 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014285
14286 rettv->vval.v_number = 0; /* OK */
14287 for (;;)
14288 {
14289 if (l != NULL)
14290 {
14291 /* list argument, get next string */
14292 if (li == NULL)
14293 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014295 li = li->li_next;
14296 }
14297
14298 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014300 break;
14301 if (lnum <= curbuf->b_ml.ml_line_count)
14302 {
14303 /* existing line, replace it */
14304 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14305 {
14306 changed_bytes(lnum, 0);
14307 check_cursor_col();
14308 rettv->vval.v_number = 0; /* OK */
14309 }
14310 }
14311 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14312 {
14313 /* lnum is one past the last line, append the line */
14314 ++added;
14315 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14316 rettv->vval.v_number = 0; /* OK */
14317 }
14318
14319 if (l == NULL) /* only one string argument */
14320 break;
14321 ++lnum;
14322 }
14323
14324 if (added > 0)
14325 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326}
14327
14328/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014329 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014330 */
14331/*ARGSUSED*/
14332 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014333set_qf_ll_list(wp, list_arg, action_arg, rettv)
14334 win_T *wp;
14335 typval_T *list_arg;
14336 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014337 typval_T *rettv;
14338{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014339#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014340 char_u *act;
14341 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014342#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014343
Bram Moolenaar2641f772005-03-25 21:58:17 +000014344 rettv->vval.v_number = -1;
14345
14346#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014347 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014348 EMSG(_(e_listreq));
14349 else
14350 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014351 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014352
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014353 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014354 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014355 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 if (act == NULL)
14357 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014358 if (*act == 'a' || *act == 'r')
14359 action = *act;
14360 }
14361
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014362 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014363 rettv->vval.v_number = 0;
14364 }
14365#endif
14366}
14367
14368/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014369 * "setloclist()" function
14370 */
14371/*ARGSUSED*/
14372 static void
14373f_setloclist(argvars, rettv)
14374 typval_T *argvars;
14375 typval_T *rettv;
14376{
14377 win_T *win;
14378
14379 rettv->vval.v_number = -1;
14380
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014381 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014382 if (win != NULL)
14383 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14384}
14385
14386/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014387 * "setpos()" function
14388 */
14389/*ARGSUSED*/
14390 static void
14391f_setpos(argvars, rettv)
14392 typval_T *argvars;
14393 typval_T *rettv;
14394{
14395 pos_T pos;
14396 int fnum;
14397 char_u *name;
14398
14399 name = get_tv_string_chk(argvars);
14400 if (name != NULL)
14401 {
14402 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14403 {
14404 --pos.col;
14405 if (name[0] == '.') /* cursor */
14406 {
14407 if (fnum == curbuf->b_fnum)
14408 {
14409 curwin->w_cursor = pos;
14410 check_cursor();
14411 }
14412 else
14413 EMSG(_(e_invarg));
14414 }
14415 else if (name[0] == '\'') /* mark */
14416 (void)setmark_pos(name[1], &pos, fnum);
14417 else
14418 EMSG(_(e_invarg));
14419 }
14420 }
14421}
14422
14423/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014424 * "setqflist()" function
14425 */
14426/*ARGSUSED*/
14427 static void
14428f_setqflist(argvars, rettv)
14429 typval_T *argvars;
14430 typval_T *rettv;
14431{
14432 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14433}
14434
14435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 * "setreg()" function
14437 */
14438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014439f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014440 typval_T *argvars;
14441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442{
14443 int regname;
14444 char_u *strregname;
14445 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 int append;
14448 char_u yank_type;
14449 long block_len;
14450
14451 block_len = -1;
14452 yank_type = MAUTO;
14453 append = FALSE;
14454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014455 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014458 if (strregname == NULL)
14459 return; /* type error; errmsg already given */
14460 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 if (regname == 0 || regname == '@')
14462 regname = '"';
14463 else if (regname == '=')
14464 return;
14465
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 stropt = get_tv_string_chk(&argvars[2]);
14469 if (stropt == NULL)
14470 return; /* type error */
14471 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 switch (*stropt)
14473 {
14474 case 'a': case 'A': /* append */
14475 append = TRUE;
14476 break;
14477 case 'v': case 'c': /* character-wise selection */
14478 yank_type = MCHAR;
14479 break;
14480 case 'V': case 'l': /* line-wise selection */
14481 yank_type = MLINE;
14482 break;
14483#ifdef FEAT_VISUAL
14484 case 'b': case Ctrl_V: /* block-wise selection */
14485 yank_type = MBLOCK;
14486 if (VIM_ISDIGIT(stropt[1]))
14487 {
14488 ++stropt;
14489 block_len = getdigits(&stropt) - 1;
14490 --stropt;
14491 }
14492 break;
14493#endif
14494 }
14495 }
14496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 strval = get_tv_string_chk(&argvars[1]);
14498 if (strval != NULL)
14499 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014501 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502}
14503
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014504/*
14505 * "settabwinvar()" function
14506 */
14507 static void
14508f_settabwinvar(argvars, rettv)
14509 typval_T *argvars;
14510 typval_T *rettv;
14511{
14512 setwinvar(argvars, rettv, 1);
14513}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514
14515/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014516 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014520 typval_T *argvars;
14521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014523 setwinvar(argvars, rettv, 0);
14524}
14525
14526/*
14527 * "setwinvar()" and "settabwinvar()" functions
14528 */
14529 static void
14530setwinvar(argvars, rettv, off)
14531 typval_T *argvars;
14532 typval_T *rettv;
14533 int off;
14534{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 win_T *win;
14536#ifdef FEAT_WINDOWS
14537 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014538 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539#endif
14540 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014541 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014543 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014545 rettv->vval.v_number = 0;
14546
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 if (check_restricted() || check_secure())
14548 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014549
14550#ifdef FEAT_WINDOWS
14551 if (off == 1)
14552 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14553 else
14554 tp = curtab;
14555#endif
14556 win = find_win_by_nr(&argvars[off], tp);
14557 varname = get_tv_string_chk(&argvars[off + 1]);
14558 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559
14560 if (win != NULL && varname != NULL && varp != NULL)
14561 {
14562#ifdef FEAT_WINDOWS
14563 /* set curwin to be our win, temporarily */
14564 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014565 save_curtab = curtab;
14566 goto_tabpage_tp(tp);
14567 if (!win_valid(win))
14568 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 curwin = win;
14570 curbuf = curwin->w_buffer;
14571#endif
14572
14573 if (*varname == '&')
14574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014575 long numval;
14576 char_u *strval;
14577 int error = FALSE;
14578
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 numval = get_tv_number_chk(varp, &error);
14581 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014582 if (!error && strval != NULL)
14583 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 }
14585 else
14586 {
14587 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14588 if (winvarname != NULL)
14589 {
14590 STRCPY(winvarname, "w:");
14591 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014592 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 vim_free(winvarname);
14594 }
14595 }
14596
14597#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014598 /* Restore current tabpage and window, if still valid (autocomands can
14599 * make them invalid). */
14600 if (valid_tabpage(save_curtab))
14601 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 if (win_valid(save_curwin))
14603 {
14604 curwin = save_curwin;
14605 curbuf = curwin->w_buffer;
14606 }
14607#endif
14608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609}
14610
14611/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 */
14614 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014615f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014616 typval_T *argvars;
14617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014619 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620
Bram Moolenaar0d660222005-01-07 21:51:51 +000014621 p = get_tv_string(&argvars[0]);
14622 rettv->vval.v_string = vim_strsave(p);
14623 simplify_filename(rettv->vval.v_string); /* simplify in place */
14624 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625}
14626
Bram Moolenaar0d660222005-01-07 21:51:51 +000014627static int
14628#ifdef __BORLANDC__
14629 _RTLENTRYF
14630#endif
14631 item_compare __ARGS((const void *s1, const void *s2));
14632static int
14633#ifdef __BORLANDC__
14634 _RTLENTRYF
14635#endif
14636 item_compare2 __ARGS((const void *s1, const void *s2));
14637
14638static int item_compare_ic;
14639static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641#define ITEM_COMPARE_FAIL 999
14642
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014646 static int
14647#ifdef __BORLANDC__
14648_RTLENTRYF
14649#endif
14650item_compare(s1, s2)
14651 const void *s1;
14652 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654 char_u *p1, *p2;
14655 char_u *tofree1, *tofree2;
14656 int res;
14657 char_u numbuf1[NUMBUFLEN];
14658 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014660 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14661 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662 if (item_compare_ic)
14663 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665 res = STRCMP(p1, p2);
14666 vim_free(tofree1);
14667 vim_free(tofree2);
14668 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669}
14670
14671 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672#ifdef __BORLANDC__
14673_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675item_compare2(s1, s2)
14676 const void *s1;
14677 const void *s2;
14678{
14679 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014681 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014684 /* shortcut after failure in previous call; compare all items equal */
14685 if (item_compare_func_err)
14686 return 0;
14687
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014688 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14689 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14691 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692
14693 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014694 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014695 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014696 clear_tv(&argv[0]);
14697 clear_tv(&argv[1]);
14698
14699 if (res == FAIL)
14700 res = ITEM_COMPARE_FAIL;
14701 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 /* return value has wrong type */
14703 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14704 if (item_compare_func_err)
14705 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706 clear_tv(&rettv);
14707 return res;
14708}
14709
14710/*
14711 * "sort({list})" function
14712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014714f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014715 typval_T *argvars;
14716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717{
Bram Moolenaar33570922005-01-25 22:26:29 +000014718 list_T *l;
14719 listitem_T *li;
14720 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014721 long len;
14722 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724 rettv->vval.v_number = 0;
14725 if (argvars[0].v_type != VAR_LIST)
14726 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 else
14728 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014730 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014731 return;
14732 rettv->vval.v_list = l;
14733 rettv->v_type = VAR_LIST;
14734 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736 len = list_len(l);
14737 if (len <= 1)
14738 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740 item_compare_ic = FALSE;
14741 item_compare_func = NULL;
14742 if (argvars[1].v_type != VAR_UNKNOWN)
14743 {
14744 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014745 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014746 else
14747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014748 int error = FALSE;
14749
14750 i = get_tv_number_chk(&argvars[1], &error);
14751 if (error)
14752 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753 if (i == 1)
14754 item_compare_ic = TRUE;
14755 else
14756 item_compare_func = get_tv_string(&argvars[1]);
14757 }
14758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014761 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014762 if (ptrs == NULL)
14763 return;
14764 i = 0;
14765 for (li = l->lv_first; li != NULL; li = li->li_next)
14766 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014768 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014769 /* test the compare function */
14770 if (item_compare_func != NULL
14771 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14772 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014773 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014775 {
14776 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778 item_compare_func == NULL ? item_compare : item_compare2);
14779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014780 if (!item_compare_func_err)
14781 {
14782 /* Clear the List and append the items in the sorted order. */
14783 l->lv_first = l->lv_last = NULL;
14784 l->lv_len = 0;
14785 for (i = 0; i < len; ++i)
14786 list_append(l, ptrs[i]);
14787 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014788 }
14789
14790 vim_free(ptrs);
14791 }
14792}
14793
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014794/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014795 * "soundfold({word})" function
14796 */
14797 static void
14798f_soundfold(argvars, rettv)
14799 typval_T *argvars;
14800 typval_T *rettv;
14801{
14802 char_u *s;
14803
14804 rettv->v_type = VAR_STRING;
14805 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014806#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014807 rettv->vval.v_string = eval_soundfold(s);
14808#else
14809 rettv->vval.v_string = vim_strsave(s);
14810#endif
14811}
14812
14813/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014814 * "spellbadword()" function
14815 */
14816/* ARGSUSED */
14817 static void
14818f_spellbadword(argvars, rettv)
14819 typval_T *argvars;
14820 typval_T *rettv;
14821{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014822 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014823 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014824 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014825
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014826 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014827 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014828
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014829#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014830 if (argvars[0].v_type == VAR_UNKNOWN)
14831 {
14832 /* Find the start and length of the badly spelled word. */
14833 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14834 if (len != 0)
14835 word = ml_get_cursor();
14836 }
14837 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14838 {
14839 char_u *str = get_tv_string_chk(&argvars[0]);
14840 int capcol = -1;
14841
14842 if (str != NULL)
14843 {
14844 /* Check the argument for spelling. */
14845 while (*str != NUL)
14846 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014847 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014848 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014849 {
14850 word = str;
14851 break;
14852 }
14853 str += len;
14854 }
14855 }
14856 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014857#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014858
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014859 list_append_string(rettv->vval.v_list, word, len);
14860 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014861 attr == HLF_SPB ? "bad" :
14862 attr == HLF_SPR ? "rare" :
14863 attr == HLF_SPL ? "local" :
14864 attr == HLF_SPC ? "caps" :
14865 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014866}
14867
14868/*
14869 * "spellsuggest()" function
14870 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014871/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014872 static void
14873f_spellsuggest(argvars, rettv)
14874 typval_T *argvars;
14875 typval_T *rettv;
14876{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014877#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014878 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014879 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014880 int maxcount;
14881 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014882 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014883 listitem_T *li;
14884 int need_capital = FALSE;
14885#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014886
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014887 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014888 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014889
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014890#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014891 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14892 {
14893 str = get_tv_string(&argvars[0]);
14894 if (argvars[1].v_type != VAR_UNKNOWN)
14895 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014896 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014897 if (maxcount <= 0)
14898 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014899 if (argvars[2].v_type != VAR_UNKNOWN)
14900 {
14901 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14902 if (typeerr)
14903 return;
14904 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014905 }
14906 else
14907 maxcount = 25;
14908
Bram Moolenaar4770d092006-01-12 23:22:24 +000014909 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014910
14911 for (i = 0; i < ga.ga_len; ++i)
14912 {
14913 str = ((char_u **)ga.ga_data)[i];
14914
14915 li = listitem_alloc();
14916 if (li == NULL)
14917 vim_free(str);
14918 else
14919 {
14920 li->li_tv.v_type = VAR_STRING;
14921 li->li_tv.v_lock = 0;
14922 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014923 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014924 }
14925 }
14926 ga_clear(&ga);
14927 }
14928#endif
14929}
14930
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014932f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014933 typval_T *argvars;
14934 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935{
14936 char_u *str;
14937 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014938 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014939 regmatch_T regmatch;
14940 char_u patbuf[NUMBUFLEN];
14941 char_u *save_cpo;
14942 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014943 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014944 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014945 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014946
14947 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14948 save_cpo = p_cpo;
14949 p_cpo = (char_u *)"";
14950
14951 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014952 if (argvars[1].v_type != VAR_UNKNOWN)
14953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014954 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14955 if (pat == NULL)
14956 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014957 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014958 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014959 }
14960 if (pat == NULL || *pat == NUL)
14961 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014963 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014965 if (typeerr)
14966 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14969 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014971 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014972 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014973 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014974 if (*str == NUL)
14975 match = FALSE; /* empty item at the end */
14976 else
14977 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978 if (match)
14979 end = regmatch.startp[0];
14980 else
14981 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014982 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14983 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014985 if (list_append_string(rettv->vval.v_list, str,
14986 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014987 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988 }
14989 if (!match)
14990 break;
14991 /* Advance to just after the match. */
14992 if (regmatch.endp[0] > str)
14993 col = 0;
14994 else
14995 {
14996 /* Don't get stuck at the same match. */
14997#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014998 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014999#else
15000 col = 1;
15001#endif
15002 }
15003 str = regmatch.endp[0];
15004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008
Bram Moolenaar0d660222005-01-07 21:51:51 +000015009 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010}
15011
Bram Moolenaar2c932302006-03-18 21:42:09 +000015012/*
15013 * "str2nr()" function
15014 */
15015 static void
15016f_str2nr(argvars, rettv)
15017 typval_T *argvars;
15018 typval_T *rettv;
15019{
15020 int base = 10;
15021 char_u *p;
15022 long n;
15023
15024 if (argvars[1].v_type != VAR_UNKNOWN)
15025 {
15026 base = get_tv_number(&argvars[1]);
15027 if (base != 8 && base != 10 && base != 16)
15028 {
15029 EMSG(_(e_invarg));
15030 return;
15031 }
15032 }
15033
15034 p = skipwhite(get_tv_string(&argvars[0]));
15035 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15036 rettv->vval.v_number = n;
15037}
15038
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039#ifdef HAVE_STRFTIME
15040/*
15041 * "strftime({format}[, {time}])" function
15042 */
15043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015044f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015045 typval_T *argvars;
15046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047{
15048 char_u result_buf[256];
15049 struct tm *curtime;
15050 time_t seconds;
15051 char_u *p;
15052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015056 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 seconds = time(NULL);
15058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015059 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 curtime = localtime(&seconds);
15061 /* MSVC returns NULL for an invalid value of seconds. */
15062 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 else
15065 {
15066# ifdef FEAT_MBYTE
15067 vimconv_T conv;
15068 char_u *enc;
15069
15070 conv.vc_type = CONV_NONE;
15071 enc = enc_locale();
15072 convert_setup(&conv, p_enc, enc);
15073 if (conv.vc_type != CONV_NONE)
15074 p = string_convert(&conv, p, NULL);
15075# endif
15076 if (p != NULL)
15077 (void)strftime((char *)result_buf, sizeof(result_buf),
15078 (char *)p, curtime);
15079 else
15080 result_buf[0] = NUL;
15081
15082# ifdef FEAT_MBYTE
15083 if (conv.vc_type != CONV_NONE)
15084 vim_free(p);
15085 convert_setup(&conv, enc, p_enc);
15086 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015087 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 else
15089# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091
15092# ifdef FEAT_MBYTE
15093 /* Release conversion descriptors */
15094 convert_setup(&conv, NULL, NULL);
15095 vim_free(enc);
15096# endif
15097 }
15098}
15099#endif
15100
15101/*
15102 * "stridx()" function
15103 */
15104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015105f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015106 typval_T *argvars;
15107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108{
15109 char_u buf[NUMBUFLEN];
15110 char_u *needle;
15111 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015112 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015114 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015116 needle = get_tv_string_chk(&argvars[1]);
15117 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015119 if (needle == NULL || haystack == NULL)
15120 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 if (argvars[2].v_type != VAR_UNKNOWN)
15123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015124 int error = FALSE;
15125
15126 start_idx = get_tv_number_chk(&argvars[2], &error);
15127 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015129 if (start_idx >= 0)
15130 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015131 }
15132
15133 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15134 if (pos != NULL)
15135 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136}
15137
15138/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015139 * "string()" function
15140 */
15141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015142f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015143 typval_T *argvars;
15144 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015145{
15146 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015147 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015150 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015151 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153}
15154
15155/*
15156 * "strlen()" function
15157 */
15158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015160 typval_T *argvars;
15161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 rettv->vval.v_number = (varnumber_T)(STRLEN(
15164 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165}
15166
15167/*
15168 * "strpart()" function
15169 */
15170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015171f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015172 typval_T *argvars;
15173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174{
15175 char_u *p;
15176 int n;
15177 int len;
15178 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015179 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015181 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 slen = (int)STRLEN(p);
15183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015184 n = get_tv_number_chk(&argvars[1], &error);
15185 if (error)
15186 len = 0;
15187 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015188 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 else
15190 len = slen - n; /* default len: all bytes that are available. */
15191
15192 /*
15193 * Only return the overlap between the specified part and the actual
15194 * string.
15195 */
15196 if (n < 0)
15197 {
15198 len += n;
15199 n = 0;
15200 }
15201 else if (n > slen)
15202 n = slen;
15203 if (len < 0)
15204 len = 0;
15205 else if (n + len > slen)
15206 len = slen - n;
15207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208 rettv->v_type = VAR_STRING;
15209 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210}
15211
15212/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213 * "strridx()" function
15214 */
15215 static void
15216f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015217 typval_T *argvars;
15218 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219{
15220 char_u buf[NUMBUFLEN];
15221 char_u *needle;
15222 char_u *haystack;
15223 char_u *rest;
15224 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015225 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015227 needle = get_tv_string_chk(&argvars[1]);
15228 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229
15230 rettv->vval.v_number = -1;
15231 if (needle == NULL || haystack == NULL)
15232 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015233
15234 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015235 if (argvars[2].v_type != VAR_UNKNOWN)
15236 {
15237 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015238 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015239 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015240 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015241 }
15242 else
15243 end_idx = haystack_len;
15244
Bram Moolenaar0d660222005-01-07 21:51:51 +000015245 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015246 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015248 lastmatch = haystack + end_idx;
15249 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015251 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252 for (rest = haystack; *rest != '\0'; ++rest)
15253 {
15254 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015255 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015256 break;
15257 lastmatch = rest;
15258 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015259 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260
15261 if (lastmatch == NULL)
15262 rettv->vval.v_number = -1;
15263 else
15264 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15265}
15266
15267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 * "strtrans()" function
15269 */
15270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015272 typval_T *argvars;
15273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015275 rettv->v_type = VAR_STRING;
15276 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277}
15278
15279/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280 * "submatch()" function
15281 */
15282 static void
15283f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015284 typval_T *argvars;
15285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286{
15287 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015288 rettv->vval.v_string =
15289 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290}
15291
15292/*
15293 * "substitute()" function
15294 */
15295 static void
15296f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015297 typval_T *argvars;
15298 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015299{
15300 char_u patbuf[NUMBUFLEN];
15301 char_u subbuf[NUMBUFLEN];
15302 char_u flagsbuf[NUMBUFLEN];
15303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015304 char_u *str = get_tv_string_chk(&argvars[0]);
15305 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15306 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15307 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15308
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015310 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15311 rettv->vval.v_string = NULL;
15312 else
15313 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015314}
15315
15316/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015317 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 */
15319/*ARGSUSED*/
15320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015321f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015322 typval_T *argvars;
15323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324{
15325 int id = 0;
15326#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015327 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 long col;
15329 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015330 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015332 lnum = get_tv_lnum(argvars); /* -1 on type error */
15333 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15334 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015336 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015337 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015338 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339#endif
15340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015341 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342}
15343
15344/*
15345 * "synIDattr(id, what [, mode])" function
15346 */
15347/*ARGSUSED*/
15348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015350 typval_T *argvars;
15351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352{
15353 char_u *p = NULL;
15354#ifdef FEAT_SYN_HL
15355 int id;
15356 char_u *what;
15357 char_u *mode;
15358 char_u modebuf[NUMBUFLEN];
15359 int modec;
15360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015361 id = get_tv_number(&argvars[0]);
15362 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015363 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015365 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 modec = TOLOWER_ASC(mode[0]);
15367 if (modec != 't' && modec != 'c'
15368#ifdef FEAT_GUI
15369 && modec != 'g'
15370#endif
15371 )
15372 modec = 0; /* replace invalid with current */
15373 }
15374 else
15375 {
15376#ifdef FEAT_GUI
15377 if (gui.in_use)
15378 modec = 'g';
15379 else
15380#endif
15381 if (t_colors > 1)
15382 modec = 'c';
15383 else
15384 modec = 't';
15385 }
15386
15387
15388 switch (TOLOWER_ASC(what[0]))
15389 {
15390 case 'b':
15391 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15392 p = highlight_color(id, what, modec);
15393 else /* bold */
15394 p = highlight_has_attr(id, HL_BOLD, modec);
15395 break;
15396
15397 case 'f': /* fg[#] */
15398 p = highlight_color(id, what, modec);
15399 break;
15400
15401 case 'i':
15402 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15403 p = highlight_has_attr(id, HL_INVERSE, modec);
15404 else /* italic */
15405 p = highlight_has_attr(id, HL_ITALIC, modec);
15406 break;
15407
15408 case 'n': /* name */
15409 p = get_highlight_name(NULL, id - 1);
15410 break;
15411
15412 case 'r': /* reverse */
15413 p = highlight_has_attr(id, HL_INVERSE, modec);
15414 break;
15415
15416 case 's': /* standout */
15417 p = highlight_has_attr(id, HL_STANDOUT, modec);
15418 break;
15419
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015420 case 'u':
15421 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15422 /* underline */
15423 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15424 else
15425 /* undercurl */
15426 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 break;
15428 }
15429
15430 if (p != NULL)
15431 p = vim_strsave(p);
15432#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 rettv->v_type = VAR_STRING;
15434 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435}
15436
15437/*
15438 * "synIDtrans(id)" function
15439 */
15440/*ARGSUSED*/
15441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015442f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015443 typval_T *argvars;
15444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445{
15446 int id;
15447
15448#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450
15451 if (id > 0)
15452 id = syn_get_final_id(id);
15453 else
15454#endif
15455 id = 0;
15456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015457 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458}
15459
15460/*
15461 * "system()" function
15462 */
15463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015464f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 typval_T *argvars;
15466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015468 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015470 char_u *infile = NULL;
15471 char_u buf[NUMBUFLEN];
15472 int err = FALSE;
15473 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015475 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015476 {
15477 /*
15478 * Write the string to a temp file, to be used for input of the shell
15479 * command.
15480 */
15481 if ((infile = vim_tempname('i')) == NULL)
15482 {
15483 EMSG(_(e_notmp));
15484 return;
15485 }
15486
15487 fd = mch_fopen((char *)infile, WRITEBIN);
15488 if (fd == NULL)
15489 {
15490 EMSG2(_(e_notopen), infile);
15491 goto done;
15492 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015493 p = get_tv_string_buf_chk(&argvars[1], buf);
15494 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015495 {
15496 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015497 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015498 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015499 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15500 err = TRUE;
15501 if (fclose(fd) != 0)
15502 err = TRUE;
15503 if (err)
15504 {
15505 EMSG(_("E677: Error writing temp file"));
15506 goto done;
15507 }
15508 }
15509
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015510 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15511 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015512
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513#ifdef USE_CR
15514 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015515 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 {
15517 char_u *s;
15518
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015519 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520 {
15521 if (*s == CAR)
15522 *s = NL;
15523 }
15524 }
15525#else
15526# ifdef USE_CRNL
15527 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015528 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 {
15530 char_u *s, *d;
15531
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015532 d = res;
15533 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 {
15535 if (s[0] == CAR && s[1] == NL)
15536 ++s;
15537 *d++ = *s;
15538 }
15539 *d = NUL;
15540 }
15541# endif
15542#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015543
15544done:
15545 if (infile != NULL)
15546 {
15547 mch_remove(infile);
15548 vim_free(infile);
15549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015550 rettv->v_type = VAR_STRING;
15551 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552}
15553
15554/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015555 * "tabpagebuflist()" function
15556 */
15557/* ARGSUSED */
15558 static void
15559f_tabpagebuflist(argvars, rettv)
15560 typval_T *argvars;
15561 typval_T *rettv;
15562{
15563#ifndef FEAT_WINDOWS
15564 rettv->vval.v_number = 0;
15565#else
15566 tabpage_T *tp;
15567 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015568
15569 if (argvars[0].v_type == VAR_UNKNOWN)
15570 wp = firstwin;
15571 else
15572 {
15573 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15574 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015575 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015576 }
15577 if (wp == NULL)
15578 rettv->vval.v_number = 0;
15579 else
15580 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015581 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015582 rettv->vval.v_number = 0;
15583 else
15584 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015585 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015586 if (list_append_number(rettv->vval.v_list,
15587 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015588 break;
15589 }
15590 }
15591#endif
15592}
15593
15594
15595/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015596 * "tabpagenr()" function
15597 */
15598/* ARGSUSED */
15599 static void
15600f_tabpagenr(argvars, rettv)
15601 typval_T *argvars;
15602 typval_T *rettv;
15603{
15604 int nr = 1;
15605#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015606 char_u *arg;
15607
15608 if (argvars[0].v_type != VAR_UNKNOWN)
15609 {
15610 arg = get_tv_string_chk(&argvars[0]);
15611 nr = 0;
15612 if (arg != NULL)
15613 {
15614 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015615 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015616 else
15617 EMSG2(_(e_invexpr2), arg);
15618 }
15619 }
15620 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015621 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015622#endif
15623 rettv->vval.v_number = nr;
15624}
15625
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015626
15627#ifdef FEAT_WINDOWS
15628static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15629
15630/*
15631 * Common code for tabpagewinnr() and winnr().
15632 */
15633 static int
15634get_winnr(tp, argvar)
15635 tabpage_T *tp;
15636 typval_T *argvar;
15637{
15638 win_T *twin;
15639 int nr = 1;
15640 win_T *wp;
15641 char_u *arg;
15642
15643 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15644 if (argvar->v_type != VAR_UNKNOWN)
15645 {
15646 arg = get_tv_string_chk(argvar);
15647 if (arg == NULL)
15648 nr = 0; /* type error; errmsg already given */
15649 else if (STRCMP(arg, "$") == 0)
15650 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15651 else if (STRCMP(arg, "#") == 0)
15652 {
15653 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15654 if (twin == NULL)
15655 nr = 0;
15656 }
15657 else
15658 {
15659 EMSG2(_(e_invexpr2), arg);
15660 nr = 0;
15661 }
15662 }
15663
15664 if (nr > 0)
15665 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15666 wp != twin; wp = wp->w_next)
15667 ++nr;
15668 return nr;
15669}
15670#endif
15671
15672/*
15673 * "tabpagewinnr()" function
15674 */
15675/* ARGSUSED */
15676 static void
15677f_tabpagewinnr(argvars, rettv)
15678 typval_T *argvars;
15679 typval_T *rettv;
15680{
15681 int nr = 1;
15682#ifdef FEAT_WINDOWS
15683 tabpage_T *tp;
15684
15685 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15686 if (tp == NULL)
15687 nr = 0;
15688 else
15689 nr = get_winnr(tp, &argvars[1]);
15690#endif
15691 rettv->vval.v_number = nr;
15692}
15693
15694
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015695/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015696 * "tagfiles()" function
15697 */
15698/*ARGSUSED*/
15699 static void
15700f_tagfiles(argvars, rettv)
15701 typval_T *argvars;
15702 typval_T *rettv;
15703{
15704 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015705 tagname_T tn;
15706 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015707
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015709 {
15710 rettv->vval.v_number = 0;
15711 return;
15712 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015713
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015714 for (first = TRUE; ; first = FALSE)
15715 if (get_tagfname(&tn, first, fname) == FAIL
15716 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015717 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015718 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015719}
15720
15721/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015722 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015723 */
15724 static void
15725f_taglist(argvars, rettv)
15726 typval_T *argvars;
15727 typval_T *rettv;
15728{
15729 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015730
15731 tag_pattern = get_tv_string(&argvars[0]);
15732
15733 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015734 if (*tag_pattern == NUL)
15735 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015736
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015737 if (rettv_list_alloc(rettv) == OK)
15738 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015739}
15740
15741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 * "tempname()" function
15743 */
15744/*ARGSUSED*/
15745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015746f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015747 typval_T *argvars;
15748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749{
15750 static int x = 'A';
15751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015752 rettv->v_type = VAR_STRING;
15753 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754
15755 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15756 * names. Skip 'I' and 'O', they are used for shell redirection. */
15757 do
15758 {
15759 if (x == 'Z')
15760 x = '0';
15761 else if (x == '9')
15762 x = 'A';
15763 else
15764 {
15765#ifdef EBCDIC
15766 if (x == 'I')
15767 x = 'J';
15768 else if (x == 'R')
15769 x = 'S';
15770 else
15771#endif
15772 ++x;
15773 }
15774 } while (x == 'I' || x == 'O');
15775}
15776
15777/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015778 * "test(list)" function: Just checking the walls...
15779 */
15780/*ARGSUSED*/
15781 static void
15782f_test(argvars, rettv)
15783 typval_T *argvars;
15784 typval_T *rettv;
15785{
15786 /* Used for unit testing. Change the code below to your liking. */
15787#if 0
15788 listitem_T *li;
15789 list_T *l;
15790 char_u *bad, *good;
15791
15792 if (argvars[0].v_type != VAR_LIST)
15793 return;
15794 l = argvars[0].vval.v_list;
15795 if (l == NULL)
15796 return;
15797 li = l->lv_first;
15798 if (li == NULL)
15799 return;
15800 bad = get_tv_string(&li->li_tv);
15801 li = li->li_next;
15802 if (li == NULL)
15803 return;
15804 good = get_tv_string(&li->li_tv);
15805 rettv->vval.v_number = test_edit_score(bad, good);
15806#endif
15807}
15808
15809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810 * "tolower(string)" function
15811 */
15812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015813f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015814 typval_T *argvars;
15815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816{
15817 char_u *p;
15818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015819 p = vim_strsave(get_tv_string(&argvars[0]));
15820 rettv->v_type = VAR_STRING;
15821 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
15823 if (p != NULL)
15824 while (*p != NUL)
15825 {
15826#ifdef FEAT_MBYTE
15827 int l;
15828
15829 if (enc_utf8)
15830 {
15831 int c, lc;
15832
15833 c = utf_ptr2char(p);
15834 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015835 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836 /* TODO: reallocate string when byte count changes. */
15837 if (utf_char2len(lc) == l)
15838 utf_char2bytes(lc, p);
15839 p += l;
15840 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015841 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 p += l; /* skip multi-byte character */
15843 else
15844#endif
15845 {
15846 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15847 ++p;
15848 }
15849 }
15850}
15851
15852/*
15853 * "toupper(string)" function
15854 */
15855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015856f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 typval_T *argvars;
15858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015860 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015861 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862}
15863
15864/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015865 * "tr(string, fromstr, tostr)" function
15866 */
15867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015868f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015869 typval_T *argvars;
15870 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015871{
15872 char_u *instr;
15873 char_u *fromstr;
15874 char_u *tostr;
15875 char_u *p;
15876#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015877 int inlen;
15878 int fromlen;
15879 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015880 int idx;
15881 char_u *cpstr;
15882 int cplen;
15883 int first = TRUE;
15884#endif
15885 char_u buf[NUMBUFLEN];
15886 char_u buf2[NUMBUFLEN];
15887 garray_T ga;
15888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015889 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015890 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15891 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015892
15893 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015894 rettv->v_type = VAR_STRING;
15895 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015896 if (fromstr == NULL || tostr == NULL)
15897 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015898 ga_init2(&ga, (int)sizeof(char), 80);
15899
15900#ifdef FEAT_MBYTE
15901 if (!has_mbyte)
15902#endif
15903 /* not multi-byte: fromstr and tostr must be the same length */
15904 if (STRLEN(fromstr) != STRLEN(tostr))
15905 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015906#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015907error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015908#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015909 EMSG2(_(e_invarg2), fromstr);
15910 ga_clear(&ga);
15911 return;
15912 }
15913
15914 /* fromstr and tostr have to contain the same number of chars */
15915 while (*instr != NUL)
15916 {
15917#ifdef FEAT_MBYTE
15918 if (has_mbyte)
15919 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015920 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015921 cpstr = instr;
15922 cplen = inlen;
15923 idx = 0;
15924 for (p = fromstr; *p != NUL; p += fromlen)
15925 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015926 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015927 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15928 {
15929 for (p = tostr; *p != NUL; p += tolen)
15930 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015931 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015932 if (idx-- == 0)
15933 {
15934 cplen = tolen;
15935 cpstr = p;
15936 break;
15937 }
15938 }
15939 if (*p == NUL) /* tostr is shorter than fromstr */
15940 goto error;
15941 break;
15942 }
15943 ++idx;
15944 }
15945
15946 if (first && cpstr == instr)
15947 {
15948 /* Check that fromstr and tostr have the same number of
15949 * (multi-byte) characters. Done only once when a character
15950 * of instr doesn't appear in fromstr. */
15951 first = FALSE;
15952 for (p = tostr; *p != NUL; p += tolen)
15953 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015954 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015955 --idx;
15956 }
15957 if (idx != 0)
15958 goto error;
15959 }
15960
15961 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015962 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015963 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015964
15965 instr += inlen;
15966 }
15967 else
15968#endif
15969 {
15970 /* When not using multi-byte chars we can do it faster. */
15971 p = vim_strchr(fromstr, *instr);
15972 if (p != NULL)
15973 ga_append(&ga, tostr[p - fromstr]);
15974 else
15975 ga_append(&ga, *instr);
15976 ++instr;
15977 }
15978 }
15979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015980 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015981}
15982
15983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 * "type(expr)" function
15985 */
15986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015987f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015988 typval_T *argvars;
15989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015991 int n;
15992
15993 switch (argvars[0].v_type)
15994 {
15995 case VAR_NUMBER: n = 0; break;
15996 case VAR_STRING: n = 1; break;
15997 case VAR_FUNC: n = 2; break;
15998 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015999 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016000 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16001 }
16002 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003}
16004
16005/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016006 * "values(dict)" function
16007 */
16008 static void
16009f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016010 typval_T *argvars;
16011 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016012{
16013 dict_list(argvars, rettv, 1);
16014}
16015
16016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 * "virtcol(string)" function
16018 */
16019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016020f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016021 typval_T *argvars;
16022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023{
16024 colnr_T vcol = 0;
16025 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016026 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016028 fp = var2fpos(&argvars[0], FALSE, &fnum);
16029 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16030 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 {
16032 getvvcol(curwin, fp, NULL, NULL, &vcol);
16033 ++vcol;
16034 }
16035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016036 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037}
16038
16039/*
16040 * "visualmode()" function
16041 */
16042/*ARGSUSED*/
16043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016044f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 typval_T *argvars;
16046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047{
16048#ifdef FEAT_VISUAL
16049 char_u str[2];
16050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016051 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 str[0] = curbuf->b_visual_mode_eval;
16053 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016054 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055
16056 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016057 if ((argvars[0].v_type == VAR_NUMBER
16058 && argvars[0].vval.v_number != 0)
16059 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016060 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 curbuf->b_visual_mode_eval = NUL;
16062#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016063 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064#endif
16065}
16066
16067/*
16068 * "winbufnr(nr)" function
16069 */
16070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016071f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016072 typval_T *argvars;
16073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074{
16075 win_T *wp;
16076
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016077 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082}
16083
16084/*
16085 * "wincol()" function
16086 */
16087/*ARGSUSED*/
16088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016089f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016090 typval_T *argvars;
16091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092{
16093 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016094 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095}
16096
16097/*
16098 * "winheight(nr)" function
16099 */
16100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016101f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016102 typval_T *argvars;
16103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104{
16105 win_T *wp;
16106
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016107 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016111 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112}
16113
16114/*
16115 * "winline()" function
16116 */
16117/*ARGSUSED*/
16118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016119f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016120 typval_T *argvars;
16121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122{
16123 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016124 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125}
16126
16127/*
16128 * "winnr()" function
16129 */
16130/* ARGSUSED */
16131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016132f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016133 typval_T *argvars;
16134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135{
16136 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016137
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016139 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016141 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142}
16143
16144/*
16145 * "winrestcmd()" function
16146 */
16147/* ARGSUSED */
16148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016149f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016150 typval_T *argvars;
16151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152{
16153#ifdef FEAT_WINDOWS
16154 win_T *wp;
16155 int winnr = 1;
16156 garray_T ga;
16157 char_u buf[50];
16158
16159 ga_init2(&ga, (int)sizeof(char), 70);
16160 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16161 {
16162 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16163 ga_concat(&ga, buf);
16164# ifdef FEAT_VERTSPLIT
16165 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16166 ga_concat(&ga, buf);
16167# endif
16168 ++winnr;
16169 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016170 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016172 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016174 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177}
16178
16179/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016180 * "winrestview()" function
16181 */
16182/* ARGSUSED */
16183 static void
16184f_winrestview(argvars, rettv)
16185 typval_T *argvars;
16186 typval_T *rettv;
16187{
16188 dict_T *dict;
16189
16190 if (argvars[0].v_type != VAR_DICT
16191 || (dict = argvars[0].vval.v_dict) == NULL)
16192 EMSG(_(e_invarg));
16193 else
16194 {
16195 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16196 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16197#ifdef FEAT_VIRTUALEDIT
16198 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16199#endif
16200 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016201 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016202
16203 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16204#ifdef FEAT_DIFF
16205 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16206#endif
16207 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16208 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16209
16210 check_cursor();
16211 changed_cline_bef_curs();
16212 invalidate_botline();
16213 redraw_later(VALID);
16214
16215 if (curwin->w_topline == 0)
16216 curwin->w_topline = 1;
16217 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16218 curwin->w_topline = curbuf->b_ml.ml_line_count;
16219#ifdef FEAT_DIFF
16220 check_topfill(curwin, TRUE);
16221#endif
16222 }
16223}
16224
16225/*
16226 * "winsaveview()" function
16227 */
16228/* ARGSUSED */
16229 static void
16230f_winsaveview(argvars, rettv)
16231 typval_T *argvars;
16232 typval_T *rettv;
16233{
16234 dict_T *dict;
16235
16236 dict = dict_alloc();
16237 if (dict == NULL)
16238 return;
16239 rettv->v_type = VAR_DICT;
16240 rettv->vval.v_dict = dict;
16241 ++dict->dv_refcount;
16242
16243 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16244 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16245#ifdef FEAT_VIRTUALEDIT
16246 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16247#endif
16248 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16249
16250 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16251#ifdef FEAT_DIFF
16252 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16253#endif
16254 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16255 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16256}
16257
16258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 * "winwidth(nr)" function
16260 */
16261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016262f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016263 typval_T *argvars;
16264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265{
16266 win_T *wp;
16267
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016268 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 else
16272#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016275 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276#endif
16277}
16278
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016280 * "writefile()" function
16281 */
16282 static void
16283f_writefile(argvars, rettv)
16284 typval_T *argvars;
16285 typval_T *rettv;
16286{
16287 int binary = FALSE;
16288 char_u *fname;
16289 FILE *fd;
16290 listitem_T *li;
16291 char_u *s;
16292 int ret = 0;
16293 int c;
16294
16295 if (argvars[0].v_type != VAR_LIST)
16296 {
16297 EMSG2(_(e_listarg), "writefile()");
16298 return;
16299 }
16300 if (argvars[0].vval.v_list == NULL)
16301 return;
16302
16303 if (argvars[2].v_type != VAR_UNKNOWN
16304 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16305 binary = TRUE;
16306
16307 /* Always open the file in binary mode, library functions have a mind of
16308 * their own about CR-LF conversion. */
16309 fname = get_tv_string(&argvars[1]);
16310 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16311 {
16312 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16313 ret = -1;
16314 }
16315 else
16316 {
16317 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16318 li = li->li_next)
16319 {
16320 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16321 {
16322 if (*s == '\n')
16323 c = putc(NUL, fd);
16324 else
16325 c = putc(*s, fd);
16326 if (c == EOF)
16327 {
16328 ret = -1;
16329 break;
16330 }
16331 }
16332 if (!binary || li->li_next != NULL)
16333 if (putc('\n', fd) == EOF)
16334 {
16335 ret = -1;
16336 break;
16337 }
16338 if (ret < 0)
16339 {
16340 EMSG(_(e_write));
16341 break;
16342 }
16343 }
16344 fclose(fd);
16345 }
16346
16347 rettv->vval.v_number = ret;
16348}
16349
16350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016352 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353 */
16354 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016355var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016358 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016360 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016362 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363
Bram Moolenaara5525202006-03-02 22:52:09 +000016364 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016365 if (varp->v_type == VAR_LIST)
16366 {
16367 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016368 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016369 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016370
16371 l = varp->vval.v_list;
16372 if (l == NULL)
16373 return NULL;
16374
16375 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016376 pos.lnum = list_find_nr(l, 0L, &error);
16377 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016378 return NULL; /* invalid line number */
16379
16380 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016381 pos.col = list_find_nr(l, 1L, &error);
16382 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016383 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016384 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016385 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016386 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016387 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016388 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016389
Bram Moolenaara5525202006-03-02 22:52:09 +000016390#ifdef FEAT_VIRTUALEDIT
16391 /* Get the virtual offset. Defaults to zero. */
16392 pos.coladd = list_find_nr(l, 2L, &error);
16393 if (error)
16394 pos.coladd = 0;
16395#endif
16396
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016397 return &pos;
16398 }
16399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016400 name = get_tv_string_chk(varp);
16401 if (name == NULL)
16402 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 if (name[0] == '.') /* cursor */
16404 return &curwin->w_cursor;
16405 if (name[0] == '\'') /* mark */
16406 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016407 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16409 return NULL;
16410 return pp;
16411 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016412
16413#ifdef FEAT_VIRTUALEDIT
16414 pos.coladd = 0;
16415#endif
16416
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016417 if (name[0] == 'w' && lnum)
16418 {
16419 pos.col = 0;
16420 if (name[1] == '0') /* "w0": first visible line */
16421 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016422 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016423 pos.lnum = curwin->w_topline;
16424 return &pos;
16425 }
16426 else if (name[1] == '$') /* "w$": last visible line */
16427 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016428 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016429 pos.lnum = curwin->w_botline - 1;
16430 return &pos;
16431 }
16432 }
16433 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 {
16435 if (lnum)
16436 {
16437 pos.lnum = curbuf->b_ml.ml_line_count;
16438 pos.col = 0;
16439 }
16440 else
16441 {
16442 pos.lnum = curwin->w_cursor.lnum;
16443 pos.col = (colnr_T)STRLEN(ml_get_curline());
16444 }
16445 return &pos;
16446 }
16447 return NULL;
16448}
16449
16450/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016451 * Convert list in "arg" into a position and optional file number.
16452 * When "fnump" is NULL there is no file number, only 3 items.
16453 * Note that the column is passed on as-is, the caller may want to decrement
16454 * it to use 1 for the first column.
16455 * Return FAIL when conversion is not possible, doesn't check the position for
16456 * validity.
16457 */
16458 static int
16459list2fpos(arg, posp, fnump)
16460 typval_T *arg;
16461 pos_T *posp;
16462 int *fnump;
16463{
16464 list_T *l = arg->vval.v_list;
16465 long i = 0;
16466 long n;
16467
16468 /* List must be: [fnum, lnum, col, coladd] */
16469 if (arg->v_type != VAR_LIST || l == NULL
16470 || l->lv_len != (fnump == NULL ? 3 : 4))
16471 return FAIL;
16472
16473 if (fnump != NULL)
16474 {
16475 n = list_find_nr(l, i++, NULL); /* fnum */
16476 if (n < 0)
16477 return FAIL;
16478 if (n == 0)
16479 n = curbuf->b_fnum; /* current buffer */
16480 *fnump = n;
16481 }
16482
16483 n = list_find_nr(l, i++, NULL); /* lnum */
16484 if (n < 0)
16485 return FAIL;
16486 posp->lnum = n;
16487
16488 n = list_find_nr(l, i++, NULL); /* col */
16489 if (n < 0)
16490 return FAIL;
16491 posp->col = n;
16492
16493#ifdef FEAT_VIRTUALEDIT
16494 n = list_find_nr(l, i, NULL);
16495 if (n < 0)
16496 return FAIL;
16497 posp->coladd = n;
16498#endif
16499
16500 return OK;
16501}
16502
16503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504 * Get the length of an environment variable name.
16505 * Advance "arg" to the first character after the name.
16506 * Return 0 for error.
16507 */
16508 static int
16509get_env_len(arg)
16510 char_u **arg;
16511{
16512 char_u *p;
16513 int len;
16514
16515 for (p = *arg; vim_isIDc(*p); ++p)
16516 ;
16517 if (p == *arg) /* no name found */
16518 return 0;
16519
16520 len = (int)(p - *arg);
16521 *arg = p;
16522 return len;
16523}
16524
16525/*
16526 * Get the length of the name of a function or internal variable.
16527 * "arg" is advanced to the first non-white character after the name.
16528 * Return 0 if something is wrong.
16529 */
16530 static int
16531get_id_len(arg)
16532 char_u **arg;
16533{
16534 char_u *p;
16535 int len;
16536
16537 /* Find the end of the name. */
16538 for (p = *arg; eval_isnamec(*p); ++p)
16539 ;
16540 if (p == *arg) /* no name found */
16541 return 0;
16542
16543 len = (int)(p - *arg);
16544 *arg = skipwhite(p);
16545
16546 return len;
16547}
16548
16549/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016550 * Get the length of the name of a variable or function.
16551 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016553 * Return -1 if curly braces expansion failed.
16554 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 * If the name contains 'magic' {}'s, expand them and return the
16556 * expanded name in an allocated string via 'alias' - caller must free.
16557 */
16558 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016559get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 char_u **arg;
16561 char_u **alias;
16562 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016563 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564{
16565 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 char_u *p;
16567 char_u *expr_start;
16568 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569
16570 *alias = NULL; /* default to no alias */
16571
16572 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16573 && (*arg)[2] == (int)KE_SNR)
16574 {
16575 /* hard coded <SNR>, already translated */
16576 *arg += 3;
16577 return get_id_len(arg) + 3;
16578 }
16579 len = eval_fname_script(*arg);
16580 if (len > 0)
16581 {
16582 /* literal "<SID>", "s:" or "<SNR>" */
16583 *arg += len;
16584 }
16585
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016587 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016589 p = find_name_end(*arg, &expr_start, &expr_end,
16590 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 if (expr_start != NULL)
16592 {
16593 char_u *temp_string;
16594
16595 if (!evaluate)
16596 {
16597 len += (int)(p - *arg);
16598 *arg = skipwhite(p);
16599 return len;
16600 }
16601
16602 /*
16603 * Include any <SID> etc in the expanded string:
16604 * Thus the -len here.
16605 */
16606 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16607 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016608 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 *alias = temp_string;
16610 *arg = skipwhite(p);
16611 return (int)STRLEN(temp_string);
16612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613
16614 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016615 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616 EMSG2(_(e_invexpr2), *arg);
16617
16618 return len;
16619}
16620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621/*
16622 * Find the end of a variable or function name, taking care of magic braces.
16623 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16624 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016625 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626 * Return a pointer to just after the name. Equal to "arg" if there is no
16627 * valid name.
16628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016630find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 char_u *arg;
16632 char_u **expr_start;
16633 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016634 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636 int mb_nest = 0;
16637 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 char_u *p;
16639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016640 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642 *expr_start = NULL;
16643 *expr_end = NULL;
16644 }
16645
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016646 /* Quick check for valid starting character. */
16647 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16648 return arg;
16649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016650 for (p = arg; *p != NUL
16651 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016652 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016653 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016654 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016655 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016656 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016657 if (*p == '\'')
16658 {
16659 /* skip over 'string' to avoid counting [ and ] inside it. */
16660 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16661 ;
16662 if (*p == NUL)
16663 break;
16664 }
16665 else if (*p == '"')
16666 {
16667 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16668 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16669 if (*p == '\\' && p[1] != NUL)
16670 ++p;
16671 if (*p == NUL)
16672 break;
16673 }
16674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016677 if (*p == '[')
16678 ++br_nest;
16679 else if (*p == ']')
16680 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016683 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016685 if (*p == '{')
16686 {
16687 mb_nest++;
16688 if (expr_start != NULL && *expr_start == NULL)
16689 *expr_start = p;
16690 }
16691 else if (*p == '}')
16692 {
16693 mb_nest--;
16694 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16695 *expr_end = p;
16696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 }
16699
16700 return p;
16701}
16702
16703/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016704 * Expands out the 'magic' {}'s in a variable/function name.
16705 * Note that this can call itself recursively, to deal with
16706 * constructs like foo{bar}{baz}{bam}
16707 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16708 * "in_start" ^
16709 * "expr_start" ^
16710 * "expr_end" ^
16711 * "in_end" ^
16712 *
16713 * Returns a new allocated string, which the caller must free.
16714 * Returns NULL for failure.
16715 */
16716 static char_u *
16717make_expanded_name(in_start, expr_start, expr_end, in_end)
16718 char_u *in_start;
16719 char_u *expr_start;
16720 char_u *expr_end;
16721 char_u *in_end;
16722{
16723 char_u c1;
16724 char_u *retval = NULL;
16725 char_u *temp_result;
16726 char_u *nextcmd = NULL;
16727
16728 if (expr_end == NULL || in_end == NULL)
16729 return NULL;
16730 *expr_start = NUL;
16731 *expr_end = NUL;
16732 c1 = *in_end;
16733 *in_end = NUL;
16734
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016735 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016736 if (temp_result != NULL && nextcmd == NULL)
16737 {
16738 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16739 + (in_end - expr_end) + 1));
16740 if (retval != NULL)
16741 {
16742 STRCPY(retval, in_start);
16743 STRCAT(retval, temp_result);
16744 STRCAT(retval, expr_end + 1);
16745 }
16746 }
16747 vim_free(temp_result);
16748
16749 *in_end = c1; /* put char back for error messages */
16750 *expr_start = '{';
16751 *expr_end = '}';
16752
16753 if (retval != NULL)
16754 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016755 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016756 if (expr_start != NULL)
16757 {
16758 /* Further expansion! */
16759 temp_result = make_expanded_name(retval, expr_start,
16760 expr_end, temp_result);
16761 vim_free(retval);
16762 retval = temp_result;
16763 }
16764 }
16765
16766 return retval;
16767}
16768
16769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016771 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 */
16773 static int
16774eval_isnamec(c)
16775 int c;
16776{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016777 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16778}
16779
16780/*
16781 * Return TRUE if character "c" can be used as the first character in a
16782 * variable or function name (excluding '{' and '}').
16783 */
16784 static int
16785eval_isnamec1(c)
16786 int c;
16787{
16788 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789}
16790
16791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 * Set number v: variable to "val".
16793 */
16794 void
16795set_vim_var_nr(idx, val)
16796 int idx;
16797 long val;
16798{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016799 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800}
16801
16802/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016803 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 */
16805 long
16806get_vim_var_nr(idx)
16807 int idx;
16808{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016809 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810}
16811
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016812#if defined(FEAT_AUTOCMD) || defined(PROTO)
16813/*
16814 * Get string v: variable value. Uses a static buffer, can only be used once.
16815 */
16816 char_u *
16817get_vim_var_str(idx)
16818 int idx;
16819{
16820 return get_tv_string(&vimvars[idx].vv_tv);
16821}
16822#endif
16823
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824/*
16825 * Set v:count, v:count1 and v:prevcount.
16826 */
16827 void
16828set_vcount(count, count1)
16829 long count;
16830 long count1;
16831{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016832 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16833 vimvars[VV_COUNT].vv_nr = count;
16834 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835}
16836
16837/*
16838 * Set string v: variable to a copy of "val".
16839 */
16840 void
16841set_vim_var_string(idx, val, len)
16842 int idx;
16843 char_u *val;
16844 int len; /* length of "val" to use or -1 (whole string) */
16845{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016846 /* Need to do this (at least) once, since we can't initialize a union.
16847 * Will always be invoked when "v:progname" is set. */
16848 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16849
Bram Moolenaare9a41262005-01-15 22:18:47 +000016850 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016852 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016854 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016856 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857}
16858
16859/*
16860 * Set v:register if needed.
16861 */
16862 void
16863set_reg_var(c)
16864 int c;
16865{
16866 char_u regname;
16867
16868 if (c == 0 || c == ' ')
16869 regname = '"';
16870 else
16871 regname = c;
16872 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016873 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874 set_vim_var_string(VV_REG, &regname, 1);
16875}
16876
16877/*
16878 * Get or set v:exception. If "oldval" == NULL, return the current value.
16879 * Otherwise, restore the value to "oldval" and return NULL.
16880 * Must always be called in pairs to save and restore v:exception! Does not
16881 * take care of memory allocations.
16882 */
16883 char_u *
16884v_exception(oldval)
16885 char_u *oldval;
16886{
16887 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016888 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889
Bram Moolenaare9a41262005-01-15 22:18:47 +000016890 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 return NULL;
16892}
16893
16894/*
16895 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16896 * Otherwise, restore the value to "oldval" and return NULL.
16897 * Must always be called in pairs to save and restore v:throwpoint! Does not
16898 * take care of memory allocations.
16899 */
16900 char_u *
16901v_throwpoint(oldval)
16902 char_u *oldval;
16903{
16904 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016905 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906
Bram Moolenaare9a41262005-01-15 22:18:47 +000016907 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908 return NULL;
16909}
16910
16911#if defined(FEAT_AUTOCMD) || defined(PROTO)
16912/*
16913 * Set v:cmdarg.
16914 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16915 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16916 * Must always be called in pairs!
16917 */
16918 char_u *
16919set_cmdarg(eap, oldarg)
16920 exarg_T *eap;
16921 char_u *oldarg;
16922{
16923 char_u *oldval;
16924 char_u *newval;
16925 unsigned len;
16926
Bram Moolenaare9a41262005-01-15 22:18:47 +000016927 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016928 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016930 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016931 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016932 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 }
16934
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016935 if (eap->force_bin == FORCE_BIN)
16936 len = 6;
16937 else if (eap->force_bin == FORCE_NOBIN)
16938 len = 8;
16939 else
16940 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016941
16942 if (eap->read_edit)
16943 len += 7;
16944
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016945 if (eap->force_ff != 0)
16946 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16947# ifdef FEAT_MBYTE
16948 if (eap->force_enc != 0)
16949 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016950 if (eap->bad_char != 0)
16951 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016952# endif
16953
16954 newval = alloc(len + 1);
16955 if (newval == NULL)
16956 return NULL;
16957
16958 if (eap->force_bin == FORCE_BIN)
16959 sprintf((char *)newval, " ++bin");
16960 else if (eap->force_bin == FORCE_NOBIN)
16961 sprintf((char *)newval, " ++nobin");
16962 else
16963 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016964
16965 if (eap->read_edit)
16966 STRCAT(newval, " ++edit");
16967
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016968 if (eap->force_ff != 0)
16969 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16970 eap->cmd + eap->force_ff);
16971# ifdef FEAT_MBYTE
16972 if (eap->force_enc != 0)
16973 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16974 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016975 if (eap->bad_char != 0)
16976 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16977 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016978# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016979 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016980 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981}
16982#endif
16983
16984/*
16985 * Get the value of internal variable "name".
16986 * Return OK or FAIL.
16987 */
16988 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016989get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 char_u *name;
16991 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016992 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016993 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994{
16995 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016996 typval_T *tv = NULL;
16997 typval_T atv;
16998 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000
17001 /* truncate the name, so that we can use strcmp() */
17002 cc = name[len];
17003 name[len] = NUL;
17004
17005 /*
17006 * Check for "b:changedtick".
17007 */
17008 if (STRCMP(name, "b:changedtick") == 0)
17009 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017010 atv.v_type = VAR_NUMBER;
17011 atv.vval.v_number = curbuf->b_changedtick;
17012 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 }
17014
17015 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 * Check for user-defined variables.
17017 */
17018 else
17019 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017020 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017022 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 }
17024
Bram Moolenaare9a41262005-01-15 22:18:47 +000017025 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017027 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017028 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 ret = FAIL;
17030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017031 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017032 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033
17034 name[len] = cc;
17035
17036 return ret;
17037}
17038
17039/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017040 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17041 * Also handle function call with Funcref variable: func(expr)
17042 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17043 */
17044 static int
17045handle_subscript(arg, rettv, evaluate, verbose)
17046 char_u **arg;
17047 typval_T *rettv;
17048 int evaluate; /* do more than finding the end */
17049 int verbose; /* give error messages */
17050{
17051 int ret = OK;
17052 dict_T *selfdict = NULL;
17053 char_u *s;
17054 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017055 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017056
17057 while (ret == OK
17058 && (**arg == '['
17059 || (**arg == '.' && rettv->v_type == VAR_DICT)
17060 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17061 && !vim_iswhite(*(*arg - 1)))
17062 {
17063 if (**arg == '(')
17064 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017065 /* need to copy the funcref so that we can clear rettv */
17066 functv = *rettv;
17067 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017068
17069 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017070 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017071 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017072 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17073 &len, evaluate, selfdict);
17074
17075 /* Clear the funcref afterwards, so that deleting it while
17076 * evaluating the arguments is possible (see test55). */
17077 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017078
17079 /* Stop the expression evaluation when immediately aborting on
17080 * error, or when an interrupt occurred or an exception was thrown
17081 * but not caught. */
17082 if (aborting())
17083 {
17084 if (ret == OK)
17085 clear_tv(rettv);
17086 ret = FAIL;
17087 }
17088 dict_unref(selfdict);
17089 selfdict = NULL;
17090 }
17091 else /* **arg == '[' || **arg == '.' */
17092 {
17093 dict_unref(selfdict);
17094 if (rettv->v_type == VAR_DICT)
17095 {
17096 selfdict = rettv->vval.v_dict;
17097 if (selfdict != NULL)
17098 ++selfdict->dv_refcount;
17099 }
17100 else
17101 selfdict = NULL;
17102 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17103 {
17104 clear_tv(rettv);
17105 ret = FAIL;
17106 }
17107 }
17108 }
17109 dict_unref(selfdict);
17110 return ret;
17111}
17112
17113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017114 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17115 * value).
17116 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017118alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017119{
Bram Moolenaar33570922005-01-25 22:26:29 +000017120 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017121}
17122
17123/*
17124 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 * The string "s" must have been allocated, it is consumed.
17126 * Return NULL for out of memory, the variable otherwise.
17127 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017128 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017129alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 char_u *s;
17131{
Bram Moolenaar33570922005-01-25 22:26:29 +000017132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017134 rettv = alloc_tv();
17135 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017137 rettv->v_type = VAR_STRING;
17138 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 }
17140 else
17141 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017142 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143}
17144
17145/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017146 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017148 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017149free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017150 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151{
17152 if (varp != NULL)
17153 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017154 switch (varp->v_type)
17155 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017156 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017157 func_unref(varp->vval.v_string);
17158 /*FALLTHROUGH*/
17159 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017160 vim_free(varp->vval.v_string);
17161 break;
17162 case VAR_LIST:
17163 list_unref(varp->vval.v_list);
17164 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017165 case VAR_DICT:
17166 dict_unref(varp->vval.v_dict);
17167 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017168 case VAR_NUMBER:
17169 case VAR_UNKNOWN:
17170 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017171 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017172 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017173 break;
17174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 vim_free(varp);
17176 }
17177}
17178
17179/*
17180 * Free the memory for a variable value and set the value to NULL or 0.
17181 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017182 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017183clear_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)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 {
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 varp->vval.v_string = NULL;
17196 break;
17197 case VAR_LIST:
17198 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017199 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017200 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017201 case VAR_DICT:
17202 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017203 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017204 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017205 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017206 varp->vval.v_number = 0;
17207 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017208 case VAR_UNKNOWN:
17209 break;
17210 default:
17211 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017213 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 }
17215}
17216
17217/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017218 * Set the value of a variable to NULL without freeing items.
17219 */
17220 static void
17221init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017222 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017223{
17224 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017225 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226}
17227
17228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 * Get the number value of a variable.
17230 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017231 * For incompatible types, return 0.
17232 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17233 * caller of incompatible types: it sets *denote to TRUE if "denote"
17234 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 */
17236 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017237get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017238 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017240 int error = FALSE;
17241
17242 return get_tv_number_chk(varp, &error); /* return 0L on error */
17243}
17244
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017245 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017246get_tv_number_chk(varp, denote)
17247 typval_T *varp;
17248 int *denote;
17249{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017250 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017252 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017254 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017255 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017256 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017257 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017258 break;
17259 case VAR_STRING:
17260 if (varp->vval.v_string != NULL)
17261 vim_str2nr(varp->vval.v_string, NULL, NULL,
17262 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017263 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017264 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017265 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017266 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017267 case VAR_DICT:
17268 EMSG(_("E728: Using a Dictionary as a number"));
17269 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017270 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017271 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017274 if (denote == NULL) /* useful for values that must be unsigned */
17275 n = -1;
17276 else
17277 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017278 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279}
17280
17281/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017282 * Get the lnum from the first argument.
17283 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017284 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 */
17286 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017287get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017288 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289{
Bram Moolenaar33570922005-01-25 22:26:29 +000017290 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291 linenr_T lnum;
17292
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017293 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 if (lnum == 0) /* no valid number, try using line() */
17295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017296 rettv.v_type = VAR_NUMBER;
17297 f_line(argvars, &rettv);
17298 lnum = rettv.vval.v_number;
17299 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 }
17301 return lnum;
17302}
17303
17304/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017305 * Get the lnum from the first argument.
17306 * Also accepts "$", then "buf" is used.
17307 * Returns 0 on error.
17308 */
17309 static linenr_T
17310get_tv_lnum_buf(argvars, buf)
17311 typval_T *argvars;
17312 buf_T *buf;
17313{
17314 if (argvars[0].v_type == VAR_STRING
17315 && argvars[0].vval.v_string != NULL
17316 && argvars[0].vval.v_string[0] == '$'
17317 && buf != NULL)
17318 return buf->b_ml.ml_line_count;
17319 return get_tv_number_chk(&argvars[0], NULL);
17320}
17321
17322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 * Get the string value of a variable.
17324 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017325 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17326 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 * If the String variable has never been set, return an empty string.
17328 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017329 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17330 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 */
17332 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335{
17336 static char_u mybuf[NUMBUFLEN];
17337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339}
17340
17341 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017342get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017343 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017344 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017346 char_u *res = get_tv_string_buf_chk(varp, buf);
17347
17348 return res != NULL ? res : (char_u *)"";
17349}
17350
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017351 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017352get_tv_string_chk(varp)
17353 typval_T *varp;
17354{
17355 static char_u mybuf[NUMBUFLEN];
17356
17357 return get_tv_string_buf_chk(varp, mybuf);
17358}
17359
17360 static char_u *
17361get_tv_string_buf_chk(varp, buf)
17362 typval_T *varp;
17363 char_u *buf;
17364{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017365 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017367 case VAR_NUMBER:
17368 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17369 return buf;
17370 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017371 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017372 break;
17373 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017374 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017375 break;
17376 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017377 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017378 break;
17379 case VAR_STRING:
17380 if (varp->vval.v_string != NULL)
17381 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017382 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017383 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017384 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017385 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017387 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388}
17389
17390/*
17391 * Find variable "name" in the list of variables.
17392 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017393 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017394 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017395 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017398find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017403 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404
Bram Moolenaara7043832005-01-21 11:56:39 +000017405 ht = find_var_ht(name, &varname);
17406 if (htp != NULL)
17407 *htp = ht;
17408 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017410 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411}
17412
17413/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017415 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017417 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017418find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017419 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017420 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017421 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017422{
Bram Moolenaar33570922005-01-25 22:26:29 +000017423 hashitem_T *hi;
17424
17425 if (*varname == NUL)
17426 {
17427 /* Must be something like "s:", otherwise "ht" would be NULL. */
17428 switch (varname[-2])
17429 {
17430 case 's': return &SCRIPT_SV(current_SID).sv_var;
17431 case 'g': return &globvars_var;
17432 case 'v': return &vimvars_var;
17433 case 'b': return &curbuf->b_bufvar;
17434 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017435#ifdef FEAT_WINDOWS
17436 case 't': return &curtab->tp_winvar;
17437#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017438 case 'l': return current_funccal == NULL
17439 ? NULL : &current_funccal->l_vars_var;
17440 case 'a': return current_funccal == NULL
17441 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017442 }
17443 return NULL;
17444 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017445
17446 hi = hash_find(ht, varname);
17447 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017448 {
17449 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017450 * worked find the variable again. Don't auto-load a script if it was
17451 * loaded already, otherwise it would be loaded every time when
17452 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017453 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017454 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017455 hi = hash_find(ht, varname);
17456 if (HASHITEM_EMPTY(hi))
17457 return NULL;
17458 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017459 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017460}
17461
17462/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017463 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017464 * Set "varname" to the start of name without ':'.
17465 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017466 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017467find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 char_u *name;
17469 char_u **varname;
17470{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017471 hashitem_T *hi;
17472
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 if (name[1] != ':')
17474 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017475 /* The name must not start with a colon or #. */
17476 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 return NULL;
17478 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017479
17480 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017481 hi = hash_find(&compat_hashtab, name);
17482 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017483 return &compat_hashtab;
17484
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017486 return &globvarht; /* global variable */
17487 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 }
17489 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017490 if (*name == 'g') /* global variable */
17491 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017492 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17493 */
17494 if (vim_strchr(name + 2, ':') != NULL
17495 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017496 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017501#ifdef FEAT_WINDOWS
17502 if (*name == 't') /* tab page variable */
17503 return &curtab->tp_vars.dv_hashtab;
17504#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017505 if (*name == 'v') /* v: variable */
17506 return &vimvarht;
17507 if (*name == 'a' && current_funccal != NULL) /* function argument */
17508 return &current_funccal->l_avars.dv_hashtab;
17509 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17510 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 if (*name == 's' /* script variable */
17512 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17513 return &SCRIPT_VARS(current_SID);
17514 return NULL;
17515}
17516
17517/*
17518 * Get the string value of a (global/local) variable.
17519 * Returns NULL when it doesn't exist.
17520 */
17521 char_u *
17522get_var_value(name)
17523 char_u *name;
17524{
Bram Moolenaar33570922005-01-25 22:26:29 +000017525 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526
Bram Moolenaara7043832005-01-21 11:56:39 +000017527 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 if (v == NULL)
17529 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017530 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531}
17532
17533/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 * sourcing this script and when executing functions defined in the script.
17536 */
17537 void
17538new_script_vars(id)
17539 scid_T id;
17540{
Bram Moolenaara7043832005-01-21 11:56:39 +000017541 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017542 hashtab_T *ht;
17543 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017544
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17546 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017547 /* Re-allocating ga_data means that an ht_array pointing to
17548 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017550 for (i = 1; i <= ga_scripts.ga_len; ++i)
17551 {
17552 ht = &SCRIPT_VARS(i);
17553 if (ht->ht_mask == HT_INIT_SIZE - 1)
17554 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 sv = &SCRIPT_SV(i);
17556 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017557 }
17558
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 while (ga_scripts.ga_len < id)
17560 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17562 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 }
17565 }
17566}
17567
17568/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017569 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17570 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 */
17572 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017573init_var_dict(dict, dict_var)
17574 dict_T *dict;
17575 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576{
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 hash_init(&dict->dv_hashtab);
17578 dict->dv_refcount = 99999;
17579 dict_var->di_tv.vval.v_dict = dict;
17580 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017581 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017582 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17583 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584}
17585
17586/*
17587 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017588 * Frees all allocated variables and the value they contain.
17589 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 */
17591 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017592vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017593 hashtab_T *ht;
17594{
17595 vars_clear_ext(ht, TRUE);
17596}
17597
17598/*
17599 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17600 */
17601 static void
17602vars_clear_ext(ht, free_val)
17603 hashtab_T *ht;
17604 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605{
Bram Moolenaara7043832005-01-21 11:56:39 +000017606 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017607 hashitem_T *hi;
17608 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609
Bram Moolenaar33570922005-01-25 22:26:29 +000017610 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017611 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017612 for (hi = ht->ht_array; todo > 0; ++hi)
17613 {
17614 if (!HASHITEM_EMPTY(hi))
17615 {
17616 --todo;
17617
Bram Moolenaar33570922005-01-25 22:26:29 +000017618 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017619 * ht_array might change then. hash_clear() takes care of it
17620 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017621 v = HI2DI(hi);
17622 if (free_val)
17623 clear_tv(&v->di_tv);
17624 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17625 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017626 }
17627 }
17628 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017629 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630}
17631
Bram Moolenaara7043832005-01-21 11:56:39 +000017632/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017633 * Delete a variable from hashtab "ht" at item "hi".
17634 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017637delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017638 hashtab_T *ht;
17639 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640{
Bram Moolenaar33570922005-01-25 22:26:29 +000017641 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017642
17643 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017644 clear_tv(&di->di_tv);
17645 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646}
17647
17648/*
17649 * List the value of one internal variable.
17650 */
17651 static void
17652list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017653 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 char_u *prefix;
17655{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017656 char_u *tofree;
17657 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017658 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017659
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017660 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017661 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017662 s == NULL ? (char_u *)"" : s);
17663 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664}
17665
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 static void
17667list_one_var_a(prefix, name, type, string)
17668 char_u *prefix;
17669 char_u *name;
17670 int type;
17671 char_u *string;
17672{
17673 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17674 if (name != NULL) /* "a:" vars don't have a name stored */
17675 msg_puts(name);
17676 msg_putchar(' ');
17677 msg_advance(22);
17678 if (type == VAR_NUMBER)
17679 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017680 else if (type == VAR_FUNC)
17681 msg_putchar('*');
17682 else if (type == VAR_LIST)
17683 {
17684 msg_putchar('[');
17685 if (*string == '[')
17686 ++string;
17687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017688 else if (type == VAR_DICT)
17689 {
17690 msg_putchar('{');
17691 if (*string == '{')
17692 ++string;
17693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 else
17695 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017696
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017698
17699 if (type == VAR_FUNC)
17700 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701}
17702
17703/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 * If the variable already exists, the value is updated.
17706 * Otherwise the variable is created.
17707 */
17708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017709set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017712 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713{
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017716 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017717 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017720 {
17721 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17722 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17723 ? name[2] : name[0]))
17724 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017725 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017726 return;
17727 }
17728 if (function_exists(name))
17729 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017730 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017731 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017732 return;
17733 }
17734 }
17735
Bram Moolenaara7043832005-01-21 11:56:39 +000017736 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017737 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017738 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017739 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017740 return;
17741 }
17742
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017743 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017746 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017747 if (var_check_ro(v->di_flags, name)
17748 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017749 return;
17750 if (v->di_tv.v_type != tv->v_type
17751 && !((v->di_tv.v_type == VAR_STRING
17752 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753 && (tv->v_type == VAR_STRING
17754 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017755 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017756 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017757 return;
17758 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017759
17760 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017761 * Handle setting internal v: variables separately: we don't change
17762 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 */
17764 if (ht == &vimvarht)
17765 {
17766 if (v->di_tv.v_type == VAR_STRING)
17767 {
17768 vim_free(v->di_tv.vval.v_string);
17769 if (copy || tv->v_type != VAR_STRING)
17770 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17771 else
17772 {
17773 /* Take over the string to avoid an extra alloc/free. */
17774 v->di_tv.vval.v_string = tv->vval.v_string;
17775 tv->vval.v_string = NULL;
17776 }
17777 }
17778 else if (v->di_tv.v_type != VAR_NUMBER)
17779 EMSG2(_(e_intern2), "set_var()");
17780 else
17781 v->di_tv.vval.v_number = get_tv_number(tv);
17782 return;
17783 }
17784
17785 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 }
17787 else /* add a new variable */
17788 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017789 /* Can't add "v:" variable. */
17790 if (ht == &vimvarht)
17791 {
17792 EMSG2(_(e_illvar), name);
17793 return;
17794 }
17795
Bram Moolenaar92124a32005-06-17 22:03:40 +000017796 /* Make sure the variable name is valid. */
17797 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017798 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17799 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017800 {
17801 EMSG2(_(e_illvar), varname);
17802 return;
17803 }
17804
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017805 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17806 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017807 if (v == NULL)
17808 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017809 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017812 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 return;
17814 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017815 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017818 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017819 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017820 else
17821 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017822 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017823 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017824 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826}
17827
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017828/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017829 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17830 * Also give an error message.
17831 */
17832 static int
17833var_check_ro(flags, name)
17834 int flags;
17835 char_u *name;
17836{
17837 if (flags & DI_FLAGS_RO)
17838 {
17839 EMSG2(_(e_readonlyvar), name);
17840 return TRUE;
17841 }
17842 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17843 {
17844 EMSG2(_(e_readonlysbx), name);
17845 return TRUE;
17846 }
17847 return FALSE;
17848}
17849
17850/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017851 * Return TRUE if typeval "tv" is set to be locked (immutable).
17852 * Also give an error message, using "name".
17853 */
17854 static int
17855tv_check_lock(lock, name)
17856 int lock;
17857 char_u *name;
17858{
17859 if (lock & VAR_LOCKED)
17860 {
17861 EMSG2(_("E741: Value is locked: %s"),
17862 name == NULL ? (char_u *)_("Unknown") : name);
17863 return TRUE;
17864 }
17865 if (lock & VAR_FIXED)
17866 {
17867 EMSG2(_("E742: Cannot change value of %s"),
17868 name == NULL ? (char_u *)_("Unknown") : name);
17869 return TRUE;
17870 }
17871 return FALSE;
17872}
17873
17874/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017876 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017877 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017880copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017881 typval_T *from;
17882 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017884 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017885 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017886 switch (from->v_type)
17887 {
17888 case VAR_NUMBER:
17889 to->vval.v_number = from->vval.v_number;
17890 break;
17891 case VAR_STRING:
17892 case VAR_FUNC:
17893 if (from->vval.v_string == NULL)
17894 to->vval.v_string = NULL;
17895 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017896 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017897 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017898 if (from->v_type == VAR_FUNC)
17899 func_ref(to->vval.v_string);
17900 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017901 break;
17902 case VAR_LIST:
17903 if (from->vval.v_list == NULL)
17904 to->vval.v_list = NULL;
17905 else
17906 {
17907 to->vval.v_list = from->vval.v_list;
17908 ++to->vval.v_list->lv_refcount;
17909 }
17910 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017911 case VAR_DICT:
17912 if (from->vval.v_dict == NULL)
17913 to->vval.v_dict = NULL;
17914 else
17915 {
17916 to->vval.v_dict = from->vval.v_dict;
17917 ++to->vval.v_dict->dv_refcount;
17918 }
17919 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017920 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017921 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017922 break;
17923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924}
17925
17926/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017927 * Make a copy of an item.
17928 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017929 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17930 * reference to an already copied list/dict can be used.
17931 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017932 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017933 static int
17934item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 typval_T *from;
17936 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017937 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017938 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017939{
17940 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017941 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017942
Bram Moolenaar33570922005-01-25 22:26:29 +000017943 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017944 {
17945 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017946 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017947 }
17948 ++recurse;
17949
17950 switch (from->v_type)
17951 {
17952 case VAR_NUMBER:
17953 case VAR_STRING:
17954 case VAR_FUNC:
17955 copy_tv(from, to);
17956 break;
17957 case VAR_LIST:
17958 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017959 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017960 if (from->vval.v_list == NULL)
17961 to->vval.v_list = NULL;
17962 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17963 {
17964 /* use the copy made earlier */
17965 to->vval.v_list = from->vval.v_list->lv_copylist;
17966 ++to->vval.v_list->lv_refcount;
17967 }
17968 else
17969 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17970 if (to->vval.v_list == NULL)
17971 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017972 break;
17973 case VAR_DICT:
17974 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017975 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017976 if (from->vval.v_dict == NULL)
17977 to->vval.v_dict = NULL;
17978 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17979 {
17980 /* use the copy made earlier */
17981 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17982 ++to->vval.v_dict->dv_refcount;
17983 }
17984 else
17985 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17986 if (to->vval.v_dict == NULL)
17987 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017988 break;
17989 default:
17990 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017991 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017992 }
17993 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017994 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017995}
17996
17997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 * ":echo expr1 ..." print each argument separated with a space, add a
17999 * newline at the end.
18000 * ":echon expr1 ..." print each argument plain.
18001 */
18002 void
18003ex_echo(eap)
18004 exarg_T *eap;
18005{
18006 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018007 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018008 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 char_u *p;
18010 int needclr = TRUE;
18011 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018012 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013
18014 if (eap->skip)
18015 ++emsg_skip;
18016 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18017 {
18018 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018019 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 {
18021 /*
18022 * Report the invalid expression unless the expression evaluation
18023 * has been cancelled due to an aborting error, an interrupt, or an
18024 * exception.
18025 */
18026 if (!aborting())
18027 EMSG2(_(e_invexpr2), p);
18028 break;
18029 }
18030 if (!eap->skip)
18031 {
18032 if (atstart)
18033 {
18034 atstart = FALSE;
18035 /* Call msg_start() after eval1(), evaluating the expression
18036 * may cause a message to appear. */
18037 if (eap->cmdidx == CMD_echo)
18038 msg_start();
18039 }
18040 else if (eap->cmdidx == CMD_echo)
18041 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018042 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018043 if (p != NULL)
18044 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018046 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018048 if (*p != TAB && needclr)
18049 {
18050 /* remove any text still there from the command */
18051 msg_clr_eos();
18052 needclr = FALSE;
18053 }
18054 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 }
18056 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018057 {
18058#ifdef FEAT_MBYTE
18059 if (has_mbyte)
18060 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018061 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018062
18063 (void)msg_outtrans_len_attr(p, i, echo_attr);
18064 p += i - 1;
18065 }
18066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018068 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018071 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018073 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 arg = skipwhite(arg);
18075 }
18076 eap->nextcmd = check_nextcmd(arg);
18077
18078 if (eap->skip)
18079 --emsg_skip;
18080 else
18081 {
18082 /* remove text that may still be there from the command */
18083 if (needclr)
18084 msg_clr_eos();
18085 if (eap->cmdidx == CMD_echo)
18086 msg_end();
18087 }
18088}
18089
18090/*
18091 * ":echohl {name}".
18092 */
18093 void
18094ex_echohl(eap)
18095 exarg_T *eap;
18096{
18097 int id;
18098
18099 id = syn_name2id(eap->arg);
18100 if (id == 0)
18101 echo_attr = 0;
18102 else
18103 echo_attr = syn_id2attr(id);
18104}
18105
18106/*
18107 * ":execute expr1 ..." execute the result of an expression.
18108 * ":echomsg expr1 ..." Print a message
18109 * ":echoerr expr1 ..." Print an error
18110 * Each gets spaces around each argument and a newline at the end for
18111 * echo commands
18112 */
18113 void
18114ex_execute(eap)
18115 exarg_T *eap;
18116{
18117 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018118 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 int ret = OK;
18120 char_u *p;
18121 garray_T ga;
18122 int len;
18123 int save_did_emsg;
18124
18125 ga_init2(&ga, 1, 80);
18126
18127 if (eap->skip)
18128 ++emsg_skip;
18129 while (*arg != NUL && *arg != '|' && *arg != '\n')
18130 {
18131 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 {
18134 /*
18135 * Report the invalid expression unless the expression evaluation
18136 * has been cancelled due to an aborting error, an interrupt, or an
18137 * exception.
18138 */
18139 if (!aborting())
18140 EMSG2(_(e_invexpr2), p);
18141 ret = FAIL;
18142 break;
18143 }
18144
18145 if (!eap->skip)
18146 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018147 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 len = (int)STRLEN(p);
18149 if (ga_grow(&ga, len + 2) == FAIL)
18150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018151 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 ret = FAIL;
18153 break;
18154 }
18155 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 ga.ga_len += len;
18159 }
18160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018161 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 arg = skipwhite(arg);
18163 }
18164
18165 if (ret != FAIL && ga.ga_data != NULL)
18166 {
18167 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018170 out_flush();
18171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 else if (eap->cmdidx == CMD_echoerr)
18173 {
18174 /* We don't want to abort following commands, restore did_emsg. */
18175 save_did_emsg = did_emsg;
18176 EMSG((char_u *)ga.ga_data);
18177 if (!force_abort)
18178 did_emsg = save_did_emsg;
18179 }
18180 else if (eap->cmdidx == CMD_execute)
18181 do_cmdline((char_u *)ga.ga_data,
18182 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18183 }
18184
18185 ga_clear(&ga);
18186
18187 if (eap->skip)
18188 --emsg_skip;
18189
18190 eap->nextcmd = check_nextcmd(arg);
18191}
18192
18193/*
18194 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18195 * "arg" points to the "&" or '+' when called, to "option" when returning.
18196 * Returns NULL when no option name found. Otherwise pointer to the char
18197 * after the option name.
18198 */
18199 static char_u *
18200find_option_end(arg, opt_flags)
18201 char_u **arg;
18202 int *opt_flags;
18203{
18204 char_u *p = *arg;
18205
18206 ++p;
18207 if (*p == 'g' && p[1] == ':')
18208 {
18209 *opt_flags = OPT_GLOBAL;
18210 p += 2;
18211 }
18212 else if (*p == 'l' && p[1] == ':')
18213 {
18214 *opt_flags = OPT_LOCAL;
18215 p += 2;
18216 }
18217 else
18218 *opt_flags = 0;
18219
18220 if (!ASCII_ISALPHA(*p))
18221 return NULL;
18222 *arg = p;
18223
18224 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18225 p += 4; /* termcap option */
18226 else
18227 while (ASCII_ISALPHA(*p))
18228 ++p;
18229 return p;
18230}
18231
18232/*
18233 * ":function"
18234 */
18235 void
18236ex_function(eap)
18237 exarg_T *eap;
18238{
18239 char_u *theline;
18240 int j;
18241 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 char_u *name = NULL;
18244 char_u *p;
18245 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018246 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 garray_T newargs;
18248 garray_T newlines;
18249 int varargs = FALSE;
18250 int mustend = FALSE;
18251 int flags = 0;
18252 ufunc_T *fp;
18253 int indent;
18254 int nesting;
18255 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018256 dictitem_T *v;
18257 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018258 static int func_nr = 0; /* number for nameless function */
18259 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018260 hashtab_T *ht;
18261 int todo;
18262 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018263 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264
18265 /*
18266 * ":function" without argument: list functions.
18267 */
18268 if (ends_excmd(*eap->arg))
18269 {
18270 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018271 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018272 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018273 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018274 {
18275 if (!HASHITEM_EMPTY(hi))
18276 {
18277 --todo;
18278 fp = HI2UF(hi);
18279 if (!isdigit(*fp->uf_name))
18280 list_func_head(fp, FALSE);
18281 }
18282 }
18283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 eap->nextcmd = check_nextcmd(eap->arg);
18285 return;
18286 }
18287
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018288 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018289 * ":function /pat": list functions matching pattern.
18290 */
18291 if (*eap->arg == '/')
18292 {
18293 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18294 if (!eap->skip)
18295 {
18296 regmatch_T regmatch;
18297
18298 c = *p;
18299 *p = NUL;
18300 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18301 *p = c;
18302 if (regmatch.regprog != NULL)
18303 {
18304 regmatch.rm_ic = p_ic;
18305
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018306 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018307 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18308 {
18309 if (!HASHITEM_EMPTY(hi))
18310 {
18311 --todo;
18312 fp = HI2UF(hi);
18313 if (!isdigit(*fp->uf_name)
18314 && vim_regexec(&regmatch, fp->uf_name, 0))
18315 list_func_head(fp, FALSE);
18316 }
18317 }
18318 }
18319 }
18320 if (*p == '/')
18321 ++p;
18322 eap->nextcmd = check_nextcmd(p);
18323 return;
18324 }
18325
18326 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018327 * Get the function name. There are these situations:
18328 * func normal function name
18329 * "name" == func, "fudi.fd_dict" == NULL
18330 * dict.func new dictionary entry
18331 * "name" == NULL, "fudi.fd_dict" set,
18332 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18333 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018334 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018335 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18336 * dict.func existing dict entry that's not a Funcref
18337 * "name" == NULL, "fudi.fd_dict" set,
18338 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018341 name = trans_function_name(&p, eap->skip, 0, &fudi);
18342 paren = (vim_strchr(p, '(') != NULL);
18343 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 {
18345 /*
18346 * Return on an invalid expression in braces, unless the expression
18347 * evaluation has been cancelled due to an aborting error, an
18348 * interrupt, or an exception.
18349 */
18350 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018351 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018352 if (!eap->skip && fudi.fd_newkey != NULL)
18353 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018354 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 else
18358 eap->skip = TRUE;
18359 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018360
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 /* An error in a function call during evaluation of an expression in magic
18362 * braces should not cause the function not to be defined. */
18363 saved_did_emsg = did_emsg;
18364 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365
18366 /*
18367 * ":function func" with only function name: list function.
18368 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018369 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 {
18371 if (!ends_excmd(*skipwhite(p)))
18372 {
18373 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018374 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 }
18376 eap->nextcmd = check_nextcmd(p);
18377 if (eap->nextcmd != NULL)
18378 *p = NUL;
18379 if (!eap->skip && !got_int)
18380 {
18381 fp = find_func(name);
18382 if (fp != NULL)
18383 {
18384 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018385 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018387 if (FUNCLINE(fp, j) == NULL)
18388 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 msg_putchar('\n');
18390 msg_outnum((long)(j + 1));
18391 if (j < 9)
18392 msg_putchar(' ');
18393 if (j < 99)
18394 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018395 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 out_flush(); /* show a line at a time */
18397 ui_breakcheck();
18398 }
18399 if (!got_int)
18400 {
18401 msg_putchar('\n');
18402 msg_puts((char_u *)" endfunction");
18403 }
18404 }
18405 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018406 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018408 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 }
18410
18411 /*
18412 * ":function name(arg1, arg2)" Define function.
18413 */
18414 p = skipwhite(p);
18415 if (*p != '(')
18416 {
18417 if (!eap->skip)
18418 {
18419 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018420 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 }
18422 /* attempt to continue by skipping some text */
18423 if (vim_strchr(p, '(') != NULL)
18424 p = vim_strchr(p, '(');
18425 }
18426 p = skipwhite(p + 1);
18427
18428 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18429 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18430
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018431 if (!eap->skip)
18432 {
18433 /* Check the name of the function. */
18434 if (name != NULL)
18435 arg = name;
18436 else
18437 arg = fudi.fd_newkey;
18438 if (arg != NULL)
18439 {
18440 if (*arg == K_SPECIAL)
18441 j = 3;
18442 else
18443 j = 0;
18444 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18445 : eval_isnamec(arg[j])))
18446 ++j;
18447 if (arg[j] != NUL)
18448 emsg_funcname(_(e_invarg2), arg);
18449 }
18450 }
18451
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 /*
18453 * Isolate the arguments: "arg1, arg2, ...)"
18454 */
18455 while (*p != ')')
18456 {
18457 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18458 {
18459 varargs = TRUE;
18460 p += 3;
18461 mustend = TRUE;
18462 }
18463 else
18464 {
18465 arg = p;
18466 while (ASCII_ISALNUM(*p) || *p == '_')
18467 ++p;
18468 if (arg == p || isdigit(*arg)
18469 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18470 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18471 {
18472 if (!eap->skip)
18473 EMSG2(_("E125: Illegal argument: %s"), arg);
18474 break;
18475 }
18476 if (ga_grow(&newargs, 1) == FAIL)
18477 goto erret;
18478 c = *p;
18479 *p = NUL;
18480 arg = vim_strsave(arg);
18481 if (arg == NULL)
18482 goto erret;
18483 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18484 *p = c;
18485 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 if (*p == ',')
18487 ++p;
18488 else
18489 mustend = TRUE;
18490 }
18491 p = skipwhite(p);
18492 if (mustend && *p != ')')
18493 {
18494 if (!eap->skip)
18495 EMSG2(_(e_invarg2), eap->arg);
18496 break;
18497 }
18498 }
18499 ++p; /* skip the ')' */
18500
Bram Moolenaare9a41262005-01-15 22:18:47 +000018501 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502 for (;;)
18503 {
18504 p = skipwhite(p);
18505 if (STRNCMP(p, "range", 5) == 0)
18506 {
18507 flags |= FC_RANGE;
18508 p += 5;
18509 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018510 else if (STRNCMP(p, "dict", 4) == 0)
18511 {
18512 flags |= FC_DICT;
18513 p += 4;
18514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 else if (STRNCMP(p, "abort", 5) == 0)
18516 {
18517 flags |= FC_ABORT;
18518 p += 5;
18519 }
18520 else
18521 break;
18522 }
18523
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018524 /* When there is a line break use what follows for the function body.
18525 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18526 if (*p == '\n')
18527 line_arg = p + 1;
18528 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 EMSG(_(e_trailing));
18530
18531 /*
18532 * Read the body of the function, until ":endfunction" is found.
18533 */
18534 if (KeyTyped)
18535 {
18536 /* Check if the function already exists, don't let the user type the
18537 * whole function before telling him it doesn't work! For a script we
18538 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018539 if (!eap->skip && !eap->forceit)
18540 {
18541 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18542 EMSG(_(e_funcdict));
18543 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018544 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018547 if (!eap->skip && did_emsg)
18548 goto erret;
18549
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 msg_putchar('\n'); /* don't overwrite the function name */
18551 cmdline_row = msg_row;
18552 }
18553
18554 indent = 2;
18555 nesting = 0;
18556 for (;;)
18557 {
18558 msg_scroll = TRUE;
18559 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018560 sourcing_lnum_off = sourcing_lnum;
18561
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018562 if (line_arg != NULL)
18563 {
18564 /* Use eap->arg, split up in parts by line breaks. */
18565 theline = line_arg;
18566 p = vim_strchr(theline, '\n');
18567 if (p == NULL)
18568 line_arg += STRLEN(line_arg);
18569 else
18570 {
18571 *p = NUL;
18572 line_arg = p + 1;
18573 }
18574 }
18575 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 theline = getcmdline(':', 0L, indent);
18577 else
18578 theline = eap->getline(':', eap->cookie, indent);
18579 if (KeyTyped)
18580 lines_left = Rows - 1;
18581 if (theline == NULL)
18582 {
18583 EMSG(_("E126: Missing :endfunction"));
18584 goto erret;
18585 }
18586
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018587 /* Detect line continuation: sourcing_lnum increased more than one. */
18588 if (sourcing_lnum > sourcing_lnum_off + 1)
18589 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18590 else
18591 sourcing_lnum_off = 0;
18592
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 if (skip_until != NULL)
18594 {
18595 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18596 * don't check for ":endfunc". */
18597 if (STRCMP(theline, skip_until) == 0)
18598 {
18599 vim_free(skip_until);
18600 skip_until = NULL;
18601 }
18602 }
18603 else
18604 {
18605 /* skip ':' and blanks*/
18606 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18607 ;
18608
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018609 /* Check for "endfunction". */
18610 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018612 if (line_arg == NULL)
18613 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 break;
18615 }
18616
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018617 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 * at "end". */
18619 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18620 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018621 else if (STRNCMP(p, "if", 2) == 0
18622 || STRNCMP(p, "wh", 2) == 0
18623 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 || STRNCMP(p, "try", 3) == 0)
18625 indent += 2;
18626
18627 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018628 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018630 if (*p == '!')
18631 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 p += eval_fname_script(p);
18633 if (ASCII_ISALPHA(*p))
18634 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018635 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 if (*skipwhite(p) == '(')
18637 {
18638 ++nesting;
18639 indent += 2;
18640 }
18641 }
18642 }
18643
18644 /* Check for ":append" or ":insert". */
18645 p = skip_range(p, NULL);
18646 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18647 || (p[0] == 'i'
18648 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18649 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18650 skip_until = vim_strsave((char_u *)".");
18651
18652 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18653 arg = skipwhite(skiptowhite(p));
18654 if (arg[0] == '<' && arg[1] =='<'
18655 && ((p[0] == 'p' && p[1] == 'y'
18656 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18657 || (p[0] == 'p' && p[1] == 'e'
18658 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18659 || (p[0] == 't' && p[1] == 'c'
18660 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18661 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18662 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018663 || (p[0] == 'm' && p[1] == 'z'
18664 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 ))
18666 {
18667 /* ":python <<" continues until a dot, like ":append" */
18668 p = skipwhite(arg + 2);
18669 if (*p == NUL)
18670 skip_until = vim_strsave((char_u *)".");
18671 else
18672 skip_until = vim_strsave(p);
18673 }
18674 }
18675
18676 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018677 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018678 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018679 if (line_arg == NULL)
18680 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018682 }
18683
18684 /* Copy the line to newly allocated memory. get_one_sourceline()
18685 * allocates 250 bytes per line, this saves 80% on average. The cost
18686 * is an extra alloc/free. */
18687 p = vim_strsave(theline);
18688 if (p != NULL)
18689 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018690 if (line_arg == NULL)
18691 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018692 theline = p;
18693 }
18694
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018695 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18696
18697 /* Add NULL lines for continuation lines, so that the line count is
18698 * equal to the index in the growarray. */
18699 while (sourcing_lnum_off-- > 0)
18700 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018701
18702 /* Check for end of eap->arg. */
18703 if (line_arg != NULL && *line_arg == NUL)
18704 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 }
18706
18707 /* Don't define the function when skipping commands or when an error was
18708 * detected. */
18709 if (eap->skip || did_emsg)
18710 goto erret;
18711
18712 /*
18713 * If there are no errors, add the function
18714 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018715 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018716 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018717 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018718 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018719 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018720 emsg_funcname("E707: Function name conflicts with variable: %s",
18721 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018722 goto erret;
18723 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018724
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018725 fp = find_func(name);
18726 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018728 if (!eap->forceit)
18729 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018730 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018731 goto erret;
18732 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018733 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018734 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018735 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018736 name);
18737 goto erret;
18738 }
18739 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018740 ga_clear_strings(&(fp->uf_args));
18741 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018742 vim_free(name);
18743 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 }
18746 else
18747 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018748 char numbuf[20];
18749
18750 fp = NULL;
18751 if (fudi.fd_newkey == NULL && !eap->forceit)
18752 {
18753 EMSG(_(e_funcdict));
18754 goto erret;
18755 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018756 if (fudi.fd_di == NULL)
18757 {
18758 /* Can't add a function to a locked dictionary */
18759 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18760 goto erret;
18761 }
18762 /* Can't change an existing function if it is locked */
18763 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18764 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018765
18766 /* Give the function a sequential number. Can only be used with a
18767 * Funcref! */
18768 vim_free(name);
18769 sprintf(numbuf, "%d", ++func_nr);
18770 name = vim_strsave((char_u *)numbuf);
18771 if (name == NULL)
18772 goto erret;
18773 }
18774
18775 if (fp == NULL)
18776 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018777 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018778 {
18779 int slen, plen;
18780 char_u *scriptname;
18781
18782 /* Check that the autoload name matches the script name. */
18783 j = FAIL;
18784 if (sourcing_name != NULL)
18785 {
18786 scriptname = autoload_name(name);
18787 if (scriptname != NULL)
18788 {
18789 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018790 plen = (int)STRLEN(p);
18791 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018792 if (slen > plen && fnamecmp(p,
18793 sourcing_name + slen - plen) == 0)
18794 j = OK;
18795 vim_free(scriptname);
18796 }
18797 }
18798 if (j == FAIL)
18799 {
18800 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18801 goto erret;
18802 }
18803 }
18804
18805 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 if (fp == NULL)
18807 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018808
18809 if (fudi.fd_dict != NULL)
18810 {
18811 if (fudi.fd_di == NULL)
18812 {
18813 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018814 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018815 if (fudi.fd_di == NULL)
18816 {
18817 vim_free(fp);
18818 goto erret;
18819 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018820 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18821 {
18822 vim_free(fudi.fd_di);
18823 goto erret;
18824 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018825 }
18826 else
18827 /* overwrite existing dict entry */
18828 clear_tv(&fudi.fd_di->di_tv);
18829 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018830 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018831 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018832 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018833
18834 /* behave like "dict" was used */
18835 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018836 }
18837
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018839 STRCPY(fp->uf_name, name);
18840 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018842 fp->uf_args = newargs;
18843 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018844#ifdef FEAT_PROFILE
18845 fp->uf_tml_count = NULL;
18846 fp->uf_tml_total = NULL;
18847 fp->uf_tml_self = NULL;
18848 fp->uf_profiling = FALSE;
18849 if (prof_def_func())
18850 func_do_profile(fp);
18851#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018852 fp->uf_varargs = varargs;
18853 fp->uf_flags = flags;
18854 fp->uf_calls = 0;
18855 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018856 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857
18858erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 ga_clear_strings(&newargs);
18860 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018861ret_free:
18862 vim_free(skip_until);
18863 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866}
18867
18868/*
18869 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018870 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018872 * flags:
18873 * TFN_INT: internal function name OK
18874 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 * Advances "pp" to just after the function name (if no error).
18876 */
18877 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018878trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 char_u **pp;
18880 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018881 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018882 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018884 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885 char_u *start;
18886 char_u *end;
18887 int lead;
18888 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018890 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018891
18892 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018893 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018895
18896 /* Check for hard coded <SNR>: already translated function ID (from a user
18897 * command). */
18898 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18899 && (*pp)[2] == (int)KE_SNR)
18900 {
18901 *pp += 3;
18902 len = get_id_len(pp) + 3;
18903 return vim_strnsave(start, len);
18904 }
18905
18906 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18907 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018909 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018911
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018912 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18913 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018914 if (end == start)
18915 {
18916 if (!skip)
18917 EMSG(_("E129: Function name required"));
18918 goto theend;
18919 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018920 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018921 {
18922 /*
18923 * Report an invalid expression in braces, unless the expression
18924 * evaluation has been cancelled due to an aborting error, an
18925 * interrupt, or an exception.
18926 */
18927 if (!aborting())
18928 {
18929 if (end != NULL)
18930 EMSG2(_(e_invarg2), start);
18931 }
18932 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018933 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018934 goto theend;
18935 }
18936
18937 if (lv.ll_tv != NULL)
18938 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018939 if (fdp != NULL)
18940 {
18941 fdp->fd_dict = lv.ll_dict;
18942 fdp->fd_newkey = lv.ll_newkey;
18943 lv.ll_newkey = NULL;
18944 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018945 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018946 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18947 {
18948 name = vim_strsave(lv.ll_tv->vval.v_string);
18949 *pp = end;
18950 }
18951 else
18952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018953 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18954 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018955 EMSG(_(e_funcref));
18956 else
18957 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018958 name = NULL;
18959 }
18960 goto theend;
18961 }
18962
18963 if (lv.ll_name == NULL)
18964 {
18965 /* Error found, but continue after the function name. */
18966 *pp = end;
18967 goto theend;
18968 }
18969
18970 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018971 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018972 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018973 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18974 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18975 {
18976 /* When there was "s:" already or the name expanded to get a
18977 * leading "s:" then remove it. */
18978 lv.ll_name += 2;
18979 len -= 2;
18980 lead = 2;
18981 }
18982 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018983 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018984 {
18985 if (lead == 2) /* skip over "s:" */
18986 lv.ll_name += 2;
18987 len = (int)(end - lv.ll_name);
18988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018989
18990 /*
18991 * Copy the function name to allocated memory.
18992 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18993 * Accept <SNR>123_name() outside a script.
18994 */
18995 if (skip)
18996 lead = 0; /* do nothing */
18997 else if (lead > 0)
18998 {
18999 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019000 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19001 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019002 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019003 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019004 if (current_SID <= 0)
19005 {
19006 EMSG(_(e_usingsid));
19007 goto theend;
19008 }
19009 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19010 lead += (int)STRLEN(sid_buf);
19011 }
19012 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019013 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019014 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019015 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019016 goto theend;
19017 }
19018 name = alloc((unsigned)(len + lead + 1));
19019 if (name != NULL)
19020 {
19021 if (lead > 0)
19022 {
19023 name[0] = K_SPECIAL;
19024 name[1] = KS_EXTRA;
19025 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019026 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019027 STRCPY(name + 3, sid_buf);
19028 }
19029 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19030 name[len + lead] = NUL;
19031 }
19032 *pp = end;
19033
19034theend:
19035 clear_lval(&lv);
19036 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037}
19038
19039/*
19040 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19041 * Return 2 if "p" starts with "s:".
19042 * Return 0 otherwise.
19043 */
19044 static int
19045eval_fname_script(p)
19046 char_u *p;
19047{
19048 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19049 || STRNICMP(p + 1, "SNR>", 4) == 0))
19050 return 5;
19051 if (p[0] == 's' && p[1] == ':')
19052 return 2;
19053 return 0;
19054}
19055
19056/*
19057 * Return TRUE if "p" starts with "<SID>" or "s:".
19058 * Only works if eval_fname_script() returned non-zero for "p"!
19059 */
19060 static int
19061eval_fname_sid(p)
19062 char_u *p;
19063{
19064 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19065}
19066
19067/*
19068 * List the head of the function: "name(arg1, arg2)".
19069 */
19070 static void
19071list_func_head(fp, indent)
19072 ufunc_T *fp;
19073 int indent;
19074{
19075 int j;
19076
19077 msg_start();
19078 if (indent)
19079 MSG_PUTS(" ");
19080 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019081 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 {
19083 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019084 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 }
19086 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019087 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019089 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 {
19091 if (j)
19092 MSG_PUTS(", ");
19093 msg_puts(FUNCARG(fp, j));
19094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019095 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 {
19097 if (j)
19098 MSG_PUTS(", ");
19099 MSG_PUTS("...");
19100 }
19101 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019102 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019103 if (p_verbose > 0)
19104 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105}
19106
19107/*
19108 * Find a function by name, return pointer to it in ufuncs.
19109 * Return NULL for unknown function.
19110 */
19111 static ufunc_T *
19112find_func(name)
19113 char_u *name;
19114{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019115 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019117 hi = hash_find(&func_hashtab, name);
19118 if (!HASHITEM_EMPTY(hi))
19119 return HI2UF(hi);
19120 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121}
19122
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019123#if defined(EXITFREE) || defined(PROTO)
19124 void
19125free_all_functions()
19126{
19127 hashitem_T *hi;
19128
19129 /* Need to start all over every time, because func_free() may change the
19130 * hash table. */
19131 while (func_hashtab.ht_used > 0)
19132 for (hi = func_hashtab.ht_array; ; ++hi)
19133 if (!HASHITEM_EMPTY(hi))
19134 {
19135 func_free(HI2UF(hi));
19136 break;
19137 }
19138}
19139#endif
19140
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019141/*
19142 * Return TRUE if a function "name" exists.
19143 */
19144 static int
19145function_exists(name)
19146 char_u *name;
19147{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019148 char_u *nm = name;
19149 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019150 int n = FALSE;
19151
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019152 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019153 nm = skipwhite(nm);
19154
19155 /* Only accept "funcname", "funcname ", "funcname (..." and
19156 * "funcname(...", not "funcname!...". */
19157 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019158 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019159 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019160 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019161 else
19162 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019163 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019164 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019165 return n;
19166}
19167
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019168/*
19169 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019170 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019171 */
19172 static int
19173builtin_function(name)
19174 char_u *name;
19175{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019176 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19177 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019178}
19179
Bram Moolenaar05159a02005-02-26 23:04:13 +000019180#if defined(FEAT_PROFILE) || defined(PROTO)
19181/*
19182 * Start profiling function "fp".
19183 */
19184 static void
19185func_do_profile(fp)
19186 ufunc_T *fp;
19187{
19188 fp->uf_tm_count = 0;
19189 profile_zero(&fp->uf_tm_self);
19190 profile_zero(&fp->uf_tm_total);
19191 if (fp->uf_tml_count == NULL)
19192 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19193 (sizeof(int) * fp->uf_lines.ga_len));
19194 if (fp->uf_tml_total == NULL)
19195 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19196 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19197 if (fp->uf_tml_self == NULL)
19198 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19199 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19200 fp->uf_tml_idx = -1;
19201 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19202 || fp->uf_tml_self == NULL)
19203 return; /* out of memory */
19204
19205 fp->uf_profiling = TRUE;
19206}
19207
19208/*
19209 * Dump the profiling results for all functions in file "fd".
19210 */
19211 void
19212func_dump_profile(fd)
19213 FILE *fd;
19214{
19215 hashitem_T *hi;
19216 int todo;
19217 ufunc_T *fp;
19218 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019219 ufunc_T **sorttab;
19220 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019221
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019222 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019223 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19224
Bram Moolenaar05159a02005-02-26 23:04:13 +000019225 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19226 {
19227 if (!HASHITEM_EMPTY(hi))
19228 {
19229 --todo;
19230 fp = HI2UF(hi);
19231 if (fp->uf_profiling)
19232 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019233 if (sorttab != NULL)
19234 sorttab[st_len++] = fp;
19235
Bram Moolenaar05159a02005-02-26 23:04:13 +000019236 if (fp->uf_name[0] == K_SPECIAL)
19237 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19238 else
19239 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19240 if (fp->uf_tm_count == 1)
19241 fprintf(fd, "Called 1 time\n");
19242 else
19243 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19244 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19245 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19246 fprintf(fd, "\n");
19247 fprintf(fd, "count total (s) self (s)\n");
19248
19249 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19250 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019251 if (FUNCLINE(fp, i) == NULL)
19252 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019253 prof_func_line(fd, fp->uf_tml_count[i],
19254 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019255 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19256 }
19257 fprintf(fd, "\n");
19258 }
19259 }
19260 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019261
19262 if (sorttab != NULL && st_len > 0)
19263 {
19264 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19265 prof_total_cmp);
19266 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19267 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19268 prof_self_cmp);
19269 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19270 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019271}
Bram Moolenaar73830342005-02-28 22:48:19 +000019272
19273 static void
19274prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19275 FILE *fd;
19276 ufunc_T **sorttab;
19277 int st_len;
19278 char *title;
19279 int prefer_self; /* when equal print only self time */
19280{
19281 int i;
19282 ufunc_T *fp;
19283
19284 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19285 fprintf(fd, "count total (s) self (s) function\n");
19286 for (i = 0; i < 20 && i < st_len; ++i)
19287 {
19288 fp = sorttab[i];
19289 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19290 prefer_self);
19291 if (fp->uf_name[0] == K_SPECIAL)
19292 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19293 else
19294 fprintf(fd, " %s()\n", fp->uf_name);
19295 }
19296 fprintf(fd, "\n");
19297}
19298
19299/*
19300 * Print the count and times for one function or function line.
19301 */
19302 static void
19303prof_func_line(fd, count, total, self, prefer_self)
19304 FILE *fd;
19305 int count;
19306 proftime_T *total;
19307 proftime_T *self;
19308 int prefer_self; /* when equal print only self time */
19309{
19310 if (count > 0)
19311 {
19312 fprintf(fd, "%5d ", count);
19313 if (prefer_self && profile_equal(total, self))
19314 fprintf(fd, " ");
19315 else
19316 fprintf(fd, "%s ", profile_msg(total));
19317 if (!prefer_self && profile_equal(total, self))
19318 fprintf(fd, " ");
19319 else
19320 fprintf(fd, "%s ", profile_msg(self));
19321 }
19322 else
19323 fprintf(fd, " ");
19324}
19325
19326/*
19327 * Compare function for total time sorting.
19328 */
19329 static int
19330#ifdef __BORLANDC__
19331_RTLENTRYF
19332#endif
19333prof_total_cmp(s1, s2)
19334 const void *s1;
19335 const void *s2;
19336{
19337 ufunc_T *p1, *p2;
19338
19339 p1 = *(ufunc_T **)s1;
19340 p2 = *(ufunc_T **)s2;
19341 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19342}
19343
19344/*
19345 * Compare function for self time sorting.
19346 */
19347 static int
19348#ifdef __BORLANDC__
19349_RTLENTRYF
19350#endif
19351prof_self_cmp(s1, s2)
19352 const void *s1;
19353 const void *s2;
19354{
19355 ufunc_T *p1, *p2;
19356
19357 p1 = *(ufunc_T **)s1;
19358 p2 = *(ufunc_T **)s2;
19359 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19360}
19361
Bram Moolenaar05159a02005-02-26 23:04:13 +000019362#endif
19363
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019364/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019365 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019366 * Return TRUE if a package was loaded.
19367 */
19368 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019369script_autoload(name, reload)
19370 char_u *name;
19371 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019372{
19373 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019374 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019375 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019376 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019377
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019378 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019379 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019380 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019381 return FALSE;
19382
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019383 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019384
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019385 /* Find the name in the list of previously loaded package names. Skip
19386 * "autoload/", it's always the same. */
19387 for (i = 0; i < ga_loaded.ga_len; ++i)
19388 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19389 break;
19390 if (!reload && i < ga_loaded.ga_len)
19391 ret = FALSE; /* was loaded already */
19392 else
19393 {
19394 /* Remember the name if it wasn't loaded already. */
19395 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19396 {
19397 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19398 tofree = NULL;
19399 }
19400
19401 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019402 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019403 ret = TRUE;
19404 }
19405
19406 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019407 return ret;
19408}
19409
19410/*
19411 * Return the autoload script name for a function or variable name.
19412 * Returns NULL when out of memory.
19413 */
19414 static char_u *
19415autoload_name(name)
19416 char_u *name;
19417{
19418 char_u *p;
19419 char_u *scriptname;
19420
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019421 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019422 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19423 if (scriptname == NULL)
19424 return FALSE;
19425 STRCPY(scriptname, "autoload/");
19426 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019427 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019428 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019429 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019430 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019431 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019432}
19433
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19435
19436/*
19437 * Function given to ExpandGeneric() to obtain the list of user defined
19438 * function names.
19439 */
19440 char_u *
19441get_user_func_name(xp, idx)
19442 expand_T *xp;
19443 int idx;
19444{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019445 static long_u done;
19446 static hashitem_T *hi;
19447 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448
19449 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019451 done = 0;
19452 hi = func_hashtab.ht_array;
19453 }
19454 if (done < func_hashtab.ht_used)
19455 {
19456 if (done++ > 0)
19457 ++hi;
19458 while (HASHITEM_EMPTY(hi))
19459 ++hi;
19460 fp = HI2UF(hi);
19461
19462 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19463 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464
19465 cat_func_name(IObuff, fp);
19466 if (xp->xp_context != EXPAND_USER_FUNC)
19467 {
19468 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019469 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 STRCAT(IObuff, ")");
19471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 return IObuff;
19473 }
19474 return NULL;
19475}
19476
19477#endif /* FEAT_CMDL_COMPL */
19478
19479/*
19480 * Copy the function name of "fp" to buffer "buf".
19481 * "buf" must be able to hold the function name plus three bytes.
19482 * Takes care of script-local function names.
19483 */
19484 static void
19485cat_func_name(buf, fp)
19486 char_u *buf;
19487 ufunc_T *fp;
19488{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019489 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 {
19491 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019492 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 }
19494 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019495 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496}
19497
19498/*
19499 * ":delfunction {name}"
19500 */
19501 void
19502ex_delfunction(eap)
19503 exarg_T *eap;
19504{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019505 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 char_u *p;
19507 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019508 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509
19510 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019511 name = trans_function_name(&p, eap->skip, 0, &fudi);
19512 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019514 {
19515 if (fudi.fd_dict != NULL && !eap->skip)
19516 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 if (!ends_excmd(*skipwhite(p)))
19520 {
19521 vim_free(name);
19522 EMSG(_(e_trailing));
19523 return;
19524 }
19525 eap->nextcmd = check_nextcmd(p);
19526 if (eap->nextcmd != NULL)
19527 *p = NUL;
19528
19529 if (!eap->skip)
19530 fp = find_func(name);
19531 vim_free(name);
19532
19533 if (!eap->skip)
19534 {
19535 if (fp == NULL)
19536 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019537 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 return;
19539 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019540 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 {
19542 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19543 return;
19544 }
19545
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019546 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019548 /* Delete the dict item that refers to the function, it will
19549 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019550 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019552 else
19553 func_free(fp);
19554 }
19555}
19556
19557/*
19558 * Free a function and remove it from the list of functions.
19559 */
19560 static void
19561func_free(fp)
19562 ufunc_T *fp;
19563{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019564 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019565
19566 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019567 ga_clear_strings(&(fp->uf_args));
19568 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019569#ifdef FEAT_PROFILE
19570 vim_free(fp->uf_tml_count);
19571 vim_free(fp->uf_tml_total);
19572 vim_free(fp->uf_tml_self);
19573#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019574
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019575 /* remove the function from the function hashtable */
19576 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19577 if (HASHITEM_EMPTY(hi))
19578 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019579 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019580 hash_remove(&func_hashtab, hi);
19581
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019582 vim_free(fp);
19583}
19584
19585/*
19586 * Unreference a Function: decrement the reference count and free it when it
19587 * becomes zero. Only for numbered functions.
19588 */
19589 static void
19590func_unref(name)
19591 char_u *name;
19592{
19593 ufunc_T *fp;
19594
19595 if (name != NULL && isdigit(*name))
19596 {
19597 fp = find_func(name);
19598 if (fp == NULL)
19599 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019600 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019601 {
19602 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019603 * when "uf_calls" becomes zero. */
19604 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019605 func_free(fp);
19606 }
19607 }
19608}
19609
19610/*
19611 * Count a reference to a Function.
19612 */
19613 static void
19614func_ref(name)
19615 char_u *name;
19616{
19617 ufunc_T *fp;
19618
19619 if (name != NULL && isdigit(*name))
19620 {
19621 fp = find_func(name);
19622 if (fp == NULL)
19623 EMSG2(_(e_intern2), "func_ref()");
19624 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019625 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 }
19627}
19628
19629/*
19630 * Call a user function.
19631 */
19632 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019633call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 ufunc_T *fp; /* pointer to function */
19635 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 typval_T *argvars; /* arguments */
19637 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 linenr_T firstline; /* first line of range */
19639 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019640 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641{
Bram Moolenaar33570922005-01-25 22:26:29 +000019642 char_u *save_sourcing_name;
19643 linenr_T save_sourcing_lnum;
19644 scid_T save_current_SID;
19645 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019646 int save_did_emsg;
19647 static int depth = 0;
19648 dictitem_T *v;
19649 int fixvar_idx = 0; /* index in fixvar[] */
19650 int i;
19651 int ai;
19652 char_u numbuf[NUMBUFLEN];
19653 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019654#ifdef FEAT_PROFILE
19655 proftime_T wait_start;
19656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657
19658 /* If depth of calling is getting too high, don't execute the function */
19659 if (depth >= p_mfd)
19660 {
19661 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019662 rettv->v_type = VAR_NUMBER;
19663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 return;
19665 }
19666 ++depth;
19667
19668 line_breakcheck(); /* check for CTRL-C hit */
19669
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019670 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019673 fc.rettv = rettv;
19674 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 fc.linenr = 0;
19676 fc.returned = FALSE;
19677 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019679 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 fc.dbg_tick = debug_tick;
19681
Bram Moolenaar33570922005-01-25 22:26:29 +000019682 /*
19683 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19684 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19685 * each argument variable and saves a lot of time.
19686 */
19687 /*
19688 * Init l: variables.
19689 */
19690 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019691 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019692 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019693 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19694 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019695 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019696 name = v->di_key;
19697 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019698 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19699 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19700 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019701 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019702 v->di_tv.vval.v_dict = selfdict;
19703 ++selfdict->dv_refcount;
19704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019705
Bram Moolenaar33570922005-01-25 22:26:29 +000019706 /*
19707 * Init a: variables.
19708 * Set a:0 to "argcount".
19709 * Set a:000 to a list with room for the "..." arguments.
19710 */
19711 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19712 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019713 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019714 v = &fc.fixvar[fixvar_idx++].var;
19715 STRCPY(v->di_key, "000");
19716 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19717 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19718 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019719 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019720 v->di_tv.vval.v_list = &fc.l_varlist;
19721 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19722 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019723 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019724
19725 /*
19726 * Set a:firstline to "firstline" and a:lastline to "lastline".
19727 * Set a:name to named arguments.
19728 * Set a:N to the "..." arguments.
19729 */
19730 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19731 (varnumber_T)firstline);
19732 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19733 (varnumber_T)lastline);
19734 for (i = 0; i < argcount; ++i)
19735 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019736 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019737 if (ai < 0)
19738 /* named argument a:name */
19739 name = FUNCARG(fp, i);
19740 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019741 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019742 /* "..." argument a:1, a:2, etc. */
19743 sprintf((char *)numbuf, "%d", ai + 1);
19744 name = numbuf;
19745 }
19746 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19747 {
19748 v = &fc.fixvar[fixvar_idx++].var;
19749 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19750 }
19751 else
19752 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019753 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19754 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019755 if (v == NULL)
19756 break;
19757 v->di_flags = DI_FLAGS_RO;
19758 }
19759 STRCPY(v->di_key, name);
19760 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019762 /* Note: the values are copied directly to avoid alloc/free.
19763 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019765 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019766
19767 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19768 {
19769 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19770 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019771 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019772 }
19773 }
19774
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 /* Don't redraw while executing the function. */
19776 ++RedrawingDisabled;
19777 save_sourcing_name = sourcing_name;
19778 save_sourcing_lnum = sourcing_lnum;
19779 sourcing_lnum = 1;
19780 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019781 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 if (sourcing_name != NULL)
19783 {
19784 if (save_sourcing_name != NULL
19785 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19786 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19787 else
19788 STRCPY(sourcing_name, "function ");
19789 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19790
19791 if (p_verbose >= 12)
19792 {
19793 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019794 verbose_enter_scroll();
19795
Bram Moolenaar555b2802005-05-19 21:08:39 +000019796 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 if (p_verbose >= 14)
19798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019800 char_u numbuf[NUMBUFLEN];
19801 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802
19803 msg_puts((char_u *)"(");
19804 for (i = 0; i < argcount; ++i)
19805 {
19806 if (i > 0)
19807 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808 if (argvars[i].v_type == VAR_NUMBER)
19809 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 else
19811 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019812 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019813 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019815 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816 }
19817 }
19818 msg_puts((char_u *)")");
19819 }
19820 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019821
19822 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 --no_wait_return;
19824 }
19825 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019826#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019827 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019828 {
19829 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19830 func_do_profile(fp);
19831 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019832 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019833 {
19834 ++fp->uf_tm_count;
19835 profile_start(&fp->uf_tm_start);
19836 profile_zero(&fp->uf_tm_children);
19837 }
19838 script_prof_save(&wait_start);
19839 }
19840#endif
19841
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019843 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 save_did_emsg = did_emsg;
19845 did_emsg = FALSE;
19846
19847 /* call do_cmdline() to execute the lines */
19848 do_cmdline(NULL, get_func_line, (void *)&fc,
19849 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19850
19851 --RedrawingDisabled;
19852
19853 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019854 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019856 clear_tv(rettv);
19857 rettv->v_type = VAR_NUMBER;
19858 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 }
19860
Bram Moolenaar05159a02005-02-26 23:04:13 +000019861#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019862 if (do_profiling == PROF_YES && (fp->uf_profiling
19863 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019864 {
19865 profile_end(&fp->uf_tm_start);
19866 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19867 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019868 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019869 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019870 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019871 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19872 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019873 }
19874 }
19875#endif
19876
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 /* when being verbose, mention the return value */
19878 if (p_verbose >= 12)
19879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019881 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019884 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019885 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019886 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19887 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019888 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019890 char_u buf[MSG_BUF_LEN];
19891 char_u numbuf[NUMBUFLEN];
19892 char_u *tofree;
19893
Bram Moolenaar555b2802005-05-19 21:08:39 +000019894 /* The value may be very long. Skip the middle part, so that we
19895 * have some idea how it starts and ends. smsg() would always
19896 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019897 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019898 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019899 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019900 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 }
19902 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019903
19904 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 --no_wait_return;
19906 }
19907
19908 vim_free(sourcing_name);
19909 sourcing_name = save_sourcing_name;
19910 sourcing_lnum = save_sourcing_lnum;
19911 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019912#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019913 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019914 script_prof_restore(&wait_start);
19915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916
19917 if (p_verbose >= 12 && sourcing_name != NULL)
19918 {
19919 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019920 verbose_enter_scroll();
19921
Bram Moolenaar555b2802005-05-19 21:08:39 +000019922 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019924
19925 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 --no_wait_return;
19927 }
19928
19929 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019930 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931
Bram Moolenaar33570922005-01-25 22:26:29 +000019932 /* The a: variables typevals were not alloced, only free the allocated
19933 * variables. */
19934 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19935
19936 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 --depth;
19938}
19939
19940/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 * Add a number variable "name" to dict "dp" with value "nr".
19942 */
19943 static void
19944add_nr_var(dp, v, name, nr)
19945 dict_T *dp;
19946 dictitem_T *v;
19947 char *name;
19948 varnumber_T nr;
19949{
19950 STRCPY(v->di_key, name);
19951 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19952 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19953 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019954 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019955 v->di_tv.vval.v_number = nr;
19956}
19957
19958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 * ":return [expr]"
19960 */
19961 void
19962ex_return(eap)
19963 exarg_T *eap;
19964{
19965 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 int returning = FALSE;
19968
19969 if (current_funccal == NULL)
19970 {
19971 EMSG(_("E133: :return not inside a function"));
19972 return;
19973 }
19974
19975 if (eap->skip)
19976 ++emsg_skip;
19977
19978 eap->nextcmd = NULL;
19979 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019980 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 {
19982 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019983 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 }
19987 /* It's safer to return also on error. */
19988 else if (!eap->skip)
19989 {
19990 /*
19991 * Return unless the expression evaluation has been cancelled due to an
19992 * aborting error, an interrupt, or an exception.
19993 */
19994 if (!aborting())
19995 returning = do_return(eap, FALSE, TRUE, NULL);
19996 }
19997
19998 /* When skipping or the return gets pending, advance to the next command
19999 * in this line (!returning). Otherwise, ignore the rest of the line.
20000 * Following lines will be ignored by get_func_line(). */
20001 if (returning)
20002 eap->nextcmd = NULL;
20003 else if (eap->nextcmd == NULL) /* no argument */
20004 eap->nextcmd = check_nextcmd(arg);
20005
20006 if (eap->skip)
20007 --emsg_skip;
20008}
20009
20010/*
20011 * Return from a function. Possibly makes the return pending. Also called
20012 * for a pending return at the ":endtry" or after returning from an extra
20013 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020014 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 * FALSE when the return gets pending.
20017 */
20018 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020019do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 exarg_T *eap;
20021 int reanimate;
20022 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024{
20025 int idx;
20026 struct condstack *cstack = eap->cstack;
20027
20028 if (reanimate)
20029 /* Undo the return. */
20030 current_funccal->returned = FALSE;
20031
20032 /*
20033 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20034 * not in its finally clause (which then is to be executed next) is found.
20035 * In this case, make the ":return" pending for execution at the ":endtry".
20036 * Otherwise, return normally.
20037 */
20038 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20039 if (idx >= 0)
20040 {
20041 cstack->cs_pending[idx] = CSTP_RETURN;
20042
20043 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020044 /* A pending return again gets pending. "rettv" points to an
20045 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 else
20049 {
20050 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020053 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020055 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 {
20057 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020059 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 else
20061 EMSG(_(e_outofmem));
20062 }
20063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020064 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065
20066 if (reanimate)
20067 {
20068 /* The pending return value could be overwritten by a ":return"
20069 * without argument in a finally clause; reset the default
20070 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071 current_funccal->rettv->v_type = VAR_NUMBER;
20072 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 }
20074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 }
20077 else
20078 {
20079 current_funccal->returned = TRUE;
20080
20081 /* If the return is carried out now, store the return value. For
20082 * a return immediately after reanimation, the value is already
20083 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020084 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020086 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020087 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 }
20091 }
20092
20093 return idx < 0;
20094}
20095
20096/*
20097 * Free the variable with a pending return value.
20098 */
20099 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020100discard_pending_return(rettv)
20101 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102{
Bram Moolenaar33570922005-01-25 22:26:29 +000020103 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104}
20105
20106/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 * is an allocated string. Used by report_pending() for verbose messages.
20109 */
20110 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020111get_return_cmd(rettv)
20112 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020114 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020115 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020116 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020118 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020119 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020120 if (s == NULL)
20121 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020122
20123 STRCPY(IObuff, ":return ");
20124 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20125 if (STRLEN(s) + 8 >= IOSIZE)
20126 STRCPY(IObuff + IOSIZE - 4, "...");
20127 vim_free(tofree);
20128 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129}
20130
20131/*
20132 * Get next function line.
20133 * Called by do_cmdline() to get the next line.
20134 * Returns allocated string, or NULL for end of function.
20135 */
20136/* ARGSUSED */
20137 char_u *
20138get_func_line(c, cookie, indent)
20139 int c; /* not used */
20140 void *cookie;
20141 int indent; /* not used */
20142{
Bram Moolenaar33570922005-01-25 22:26:29 +000020143 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020144 ufunc_T *fp = fcp->func;
20145 char_u *retval;
20146 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147
20148 /* If breakpoints have been added/deleted need to check for it. */
20149 if (fcp->dbg_tick != debug_tick)
20150 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020151 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 sourcing_lnum);
20153 fcp->dbg_tick = debug_tick;
20154 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020155#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020156 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020157 func_line_end(cookie);
20158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159
Bram Moolenaar05159a02005-02-26 23:04:13 +000020160 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020161 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20162 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 retval = NULL;
20164 else
20165 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020166 /* Skip NULL lines (continuation lines). */
20167 while (fcp->linenr < gap->ga_len
20168 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20169 ++fcp->linenr;
20170 if (fcp->linenr >= gap->ga_len)
20171 retval = NULL;
20172 else
20173 {
20174 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20175 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020176#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020177 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020178 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020179#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 }
20182
20183 /* Did we encounter a breakpoint? */
20184 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20185 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020186 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020188 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 sourcing_lnum);
20190 fcp->dbg_tick = debug_tick;
20191 }
20192
20193 return retval;
20194}
20195
Bram Moolenaar05159a02005-02-26 23:04:13 +000020196#if defined(FEAT_PROFILE) || defined(PROTO)
20197/*
20198 * Called when starting to read a function line.
20199 * "sourcing_lnum" must be correct!
20200 * When skipping lines it may not actually be executed, but we won't find out
20201 * until later and we need to store the time now.
20202 */
20203 void
20204func_line_start(cookie)
20205 void *cookie;
20206{
20207 funccall_T *fcp = (funccall_T *)cookie;
20208 ufunc_T *fp = fcp->func;
20209
20210 if (fp->uf_profiling && sourcing_lnum >= 1
20211 && sourcing_lnum <= fp->uf_lines.ga_len)
20212 {
20213 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020214 /* Skip continuation lines. */
20215 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20216 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020217 fp->uf_tml_execed = FALSE;
20218 profile_start(&fp->uf_tml_start);
20219 profile_zero(&fp->uf_tml_children);
20220 profile_get_wait(&fp->uf_tml_wait);
20221 }
20222}
20223
20224/*
20225 * Called when actually executing a function line.
20226 */
20227 void
20228func_line_exec(cookie)
20229 void *cookie;
20230{
20231 funccall_T *fcp = (funccall_T *)cookie;
20232 ufunc_T *fp = fcp->func;
20233
20234 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20235 fp->uf_tml_execed = TRUE;
20236}
20237
20238/*
20239 * Called when done with a function line.
20240 */
20241 void
20242func_line_end(cookie)
20243 void *cookie;
20244{
20245 funccall_T *fcp = (funccall_T *)cookie;
20246 ufunc_T *fp = fcp->func;
20247
20248 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20249 {
20250 if (fp->uf_tml_execed)
20251 {
20252 ++fp->uf_tml_count[fp->uf_tml_idx];
20253 profile_end(&fp->uf_tml_start);
20254 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020255 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020256 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20257 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020258 }
20259 fp->uf_tml_idx = -1;
20260 }
20261}
20262#endif
20263
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264/*
20265 * Return TRUE if the currently active function should be ended, because a
20266 * return was encountered or an error occured. Used inside a ":while".
20267 */
20268 int
20269func_has_ended(cookie)
20270 void *cookie;
20271{
Bram Moolenaar33570922005-01-25 22:26:29 +000020272 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273
20274 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20275 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020276 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 || fcp->returned);
20278}
20279
20280/*
20281 * return TRUE if cookie indicates a function which "abort"s on errors.
20282 */
20283 int
20284func_has_abort(cookie)
20285 void *cookie;
20286{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020287 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288}
20289
20290#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20291typedef enum
20292{
20293 VAR_FLAVOUR_DEFAULT,
20294 VAR_FLAVOUR_SESSION,
20295 VAR_FLAVOUR_VIMINFO
20296} var_flavour_T;
20297
20298static var_flavour_T var_flavour __ARGS((char_u *varname));
20299
20300 static var_flavour_T
20301var_flavour(varname)
20302 char_u *varname;
20303{
20304 char_u *p = varname;
20305
20306 if (ASCII_ISUPPER(*p))
20307 {
20308 while (*(++p))
20309 if (ASCII_ISLOWER(*p))
20310 return VAR_FLAVOUR_SESSION;
20311 return VAR_FLAVOUR_VIMINFO;
20312 }
20313 else
20314 return VAR_FLAVOUR_DEFAULT;
20315}
20316#endif
20317
20318#if defined(FEAT_VIMINFO) || defined(PROTO)
20319/*
20320 * Restore global vars that start with a capital from the viminfo file
20321 */
20322 int
20323read_viminfo_varlist(virp, writing)
20324 vir_T *virp;
20325 int writing;
20326{
20327 char_u *tab;
20328 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020329 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330
20331 if (!writing && (find_viminfo_parameter('!') != NULL))
20332 {
20333 tab = vim_strchr(virp->vir_line + 1, '\t');
20334 if (tab != NULL)
20335 {
20336 *tab++ = '\0'; /* isolate the variable name */
20337 if (*tab == 'S') /* string var */
20338 is_string = TRUE;
20339
20340 tab = vim_strchr(tab, '\t');
20341 if (tab != NULL)
20342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 if (is_string)
20344 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020345 tv.v_type = VAR_STRING;
20346 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 }
20349 else
20350 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020351 tv.v_type = VAR_NUMBER;
20352 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020354 set_var(virp->vir_line + 1, &tv, FALSE);
20355 if (is_string)
20356 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 }
20358 }
20359 }
20360
20361 return viminfo_readline(virp);
20362}
20363
20364/*
20365 * Write global vars that start with a capital to the viminfo file
20366 */
20367 void
20368write_viminfo_varlist(fp)
20369 FILE *fp;
20370{
Bram Moolenaar33570922005-01-25 22:26:29 +000020371 hashitem_T *hi;
20372 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020373 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020374 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020375 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020376 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020377 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378
20379 if (find_viminfo_parameter('!') == NULL)
20380 return;
20381
20382 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020383
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020384 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020385 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020387 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020389 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 this_var = HI2DI(hi);
20391 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020392 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020393 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020394 {
20395 case VAR_STRING: s = "STR"; break;
20396 case VAR_NUMBER: s = "NUM"; break;
20397 default: continue;
20398 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020399 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020400 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020401 if (p != NULL)
20402 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020403 vim_free(tofree);
20404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 }
20406 }
20407}
20408#endif
20409
20410#if defined(FEAT_SESSION) || defined(PROTO)
20411 int
20412store_session_globals(fd)
20413 FILE *fd;
20414{
Bram Moolenaar33570922005-01-25 22:26:29 +000020415 hashitem_T *hi;
20416 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020417 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 char_u *p, *t;
20419
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020420 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020421 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020423 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020425 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020426 this_var = HI2DI(hi);
20427 if ((this_var->di_tv.v_type == VAR_NUMBER
20428 || this_var->di_tv.v_type == VAR_STRING)
20429 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020430 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020431 /* Escape special characters with a backslash. Turn a LF and
20432 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020433 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020434 (char_u *)"\\\"\n\r");
20435 if (p == NULL) /* out of memory */
20436 break;
20437 for (t = p; *t != NUL; ++t)
20438 if (*t == '\n')
20439 *t = 'n';
20440 else if (*t == '\r')
20441 *t = 'r';
20442 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020443 this_var->di_key,
20444 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20445 : ' ',
20446 p,
20447 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20448 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020449 || put_eol(fd) == FAIL)
20450 {
20451 vim_free(p);
20452 return FAIL;
20453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 vim_free(p);
20455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 }
20457 }
20458 return OK;
20459}
20460#endif
20461
Bram Moolenaar661b1822005-07-28 22:36:45 +000020462/*
20463 * Display script name where an item was last set.
20464 * Should only be invoked when 'verbose' is non-zero.
20465 */
20466 void
20467last_set_msg(scriptID)
20468 scid_T scriptID;
20469{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020470 char_u *p;
20471
Bram Moolenaar661b1822005-07-28 22:36:45 +000020472 if (scriptID != 0)
20473 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020474 p = home_replace_save(NULL, get_scriptname(scriptID));
20475 if (p != NULL)
20476 {
20477 verbose_enter();
20478 MSG_PUTS(_("\n\tLast set from "));
20479 MSG_PUTS(p);
20480 vim_free(p);
20481 verbose_leave();
20482 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020483 }
20484}
20485
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486#endif /* FEAT_EVAL */
20487
20488#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20489
20490
20491#ifdef WIN3264
20492/*
20493 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20494 */
20495static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20496static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20497static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20498
20499/*
20500 * Get the short pathname of a file.
20501 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20502 */
20503 static int
20504get_short_pathname(fnamep, bufp, fnamelen)
20505 char_u **fnamep;
20506 char_u **bufp;
20507 int *fnamelen;
20508{
20509 int l,len;
20510 char_u *newbuf;
20511
20512 len = *fnamelen;
20513
20514 l = GetShortPathName(*fnamep, *fnamep, len);
20515 if (l > len - 1)
20516 {
20517 /* If that doesn't work (not enough space), then save the string
20518 * and try again with a new buffer big enough
20519 */
20520 newbuf = vim_strnsave(*fnamep, l);
20521 if (newbuf == NULL)
20522 return 0;
20523
20524 vim_free(*bufp);
20525 *fnamep = *bufp = newbuf;
20526
20527 l = GetShortPathName(*fnamep,*fnamep,l+1);
20528
20529 /* Really should always succeed, as the buffer is big enough */
20530 }
20531
20532 *fnamelen = l;
20533 return 1;
20534}
20535
20536/*
20537 * Create a short path name. Returns the length of the buffer it needs.
20538 * Doesn't copy over the end of the buffer passed in.
20539 */
20540 static int
20541shortpath_for_invalid_fname(fname, bufp, fnamelen)
20542 char_u **fname;
20543 char_u **bufp;
20544 int *fnamelen;
20545{
20546 char_u *s, *p, *pbuf2, *pbuf3;
20547 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020548 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549
20550 /* Make a copy */
20551 len2 = *fnamelen;
20552 pbuf2 = vim_strnsave(*fname, len2);
20553 pbuf3 = NULL;
20554
20555 s = pbuf2 + len2 - 1; /* Find the end */
20556 slen = 1;
20557 plen = len2;
20558
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020559 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 {
20561 --s;
20562 ++slen;
20563 --plen;
20564 }
20565
20566 do
20567 {
20568 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020569 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 {
20571 --s;
20572 ++slen;
20573 --plen;
20574 }
20575 if (s <= pbuf2)
20576 break;
20577
20578 /* Remeber the character that is about to be blatted */
20579 ch = *s;
20580 *s = 0; /* get_short_pathname requires a null-terminated string */
20581
20582 /* Try it in situ */
20583 p = pbuf2;
20584 if (!get_short_pathname(&p, &pbuf3, &plen))
20585 {
20586 vim_free(pbuf2);
20587 return -1;
20588 }
20589 *s = ch; /* Preserve the string */
20590 } while (plen == 0);
20591
20592 if (plen > 0)
20593 {
20594 /* Remeber the length of the new string. */
20595 *fnamelen = len = plen + slen;
20596 vim_free(*bufp);
20597 if (len > len2)
20598 {
20599 /* If there's not enough space in the currently allocated string,
20600 * then copy it to a buffer big enough.
20601 */
20602 *fname= *bufp = vim_strnsave(p, len);
20603 if (*fname == NULL)
20604 return -1;
20605 }
20606 else
20607 {
20608 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20609 *fname = *bufp = pbuf2;
20610 if (p != pbuf2)
20611 strncpy(*fname, p, plen);
20612 pbuf2 = NULL;
20613 }
20614 /* Concat the next bit */
20615 strncpy(*fname + plen, s, slen);
20616 (*fname)[len] = '\0';
20617 }
20618 vim_free(pbuf3);
20619 vim_free(pbuf2);
20620 return 0;
20621}
20622
20623/*
20624 * Get a pathname for a partial path.
20625 */
20626 static int
20627shortpath_for_partial(fnamep, bufp, fnamelen)
20628 char_u **fnamep;
20629 char_u **bufp;
20630 int *fnamelen;
20631{
20632 int sepcount, len, tflen;
20633 char_u *p;
20634 char_u *pbuf, *tfname;
20635 int hasTilde;
20636
20637 /* Count up the path seperators from the RHS.. so we know which part
20638 * of the path to return.
20639 */
20640 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020641 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 if (vim_ispathsep(*p))
20643 ++sepcount;
20644
20645 /* Need full path first (use expand_env() to remove a "~/") */
20646 hasTilde = (**fnamep == '~');
20647 if (hasTilde)
20648 pbuf = tfname = expand_env_save(*fnamep);
20649 else
20650 pbuf = tfname = FullName_save(*fnamep, FALSE);
20651
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020652 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653
20654 if (!get_short_pathname(&tfname, &pbuf, &len))
20655 return -1;
20656
20657 if (len == 0)
20658 {
20659 /* Don't have a valid filename, so shorten the rest of the
20660 * path if we can. This CAN give us invalid 8.3 filenames, but
20661 * there's not a lot of point in guessing what it might be.
20662 */
20663 len = tflen;
20664 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20665 return -1;
20666 }
20667
20668 /* Count the paths backward to find the beginning of the desired string. */
20669 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020670 {
20671#ifdef FEAT_MBYTE
20672 if (has_mbyte)
20673 p -= mb_head_off(tfname, p);
20674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 if (vim_ispathsep(*p))
20676 {
20677 if (sepcount == 0 || (hasTilde && sepcount == 1))
20678 break;
20679 else
20680 sepcount --;
20681 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 if (hasTilde)
20684 {
20685 --p;
20686 if (p >= tfname)
20687 *p = '~';
20688 else
20689 return -1;
20690 }
20691 else
20692 ++p;
20693
20694 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20695 vim_free(*bufp);
20696 *fnamelen = (int)STRLEN(p);
20697 *bufp = pbuf;
20698 *fnamep = p;
20699
20700 return 0;
20701}
20702#endif /* WIN3264 */
20703
20704/*
20705 * Adjust a filename, according to a string of modifiers.
20706 * *fnamep must be NUL terminated when called. When returning, the length is
20707 * determined by *fnamelen.
20708 * Returns valid flags.
20709 * When there is an error, *fnamep is set to NULL.
20710 */
20711 int
20712modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20713 char_u *src; /* string with modifiers */
20714 int *usedlen; /* characters after src that are used */
20715 char_u **fnamep; /* file name so far */
20716 char_u **bufp; /* buffer for allocated file name or NULL */
20717 int *fnamelen; /* length of fnamep */
20718{
20719 int valid = 0;
20720 char_u *tail;
20721 char_u *s, *p, *pbuf;
20722 char_u dirname[MAXPATHL];
20723 int c;
20724 int has_fullname = 0;
20725#ifdef WIN3264
20726 int has_shortname = 0;
20727#endif
20728
20729repeat:
20730 /* ":p" - full path/file_name */
20731 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20732 {
20733 has_fullname = 1;
20734
20735 valid |= VALID_PATH;
20736 *usedlen += 2;
20737
20738 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20739 if ((*fnamep)[0] == '~'
20740#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20741 && ((*fnamep)[1] == '/'
20742# ifdef BACKSLASH_IN_FILENAME
20743 || (*fnamep)[1] == '\\'
20744# endif
20745 || (*fnamep)[1] == NUL)
20746
20747#endif
20748 )
20749 {
20750 *fnamep = expand_env_save(*fnamep);
20751 vim_free(*bufp); /* free any allocated file name */
20752 *bufp = *fnamep;
20753 if (*fnamep == NULL)
20754 return -1;
20755 }
20756
20757 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020758 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 {
20760 if (vim_ispathsep(*p)
20761 && p[1] == '.'
20762 && (p[2] == NUL
20763 || vim_ispathsep(p[2])
20764 || (p[2] == '.'
20765 && (p[3] == NUL || vim_ispathsep(p[3])))))
20766 break;
20767 }
20768
20769 /* FullName_save() is slow, don't use it when not needed. */
20770 if (*p != NUL || !vim_isAbsName(*fnamep))
20771 {
20772 *fnamep = FullName_save(*fnamep, *p != NUL);
20773 vim_free(*bufp); /* free any allocated file name */
20774 *bufp = *fnamep;
20775 if (*fnamep == NULL)
20776 return -1;
20777 }
20778
20779 /* Append a path separator to a directory. */
20780 if (mch_isdir(*fnamep))
20781 {
20782 /* Make room for one or two extra characters. */
20783 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20784 vim_free(*bufp); /* free any allocated file name */
20785 *bufp = *fnamep;
20786 if (*fnamep == NULL)
20787 return -1;
20788 add_pathsep(*fnamep);
20789 }
20790 }
20791
20792 /* ":." - path relative to the current directory */
20793 /* ":~" - path relative to the home directory */
20794 /* ":8" - shortname path - postponed till after */
20795 while (src[*usedlen] == ':'
20796 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20797 {
20798 *usedlen += 2;
20799 if (c == '8')
20800 {
20801#ifdef WIN3264
20802 has_shortname = 1; /* Postpone this. */
20803#endif
20804 continue;
20805 }
20806 pbuf = NULL;
20807 /* Need full path first (use expand_env() to remove a "~/") */
20808 if (!has_fullname)
20809 {
20810 if (c == '.' && **fnamep == '~')
20811 p = pbuf = expand_env_save(*fnamep);
20812 else
20813 p = pbuf = FullName_save(*fnamep, FALSE);
20814 }
20815 else
20816 p = *fnamep;
20817
20818 has_fullname = 0;
20819
20820 if (p != NULL)
20821 {
20822 if (c == '.')
20823 {
20824 mch_dirname(dirname, MAXPATHL);
20825 s = shorten_fname(p, dirname);
20826 if (s != NULL)
20827 {
20828 *fnamep = s;
20829 if (pbuf != NULL)
20830 {
20831 vim_free(*bufp); /* free any allocated file name */
20832 *bufp = pbuf;
20833 pbuf = NULL;
20834 }
20835 }
20836 }
20837 else
20838 {
20839 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20840 /* Only replace it when it starts with '~' */
20841 if (*dirname == '~')
20842 {
20843 s = vim_strsave(dirname);
20844 if (s != NULL)
20845 {
20846 *fnamep = s;
20847 vim_free(*bufp);
20848 *bufp = s;
20849 }
20850 }
20851 }
20852 vim_free(pbuf);
20853 }
20854 }
20855
20856 tail = gettail(*fnamep);
20857 *fnamelen = (int)STRLEN(*fnamep);
20858
20859 /* ":h" - head, remove "/file_name", can be repeated */
20860 /* Don't remove the first "/" or "c:\" */
20861 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20862 {
20863 valid |= VALID_HEAD;
20864 *usedlen += 2;
20865 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020866 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 --tail;
20868 *fnamelen = (int)(tail - *fnamep);
20869#ifdef VMS
20870 if (*fnamelen > 0)
20871 *fnamelen += 1; /* the path separator is part of the path */
20872#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020873 while (tail > s && !after_pathsep(s, tail))
20874 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 }
20876
20877 /* ":8" - shortname */
20878 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20879 {
20880 *usedlen += 2;
20881#ifdef WIN3264
20882 has_shortname = 1;
20883#endif
20884 }
20885
20886#ifdef WIN3264
20887 /* Check shortname after we have done 'heads' and before we do 'tails'
20888 */
20889 if (has_shortname)
20890 {
20891 pbuf = NULL;
20892 /* Copy the string if it is shortened by :h */
20893 if (*fnamelen < (int)STRLEN(*fnamep))
20894 {
20895 p = vim_strnsave(*fnamep, *fnamelen);
20896 if (p == 0)
20897 return -1;
20898 vim_free(*bufp);
20899 *bufp = *fnamep = p;
20900 }
20901
20902 /* Split into two implementations - makes it easier. First is where
20903 * there isn't a full name already, second is where there is.
20904 */
20905 if (!has_fullname && !vim_isAbsName(*fnamep))
20906 {
20907 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20908 return -1;
20909 }
20910 else
20911 {
20912 int l;
20913
20914 /* Simple case, already have the full-name
20915 * Nearly always shorter, so try first time. */
20916 l = *fnamelen;
20917 if (!get_short_pathname(fnamep, bufp, &l))
20918 return -1;
20919
20920 if (l == 0)
20921 {
20922 /* Couldn't find the filename.. search the paths.
20923 */
20924 l = *fnamelen;
20925 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20926 return -1;
20927 }
20928 *fnamelen = l;
20929 }
20930 }
20931#endif /* WIN3264 */
20932
20933 /* ":t" - tail, just the basename */
20934 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20935 {
20936 *usedlen += 2;
20937 *fnamelen -= (int)(tail - *fnamep);
20938 *fnamep = tail;
20939 }
20940
20941 /* ":e" - extension, can be repeated */
20942 /* ":r" - root, without extension, can be repeated */
20943 while (src[*usedlen] == ':'
20944 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20945 {
20946 /* find a '.' in the tail:
20947 * - for second :e: before the current fname
20948 * - otherwise: The last '.'
20949 */
20950 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20951 s = *fnamep - 2;
20952 else
20953 s = *fnamep + *fnamelen - 1;
20954 for ( ; s > tail; --s)
20955 if (s[0] == '.')
20956 break;
20957 if (src[*usedlen + 1] == 'e') /* :e */
20958 {
20959 if (s > tail)
20960 {
20961 *fnamelen += (int)(*fnamep - (s + 1));
20962 *fnamep = s + 1;
20963#ifdef VMS
20964 /* cut version from the extension */
20965 s = *fnamep + *fnamelen - 1;
20966 for ( ; s > *fnamep; --s)
20967 if (s[0] == ';')
20968 break;
20969 if (s > *fnamep)
20970 *fnamelen = s - *fnamep;
20971#endif
20972 }
20973 else if (*fnamep <= tail)
20974 *fnamelen = 0;
20975 }
20976 else /* :r */
20977 {
20978 if (s > tail) /* remove one extension */
20979 *fnamelen = (int)(s - *fnamep);
20980 }
20981 *usedlen += 2;
20982 }
20983
20984 /* ":s?pat?foo?" - substitute */
20985 /* ":gs?pat?foo?" - global substitute */
20986 if (src[*usedlen] == ':'
20987 && (src[*usedlen + 1] == 's'
20988 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20989 {
20990 char_u *str;
20991 char_u *pat;
20992 char_u *sub;
20993 int sep;
20994 char_u *flags;
20995 int didit = FALSE;
20996
20997 flags = (char_u *)"";
20998 s = src + *usedlen + 2;
20999 if (src[*usedlen + 1] == 'g')
21000 {
21001 flags = (char_u *)"g";
21002 ++s;
21003 }
21004
21005 sep = *s++;
21006 if (sep)
21007 {
21008 /* find end of pattern */
21009 p = vim_strchr(s, sep);
21010 if (p != NULL)
21011 {
21012 pat = vim_strnsave(s, (int)(p - s));
21013 if (pat != NULL)
21014 {
21015 s = p + 1;
21016 /* find end of substitution */
21017 p = vim_strchr(s, sep);
21018 if (p != NULL)
21019 {
21020 sub = vim_strnsave(s, (int)(p - s));
21021 str = vim_strnsave(*fnamep, *fnamelen);
21022 if (sub != NULL && str != NULL)
21023 {
21024 *usedlen = (int)(p + 1 - src);
21025 s = do_string_sub(str, pat, sub, flags);
21026 if (s != NULL)
21027 {
21028 *fnamep = s;
21029 *fnamelen = (int)STRLEN(s);
21030 vim_free(*bufp);
21031 *bufp = s;
21032 didit = TRUE;
21033 }
21034 }
21035 vim_free(sub);
21036 vim_free(str);
21037 }
21038 vim_free(pat);
21039 }
21040 }
21041 /* after using ":s", repeat all the modifiers */
21042 if (didit)
21043 goto repeat;
21044 }
21045 }
21046
21047 return valid;
21048}
21049
21050/*
21051 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21052 * "flags" can be "g" to do a global substitute.
21053 * Returns an allocated string, NULL for error.
21054 */
21055 char_u *
21056do_string_sub(str, pat, sub, flags)
21057 char_u *str;
21058 char_u *pat;
21059 char_u *sub;
21060 char_u *flags;
21061{
21062 int sublen;
21063 regmatch_T regmatch;
21064 int i;
21065 int do_all;
21066 char_u *tail;
21067 garray_T ga;
21068 char_u *ret;
21069 char_u *save_cpo;
21070
21071 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21072 save_cpo = p_cpo;
21073 p_cpo = (char_u *)"";
21074
21075 ga_init2(&ga, 1, 200);
21076
21077 do_all = (flags[0] == 'g');
21078
21079 regmatch.rm_ic = p_ic;
21080 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21081 if (regmatch.regprog != NULL)
21082 {
21083 tail = str;
21084 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21085 {
21086 /*
21087 * Get some space for a temporary buffer to do the substitution
21088 * into. It will contain:
21089 * - The text up to where the match is.
21090 * - The substituted text.
21091 * - The text after the match.
21092 */
21093 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21094 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21095 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21096 {
21097 ga_clear(&ga);
21098 break;
21099 }
21100
21101 /* copy the text up to where the match is */
21102 i = (int)(regmatch.startp[0] - tail);
21103 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21104 /* add the substituted text */
21105 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21106 + ga.ga_len + i, TRUE, TRUE, FALSE);
21107 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 /* avoid getting stuck on a match with an empty string */
21109 if (tail == regmatch.endp[0])
21110 {
21111 if (*tail == NUL)
21112 break;
21113 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21114 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 }
21116 else
21117 {
21118 tail = regmatch.endp[0];
21119 if (*tail == NUL)
21120 break;
21121 }
21122 if (!do_all)
21123 break;
21124 }
21125
21126 if (ga.ga_data != NULL)
21127 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21128
21129 vim_free(regmatch.regprog);
21130 }
21131
21132 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21133 ga_clear(&ga);
21134 p_cpo = save_cpo;
21135
21136 return ret;
21137}
21138
21139#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */