blob: f2abba7b26dee62de60678621bd43f57f0b5f157 [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 Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000480static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
489static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000502static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000516static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000518static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000524static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000532static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000533static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000536static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000557static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000563static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000579static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585#ifdef vim_mkdir
586static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
587#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000591static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000593static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000594static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000596static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000597static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000610static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000612static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000619static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000620static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000621static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000623static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
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 Moolenaar4e957af2006-09-02 11:41:07 +0000704static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000705static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000707static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
709static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
710static int eval_fname_script __ARGS((char_u *p));
711static int eval_fname_sid __ARGS((char_u *p));
712static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static ufunc_T *find_func __ARGS((char_u *name));
714static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000715static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000716#ifdef FEAT_PROFILE
717static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000718static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
719static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
720static int
721# ifdef __BORLANDC__
722 _RTLENTRYF
723# endif
724 prof_total_cmp __ARGS((const void *s1, const void *s2));
725static int
726# ifdef __BORLANDC__
727 _RTLENTRYF
728# endif
729 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000730#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000731static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000732static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000733static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void func_free __ARGS((ufunc_T *fp));
735static void func_unref __ARGS((char_u *name));
736static void func_ref __ARGS((char_u *name));
737static 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));
738static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000739static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
740static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000741static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000742static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000743static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000745/* Character used as separated in autoload function/variable names. */
746#define AUTOLOAD_CHAR '#'
747
Bram Moolenaar33570922005-01-25 22:26:29 +0000748/*
749 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000750 */
751 void
752eval_init()
753{
Bram Moolenaar33570922005-01-25 22:26:29 +0000754 int i;
755 struct vimvar *p;
756
757 init_var_dict(&globvardict, &globvars_var);
758 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000759 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000760 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761
762 for (i = 0; i < VV_LEN; ++i)
763 {
764 p = &vimvars[i];
765 STRCPY(p->vv_di.di_key, p->vv_name);
766 if (p->vv_flags & VV_RO)
767 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
768 else if (p->vv_flags & VV_RO_SBX)
769 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
770 else
771 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772
773 /* add to v: scope dict, unless the value is not always available */
774 if (p->vv_type != VAR_UNKNOWN)
775 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000777 /* add to compat scope dict */
778 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000779 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000780}
781
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000782#if defined(EXITFREE) || defined(PROTO)
783 void
784eval_clear()
785{
786 int i;
787 struct vimvar *p;
788
789 for (i = 0; i < VV_LEN; ++i)
790 {
791 p = &vimvars[i];
792 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000793 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000794 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000795 p->vv_di.di_tv.vval.v_string = NULL;
796 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000797 }
798 hash_clear(&vimvarht);
799 hash_clear(&compat_hashtab);
800
801 /* script-local variables */
802 for (i = 1; i <= ga_scripts.ga_len; ++i)
803 vars_clear(&SCRIPT_VARS(i));
804 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000805 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000806
807 /* global variables */
808 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000809
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000810 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000811 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000812 hash_clear(&func_hashtab);
813
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000814 /* autoloaded script names */
815 ga_clear_strings(&ga_loaded);
816
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000817 /* unreferenced lists and dicts */
818 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000819}
820#endif
821
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 * Return the name of the executed function.
824 */
825 char_u *
826func_name(cookie)
827 void *cookie;
828{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000829 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830}
831
832/*
833 * Return the address holding the next breakpoint line for a funccall cookie.
834 */
835 linenr_T *
836func_breakpoint(cookie)
837 void *cookie;
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841
842/*
843 * Return the address holding the debug tick for a funccall cookie.
844 */
845 int *
846func_dbg_tick(cookie)
847 void *cookie;
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
852/*
853 * Return the nesting level for a funccall cookie.
854 */
855 int
856func_level(cookie)
857 void *cookie;
858{
Bram Moolenaar33570922005-01-25 22:26:29 +0000859 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861
862/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000863funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865/*
866 * Return TRUE when a function was ended by a ":return" command.
867 */
868 int
869current_func_returned()
870{
871 return current_funccal->returned;
872}
873
874
875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * Set an internal variable to a string value. Creates the variable if it does
877 * not already exist.
878 */
879 void
880set_internal_string_var(name, value)
881 char_u *name;
882 char_u *value;
883{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000884 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 val = vim_strsave(value);
888 if (val != NULL)
889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000891 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000893 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
896 }
897}
898
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000899static lval_T *redir_lval = NULL;
900static char_u *redir_endp = NULL;
901static char_u *redir_varname = NULL;
902
903/*
904 * Start recording command output to a variable
905 * Returns OK if successfully completed the setup. FAIL otherwise.
906 */
907 int
908var_redir_start(name, append)
909 char_u *name;
910 int append; /* append to an existing variable */
911{
912 int save_emsg;
913 int err;
914 typval_T tv;
915
916 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000917 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000918 {
919 EMSG(_(e_invarg));
920 return FAIL;
921 }
922
923 redir_varname = vim_strsave(name);
924 if (redir_varname == NULL)
925 return FAIL;
926
927 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
928 if (redir_lval == NULL)
929 {
930 var_redir_stop();
931 return FAIL;
932 }
933
934 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000935 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
936 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000937 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
938 {
939 if (redir_endp != NULL && *redir_endp != NUL)
940 /* Trailing characters are present after the variable name */
941 EMSG(_(e_trailing));
942 else
943 EMSG(_(e_invarg));
944 var_redir_stop();
945 return FAIL;
946 }
947
948 /* check if we can write to the variable: set it to or append an empty
949 * string */
950 save_emsg = did_emsg;
951 did_emsg = FALSE;
952 tv.v_type = VAR_STRING;
953 tv.vval.v_string = (char_u *)"";
954 if (append)
955 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
956 else
957 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
958 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000959 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000960 if (err)
961 {
962 var_redir_stop();
963 return FAIL;
964 }
965 if (redir_lval->ll_newkey != NULL)
966 {
967 /* Dictionary item was created, don't do it again. */
968 vim_free(redir_lval->ll_newkey);
969 redir_lval->ll_newkey = NULL;
970 }
971
972 return OK;
973}
974
975/*
976 * Append "value[len]" to the variable set by var_redir_start().
977 */
978 void
979var_redir_str(value, len)
980 char_u *value;
981 int len;
982{
983 char_u *val;
984 typval_T tv;
985 int save_emsg;
986 int err;
987
988 if (redir_lval == NULL)
989 return;
990
991 if (len == -1)
992 /* Append the entire string */
993 val = vim_strsave(value);
994 else
995 /* Append only the specified number of characters */
996 val = vim_strnsave(value, len);
997 if (val == NULL)
998 return;
999
1000 tv.v_type = VAR_STRING;
1001 tv.vval.v_string = val;
1002
1003 save_emsg = did_emsg;
1004 did_emsg = FALSE;
1005 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1006 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001007 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008 if (err)
1009 var_redir_stop();
1010
1011 vim_free(tv.vval.v_string);
1012}
1013
1014/*
1015 * Stop redirecting command output to a variable.
1016 */
1017 void
1018var_redir_stop()
1019{
1020 if (redir_lval != NULL)
1021 {
1022 clear_lval(redir_lval);
1023 vim_free(redir_lval);
1024 redir_lval = NULL;
1025 }
1026 vim_free(redir_varname);
1027 redir_varname = NULL;
1028}
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030# if defined(FEAT_MBYTE) || defined(PROTO)
1031 int
1032eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1033 char_u *enc_from;
1034 char_u *enc_to;
1035 char_u *fname_from;
1036 char_u *fname_to;
1037{
1038 int err = FALSE;
1039
1040 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1041 set_vim_var_string(VV_CC_TO, enc_to, -1);
1042 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1043 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1044 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1045 err = TRUE;
1046 set_vim_var_string(VV_CC_FROM, NULL, -1);
1047 set_vim_var_string(VV_CC_TO, NULL, -1);
1048 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1049 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1050
1051 if (err)
1052 return FAIL;
1053 return OK;
1054}
1055# endif
1056
1057# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1058 int
1059eval_printexpr(fname, args)
1060 char_u *fname;
1061 char_u *args;
1062{
1063 int err = FALSE;
1064
1065 set_vim_var_string(VV_FNAME_IN, fname, -1);
1066 set_vim_var_string(VV_CMDARG, args, -1);
1067 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1068 err = TRUE;
1069 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1070 set_vim_var_string(VV_CMDARG, NULL, -1);
1071
1072 if (err)
1073 {
1074 mch_remove(fname);
1075 return FAIL;
1076 }
1077 return OK;
1078}
1079# endif
1080
1081# if defined(FEAT_DIFF) || defined(PROTO)
1082 void
1083eval_diff(origfile, newfile, outfile)
1084 char_u *origfile;
1085 char_u *newfile;
1086 char_u *outfile;
1087{
1088 int err = FALSE;
1089
1090 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1091 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1092 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1093 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1094 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1095 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1096 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1097}
1098
1099 void
1100eval_patch(origfile, difffile, outfile)
1101 char_u *origfile;
1102 char_u *difffile;
1103 char_u *outfile;
1104{
1105 int err;
1106
1107 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1108 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1109 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1110 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1111 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1112 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1113 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1114}
1115# endif
1116
1117/*
1118 * Top level evaluation function, returning a boolean.
1119 * Sets "error" to TRUE if there was an error.
1120 * Return TRUE or FALSE.
1121 */
1122 int
1123eval_to_bool(arg, error, nextcmd, skip)
1124 char_u *arg;
1125 int *error;
1126 char_u **nextcmd;
1127 int skip; /* only parse, don't execute */
1128{
Bram Moolenaar33570922005-01-25 22:26:29 +00001129 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int retval = FALSE;
1131
1132 if (skip)
1133 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001134 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 else
1137 {
1138 *error = FALSE;
1139 if (!skip)
1140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001141 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
1144 }
1145 if (skip)
1146 --emsg_skip;
1147
1148 return retval;
1149}
1150
1151/*
1152 * Top level evaluation function, returning a string. If "skip" is TRUE,
1153 * only parsing to "nextcmd" is done, without reporting errors. Return
1154 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1155 */
1156 char_u *
1157eval_to_string_skip(arg, nextcmd, skip)
1158 char_u *arg;
1159 char_u **nextcmd;
1160 int skip; /* only parse, don't execute */
1161{
Bram Moolenaar33570922005-01-25 22:26:29 +00001162 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 char_u *retval;
1164
1165 if (skip)
1166 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 retval = NULL;
1169 else
1170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 retval = vim_strsave(get_tv_string(&tv));
1172 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 if (skip)
1175 --emsg_skip;
1176
1177 return retval;
1178}
1179
1180/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001181 * Skip over an expression at "*pp".
1182 * Return FAIL for an error, OK otherwise.
1183 */
1184 int
1185skip_expr(pp)
1186 char_u **pp;
1187{
Bram Moolenaar33570922005-01-25 22:26:29 +00001188 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001189
1190 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001191 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001192}
1193
1194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 * Top level evaluation function, returning a string.
1196 * Return pointer to allocated memory, or NULL for failure.
1197 */
1198 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001199eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 char_u *arg;
1201 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001202 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203{
Bram Moolenaar33570922005-01-25 22:26:29 +00001204 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001206 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 retval = NULL;
1210 else
1211 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001212 if (dolist && tv.v_type == VAR_LIST)
1213 {
1214 ga_init2(&ga, (int)sizeof(char), 80);
1215 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1216 ga_append(&ga, NUL);
1217 retval = (char_u *)ga.ga_data;
1218 }
1219 else
1220 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001221 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223
1224 return retval;
1225}
1226
1227/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001228 * Call eval_to_string() without using current local variables and using
1229 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
1231 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001232eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 char_u *arg;
1234 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001235 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
1237 char_u *retval;
1238 void *save_funccalp;
1239
1240 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001241 if (use_sandbox)
1242 ++sandbox;
1243 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001244 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001245 if (use_sandbox)
1246 --sandbox;
1247 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 restore_funccal(save_funccalp);
1249 return retval;
1250}
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252/*
1253 * Top level evaluation function, returning a number.
1254 * Evaluates "expr" silently.
1255 * Returns -1 for an error.
1256 */
1257 int
1258eval_to_number(expr)
1259 char_u *expr;
1260{
Bram Moolenaar33570922005-01-25 22:26:29 +00001261 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001263 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 ++emsg_off;
1266
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 retval = -1;
1269 else
1270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001271 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 --emsg_off;
1275
1276 return retval;
1277}
1278
Bram Moolenaara40058a2005-07-11 22:42:07 +00001279/*
1280 * Prepare v: variable "idx" to be used.
1281 * Save the current typeval in "save_tv".
1282 * When not used yet add the variable to the v: hashtable.
1283 */
1284 static void
1285prepare_vimvar(idx, save_tv)
1286 int idx;
1287 typval_T *save_tv;
1288{
1289 *save_tv = vimvars[idx].vv_tv;
1290 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1291 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1292}
1293
1294/*
1295 * Restore v: variable "idx" to typeval "save_tv".
1296 * When no longer defined, remove the variable from the v: hashtable.
1297 */
1298 static void
1299restore_vimvar(idx, save_tv)
1300 int idx;
1301 typval_T *save_tv;
1302{
1303 hashitem_T *hi;
1304
1305 clear_tv(&vimvars[idx].vv_tv);
1306 vimvars[idx].vv_tv = *save_tv;
1307 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1308 {
1309 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1310 if (HASHITEM_EMPTY(hi))
1311 EMSG2(_(e_intern2), "restore_vimvar()");
1312 else
1313 hash_remove(&vimvarht, hi);
1314 }
1315}
1316
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001317#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001318/*
1319 * Evaluate an expression to a list with suggestions.
1320 * For the "expr:" part of 'spellsuggest'.
1321 */
1322 list_T *
1323eval_spell_expr(badword, expr)
1324 char_u *badword;
1325 char_u *expr;
1326{
1327 typval_T save_val;
1328 typval_T rettv;
1329 list_T *list = NULL;
1330 char_u *p = skipwhite(expr);
1331
1332 /* Set "v:val" to the bad word. */
1333 prepare_vimvar(VV_VAL, &save_val);
1334 vimvars[VV_VAL].vv_type = VAR_STRING;
1335 vimvars[VV_VAL].vv_str = badword;
1336 if (p_verbose == 0)
1337 ++emsg_off;
1338
1339 if (eval1(&p, &rettv, TRUE) == OK)
1340 {
1341 if (rettv.v_type != VAR_LIST)
1342 clear_tv(&rettv);
1343 else
1344 list = rettv.vval.v_list;
1345 }
1346
1347 if (p_verbose == 0)
1348 --emsg_off;
1349 vimvars[VV_VAL].vv_str = NULL;
1350 restore_vimvar(VV_VAL, &save_val);
1351
1352 return list;
1353}
1354
1355/*
1356 * "list" is supposed to contain two items: a word and a number. Return the
1357 * word in "pp" and the number as the return value.
1358 * Return -1 if anything isn't right.
1359 * Used to get the good word and score from the eval_spell_expr() result.
1360 */
1361 int
1362get_spellword(list, pp)
1363 list_T *list;
1364 char_u **pp;
1365{
1366 listitem_T *li;
1367
1368 li = list->lv_first;
1369 if (li == NULL)
1370 return -1;
1371 *pp = get_tv_string(&li->li_tv);
1372
1373 li = li->li_next;
1374 if (li == NULL)
1375 return -1;
1376 return get_tv_number(&li->li_tv);
1377}
1378#endif
1379
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001380/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001381 * Top level evaluation function.
1382 * Returns an allocated typval_T with the result.
1383 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001384 */
1385 typval_T *
1386eval_expr(arg, nextcmd)
1387 char_u *arg;
1388 char_u **nextcmd;
1389{
1390 typval_T *tv;
1391
1392 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001393 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001394 {
1395 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001396 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001397 }
1398
1399 return tv;
1400}
1401
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1404/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001407 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 static int
1410call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 char_u *func;
1412 int argc;
1413 char_u **argv;
1414 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 long n;
1419 int len;
1420 int i;
1421 int doesrange;
1422 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001425 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
1429 for (i = 0; i < argc; i++)
1430 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001431 /* Pass a NULL or empty argument as an empty string */
1432 if (argv[i] == NULL || *argv[i] == NUL)
1433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001434 argvars[i].v_type = VAR_STRING;
1435 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001436 continue;
1437 }
1438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 /* Recognize a number argument, the others must be strings. */
1440 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1441 if (len != 0 && len == (int)STRLEN(argv[i]))
1442 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001443 argvars[i].v_type = VAR_NUMBER;
1444 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 else
1447 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001448 argvars[i].v_type = VAR_STRING;
1449 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451 }
1452
1453 if (safe)
1454 {
1455 save_funccalp = save_funccal();
1456 ++sandbox;
1457 }
1458
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001459 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1460 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001462 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (safe)
1464 {
1465 --sandbox;
1466 restore_funccal(save_funccalp);
1467 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001468 vim_free(argvars);
1469
1470 if (ret == FAIL)
1471 clear_tv(rettv);
1472
1473 return ret;
1474}
1475
1476/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001477 * Call vimL function "func" and return the result as a string.
1478 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 * Uses argv[argc] for the function arguments.
1480 */
1481 void *
1482call_func_retstr(func, argc, argv, safe)
1483 char_u *func;
1484 int argc;
1485 char_u **argv;
1486 int safe; /* use the sandbox */
1487{
1488 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001489 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001490
1491 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1492 return NULL;
1493
1494 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001495 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return retval;
1497}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001498
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001499#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001500/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001501 * Call vimL function "func" and return the result as a number.
1502 * Returns -1 when calling the function fails.
1503 * Uses argv[argc] for the function arguments.
1504 */
1505 long
1506call_func_retnr(func, argc, argv, safe)
1507 char_u *func;
1508 int argc;
1509 char_u **argv;
1510 int safe; /* use the sandbox */
1511{
1512 typval_T rettv;
1513 long retval;
1514
1515 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1516 return -1;
1517
1518 retval = get_tv_number_chk(&rettv, NULL);
1519 clear_tv(&rettv);
1520 return retval;
1521}
1522#endif
1523
1524/*
1525 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001526 * Uses argv[argc] for the function arguments.
1527 */
1528 void *
1529call_func_retlist(func, argc, argv, safe)
1530 char_u *func;
1531 int argc;
1532 char_u **argv;
1533 int safe; /* use the sandbox */
1534{
1535 typval_T rettv;
1536
1537 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1538 return NULL;
1539
1540 if (rettv.v_type != VAR_LIST)
1541 {
1542 clear_tv(&rettv);
1543 return NULL;
1544 }
1545
1546 return rettv.vval.v_list;
1547}
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#endif
1550
1551/*
1552 * Save the current function call pointer, and set it to NULL.
1553 * Used when executing autocommands and for ":source".
1554 */
1555 void *
1556save_funccal()
1557{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001558 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 current_funccal = NULL;
1561 return (void *)fc;
1562}
1563
1564 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001565restore_funccal(vfc)
1566 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001568 funccall_T *fc = (funccall_T *)vfc;
1569
1570 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571}
1572
Bram Moolenaar05159a02005-02-26 23:04:13 +00001573#if defined(FEAT_PROFILE) || defined(PROTO)
1574/*
1575 * Prepare profiling for entering a child or something else that is not
1576 * counted for the script/function itself.
1577 * Should always be called in pair with prof_child_exit().
1578 */
1579 void
1580prof_child_enter(tm)
1581 proftime_T *tm; /* place to store waittime */
1582{
1583 funccall_T *fc = current_funccal;
1584
1585 if (fc != NULL && fc->func->uf_profiling)
1586 profile_start(&fc->prof_child);
1587 script_prof_save(tm);
1588}
1589
1590/*
1591 * Take care of time spent in a child.
1592 * Should always be called after prof_child_enter().
1593 */
1594 void
1595prof_child_exit(tm)
1596 proftime_T *tm; /* where waittime was stored */
1597{
1598 funccall_T *fc = current_funccal;
1599
1600 if (fc != NULL && fc->func->uf_profiling)
1601 {
1602 profile_end(&fc->prof_child);
1603 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1604 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1605 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1606 }
1607 script_prof_restore(tm);
1608}
1609#endif
1610
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612#ifdef FEAT_FOLDING
1613/*
1614 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1615 * it in "*cp". Doesn't give error messages.
1616 */
1617 int
1618eval_foldexpr(arg, cp)
1619 char_u *arg;
1620 int *cp;
1621{
Bram Moolenaar33570922005-01-25 22:26:29 +00001622 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 int retval;
1624 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001625 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1626 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001629 if (use_sandbox)
1630 ++sandbox;
1631 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 retval = 0;
1635 else
1636 {
1637 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 if (tv.v_type == VAR_NUMBER)
1639 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001640 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 retval = 0;
1642 else
1643 {
1644 /* If the result is a string, check if there is a non-digit before
1645 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (!VIM_ISDIGIT(*s) && *s != '-')
1648 *cp = *s++;
1649 retval = atol((char *)s);
1650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 }
1653 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001654 if (use_sandbox)
1655 --sandbox;
1656 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658 return retval;
1659}
1660#endif
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 * ":let" list all variable values
1664 * ":let var1 var2" list variable values
1665 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001666 * ":let var += expr" assignment command.
1667 * ":let var -= expr" assignment command.
1668 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 */
1671 void
1672ex_let(eap)
1673 exarg_T *eap;
1674{
1675 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001677 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 int var_count = 0;
1680 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001682 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaardb552d602006-03-23 22:59:57 +00001684 argend = skip_var_list(arg, &var_count, &semicolon);
1685 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001687 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1688 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001689 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001692 /*
1693 * ":let" without "=": list variables
1694 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 if (*arg == '[')
1696 EMSG(_(e_invarg));
1697 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 /* ":let var1 var2" */
1699 arg = list_arg_vars(eap, arg);
1700 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001702 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001703 list_glob_vars();
1704 list_buf_vars();
1705 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001706#ifdef FEAT_WINDOWS
1707 list_tab_vars();
1708#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001709 list_script_vars();
1710 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001711 list_vim_vars();
1712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 eap->nextcmd = check_nextcmd(arg);
1714 }
1715 else
1716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 op[0] = '=';
1718 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001719 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 {
1721 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1722 op[0] = expr[-1]; /* +=, -= or .= */
1723 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (eap->skip)
1727 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 if (eap->skip)
1730 {
1731 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 --emsg_skip;
1734 }
1735 else if (i != FAIL)
1736 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001739 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
1741 }
1742}
1743
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744/*
1745 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1746 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001747 * When "nextchars" is not NULL it points to a string with characters that
1748 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1749 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 * Returns OK or FAIL;
1751 */
1752 static int
1753ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1754 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001755 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 int copy; /* copy values from "tv", don't move */
1757 int semicolon; /* from skip_var_list() */
1758 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760{
1761 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001762 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001764 listitem_T *item;
1765 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766
1767 if (*arg != '[')
1768 {
1769 /*
1770 * ":let var = expr" or ":for var in list"
1771 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 return FAIL;
1774 return OK;
1775 }
1776
1777 /*
1778 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1779 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001780 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 {
1782 EMSG(_(e_listreq));
1783 return FAIL;
1784 }
1785
1786 i = list_len(l);
1787 if (semicolon == 0 && var_count < i)
1788 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001789 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 return FAIL;
1791 }
1792 if (var_count - semicolon > i)
1793 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001794 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 return FAIL;
1796 }
1797
1798 item = l->lv_first;
1799 while (*arg != ']')
1800 {
1801 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001802 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 item = item->li_next;
1804 if (arg == NULL)
1805 return FAIL;
1806
1807 arg = skipwhite(arg);
1808 if (*arg == ';')
1809 {
1810 /* Put the rest of the list (may be empty) in the var after ';'.
1811 * Create a new list for this. */
1812 l = list_alloc();
1813 if (l == NULL)
1814 return FAIL;
1815 while (item != NULL)
1816 {
1817 list_append_tv(l, &item->li_tv);
1818 item = item->li_next;
1819 }
1820
1821 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001822 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 ltv.vval.v_list = l;
1824 l->lv_refcount = 1;
1825
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1827 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 clear_tv(&ltv);
1829 if (arg == NULL)
1830 return FAIL;
1831 break;
1832 }
1833 else if (*arg != ',' && *arg != ']')
1834 {
1835 EMSG2(_(e_intern2), "ex_let_vars()");
1836 return FAIL;
1837 }
1838 }
1839
1840 return OK;
1841}
1842
1843/*
1844 * Skip over assignable variable "var" or list of variables "[var, var]".
1845 * Used for ":let varvar = expr" and ":for varvar in expr".
1846 * For "[var, var]" increment "*var_count" for each variable.
1847 * for "[var, var; var]" set "semicolon".
1848 * Return NULL for an error.
1849 */
1850 static char_u *
1851skip_var_list(arg, var_count, semicolon)
1852 char_u *arg;
1853 int *var_count;
1854 int *semicolon;
1855{
1856 char_u *p, *s;
1857
1858 if (*arg == '[')
1859 {
1860 /* "[var, var]": find the matching ']'. */
1861 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001862 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 {
1864 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1865 s = skip_var_one(p);
1866 if (s == p)
1867 {
1868 EMSG2(_(e_invarg2), p);
1869 return NULL;
1870 }
1871 ++*var_count;
1872
1873 p = skipwhite(s);
1874 if (*p == ']')
1875 break;
1876 else if (*p == ';')
1877 {
1878 if (*semicolon == 1)
1879 {
1880 EMSG(_("Double ; in list of variables"));
1881 return NULL;
1882 }
1883 *semicolon = 1;
1884 }
1885 else if (*p != ',')
1886 {
1887 EMSG2(_(e_invarg2), p);
1888 return NULL;
1889 }
1890 }
1891 return p + 1;
1892 }
1893 else
1894 return skip_var_one(arg);
1895}
1896
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001897/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001898 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1899 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001900 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 static char_u *
1902skip_var_one(arg)
1903 char_u *arg;
1904{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001905 if (*arg == '@' && arg[1] != NUL)
1906 return arg + 2;
1907 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1908 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909}
1910
Bram Moolenaara7043832005-01-21 11:56:39 +00001911/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 * List variables for hashtab "ht" with prefix "prefix".
1913 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001914 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001916list_hashtable_vars(ht, prefix, empty)
1917 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001918 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001920{
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 hashitem_T *hi;
1922 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001923 int todo;
1924
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001925 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001926 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1927 {
1928 if (!HASHITEM_EMPTY(hi))
1929 {
1930 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 di = HI2DI(hi);
1932 if (empty || di->di_tv.v_type != VAR_STRING
1933 || di->di_tv.vval.v_string != NULL)
1934 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 }
1936 }
1937}
1938
1939/*
1940 * List global variables.
1941 */
1942 static void
1943list_glob_vars()
1944{
Bram Moolenaar33570922005-01-25 22:26:29 +00001945 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001946}
1947
1948/*
1949 * List buffer variables.
1950 */
1951 static void
1952list_buf_vars()
1953{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001954 char_u numbuf[NUMBUFLEN];
1955
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001957
1958 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1959 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001960}
1961
1962/*
1963 * List window variables.
1964 */
1965 static void
1966list_win_vars()
1967{
Bram Moolenaar33570922005-01-25 22:26:29 +00001968 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001969}
1970
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001971#ifdef FEAT_WINDOWS
1972/*
1973 * List tab page variables.
1974 */
1975 static void
1976list_tab_vars()
1977{
1978 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1979}
1980#endif
1981
Bram Moolenaara7043832005-01-21 11:56:39 +00001982/*
1983 * List Vim variables.
1984 */
1985 static void
1986list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001987{
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989}
1990
1991/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001992 * List script-local variables, if there is a script.
1993 */
1994 static void
1995list_script_vars()
1996{
1997 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1998 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1999}
2000
2001/*
2002 * List function variables, if there is a function.
2003 */
2004 static void
2005list_func_vars()
2006{
2007 if (current_funccal != NULL)
2008 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2009 (char_u *)"l:", FALSE);
2010}
2011
2012/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 * List variables in "arg".
2014 */
2015 static char_u *
2016list_arg_vars(eap, arg)
2017 exarg_T *eap;
2018 char_u *arg;
2019{
2020 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002021 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002023 char_u *name_start;
2024 char_u *arg_subsc;
2025 char_u *tofree;
2026 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027
2028 while (!ends_excmd(*arg) && !got_int)
2029 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002030 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002032 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2034 {
2035 emsg_severe = TRUE;
2036 EMSG(_(e_trailing));
2037 break;
2038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042 /* get_name_len() takes care of expanding curly braces */
2043 name_start = name = arg;
2044 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2045 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002047 /* This is mainly to keep test 49 working: when expanding
2048 * curly braces fails overrule the exception error message. */
2049 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002051 emsg_severe = TRUE;
2052 EMSG2(_(e_invarg2), arg);
2053 break;
2054 }
2055 error = TRUE;
2056 }
2057 else
2058 {
2059 if (tofree != NULL)
2060 name = tofree;
2061 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 else
2064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 /* handle d.key, l[idx], f(expr) */
2066 arg_subsc = arg;
2067 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002068 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002072 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 case 'g': list_glob_vars(); break;
2076 case 'b': list_buf_vars(); break;
2077 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002078#ifdef FEAT_WINDOWS
2079 case 't': list_tab_vars(); break;
2080#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002082 case 's': list_script_vars(); break;
2083 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 default:
2085 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 }
2088 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002089 {
2090 char_u numbuf[NUMBUFLEN];
2091 char_u *tf;
2092 int c;
2093 char_u *s;
2094
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002095 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096 c = *arg;
2097 *arg = NUL;
2098 list_one_var_a((char_u *)"",
2099 arg == arg_subsc ? name : name_start,
2100 tv.v_type, s == NULL ? (char_u *)"" : s);
2101 *arg = c;
2102 vim_free(tf);
2103 }
2104 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 }
2107 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002108
2109 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002111
2112 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 }
2114
2115 return arg;
2116}
2117
2118/*
2119 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2120 * Returns a pointer to the char just after the var name.
2121 * Returns NULL if there is an error.
2122 */
2123 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002124ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002126 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002128 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002129 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130{
2131 int c1;
2132 char_u *name;
2133 char_u *p;
2134 char_u *arg_end = NULL;
2135 int len;
2136 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002137 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138
2139 /*
2140 * ":let $VAR = expr": Set environment variable.
2141 */
2142 if (*arg == '$')
2143 {
2144 /* Find the end of the name. */
2145 ++arg;
2146 name = arg;
2147 len = get_env_len(&arg);
2148 if (len == 0)
2149 EMSG2(_(e_invarg2), name - 1);
2150 else
2151 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 if (op != NULL && (*op == '+' || *op == '-'))
2153 EMSG2(_(e_letwrong), op);
2154 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002155 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156 EMSG(_(e_letunexp));
2157 else
2158 {
2159 c1 = name[len];
2160 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002161 p = get_tv_string_chk(tv);
2162 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 {
2164 int mustfree = FALSE;
2165 char_u *s = vim_getenv(name, &mustfree);
2166
2167 if (s != NULL)
2168 {
2169 p = tofree = concat_str(s, p);
2170 if (mustfree)
2171 vim_free(s);
2172 }
2173 }
2174 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002175 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002176 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002177 if (STRICMP(name, "HOME") == 0)
2178 init_homedir();
2179 else if (didset_vim && STRICMP(name, "VIM") == 0)
2180 didset_vim = FALSE;
2181 else if (didset_vimruntime
2182 && STRICMP(name, "VIMRUNTIME") == 0)
2183 didset_vimruntime = FALSE;
2184 arg_end = arg;
2185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 }
2189 }
2190 }
2191
2192 /*
2193 * ":let &option = expr": Set option value.
2194 * ":let &l:option = expr": Set local option value.
2195 * ":let &g:option = expr": Set global option value.
2196 */
2197 else if (*arg == '&')
2198 {
2199 /* Find the end of the name. */
2200 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002201 if (p == NULL || (endchars != NULL
2202 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 EMSG(_(e_letunexp));
2204 else
2205 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206 long n;
2207 int opt_type;
2208 long numval;
2209 char_u *stringval = NULL;
2210 char_u *s;
2211
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 c1 = *p;
2213 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002214
2215 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002216 s = get_tv_string_chk(tv); /* != NULL if number or string */
2217 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002218 {
2219 opt_type = get_option_value(arg, &numval,
2220 &stringval, opt_flags);
2221 if ((opt_type == 1 && *op == '.')
2222 || (opt_type == 0 && *op != '.'))
2223 EMSG2(_(e_letwrong), op);
2224 else
2225 {
2226 if (opt_type == 1) /* number */
2227 {
2228 if (*op == '+')
2229 n = numval + n;
2230 else
2231 n = numval - n;
2232 }
2233 else if (opt_type == 0 && stringval != NULL) /* string */
2234 {
2235 s = concat_str(stringval, s);
2236 vim_free(stringval);
2237 stringval = s;
2238 }
2239 }
2240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002241 if (s != NULL)
2242 {
2243 set_option_value(arg, n, s, opt_flags);
2244 arg_end = p;
2245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002247 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 }
2249 }
2250
2251 /*
2252 * ":let @r = expr": Set register contents.
2253 */
2254 else if (*arg == '@')
2255 {
2256 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257 if (op != NULL && (*op == '+' || *op == '-'))
2258 EMSG2(_(e_letwrong), op);
2259 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002260 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 EMSG(_(e_letunexp));
2262 else
2263 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002264 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002265 char_u *s;
2266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267 p = get_tv_string_chk(tv);
2268 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002269 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002270 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002271 if (s != NULL)
2272 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002273 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002274 vim_free(s);
2275 }
2276 }
2277 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002279 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002280 arg_end = arg + 1;
2281 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002282 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
2285
2286 /*
2287 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002290 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002292 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002294 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2298 EMSG(_(e_letunexp));
2299 else
2300 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 arg_end = p;
2303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 }
2307
2308 else
2309 EMSG2(_(e_invarg2), arg);
2310
2311 return arg_end;
2312}
2313
2314/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002315 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2316 */
2317 static int
2318check_changedtick(arg)
2319 char_u *arg;
2320{
2321 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2322 {
2323 EMSG2(_(e_readonlyvar), arg);
2324 return TRUE;
2325 }
2326 return FALSE;
2327}
2328
2329/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 * Get an lval: variable, Dict item or List item that can be assigned a value
2331 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2332 * "name.key", "name.key[expr]" etc.
2333 * Indexing only works if "name" is an existing List or Dictionary.
2334 * "name" points to the start of the name.
2335 * If "rettv" is not NULL it points to the value to be assigned.
2336 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2337 * wrong; must end in space or cmd separator.
2338 *
2339 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002340 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 */
2343 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002344get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002346 typval_T *rettv;
2347 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 int unlet;
2349 int skip;
2350 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002351 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 char_u *expr_start, *expr_end;
2355 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002356 dictitem_T *v;
2357 typval_T var1;
2358 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002360 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002363 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002366 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367
2368 if (skip)
2369 {
2370 /* When skipping just find the end of the name. */
2371 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002372 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 }
2374
2375 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002376 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 if (expr_start != NULL)
2378 {
2379 /* Don't expand the name when we already know there is an error. */
2380 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2381 && *p != '[' && *p != '.')
2382 {
2383 EMSG(_(e_trailing));
2384 return NULL;
2385 }
2386
2387 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2388 if (lp->ll_exp_name == NULL)
2389 {
2390 /* Report an invalid expression in braces, unless the
2391 * expression evaluation has been cancelled due to an
2392 * aborting error, an interrupt, or an exception. */
2393 if (!aborting() && !quiet)
2394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002395 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 EMSG2(_(e_invarg2), name);
2397 return NULL;
2398 }
2399 }
2400 lp->ll_name = lp->ll_exp_name;
2401 }
2402 else
2403 lp->ll_name = name;
2404
2405 /* Without [idx] or .key we are done. */
2406 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2407 return p;
2408
2409 cc = *p;
2410 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002411 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (v == NULL && !quiet)
2413 EMSG2(_(e_undefvar), lp->ll_name);
2414 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 if (v == NULL)
2416 return NULL;
2417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 /*
2419 * Loop until no more [idx] or .key is following.
2420 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002421 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2425 && !(lp->ll_tv->v_type == VAR_DICT
2426 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 if (!quiet)
2429 EMSG(_("E689: Can only index a List or Dictionary"));
2430 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (!quiet)
2435 EMSG(_("E708: [:] must come last"));
2436 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002438
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 len = -1;
2440 if (*p == '.')
2441 {
2442 key = p + 1;
2443 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2444 ;
2445 if (len == 0)
2446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (!quiet)
2448 EMSG(_(e_emptykey));
2449 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 }
2451 p = key + len;
2452 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002453 else
2454 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002456 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 if (*p == ':')
2458 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002459 else
2460 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 empty1 = FALSE;
2462 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 if (get_tv_string_chk(&var1) == NULL)
2465 {
2466 /* not a number or string */
2467 clear_tv(&var1);
2468 return NULL;
2469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 }
2471
2472 /* Optionally get the second index [ :expr]. */
2473 if (*p == ':')
2474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002479 if (!empty1)
2480 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (rettv != NULL && (rettv->v_type != VAR_LIST
2484 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (!quiet)
2487 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 if (!empty1)
2489 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 }
2492 p = skipwhite(p + 1);
2493 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 else
2496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2499 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 if (!empty1)
2501 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 if (get_tv_string_chk(&var2) == NULL)
2505 {
2506 /* not a number or string */
2507 if (!empty1)
2508 clear_tv(&var1);
2509 clear_tv(&var2);
2510 return NULL;
2511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (!quiet)
2521 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 if (!empty1)
2523 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 }
2528
2529 /* Skip to past ']'. */
2530 ++p;
2531 }
2532
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 {
2535 if (len == -1)
2536 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002538 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 if (*key == NUL)
2540 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 if (!quiet)
2542 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 }
2546 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 lp->ll_list = NULL;
2548 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002549 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002552 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002556 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 if (len == -1)
2558 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 }
2561 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 if (len == -1)
2566 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 p = NULL;
2569 break;
2570 }
2571 if (len == -1)
2572 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 }
2575 else
2576 {
2577 /*
2578 * Get the number and item for the only or first index of the List.
2579 */
2580 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 else
2583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002584 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 clear_tv(&var1);
2586 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002587 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 lp->ll_list = lp->ll_tv->vval.v_list;
2589 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2590 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002592 if (lp->ll_n1 < 0)
2593 {
2594 lp->ll_n1 = 0;
2595 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2596 }
2597 }
2598 if (lp->ll_li == NULL)
2599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 }
2604
2605 /*
2606 * May need to find the item or absolute index for the second
2607 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 * When no index given: "lp->ll_empty2" is TRUE.
2609 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002613 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 }
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2624 if (lp->ll_n1 < 0)
2625 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2626 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 }
2629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 }
2633
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 return p;
2635}
2636
2637/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002638 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 */
2640 static void
2641clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643{
2644 vim_free(lp->ll_exp_name);
2645 vim_free(lp->ll_newkey);
2646}
2647
2648/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002649 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 */
2653 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002654set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002655 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002657 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660{
2661 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002662 listitem_T *ri;
2663 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664
2665 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 cc = *endp;
2670 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 if (op != NULL && *op != '=')
2672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002674
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002675 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002676 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002677 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002678 {
2679 if (tv_op(&tv, rettv, op) == OK)
2680 set_var(lp->ll_name, &tv, FALSE);
2681 clear_tv(&tv);
2682 }
2683 }
2684 else
2685 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002689 else if (tv_check_lock(lp->ll_newkey == NULL
2690 ? lp->ll_tv->v_lock
2691 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2692 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 else if (lp->ll_range)
2694 {
2695 /*
2696 * Assign the List values to the list items.
2697 */
2698 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002699 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 if (op != NULL && *op != '=')
2701 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2702 else
2703 {
2704 clear_tv(&lp->ll_li->li_tv);
2705 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2706 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 ri = ri->li_next;
2708 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2709 break;
2710 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002713 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 ri = NULL;
2716 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_li = lp->ll_li->li_next;
2720 ++lp->ll_n1;
2721 }
2722 if (ri != NULL)
2723 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 else if (lp->ll_empty2
2725 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 : lp->ll_n1 != lp->ll_n2)
2727 EMSG(_("E711: List value has not enough items"));
2728 }
2729 else
2730 {
2731 /*
2732 * Assign to a List or Dictionary item.
2733 */
2734 if (lp->ll_newkey != NULL)
2735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 if (op != NULL && *op != '=')
2737 {
2738 EMSG2(_(e_letwrong), op);
2739 return;
2740 }
2741
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002743 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (di == NULL)
2745 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002746 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2747 {
2748 vim_free(di);
2749 return;
2750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 else if (op != NULL && *op != '=')
2754 {
2755 tv_op(lp->ll_tv, rettv, op);
2756 return;
2757 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002758 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 /*
2762 * Assign the value to the variable or list item.
2763 */
2764 if (copy)
2765 copy_tv(rettv, lp->ll_tv);
2766 else
2767 {
2768 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002769 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002771 }
2772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002773}
2774
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2777 * Returns OK or FAIL.
2778 */
2779 static int
2780tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002781 typval_T *tv1;
2782 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002783 char_u *op;
2784{
2785 long n;
2786 char_u numbuf[NUMBUFLEN];
2787 char_u *s;
2788
2789 /* Can't do anything with a Funcref or a Dict on the right. */
2790 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2791 {
2792 switch (tv1->v_type)
2793 {
2794 case VAR_DICT:
2795 case VAR_FUNC:
2796 break;
2797
2798 case VAR_LIST:
2799 if (*op != '+' || tv2->v_type != VAR_LIST)
2800 break;
2801 /* List += List */
2802 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2803 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2804 return OK;
2805
2806 case VAR_NUMBER:
2807 case VAR_STRING:
2808 if (tv2->v_type == VAR_LIST)
2809 break;
2810 if (*op == '+' || *op == '-')
2811 {
2812 /* nr += nr or nr -= nr*/
2813 n = get_tv_number(tv1);
2814 if (*op == '+')
2815 n += get_tv_number(tv2);
2816 else
2817 n -= get_tv_number(tv2);
2818 clear_tv(tv1);
2819 tv1->v_type = VAR_NUMBER;
2820 tv1->vval.v_number = n;
2821 }
2822 else
2823 {
2824 /* str .= str */
2825 s = get_tv_string(tv1);
2826 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2827 clear_tv(tv1);
2828 tv1->v_type = VAR_STRING;
2829 tv1->vval.v_string = s;
2830 }
2831 return OK;
2832 }
2833 }
2834
2835 EMSG2(_(e_letwrong), op);
2836 return FAIL;
2837}
2838
2839/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002840 * Add a watcher to a list.
2841 */
2842 static void
2843list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 list_T *l;
2845 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846{
2847 lw->lw_next = l->lv_watch;
2848 l->lv_watch = lw;
2849}
2850
2851/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002852 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 * No warning when it isn't found...
2854 */
2855 static void
2856list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002857 list_T *l;
2858 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002859{
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002861
2862 lwp = &l->lv_watch;
2863 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2864 {
2865 if (lw == lwrem)
2866 {
2867 *lwp = lw->lw_next;
2868 break;
2869 }
2870 lwp = &lw->lw_next;
2871 }
2872}
2873
2874/*
2875 * Just before removing an item from a list: advance watchers to the next
2876 * item.
2877 */
2878 static void
2879list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 list_T *l;
2881 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882{
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884
2885 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2886 if (lw->lw_item == item)
2887 lw->lw_item = item->li_next;
2888}
2889
2890/*
2891 * Evaluate the expression used in a ":for var in expr" command.
2892 * "arg" points to "var".
2893 * Set "*errp" to TRUE for an error, FALSE otherwise;
2894 * Return a pointer that holds the info. Null when there is an error.
2895 */
2896 void *
2897eval_for_line(arg, errp, nextcmdp, skip)
2898 char_u *arg;
2899 int *errp;
2900 char_u **nextcmdp;
2901 int skip;
2902{
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 typval_T tv;
2906 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907
2908 *errp = TRUE; /* default: there is an error */
2909
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 if (fi == NULL)
2912 return NULL;
2913
2914 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2915 if (expr == NULL)
2916 return fi;
2917
2918 expr = skipwhite(expr);
2919 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2920 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002921 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922 return fi;
2923 }
2924
2925 if (skip)
2926 ++emsg_skip;
2927 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2928 {
2929 *errp = FALSE;
2930 if (!skip)
2931 {
2932 l = tv.vval.v_list;
2933 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002934 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002936 clear_tv(&tv);
2937 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002938 else
2939 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002940 /* No need to increment the refcount, it's already set for the
2941 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942 fi->fi_list = l;
2943 list_add_watch(l, &fi->fi_lw);
2944 fi->fi_lw.lw_item = l->lv_first;
2945 }
2946 }
2947 }
2948 if (skip)
2949 --emsg_skip;
2950
2951 return fi;
2952}
2953
2954/*
2955 * Use the first item in a ":for" list. Advance to the next.
2956 * Assign the values to the variable (list). "arg" points to the first one.
2957 * Return TRUE when a valid item was found, FALSE when at end of list or
2958 * something wrong.
2959 */
2960 int
2961next_for_item(fi_void, arg)
2962 void *fi_void;
2963 char_u *arg;
2964{
Bram Moolenaar33570922005-01-25 22:26:29 +00002965 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002966 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002967 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002968
2969 item = fi->fi_lw.lw_item;
2970 if (item == NULL)
2971 result = FALSE;
2972 else
2973 {
2974 fi->fi_lw.lw_item = item->li_next;
2975 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2976 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2977 }
2978 return result;
2979}
2980
2981/*
2982 * Free the structure used to store info used by ":for".
2983 */
2984 void
2985free_for_info(fi_void)
2986 void *fi_void;
2987{
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002989
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002990 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002991 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002992 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002993 list_unref(fi->fi_list);
2994 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002995 vim_free(fi);
2996}
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2999
3000 void
3001set_context_for_expression(xp, arg, cmdidx)
3002 expand_T *xp;
3003 char_u *arg;
3004 cmdidx_T cmdidx;
3005{
3006 int got_eq = FALSE;
3007 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003008 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010 if (cmdidx == CMD_let)
3011 {
3012 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003013 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003014 {
3015 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003016 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003017 {
3018 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003020 if (vim_iswhite(*p))
3021 break;
3022 }
3023 return;
3024 }
3025 }
3026 else
3027 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3028 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 while ((xp->xp_pattern = vim_strpbrk(arg,
3030 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3031 {
3032 c = *xp->xp_pattern;
3033 if (c == '&')
3034 {
3035 c = xp->xp_pattern[1];
3036 if (c == '&')
3037 {
3038 ++xp->xp_pattern;
3039 xp->xp_context = cmdidx != CMD_let || got_eq
3040 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3041 }
3042 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003045 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3046 xp->xp_pattern += 2;
3047
3048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 else if (c == '$')
3051 {
3052 /* environment variable */
3053 xp->xp_context = EXPAND_ENV_VARS;
3054 }
3055 else if (c == '=')
3056 {
3057 got_eq = TRUE;
3058 xp->xp_context = EXPAND_EXPRESSION;
3059 }
3060 else if (c == '<'
3061 && xp->xp_context == EXPAND_FUNCTIONS
3062 && vim_strchr(xp->xp_pattern, '(') == NULL)
3063 {
3064 /* Function name can start with "<SNR>" */
3065 break;
3066 }
3067 else if (cmdidx != CMD_let || got_eq)
3068 {
3069 if (c == '"') /* string */
3070 {
3071 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3072 if (c == '\\' && xp->xp_pattern[1] != NUL)
3073 ++xp->xp_pattern;
3074 xp->xp_context = EXPAND_NOTHING;
3075 }
3076 else if (c == '\'') /* literal string */
3077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003078 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3080 /* skip */ ;
3081 xp->xp_context = EXPAND_NOTHING;
3082 }
3083 else if (c == '|')
3084 {
3085 if (xp->xp_pattern[1] == '|')
3086 {
3087 ++xp->xp_pattern;
3088 xp->xp_context = EXPAND_EXPRESSION;
3089 }
3090 else
3091 xp->xp_context = EXPAND_COMMANDS;
3092 }
3093 else
3094 xp->xp_context = EXPAND_EXPRESSION;
3095 }
3096 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097 /* Doesn't look like something valid, expand as an expression
3098 * anyway. */
3099 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 arg = xp->xp_pattern;
3101 if (*arg != NUL)
3102 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3103 /* skip */ ;
3104 }
3105 xp->xp_pattern = arg;
3106}
3107
3108#endif /* FEAT_CMDL_COMPL */
3109
3110/*
3111 * ":1,25call func(arg1, arg2)" function call.
3112 */
3113 void
3114ex_call(eap)
3115 exarg_T *eap;
3116{
3117 char_u *arg = eap->arg;
3118 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 linenr_T lnum;
3124 int doesrange;
3125 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003128 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3129 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 if (tofree == NULL)
3131 return;
3132
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003133 /* Increase refcount on dictionary, it could get deleted when evaluating
3134 * the arguments. */
3135 if (fudi.fd_dict != NULL)
3136 ++fudi.fd_dict->dv_refcount;
3137
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003138 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003139 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
Bram Moolenaar532c7802005-01-27 14:44:31 +00003142 /* Skip white space to allow ":call func ()". Not good, but required for
3143 * backward compatibility. */
3144 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003145 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 if (*startarg != '(')
3148 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003149 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 goto end;
3151 }
3152
3153 /*
3154 * When skipping, evaluate the function once, to find the end of the
3155 * arguments.
3156 * When the function takes a range, this is discovered after the first
3157 * call, and the loop is broken.
3158 */
3159 if (eap->skip)
3160 {
3161 ++emsg_skip;
3162 lnum = eap->line2; /* do it once, also with an invalid range */
3163 }
3164 else
3165 lnum = eap->line1;
3166 for ( ; lnum <= eap->line2; ++lnum)
3167 {
3168 if (!eap->skip && eap->addr_count > 0)
3169 {
3170 curwin->w_cursor.lnum = lnum;
3171 curwin->w_cursor.col = 0;
3172 }
3173 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003174 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003175 eap->line1, eap->line2, &doesrange,
3176 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
3178 failed = TRUE;
3179 break;
3180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003181 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (doesrange || eap->skip)
3183 break;
3184 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003185 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003187 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (aborting())
3189 break;
3190 }
3191 if (eap->skip)
3192 --emsg_skip;
3193
3194 if (!failed)
3195 {
3196 /* Check for trailing illegal characters and a following command. */
3197 if (!ends_excmd(*arg))
3198 {
3199 emsg_severe = TRUE;
3200 EMSG(_(e_trailing));
3201 }
3202 else
3203 eap->nextcmd = check_nextcmd(arg);
3204 }
3205
3206end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003207 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003208 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209}
3210
3211/*
3212 * ":unlet[!] var1 ... " command.
3213 */
3214 void
3215ex_unlet(eap)
3216 exarg_T *eap;
3217{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218 ex_unletlock(eap, eap->arg, 0);
3219}
3220
3221/*
3222 * ":lockvar" and ":unlockvar" commands
3223 */
3224 void
3225ex_lockvar(eap)
3226 exarg_T *eap;
3227{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003229 int deep = 2;
3230
3231 if (eap->forceit)
3232 deep = -1;
3233 else if (vim_isdigit(*arg))
3234 {
3235 deep = getdigits(&arg);
3236 arg = skipwhite(arg);
3237 }
3238
3239 ex_unletlock(eap, arg, deep);
3240}
3241
3242/*
3243 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3244 */
3245 static void
3246ex_unletlock(eap, argstart, deep)
3247 exarg_T *eap;
3248 char_u *argstart;
3249 int deep;
3250{
3251 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003254 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256 do
3257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003259 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3260 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003261 if (lv.ll_name == NULL)
3262 error = TRUE; /* error but continue parsing */
3263 if (name_end == NULL || (!vim_iswhite(*name_end)
3264 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003266 if (name_end != NULL)
3267 {
3268 emsg_severe = TRUE;
3269 EMSG(_(e_trailing));
3270 }
3271 if (!(eap->skip || error))
3272 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 break;
3274 }
3275
3276 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277 {
3278 if (eap->cmdidx == CMD_unlet)
3279 {
3280 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3281 error = TRUE;
3282 }
3283 else
3284 {
3285 if (do_lock_var(&lv, name_end, deep,
3286 eap->cmdidx == CMD_lockvar) == FAIL)
3287 error = TRUE;
3288 }
3289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003291 if (!eap->skip)
3292 clear_lval(&lv);
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 arg = skipwhite(name_end);
3295 } while (!ends_excmd(*arg));
3296
3297 eap->nextcmd = check_nextcmd(arg);
3298}
3299
Bram Moolenaar8c711452005-01-14 21:53:12 +00003300 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003301do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003303 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003304 int forceit;
3305{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 int ret = OK;
3307 int cc;
3308
3309 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003310 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 cc = *name_end;
3312 *name_end = NUL;
3313
3314 /* Normal name or expanded name. */
3315 if (check_changedtick(lp->ll_name))
3316 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003317 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003318 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003319 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003320 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003321 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3322 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003323 else if (lp->ll_range)
3324 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003326
3327 /* Delete a range of List items. */
3328 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3329 {
3330 li = lp->ll_li->li_next;
3331 listitem_remove(lp->ll_list, lp->ll_li);
3332 lp->ll_li = li;
3333 ++lp->ll_n1;
3334 }
3335 }
3336 else
3337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003338 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003339 /* unlet a List item. */
3340 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003341 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003342 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003343 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003344 }
3345
3346 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003347}
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349/*
3350 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003351 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
3353 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003354do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003356 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 hashtab_T *ht;
3359 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003360 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 ht = find_var_ht(name, &varname);
3363 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003365 hi = hash_find(ht, varname);
3366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003367 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003368 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3369 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003370 if (var_check_ro(HI2DI(hi)->di_flags, name))
3371 return FAIL;
3372 delete_var(ht, hi);
3373 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003376 if (forceit)
3377 return OK;
3378 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 return FAIL;
3380}
3381
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003382/*
3383 * Lock or unlock variable indicated by "lp".
3384 * "deep" is the levels to go (-1 for unlimited);
3385 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3386 */
3387 static int
3388do_lock_var(lp, name_end, deep, lock)
3389 lval_T *lp;
3390 char_u *name_end;
3391 int deep;
3392 int lock;
3393{
3394 int ret = OK;
3395 int cc;
3396 dictitem_T *di;
3397
3398 if (deep == 0) /* nothing to do */
3399 return OK;
3400
3401 if (lp->ll_tv == NULL)
3402 {
3403 cc = *name_end;
3404 *name_end = NUL;
3405
3406 /* Normal name or expanded name. */
3407 if (check_changedtick(lp->ll_name))
3408 ret = FAIL;
3409 else
3410 {
3411 di = find_var(lp->ll_name, NULL);
3412 if (di == NULL)
3413 ret = FAIL;
3414 else
3415 {
3416 if (lock)
3417 di->di_flags |= DI_FLAGS_LOCK;
3418 else
3419 di->di_flags &= ~DI_FLAGS_LOCK;
3420 item_lock(&di->di_tv, deep, lock);
3421 }
3422 }
3423 *name_end = cc;
3424 }
3425 else if (lp->ll_range)
3426 {
3427 listitem_T *li = lp->ll_li;
3428
3429 /* (un)lock a range of List items. */
3430 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3431 {
3432 item_lock(&li->li_tv, deep, lock);
3433 li = li->li_next;
3434 ++lp->ll_n1;
3435 }
3436 }
3437 else if (lp->ll_list != NULL)
3438 /* (un)lock a List item. */
3439 item_lock(&lp->ll_li->li_tv, deep, lock);
3440 else
3441 /* un(lock) a Dictionary item. */
3442 item_lock(&lp->ll_di->di_tv, deep, lock);
3443
3444 return ret;
3445}
3446
3447/*
3448 * Lock or unlock an item. "deep" is nr of levels to go.
3449 */
3450 static void
3451item_lock(tv, deep, lock)
3452 typval_T *tv;
3453 int deep;
3454 int lock;
3455{
3456 static int recurse = 0;
3457 list_T *l;
3458 listitem_T *li;
3459 dict_T *d;
3460 hashitem_T *hi;
3461 int todo;
3462
3463 if (recurse >= DICT_MAXNEST)
3464 {
3465 EMSG(_("E743: variable nested too deep for (un)lock"));
3466 return;
3467 }
3468 if (deep == 0)
3469 return;
3470 ++recurse;
3471
3472 /* lock/unlock the item itself */
3473 if (lock)
3474 tv->v_lock |= VAR_LOCKED;
3475 else
3476 tv->v_lock &= ~VAR_LOCKED;
3477
3478 switch (tv->v_type)
3479 {
3480 case VAR_LIST:
3481 if ((l = tv->vval.v_list) != NULL)
3482 {
3483 if (lock)
3484 l->lv_lock |= VAR_LOCKED;
3485 else
3486 l->lv_lock &= ~VAR_LOCKED;
3487 if (deep < 0 || deep > 1)
3488 /* recursive: lock/unlock the items the List contains */
3489 for (li = l->lv_first; li != NULL; li = li->li_next)
3490 item_lock(&li->li_tv, deep - 1, lock);
3491 }
3492 break;
3493 case VAR_DICT:
3494 if ((d = tv->vval.v_dict) != NULL)
3495 {
3496 if (lock)
3497 d->dv_lock |= VAR_LOCKED;
3498 else
3499 d->dv_lock &= ~VAR_LOCKED;
3500 if (deep < 0 || deep > 1)
3501 {
3502 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003503 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003504 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3505 {
3506 if (!HASHITEM_EMPTY(hi))
3507 {
3508 --todo;
3509 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3510 }
3511 }
3512 }
3513 }
3514 }
3515 --recurse;
3516}
3517
Bram Moolenaara40058a2005-07-11 22:42:07 +00003518/*
3519 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3520 * it refers to a List or Dictionary that is locked.
3521 */
3522 static int
3523tv_islocked(tv)
3524 typval_T *tv;
3525{
3526 return (tv->v_lock & VAR_LOCKED)
3527 || (tv->v_type == VAR_LIST
3528 && tv->vval.v_list != NULL
3529 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3530 || (tv->v_type == VAR_DICT
3531 && tv->vval.v_dict != NULL
3532 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3533}
3534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3536/*
3537 * Delete all "menutrans_" variables.
3538 */
3539 void
3540del_menutrans_vars()
3541{
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003543 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
Bram Moolenaar33570922005-01-25 22:26:29 +00003545 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003546 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003548 {
3549 if (!HASHITEM_EMPTY(hi))
3550 {
3551 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3553 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 }
3555 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003556 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557}
3558#endif
3559
3560#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3561
3562/*
3563 * Local string buffer for the next two functions to store a variable name
3564 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3565 * get_user_var_name().
3566 */
3567
3568static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3569
3570static char_u *varnamebuf = NULL;
3571static int varnamebuflen = 0;
3572
3573/*
3574 * Function to concatenate a prefix and a variable name.
3575 */
3576 static char_u *
3577cat_prefix_varname(prefix, name)
3578 int prefix;
3579 char_u *name;
3580{
3581 int len;
3582
3583 len = (int)STRLEN(name) + 3;
3584 if (len > varnamebuflen)
3585 {
3586 vim_free(varnamebuf);
3587 len += 10; /* some additional space */
3588 varnamebuf = alloc(len);
3589 if (varnamebuf == NULL)
3590 {
3591 varnamebuflen = 0;
3592 return NULL;
3593 }
3594 varnamebuflen = len;
3595 }
3596 *varnamebuf = prefix;
3597 varnamebuf[1] = ':';
3598 STRCPY(varnamebuf + 2, name);
3599 return varnamebuf;
3600}
3601
3602/*
3603 * Function given to ExpandGeneric() to obtain the list of user defined
3604 * (global/buffer/window/built-in) variable names.
3605 */
3606/*ARGSUSED*/
3607 char_u *
3608get_user_var_name(xp, idx)
3609 expand_T *xp;
3610 int idx;
3611{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003612 static long_u gdone;
3613 static long_u bdone;
3614 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003615#ifdef FEAT_WINDOWS
3616 static long_u tdone;
3617#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003618 static int vidx;
3619 static hashitem_T *hi;
3620 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003623 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003624 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003625#ifdef FEAT_WINDOWS
3626 tdone = 0;
3627#endif
3628 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003629
3630 /* Global variables */
3631 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003633 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003634 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003635 else
3636 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003637 while (HASHITEM_EMPTY(hi))
3638 ++hi;
3639 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3640 return cat_prefix_varname('g', hi->hi_key);
3641 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003643
3644 /* b: variables */
3645 ht = &curbuf->b_vars.dv_hashtab;
3646 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003649 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003650 else
3651 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 while (HASHITEM_EMPTY(hi))
3653 ++hi;
3654 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003656 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003658 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return (char_u *)"b:changedtick";
3660 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003661
3662 /* w: variables */
3663 ht = &curwin->w_vars.dv_hashtab;
3664 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003666 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003667 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003668 else
3669 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 while (HASHITEM_EMPTY(hi))
3671 ++hi;
3672 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003674
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003675#ifdef FEAT_WINDOWS
3676 /* t: variables */
3677 ht = &curtab->tp_vars.dv_hashtab;
3678 if (tdone < ht->ht_used)
3679 {
3680 if (tdone++ == 0)
3681 hi = ht->ht_array;
3682 else
3683 ++hi;
3684 while (HASHITEM_EMPTY(hi))
3685 ++hi;
3686 return cat_prefix_varname('t', hi->hi_key);
3687 }
3688#endif
3689
Bram Moolenaar33570922005-01-25 22:26:29 +00003690 /* v: variables */
3691 if (vidx < VV_LEN)
3692 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
3694 vim_free(varnamebuf);
3695 varnamebuf = NULL;
3696 varnamebuflen = 0;
3697 return NULL;
3698}
3699
3700#endif /* FEAT_CMDL_COMPL */
3701
3702/*
3703 * types for expressions.
3704 */
3705typedef enum
3706{
3707 TYPE_UNKNOWN = 0
3708 , TYPE_EQUAL /* == */
3709 , TYPE_NEQUAL /* != */
3710 , TYPE_GREATER /* > */
3711 , TYPE_GEQUAL /* >= */
3712 , TYPE_SMALLER /* < */
3713 , TYPE_SEQUAL /* <= */
3714 , TYPE_MATCH /* =~ */
3715 , TYPE_NOMATCH /* !~ */
3716} exptype_T;
3717
3718/*
3719 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3722 */
3723
3724/*
3725 * Handle zero level expression.
3726 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003728 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 * Return OK or FAIL.
3730 */
3731 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 char_u **nextcmd;
3736 int evaluate;
3737{
3738 int ret;
3739 char_u *p;
3740
3741 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (ret == FAIL || !ends_excmd(*p))
3744 {
3745 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003746 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 /*
3748 * Report the invalid expression unless the expression evaluation has
3749 * been cancelled due to an aborting error, an interrupt, or an
3750 * exception.
3751 */
3752 if (!aborting())
3753 EMSG2(_(e_invexpr2), arg);
3754 ret = FAIL;
3755 }
3756 if (nextcmd != NULL)
3757 *nextcmd = check_nextcmd(p);
3758
3759 return ret;
3760}
3761
3762/*
3763 * Handle top level expression:
3764 * expr1 ? expr0 : expr0
3765 *
3766 * "arg" must point to the first non-white of the expression.
3767 * "arg" is advanced to the next non-white after the recognized expression.
3768 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003769 * Note: "rettv.v_lock" is not set.
3770 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 * Return OK or FAIL.
3772 */
3773 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003774eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 int evaluate;
3778{
3779 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 /*
3783 * Get the first variable.
3784 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787
3788 if ((*arg)[0] == '?')
3789 {
3790 result = FALSE;
3791 if (evaluate)
3792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003793 int error = FALSE;
3794
3795 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003798 if (error)
3799 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801
3802 /*
3803 * Get the second variable.
3804 */
3805 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return FAIL;
3808
3809 /*
3810 * Check for the ":".
3811 */
3812 if ((*arg)[0] != ':')
3813 {
3814 EMSG(_("E109: Missing ':' after '?'"));
3815 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003816 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 return FAIL;
3818 }
3819
3820 /*
3821 * Get the third variable.
3822 */
3823 *arg = skipwhite(*arg + 1);
3824 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3825 {
3826 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 return FAIL;
3829 }
3830 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833
3834 return OK;
3835}
3836
3837/*
3838 * Handle first level expression:
3839 * expr2 || expr2 || expr2 logical OR
3840 *
3841 * "arg" must point to the first non-white of the expression.
3842 * "arg" is advanced to the next non-white after the recognized expression.
3843 *
3844 * Return OK or FAIL.
3845 */
3846 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 int evaluate;
3851{
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 long result;
3854 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
3857 /*
3858 * Get the first variable.
3859 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return FAIL;
3862
3863 /*
3864 * Repeat until there is no following "||".
3865 */
3866 first = TRUE;
3867 result = FALSE;
3868 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3869 {
3870 if (evaluate && first)
3871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003872 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003874 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003875 if (error)
3876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 first = FALSE;
3878 }
3879
3880 /*
3881 * Get the second variable.
3882 */
3883 *arg = skipwhite(*arg + 2);
3884 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3885 return FAIL;
3886
3887 /*
3888 * Compute the result.
3889 */
3890 if (evaluate && !result)
3891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003892 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003894 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003895 if (error)
3896 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898 if (evaluate)
3899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900 rettv->v_type = VAR_NUMBER;
3901 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 }
3904
3905 return OK;
3906}
3907
3908/*
3909 * Handle second level expression:
3910 * expr3 && expr3 && expr3 logical AND
3911 *
3912 * "arg" must point to the first non-white of the expression.
3913 * "arg" is advanced to the next non-white after the recognized expression.
3914 *
3915 * Return OK or FAIL.
3916 */
3917 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 int evaluate;
3922{
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 long result;
3925 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003926 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 /*
3929 * Get the first variable.
3930 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003931 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return FAIL;
3933
3934 /*
3935 * Repeat until there is no following "&&".
3936 */
3937 first = TRUE;
3938 result = TRUE;
3939 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3940 {
3941 if (evaluate && first)
3942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003945 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003946 if (error)
3947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 first = FALSE;
3949 }
3950
3951 /*
3952 * Get the second variable.
3953 */
3954 *arg = skipwhite(*arg + 2);
3955 if (eval4(arg, &var2, evaluate && result) == FAIL)
3956 return FAIL;
3957
3958 /*
3959 * Compute the result.
3960 */
3961 if (evaluate && result)
3962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003963 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003966 if (error)
3967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 if (evaluate)
3970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003971 rettv->v_type = VAR_NUMBER;
3972 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 }
3975
3976 return OK;
3977}
3978
3979/*
3980 * Handle third level expression:
3981 * var1 == var2
3982 * var1 =~ var2
3983 * var1 != var2
3984 * var1 !~ var2
3985 * var1 > var2
3986 * var1 >= var2
3987 * var1 < var2
3988 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003989 * var1 is var2
3990 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 *
3992 * "arg" must point to the first non-white of the expression.
3993 * "arg" is advanced to the next non-white after the recognized expression.
3994 *
3995 * Return OK or FAIL.
3996 */
3997 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 int evaluate;
4002{
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 char_u *p;
4005 int i;
4006 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004007 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 int len = 2;
4009 long n1, n2;
4010 char_u *s1, *s2;
4011 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4012 regmatch_T regmatch;
4013 int ic;
4014 char_u *save_cpo;
4015
4016 /*
4017 * Get the first variable.
4018 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 return FAIL;
4021
4022 p = *arg;
4023 switch (p[0])
4024 {
4025 case '=': if (p[1] == '=')
4026 type = TYPE_EQUAL;
4027 else if (p[1] == '~')
4028 type = TYPE_MATCH;
4029 break;
4030 case '!': if (p[1] == '=')
4031 type = TYPE_NEQUAL;
4032 else if (p[1] == '~')
4033 type = TYPE_NOMATCH;
4034 break;
4035 case '>': if (p[1] != '=')
4036 {
4037 type = TYPE_GREATER;
4038 len = 1;
4039 }
4040 else
4041 type = TYPE_GEQUAL;
4042 break;
4043 case '<': if (p[1] != '=')
4044 {
4045 type = TYPE_SMALLER;
4046 len = 1;
4047 }
4048 else
4049 type = TYPE_SEQUAL;
4050 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004051 case 'i': if (p[1] == 's')
4052 {
4053 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4054 len = 5;
4055 if (!vim_isIDc(p[len]))
4056 {
4057 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4058 type_is = TRUE;
4059 }
4060 }
4061 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063
4064 /*
4065 * If there is a comparitive operator, use it.
4066 */
4067 if (type != TYPE_UNKNOWN)
4068 {
4069 /* extra question mark appended: ignore case */
4070 if (p[len] == '?')
4071 {
4072 ic = TRUE;
4073 ++len;
4074 }
4075 /* extra '#' appended: match case */
4076 else if (p[len] == '#')
4077 {
4078 ic = FALSE;
4079 ++len;
4080 }
4081 /* nothing appened: use 'ignorecase' */
4082 else
4083 ic = p_ic;
4084
4085 /*
4086 * Get the second variable.
4087 */
4088 *arg = skipwhite(p + len);
4089 if (eval5(arg, &var2, evaluate) == FAIL)
4090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return FAIL;
4093 }
4094
4095 if (evaluate)
4096 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004097 if (type_is && rettv->v_type != var2.v_type)
4098 {
4099 /* For "is" a different type always means FALSE, for "notis"
4100 * it means TRUE. */
4101 n1 = (type == TYPE_NEQUAL);
4102 }
4103 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4104 {
4105 if (type_is)
4106 {
4107 n1 = (rettv->v_type == var2.v_type
4108 && rettv->vval.v_list == var2.vval.v_list);
4109 if (type == TYPE_NEQUAL)
4110 n1 = !n1;
4111 }
4112 else if (rettv->v_type != var2.v_type
4113 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4114 {
4115 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004116 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004117 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004118 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004119 clear_tv(rettv);
4120 clear_tv(&var2);
4121 return FAIL;
4122 }
4123 else
4124 {
4125 /* Compare two Lists for being equal or unequal. */
4126 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4127 if (type == TYPE_NEQUAL)
4128 n1 = !n1;
4129 }
4130 }
4131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004132 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4133 {
4134 if (type_is)
4135 {
4136 n1 = (rettv->v_type == var2.v_type
4137 && rettv->vval.v_dict == var2.vval.v_dict);
4138 if (type == TYPE_NEQUAL)
4139 n1 = !n1;
4140 }
4141 else if (rettv->v_type != var2.v_type
4142 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4143 {
4144 if (rettv->v_type != var2.v_type)
4145 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4146 else
4147 EMSG(_("E736: Invalid operation for Dictionary"));
4148 clear_tv(rettv);
4149 clear_tv(&var2);
4150 return FAIL;
4151 }
4152 else
4153 {
4154 /* Compare two Dictionaries for being equal or unequal. */
4155 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4156 if (type == TYPE_NEQUAL)
4157 n1 = !n1;
4158 }
4159 }
4160
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004161 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4162 {
4163 if (rettv->v_type != var2.v_type
4164 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4165 {
4166 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004167 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004168 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004169 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004170 clear_tv(rettv);
4171 clear_tv(&var2);
4172 return FAIL;
4173 }
4174 else
4175 {
4176 /* Compare two Funcrefs for being equal or unequal. */
4177 if (rettv->vval.v_string == NULL
4178 || var2.vval.v_string == NULL)
4179 n1 = FALSE;
4180 else
4181 n1 = STRCMP(rettv->vval.v_string,
4182 var2.vval.v_string) == 0;
4183 if (type == TYPE_NEQUAL)
4184 n1 = !n1;
4185 }
4186 }
4187
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 /*
4189 * If one of the two variables is a number, compare as a number.
4190 * When using "=~" or "!~", always compare as string.
4191 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004192 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 n1 = get_tv_number(rettv);
4196 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 switch (type)
4198 {
4199 case TYPE_EQUAL: n1 = (n1 == n2); break;
4200 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4201 case TYPE_GREATER: n1 = (n1 > n2); break;
4202 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4203 case TYPE_SMALLER: n1 = (n1 < n2); break;
4204 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4205 case TYPE_UNKNOWN:
4206 case TYPE_MATCH:
4207 case TYPE_NOMATCH: break; /* avoid gcc warning */
4208 }
4209 }
4210 else
4211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 s1 = get_tv_string_buf(rettv, buf1);
4213 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4215 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4216 else
4217 i = 0;
4218 n1 = FALSE;
4219 switch (type)
4220 {
4221 case TYPE_EQUAL: n1 = (i == 0); break;
4222 case TYPE_NEQUAL: n1 = (i != 0); break;
4223 case TYPE_GREATER: n1 = (i > 0); break;
4224 case TYPE_GEQUAL: n1 = (i >= 0); break;
4225 case TYPE_SMALLER: n1 = (i < 0); break;
4226 case TYPE_SEQUAL: n1 = (i <= 0); break;
4227
4228 case TYPE_MATCH:
4229 case TYPE_NOMATCH:
4230 /* avoid 'l' flag in 'cpoptions' */
4231 save_cpo = p_cpo;
4232 p_cpo = (char_u *)"";
4233 regmatch.regprog = vim_regcomp(s2,
4234 RE_MAGIC + RE_STRING);
4235 regmatch.rm_ic = ic;
4236 if (regmatch.regprog != NULL)
4237 {
4238 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4239 vim_free(regmatch.regprog);
4240 if (type == TYPE_NOMATCH)
4241 n1 = !n1;
4242 }
4243 p_cpo = save_cpo;
4244 break;
4245
4246 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4247 }
4248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
4250 clear_tv(&var2);
4251 rettv->v_type = VAR_NUMBER;
4252 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * Handle fourth level expression:
4261 * + number addition
4262 * - number subtraction
4263 * . string concatenation
4264 *
4265 * "arg" must point to the first non-white of the expression.
4266 * "arg" is advanced to the next non-white after the recognized expression.
4267 *
4268 * Return OK or FAIL.
4269 */
4270 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int evaluate;
4275{
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T var2;
4277 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int op;
4279 long n1, n2;
4280 char_u *s1, *s2;
4281 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4282 char_u *p;
4283
4284 /*
4285 * Get the first variable.
4286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289
4290 /*
4291 * Repeat computing, until no '+', '-' or '.' is following.
4292 */
4293 for (;;)
4294 {
4295 op = **arg;
4296 if (op != '+' && op != '-' && op != '.')
4297 break;
4298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299 if (op != '+' || rettv->v_type != VAR_LIST)
4300 {
4301 /* For "list + ...", an illegal use of the first operand as
4302 * a number cannot be determined before evaluating the 2nd
4303 * operand: if this is also a list, all is ok.
4304 * For "something . ...", "something - ..." or "non-list + ...",
4305 * we know that the first operand needs to be a string or number
4306 * without evaluating the 2nd operand. So check before to avoid
4307 * side effects after an error. */
4308 if (evaluate && get_tv_string_chk(rettv) == NULL)
4309 {
4310 clear_tv(rettv);
4311 return FAIL;
4312 }
4313 }
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 /*
4316 * Get the second variable.
4317 */
4318 *arg = skipwhite(*arg + 1);
4319 if (eval6(arg, &var2, evaluate) == FAIL)
4320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 return FAIL;
4323 }
4324
4325 if (evaluate)
4326 {
4327 /*
4328 * Compute the result.
4329 */
4330 if (op == '.')
4331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4333 s2 = get_tv_string_buf_chk(&var2, buf2);
4334 if (s2 == NULL) /* type error ? */
4335 {
4336 clear_tv(rettv);
4337 clear_tv(&var2);
4338 return FAIL;
4339 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004340 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(rettv);
4342 rettv->v_type = VAR_STRING;
4343 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004345 else if (op == '+' && rettv->v_type == VAR_LIST
4346 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004347 {
4348 /* concatenate Lists */
4349 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4350 &var3) == FAIL)
4351 {
4352 clear_tv(rettv);
4353 clear_tv(&var2);
4354 return FAIL;
4355 }
4356 clear_tv(rettv);
4357 *rettv = var3;
4358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 else
4360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 int error = FALSE;
4362
4363 n1 = get_tv_number_chk(rettv, &error);
4364 if (error)
4365 {
4366 /* This can only happen for "list + non-list".
4367 * For "non-list + ..." or "something - ...", we returned
4368 * before evaluating the 2nd operand. */
4369 clear_tv(rettv);
4370 return FAIL;
4371 }
4372 n2 = get_tv_number_chk(&var2, &error);
4373 if (error)
4374 {
4375 clear_tv(rettv);
4376 clear_tv(&var2);
4377 return FAIL;
4378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004379 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 if (op == '+')
4381 n1 = n1 + n2;
4382 else
4383 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 rettv->v_type = VAR_NUMBER;
4385 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 }
4390 return OK;
4391}
4392
4393/*
4394 * Handle fifth level expression:
4395 * * number multiplication
4396 * / number division
4397 * % number modulo
4398 *
4399 * "arg" must point to the first non-white of the expression.
4400 * "arg" is advanced to the next non-white after the recognized expression.
4401 *
4402 * Return OK or FAIL.
4403 */
4404 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 int evaluate;
4409{
Bram Moolenaar33570922005-01-25 22:26:29 +00004410 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 int op;
4412 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415 /*
4416 * Get the first variable.
4417 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004418 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 return FAIL;
4420
4421 /*
4422 * Repeat computing, until no '*', '/' or '%' is following.
4423 */
4424 for (;;)
4425 {
4426 op = **arg;
4427 if (op != '*' && op != '/' && op != '%')
4428 break;
4429
4430 if (evaluate)
4431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004432 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004433 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004434 if (error)
4435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 else
4438 n1 = 0;
4439
4440 /*
4441 * Get the second variable.
4442 */
4443 *arg = skipwhite(*arg + 1);
4444 if (eval7(arg, &var2, evaluate) == FAIL)
4445 return FAIL;
4446
4447 if (evaluate)
4448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004449 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004451 if (error)
4452 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 /*
4455 * Compute the result.
4456 */
4457 if (op == '*')
4458 n1 = n1 * n2;
4459 else if (op == '/')
4460 {
4461 if (n2 == 0) /* give an error message? */
4462 n1 = 0x7fffffffL;
4463 else
4464 n1 = n1 / n2;
4465 }
4466 else
4467 {
4468 if (n2 == 0) /* give an error message? */
4469 n1 = 0;
4470 else
4471 n1 = n1 % n2;
4472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 rettv->v_type = VAR_NUMBER;
4474 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 }
4477
4478 return OK;
4479}
4480
4481/*
4482 * Handle sixth level expression:
4483 * number number constant
4484 * "string" string contstant
4485 * 'string' literal string contstant
4486 * &option-name option value
4487 * @r register contents
4488 * identifier variable value
4489 * function() function call
4490 * $VAR environment variable
4491 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004492 * [expr, expr] List
4493 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 *
4495 * Also handle:
4496 * ! in front logical NOT
4497 * - in front unary minus
4498 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004499 * trailing [] subscript in String or List
4500 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 *
4502 * "arg" must point to the first non-white of the expression.
4503 * "arg" is advanced to the next non-white after the recognized expression.
4504 *
4505 * Return OK or FAIL.
4506 */
4507 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004508eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 int evaluate;
4512{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 long n;
4514 int len;
4515 char_u *s;
4516 int val;
4517 char_u *start_leader, *end_leader;
4518 int ret = OK;
4519 char_u *alias;
4520
4521 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004523 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004525 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527 /*
4528 * Skip '!' and '-' characters. They are handled later.
4529 */
4530 start_leader = *arg;
4531 while (**arg == '!' || **arg == '-' || **arg == '+')
4532 *arg = skipwhite(*arg + 1);
4533 end_leader = *arg;
4534
4535 switch (**arg)
4536 {
4537 /*
4538 * Number constant.
4539 */
4540 case '0':
4541 case '1':
4542 case '2':
4543 case '3':
4544 case '4':
4545 case '5':
4546 case '6':
4547 case '7':
4548 case '8':
4549 case '9':
4550 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4551 *arg += len;
4552 if (evaluate)
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 rettv->v_type = VAR_NUMBER;
4555 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 break;
4558
4559 /*
4560 * String constant: "string".
4561 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 break;
4564
4565 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004566 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004568 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 break;
4570
4571 /*
4572 * List: [expr, expr]
4573 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 break;
4576
4577 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004578 * Dictionary: {key: val, key: val}
4579 */
4580 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4581 break;
4582
4583 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004584 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004586 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 break;
4588
4589 /*
4590 * Environment variable: $VAR.
4591 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 break;
4594
4595 /*
4596 * Register contents: @r.
4597 */
4598 case '@': ++*arg;
4599 if (evaluate)
4600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004602 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604 if (**arg != NUL)
4605 ++*arg;
4606 break;
4607
4608 /*
4609 * nested expression: (expression).
4610 */
4611 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (**arg == ')')
4614 ++*arg;
4615 else if (ret == OK)
4616 {
4617 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 ret = FAIL;
4620 }
4621 break;
4622
Bram Moolenaar8c711452005-01-14 21:53:12 +00004623 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 break;
4625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626
4627 if (ret == NOTDONE)
4628 {
4629 /*
4630 * Must be a variable or function name.
4631 * Can also be a curly-braces kind of name: {expr}.
4632 */
4633 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004634 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 if (alias != NULL)
4636 s = alias;
4637
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004638 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 ret = FAIL;
4640 else
4641 {
4642 if (**arg == '(') /* recursive! */
4643 {
4644 /* If "s" is the name of a variable of type VAR_FUNC
4645 * use its contents. */
4646 s = deref_func_name(s, &len);
4647
4648 /* Invoke the function. */
4649 ret = get_func_tv(s, len, rettv, arg,
4650 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004651 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004652 /* Stop the expression evaluation when immediately
4653 * aborting on error, or when an interrupt occurred or
4654 * an exception was thrown but not caught. */
4655 if (aborting())
4656 {
4657 if (ret == OK)
4658 clear_tv(rettv);
4659 ret = FAIL;
4660 }
4661 }
4662 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004663 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004664 else
4665 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 }
4667
4668 if (alias != NULL)
4669 vim_free(alias);
4670 }
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 *arg = skipwhite(*arg);
4673
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004674 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4675 * expr(expr). */
4676 if (ret == OK)
4677 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /*
4680 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4681 */
4682 if (ret == OK && evaluate && end_leader > start_leader)
4683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004684 int error = FALSE;
4685
4686 val = get_tv_number_chk(rettv, &error);
4687 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 clear_tv(rettv);
4690 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 else
4693 {
4694 while (end_leader > start_leader)
4695 {
4696 --end_leader;
4697 if (*end_leader == '!')
4698 val = !val;
4699 else if (*end_leader == '-')
4700 val = -val;
4701 }
4702 clear_tv(rettv);
4703 rettv->v_type = VAR_NUMBER;
4704 rettv->vval.v_number = val;
4705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707
4708 return ret;
4709}
4710
4711/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004712 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4713 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4715 */
4716 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004717eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004721 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722{
4723 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004724 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726 long len = -1;
4727 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004733 if (verbose)
4734 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 return FAIL;
4736 }
4737
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 /*
4741 * dict.name
4742 */
4743 key = *arg + 1;
4744 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4745 ;
4746 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004748 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 }
4750 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 /*
4753 * something[idx]
4754 *
4755 * Get the (first) variable from inside the [].
4756 */
4757 *arg = skipwhite(*arg + 1);
4758 if (**arg == ':')
4759 empty1 = TRUE;
4760 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4761 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4763 {
4764 /* not a number or string */
4765 clear_tv(&var1);
4766 return FAIL;
4767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004768
4769 /*
4770 * Get the second variable from inside the [:].
4771 */
4772 if (**arg == ':')
4773 {
4774 range = TRUE;
4775 *arg = skipwhite(*arg + 1);
4776 if (**arg == ']')
4777 empty2 = TRUE;
4778 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 if (!empty1)
4781 clear_tv(&var1);
4782 return FAIL;
4783 }
4784 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4785 {
4786 /* not a number or string */
4787 if (!empty1)
4788 clear_tv(&var1);
4789 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 return FAIL;
4791 }
4792 }
4793
4794 /* Check for the ']'. */
4795 if (**arg != ']')
4796 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004797 if (verbose)
4798 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 clear_tv(&var1);
4800 if (range)
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
4804 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 }
4806
4807 if (evaluate)
4808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 n1 = 0;
4810 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 n1 = get_tv_number(&var1);
4813 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 }
4815 if (range)
4816 {
4817 if (empty2)
4818 n2 = -1;
4819 else
4820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 n2 = get_tv_number(&var2);
4822 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 }
4824 }
4825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004826 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 {
4828 case VAR_NUMBER:
4829 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 len = (long)STRLEN(s);
4832 if (range)
4833 {
4834 /* The resulting variable is a substring. If the indexes
4835 * are out of range the result is empty. */
4836 if (n1 < 0)
4837 {
4838 n1 = len + n1;
4839 if (n1 < 0)
4840 n1 = 0;
4841 }
4842 if (n2 < 0)
4843 n2 = len + n2;
4844 else if (n2 >= len)
4845 n2 = len;
4846 if (n1 >= len || n2 < 0 || n1 > n2)
4847 s = NULL;
4848 else
4849 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4850 }
4851 else
4852 {
4853 /* The resulting variable is a string of a single
4854 * character. If the index is too big or negative the
4855 * result is empty. */
4856 if (n1 >= len || n1 < 0)
4857 s = NULL;
4858 else
4859 s = vim_strnsave(s + n1, 1);
4860 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004861 clear_tv(rettv);
4862 rettv->v_type = VAR_STRING;
4863 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 break;
4865
4866 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004867 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 if (n1 < 0)
4869 n1 = len + n1;
4870 if (!empty1 && (n1 < 0 || n1 >= len))
4871 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004872 /* For a range we allow invalid values and return an empty
4873 * list. A list index out of range is an error. */
4874 if (!range)
4875 {
4876 if (verbose)
4877 EMSGN(_(e_listidx), n1);
4878 return FAIL;
4879 }
4880 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 }
4882 if (range)
4883 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004884 list_T *l;
4885 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886
4887 if (n2 < 0)
4888 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004889 else if (n2 >= len)
4890 n2 = len - 1;
4891 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004892 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 l = list_alloc();
4894 if (l == NULL)
4895 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004897 n1 <= n2; ++n1)
4898 {
4899 if (list_append_tv(l, &item->li_tv) == FAIL)
4900 {
4901 list_free(l);
4902 return FAIL;
4903 }
4904 item = item->li_next;
4905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 clear_tv(rettv);
4907 rettv->v_type = VAR_LIST;
4908 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004909 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910 }
4911 else
4912 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004913 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
4915 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 }
4917 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004918
4919 case VAR_DICT:
4920 if (range)
4921 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004922 if (verbose)
4923 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 if (len == -1)
4925 clear_tv(&var1);
4926 return FAIL;
4927 }
4928 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004929 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930
4931 if (len == -1)
4932 {
4933 key = get_tv_string(&var1);
4934 if (*key == NUL)
4935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004936 if (verbose)
4937 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 clear_tv(&var1);
4939 return FAIL;
4940 }
4941 }
4942
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004943 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004945 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004946 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 if (len == -1)
4948 clear_tv(&var1);
4949 if (item == NULL)
4950 return FAIL;
4951
4952 copy_tv(&item->di_tv, &var1);
4953 clear_tv(rettv);
4954 *rettv = var1;
4955 }
4956 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 }
4958 }
4959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 return OK;
4961}
4962
4963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 * Get an option value.
4965 * "arg" points to the '&' or '+' before the option name.
4966 * "arg" is advanced to character after the option name.
4967 * Return OK or FAIL.
4968 */
4969 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004970get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004972 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 int evaluate;
4974{
4975 char_u *option_end;
4976 long numval;
4977 char_u *stringval;
4978 int opt_type;
4979 int c;
4980 int working = (**arg == '+'); /* has("+option") */
4981 int ret = OK;
4982 int opt_flags;
4983
4984 /*
4985 * Isolate the option name and find its value.
4986 */
4987 option_end = find_option_end(arg, &opt_flags);
4988 if (option_end == NULL)
4989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 EMSG2(_("E112: Option name missing: %s"), *arg);
4992 return FAIL;
4993 }
4994
4995 if (!evaluate)
4996 {
4997 *arg = option_end;
4998 return OK;
4999 }
5000
5001 c = *option_end;
5002 *option_end = NUL;
5003 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 if (opt_type == -3) /* invalid name */
5007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005008 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 EMSG2(_("E113: Unknown option: %s"), *arg);
5010 ret = FAIL;
5011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005012 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
5014 if (opt_type == -2) /* hidden string option */
5015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 rettv->v_type = VAR_STRING;
5017 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 else if (opt_type == -1) /* hidden number option */
5020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005021 rettv->v_type = VAR_NUMBER;
5022 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 else if (opt_type == 1) /* number option */
5025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005026 rettv->v_type = VAR_NUMBER;
5027 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029 else /* string option */
5030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 rettv->v_type = VAR_STRING;
5032 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034 }
5035 else if (working && (opt_type == -2 || opt_type == -1))
5036 ret = FAIL;
5037
5038 *option_end = c; /* put back for error messages */
5039 *arg = option_end;
5040
5041 return ret;
5042}
5043
5044/*
5045 * Allocate a variable for a string constant.
5046 * Return OK or FAIL.
5047 */
5048 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005049get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 int evaluate;
5053{
5054 char_u *p;
5055 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int extra = 0;
5057
5058 /*
5059 * Find the end of the string, skipping backslashed characters.
5060 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 if (*p == '\\' && p[1] != NUL)
5064 {
5065 ++p;
5066 /* A "\<x>" form occupies at least 4 characters, and produces up
5067 * to 6 characters: reserve space for 2 extra */
5068 if (*p == '<')
5069 extra += 2;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072
5073 if (*p != '"')
5074 {
5075 EMSG2(_("E114: Missing quote: %s"), *arg);
5076 return FAIL;
5077 }
5078
5079 /* If only parsing, set *arg and return here */
5080 if (!evaluate)
5081 {
5082 *arg = p + 1;
5083 return OK;
5084 }
5085
5086 /*
5087 * Copy the string into allocated memory, handling backslashed
5088 * characters.
5089 */
5090 name = alloc((unsigned)(p - *arg + extra));
5091 if (name == NULL)
5092 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 rettv->v_type = VAR_STRING;
5094 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 if (*p == '\\')
5099 {
5100 switch (*++p)
5101 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 case 'b': *name++ = BS; ++p; break;
5103 case 'e': *name++ = ESC; ++p; break;
5104 case 'f': *name++ = FF; ++p; break;
5105 case 'n': *name++ = NL; ++p; break;
5106 case 'r': *name++ = CAR; ++p; break;
5107 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109 case 'X': /* hex: "\x1", "\x12" */
5110 case 'x':
5111 case 'u': /* Unicode: "\u0023" */
5112 case 'U':
5113 if (vim_isxdigit(p[1]))
5114 {
5115 int n, nr;
5116 int c = toupper(*p);
5117
5118 if (c == 'X')
5119 n = 2;
5120 else
5121 n = 4;
5122 nr = 0;
5123 while (--n >= 0 && vim_isxdigit(p[1]))
5124 {
5125 ++p;
5126 nr = (nr << 4) + hex2nr(*p);
5127 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#ifdef FEAT_MBYTE
5130 /* For "\u" store the number according to
5131 * 'encoding'. */
5132 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 else
5135#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 break;
5139
5140 /* octal: "\1", "\12", "\123" */
5141 case '0':
5142 case '1':
5143 case '2':
5144 case '3':
5145 case '4':
5146 case '5':
5147 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 case '7': *name = *p++ - '0';
5149 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 *name = (*name << 3) + *p++ - '0';
5152 if (*p >= '0' && *p <= '7')
5153 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 break;
5157
5158 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (extra != 0)
5161 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164 }
5165 /* FALLTHROUGH */
5166
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169 }
5170 }
5171 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 *arg = p + 1;
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 return OK;
5179}
5180
5181/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 * Return OK or FAIL.
5184 */
5185 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 int evaluate;
5190{
5191 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005192 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005193 int reduce = 0;
5194
5195 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005197 */
5198 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5199 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005201 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005203 break;
5204 ++reduce;
5205 ++p;
5206 }
5207 }
5208
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005210 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005212 return FAIL;
5213 }
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005216 if (!evaluate)
5217 {
5218 *arg = p + 1;
5219 return OK;
5220 }
5221
5222 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005224 */
5225 str = alloc((unsigned)((p - *arg) - reduce));
5226 if (str == NULL)
5227 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 rettv->v_type = VAR_STRING;
5229 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005230
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005232 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005234 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005236 break;
5237 ++p;
5238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005242 *arg = p + 1;
5243
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005244 return OK;
5245}
5246
5247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 * Allocate a variable for a List and fill it from "*arg".
5249 * Return OK or FAIL.
5250 */
5251 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005254 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 int evaluate;
5256{
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 list_T *l = NULL;
5258 typval_T tv;
5259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260
5261 if (evaluate)
5262 {
5263 l = list_alloc();
5264 if (l == NULL)
5265 return FAIL;
5266 }
5267
5268 *arg = skipwhite(*arg + 1);
5269 while (**arg != ']' && **arg != NUL)
5270 {
5271 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5272 goto failret;
5273 if (evaluate)
5274 {
5275 item = listitem_alloc();
5276 if (item != NULL)
5277 {
5278 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005279 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 list_append(l, item);
5281 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005282 else
5283 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 }
5285
5286 if (**arg == ']')
5287 break;
5288 if (**arg != ',')
5289 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 goto failret;
5292 }
5293 *arg = skipwhite(*arg + 1);
5294 }
5295
5296 if (**arg != ']')
5297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299failret:
5300 if (evaluate)
5301 list_free(l);
5302 return FAIL;
5303 }
5304
5305 *arg = skipwhite(*arg + 1);
5306 if (evaluate)
5307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 rettv->v_type = VAR_LIST;
5309 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 ++l->lv_refcount;
5311 }
5312
5313 return OK;
5314}
5315
5316/*
5317 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005318 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005320 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321list_alloc()
5322{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005323 list_T *l;
5324
5325 l = (list_T *)alloc_clear(sizeof(list_T));
5326 if (l != NULL)
5327 {
5328 /* Prepend the list to the list of lists for garbage collection. */
5329 if (first_list != NULL)
5330 first_list->lv_used_prev = l;
5331 l->lv_used_prev = NULL;
5332 l->lv_used_next = first_list;
5333 first_list = l;
5334 }
5335 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336}
5337
5338/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005339 * Allocate an empty list for a return value.
5340 * Returns OK or FAIL.
5341 */
5342 static int
5343rettv_list_alloc(rettv)
5344 typval_T *rettv;
5345{
5346 list_T *l = list_alloc();
5347
5348 if (l == NULL)
5349 return FAIL;
5350
5351 rettv->vval.v_list = l;
5352 rettv->v_type = VAR_LIST;
5353 ++l->lv_refcount;
5354 return OK;
5355}
5356
5357/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 * Unreference a list: decrement the reference count and free it when it
5359 * becomes zero.
5360 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005361 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005365 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5366 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367}
5368
5369/*
5370 * Free a list, including all items it points to.
5371 * Ignores the reference count.
5372 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005373 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005375 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376{
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378
Bram Moolenaard9fba312005-06-26 22:34:35 +00005379 /* Avoid that recursive reference to the list frees us again. */
5380 l->lv_refcount = DEL_REFCOUNT;
5381
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005382 /* Remove the list from the list of lists for garbage collection. */
5383 if (l->lv_used_prev == NULL)
5384 first_list = l->lv_used_next;
5385 else
5386 l->lv_used_prev->lv_used_next = l->lv_used_next;
5387 if (l->lv_used_next != NULL)
5388 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5389
Bram Moolenaard9fba312005-06-26 22:34:35 +00005390 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005392 /* Remove the item before deleting it. */
5393 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 listitem_free(item);
5395 }
5396 vim_free(l);
5397}
5398
5399/*
5400 * Allocate a list item.
5401 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403listitem_alloc()
5404{
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406}
5407
5408/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005409 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 */
5411 static void
5412listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 vim_free(item);
5417}
5418
5419/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005420 * Remove a list item from a List and free it. Also clears the value.
5421 */
5422 static void
5423listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005424 list_T *l;
5425 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005426{
5427 list_remove(l, item, item);
5428 listitem_free(item);
5429}
5430
5431/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 * Get the number of items in a list.
5433 */
5434 static long
5435list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 if (l == NULL)
5439 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441}
5442
5443/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005444 * Return TRUE when two lists have exactly the same values.
5445 */
5446 static int
5447list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005448 list_T *l1;
5449 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005450 int ic; /* ignore case for strings */
5451{
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005453
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005454 if (list_len(l1) != list_len(l2))
5455 return FALSE;
5456
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005457 for (item1 = l1->lv_first, item2 = l2->lv_first;
5458 item1 != NULL && item2 != NULL;
5459 item1 = item1->li_next, item2 = item2->li_next)
5460 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5461 return FALSE;
5462 return item1 == NULL && item2 == NULL;
5463}
5464
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005465#if defined(FEAT_PYTHON) || defined(PROTO)
5466/*
5467 * Return the dictitem that an entry in a hashtable points to.
5468 */
5469 dictitem_T *
5470dict_lookup(hi)
5471 hashitem_T *hi;
5472{
5473 return HI2DI(hi);
5474}
5475#endif
5476
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005477/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005478 * Return TRUE when two dictionaries have exactly the same key/values.
5479 */
5480 static int
5481dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005482 dict_T *d1;
5483 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005484 int ic; /* ignore case for strings */
5485{
Bram Moolenaar33570922005-01-25 22:26:29 +00005486 hashitem_T *hi;
5487 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005488 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005489
5490 if (dict_len(d1) != dict_len(d2))
5491 return FALSE;
5492
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005493 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005496 if (!HASHITEM_EMPTY(hi))
5497 {
5498 item2 = dict_find(d2, hi->hi_key, -1);
5499 if (item2 == NULL)
5500 return FALSE;
5501 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5502 return FALSE;
5503 --todo;
5504 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005505 }
5506 return TRUE;
5507}
5508
5509/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005510 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005511 * Compares the items just like "==" would compare them, but strings and
5512 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513 */
5514 static int
5515tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005516 typval_T *tv1;
5517 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005518 int ic; /* ignore case */
5519{
5520 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005521 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005522
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005523 if (tv1->v_type != tv2->v_type)
5524 return FALSE;
5525
5526 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005527 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005528 case VAR_LIST:
5529 /* recursive! */
5530 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5531
5532 case VAR_DICT:
5533 /* recursive! */
5534 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5535
5536 case VAR_FUNC:
5537 return (tv1->vval.v_string != NULL
5538 && tv2->vval.v_string != NULL
5539 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5540
5541 case VAR_NUMBER:
5542 return tv1->vval.v_number == tv2->vval.v_number;
5543
5544 case VAR_STRING:
5545 s1 = get_tv_string_buf(tv1, buf1);
5546 s2 = get_tv_string_buf(tv2, buf2);
5547 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005549
5550 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005551 return TRUE;
5552}
5553
5554/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 * Locate item with index "n" in list "l" and return it.
5556 * A negative index is counted from the end; -1 is the last item.
5557 * Returns NULL when "n" is out of range.
5558 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005559 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005561 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 long n;
5563{
Bram Moolenaar33570922005-01-25 22:26:29 +00005564 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 long idx;
5566
5567 if (l == NULL)
5568 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005569
5570 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005572 n = l->lv_len + n;
5573
5574 /* Check for index out of range. */
5575 if (n < 0 || n >= l->lv_len)
5576 return NULL;
5577
5578 /* When there is a cached index may start search from there. */
5579 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005581 if (n < l->lv_idx / 2)
5582 {
5583 /* closest to the start of the list */
5584 item = l->lv_first;
5585 idx = 0;
5586 }
5587 else if (n > (l->lv_idx + l->lv_len) / 2)
5588 {
5589 /* closest to the end of the list */
5590 item = l->lv_last;
5591 idx = l->lv_len - 1;
5592 }
5593 else
5594 {
5595 /* closest to the cached index */
5596 item = l->lv_idx_item;
5597 idx = l->lv_idx;
5598 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 }
5600 else
5601 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005602 if (n < l->lv_len / 2)
5603 {
5604 /* closest to the start of the list */
5605 item = l->lv_first;
5606 idx = 0;
5607 }
5608 else
5609 {
5610 /* closest to the end of the list */
5611 item = l->lv_last;
5612 idx = l->lv_len - 1;
5613 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005615
5616 while (n > idx)
5617 {
5618 /* search forward */
5619 item = item->li_next;
5620 ++idx;
5621 }
5622 while (n < idx)
5623 {
5624 /* search backward */
5625 item = item->li_prev;
5626 --idx;
5627 }
5628
5629 /* cache the used index */
5630 l->lv_idx = idx;
5631 l->lv_idx_item = item;
5632
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 return item;
5634}
5635
5636/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005637 * Get list item "l[idx]" as a number.
5638 */
5639 static long
5640list_find_nr(l, idx, errorp)
5641 list_T *l;
5642 long idx;
5643 int *errorp; /* set to TRUE when something wrong */
5644{
5645 listitem_T *li;
5646
5647 li = list_find(l, idx);
5648 if (li == NULL)
5649 {
5650 if (errorp != NULL)
5651 *errorp = TRUE;
5652 return -1L;
5653 }
5654 return get_tv_number_chk(&li->li_tv, errorp);
5655}
5656
5657/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005658 * Locate "item" list "l" and return its index.
5659 * Returns -1 when "item" is not in the list.
5660 */
5661 static long
5662list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005663 list_T *l;
5664 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005665{
5666 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005668
5669 if (l == NULL)
5670 return -1;
5671 idx = 0;
5672 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5673 ++idx;
5674 if (li == NULL)
5675 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005676 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005677}
5678
5679/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 * Append item "item" to the end of list "l".
5681 */
5682 static void
5683list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005684 list_T *l;
5685 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686{
5687 if (l->lv_last == NULL)
5688 {
5689 /* empty list */
5690 l->lv_first = item;
5691 l->lv_last = item;
5692 item->li_prev = NULL;
5693 }
5694 else
5695 {
5696 l->lv_last->li_next = item;
5697 item->li_prev = l->lv_last;
5698 l->lv_last = item;
5699 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005700 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701 item->li_next = NULL;
5702}
5703
5704/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005705 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005706 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005707 */
5708 static int
5709list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005710 list_T *l;
5711 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005713 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714
Bram Moolenaar05159a02005-02-26 23:04:13 +00005715 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005717 copy_tv(tv, &li->li_tv);
5718 list_append(l, li);
5719 return OK;
5720}
5721
5722/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005723 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005724 * Return FAIL when out of memory.
5725 */
5726 int
5727list_append_dict(list, dict)
5728 list_T *list;
5729 dict_T *dict;
5730{
5731 listitem_T *li = listitem_alloc();
5732
5733 if (li == NULL)
5734 return FAIL;
5735 li->li_tv.v_type = VAR_DICT;
5736 li->li_tv.v_lock = 0;
5737 li->li_tv.vval.v_dict = dict;
5738 list_append(list, li);
5739 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 return OK;
5741}
5742
5743/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005744 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005745 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005746 * Returns FAIL when out of memory.
5747 */
5748 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005749list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005750 list_T *l;
5751 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005752 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005753{
5754 listitem_T *li = listitem_alloc();
5755
5756 if (li == NULL)
5757 return FAIL;
5758 list_append(l, li);
5759 li->li_tv.v_type = VAR_STRING;
5760 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005761 if (str == NULL)
5762 li->li_tv.vval.v_string = NULL;
5763 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005764 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005765 return FAIL;
5766 return OK;
5767}
5768
5769/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005770 * Append "n" to list "l".
5771 * Returns FAIL when out of memory.
5772 */
5773 static int
5774list_append_number(l, n)
5775 list_T *l;
5776 varnumber_T n;
5777{
5778 listitem_T *li;
5779
5780 li = listitem_alloc();
5781 if (li == NULL)
5782 return FAIL;
5783 li->li_tv.v_type = VAR_NUMBER;
5784 li->li_tv.v_lock = 0;
5785 li->li_tv.vval.v_number = n;
5786 list_append(l, li);
5787 return OK;
5788}
5789
5790/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005791 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005792 * If "item" is NULL append at the end.
5793 * Return FAIL when out of memory.
5794 */
5795 static int
5796list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005797 list_T *l;
5798 typval_T *tv;
5799 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005800{
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005802
5803 if (ni == NULL)
5804 return FAIL;
5805 copy_tv(tv, &ni->li_tv);
5806 if (item == NULL)
5807 /* Append new item at end of list. */
5808 list_append(l, ni);
5809 else
5810 {
5811 /* Insert new item before existing item. */
5812 ni->li_prev = item->li_prev;
5813 ni->li_next = item;
5814 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005815 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005816 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005817 ++l->lv_idx;
5818 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005819 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005820 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005821 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005822 l->lv_idx_item = NULL;
5823 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005824 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005825 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005826 }
5827 return OK;
5828}
5829
5830/*
5831 * Extend "l1" with "l2".
5832 * If "bef" is NULL append at the end, otherwise insert before this item.
5833 * Returns FAIL when out of memory.
5834 */
5835 static int
5836list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 list_T *l1;
5838 list_T *l2;
5839 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840{
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842
5843 for (item = l2->lv_first; item != NULL; item = item->li_next)
5844 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5845 return FAIL;
5846 return OK;
5847}
5848
5849/*
5850 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5851 * Return FAIL when out of memory.
5852 */
5853 static int
5854list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 list_T *l1;
5856 list_T *l2;
5857 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005858{
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005860
5861 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005862 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863 if (l == NULL)
5864 return FAIL;
5865 tv->v_type = VAR_LIST;
5866 tv->vval.v_list = l;
5867
5868 /* append all items from the second list */
5869 return list_extend(l, l2, NULL);
5870}
5871
5872/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005873 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005875 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 * Returns NULL when out of memory.
5877 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005882 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883{
Bram Moolenaar33570922005-01-25 22:26:29 +00005884 list_T *copy;
5885 listitem_T *item;
5886 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887
5888 if (orig == NULL)
5889 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890
5891 copy = list_alloc();
5892 if (copy != NULL)
5893 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005894 if (copyID != 0)
5895 {
5896 /* Do this before adding the items, because one of the items may
5897 * refer back to this list. */
5898 orig->lv_copyID = copyID;
5899 orig->lv_copylist = copy;
5900 }
5901 for (item = orig->lv_first; item != NULL && !got_int;
5902 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 {
5904 ni = listitem_alloc();
5905 if (ni == NULL)
5906 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005907 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005908 {
5909 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5910 {
5911 vim_free(ni);
5912 break;
5913 }
5914 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005916 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 list_append(copy, ni);
5918 }
5919 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005920 if (item != NULL)
5921 {
5922 list_unref(copy);
5923 copy = NULL;
5924 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 }
5926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 return copy;
5928}
5929
5930/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005932 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005934 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005935list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
5937 listitem_T *item;
5938 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005939{
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005942 /* notify watchers */
5943 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005945 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005946 list_fix_watch(l, ip);
5947 if (ip == item2)
5948 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005950
5951 if (item2->li_next == NULL)
5952 l->lv_last = item->li_prev;
5953 else
5954 item2->li_next->li_prev = item->li_prev;
5955 if (item->li_prev == NULL)
5956 l->lv_first = item2->li_next;
5957 else
5958 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005959 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960}
5961
5962/*
5963 * Return an allocated string with the string representation of a list.
5964 * May return NULL.
5965 */
5966 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005967list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005968 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005969 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970{
5971 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972
5973 if (tv->vval.v_list == NULL)
5974 return NULL;
5975 ga_init2(&ga, (int)sizeof(char), 80);
5976 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005977 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005978 {
5979 vim_free(ga.ga_data);
5980 return NULL;
5981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 ga_append(&ga, ']');
5983 ga_append(&ga, NUL);
5984 return (char_u *)ga.ga_data;
5985}
5986
5987/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005988 * Join list "l" into a string in "*gap", using separator "sep".
5989 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005990 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005992 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005993list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005994 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996 char_u *sep;
5997 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005998 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005999{
6000 int first = TRUE;
6001 char_u *tofree;
6002 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006004 char_u *s;
6005
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006006 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006007 {
6008 if (first)
6009 first = FALSE;
6010 else
6011 ga_concat(gap, sep);
6012
6013 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006014 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006016 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006017 if (s != NULL)
6018 ga_concat(gap, s);
6019 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006020 if (s == NULL)
6021 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006022 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006023 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006024}
6025
6026/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006027 * Garbage collection for lists and dictionaries.
6028 *
6029 * We use reference counts to be able to free most items right away when they
6030 * are no longer used. But for composite items it's possible that it becomes
6031 * unused while the reference count is > 0: When there is a recursive
6032 * reference. Example:
6033 * :let l = [1, 2, 3]
6034 * :let d = {9: l}
6035 * :let l[1] = d
6036 *
6037 * Since this is quite unusual we handle this with garbage collection: every
6038 * once in a while find out which lists and dicts are not referenced from any
6039 * variable.
6040 *
6041 * Here is a good reference text about garbage collection (refers to Python
6042 * but it applies to all reference-counting mechanisms):
6043 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045
6046/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 * Do garbage collection for lists and dicts.
6048 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006049 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006050 int
6051garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006052{
6053 dict_T *dd;
6054 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 int copyID = ++current_copyID;
6056 buf_T *buf;
6057 win_T *wp;
6058 int i;
6059 funccall_T *fc;
6060 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006061#ifdef FEAT_WINDOWS
6062 tabpage_T *tp;
6063#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006065 /* Only do this once. */
6066 want_garbage_collect = FALSE;
6067 may_garbage_collect = FALSE;
6068
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069 /*
6070 * 1. Go through all accessible variables and mark all lists and dicts
6071 * with copyID.
6072 */
6073 /* script-local variables */
6074 for (i = 1; i <= ga_scripts.ga_len; ++i)
6075 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6076
6077 /* buffer-local variables */
6078 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6079 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6080
6081 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006082 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006083 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6084
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006085#ifdef FEAT_WINDOWS
6086 /* tabpage-local variables */
6087 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6088 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6089#endif
6090
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006091 /* global variables */
6092 set_ref_in_ht(&globvarht, copyID);
6093
6094 /* function-local variables */
6095 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6096 {
6097 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6098 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6099 }
6100
6101 /*
6102 * 2. Go through the list of dicts and free items without the copyID.
6103 */
6104 for (dd = first_dict; dd != NULL; )
6105 if (dd->dv_copyID != copyID)
6106 {
6107 dict_free(dd);
6108 did_free = TRUE;
6109
6110 /* restart, next dict may also have been freed */
6111 dd = first_dict;
6112 }
6113 else
6114 dd = dd->dv_used_next;
6115
6116 /*
6117 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006118 * But don't free a list that has a watcher (used in a for loop), these
6119 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006120 */
6121 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006122 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006123 {
6124 list_free(ll);
6125 did_free = TRUE;
6126
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006127 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006128 ll = first_list;
6129 }
6130 else
6131 ll = ll->lv_used_next;
6132
6133 return did_free;
6134}
6135
6136/*
6137 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6138 */
6139 static void
6140set_ref_in_ht(ht, copyID)
6141 hashtab_T *ht;
6142 int copyID;
6143{
6144 int todo;
6145 hashitem_T *hi;
6146
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006147 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006148 for (hi = ht->ht_array; todo > 0; ++hi)
6149 if (!HASHITEM_EMPTY(hi))
6150 {
6151 --todo;
6152 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6153 }
6154}
6155
6156/*
6157 * Mark all lists and dicts referenced through list "l" with "copyID".
6158 */
6159 static void
6160set_ref_in_list(l, copyID)
6161 list_T *l;
6162 int copyID;
6163{
6164 listitem_T *li;
6165
6166 for (li = l->lv_first; li != NULL; li = li->li_next)
6167 set_ref_in_item(&li->li_tv, copyID);
6168}
6169
6170/*
6171 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6172 */
6173 static void
6174set_ref_in_item(tv, copyID)
6175 typval_T *tv;
6176 int copyID;
6177{
6178 dict_T *dd;
6179 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006180
6181 switch (tv->v_type)
6182 {
6183 case VAR_DICT:
6184 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006185 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006186 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006187 /* Didn't see this dict yet. */
6188 dd->dv_copyID = copyID;
6189 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006190 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006191 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006192
6193 case VAR_LIST:
6194 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006195 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006196 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006197 /* Didn't see this list yet. */
6198 ll->lv_copyID = copyID;
6199 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006200 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006201 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006202 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006203 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006204}
6205
6206/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006207 * Allocate an empty header for a dictionary.
6208 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006209 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006210dict_alloc()
6211{
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006213
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006216 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006217 /* Add the list to the hashtable for garbage collection. */
6218 if (first_dict != NULL)
6219 first_dict->dv_used_prev = d;
6220 d->dv_used_next = first_dict;
6221 d->dv_used_prev = NULL;
6222
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006224 d->dv_lock = 0;
6225 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006226 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006228 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006229}
6230
6231/*
6232 * Unreference a Dictionary: decrement the reference count and free it when it
6233 * becomes zero.
6234 */
6235 static void
6236dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006238{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006239 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6240 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006241}
6242
6243/*
6244 * Free a Dictionary, including all items it contains.
6245 * Ignores the reference count.
6246 */
6247 static void
6248dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006250{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006253 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006254
Bram Moolenaard9fba312005-06-26 22:34:35 +00006255 /* Avoid that recursive reference to the dict frees us again. */
6256 d->dv_refcount = DEL_REFCOUNT;
6257
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006258 /* Remove the dict from the list of dicts for garbage collection. */
6259 if (d->dv_used_prev == NULL)
6260 first_dict = d->dv_used_next;
6261 else
6262 d->dv_used_prev->dv_used_next = d->dv_used_next;
6263 if (d->dv_used_next != NULL)
6264 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6265
6266 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006267 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006268 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006269 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006271 if (!HASHITEM_EMPTY(hi))
6272 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006273 /* Remove the item before deleting it, just in case there is
6274 * something recursive causing trouble. */
6275 di = HI2DI(hi);
6276 hash_remove(&d->dv_hashtab, hi);
6277 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278 --todo;
6279 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006280 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006282 vim_free(d);
6283}
6284
6285/*
6286 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287 * The "key" is copied to the new item.
6288 * Note that the value of the item "di_tv" still needs to be initialized!
6289 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006292dictitem_alloc(key)
6293 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006294{
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006297 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006301 di->di_flags = 0;
6302 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304}
6305
6306/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006307 * Make a copy of a Dictionary item.
6308 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006310dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006312{
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006314
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006315 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6316 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006317 if (di != NULL)
6318 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006319 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006320 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006321 copy_tv(&org->di_tv, &di->di_tv);
6322 }
6323 return di;
6324}
6325
6326/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006327 * Remove item "item" from Dictionary "dict" and free it.
6328 */
6329 static void
6330dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 dict_T *dict;
6332 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006333{
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006335
Bram Moolenaar33570922005-01-25 22:26:29 +00006336 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006337 if (HASHITEM_EMPTY(hi))
6338 EMSG2(_(e_intern2), "dictitem_remove()");
6339 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006341 dictitem_free(item);
6342}
6343
6344/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345 * Free a dict item. Also clears the value.
6346 */
6347 static void
6348dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006350{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351 clear_tv(&item->di_tv);
6352 vim_free(item);
6353}
6354
6355/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006356 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6357 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359 * Returns NULL when out of memory.
6360 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006362dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006364 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006365 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366{
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 dict_T *copy;
6368 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006371
6372 if (orig == NULL)
6373 return NULL;
6374
6375 copy = dict_alloc();
6376 if (copy != NULL)
6377 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006378 if (copyID != 0)
6379 {
6380 orig->dv_copyID = copyID;
6381 orig->dv_copydict = copy;
6382 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006383 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006384 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006386 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006388 --todo;
6389
6390 di = dictitem_alloc(hi->hi_key);
6391 if (di == NULL)
6392 break;
6393 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006394 {
6395 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6396 copyID) == FAIL)
6397 {
6398 vim_free(di);
6399 break;
6400 }
6401 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006402 else
6403 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6404 if (dict_add(copy, di) == FAIL)
6405 {
6406 dictitem_free(di);
6407 break;
6408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006410 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006411
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006413 if (todo > 0)
6414 {
6415 dict_unref(copy);
6416 copy = NULL;
6417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006418 }
6419
6420 return copy;
6421}
6422
6423/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006425 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006427 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 dict_T *d;
6430 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006431{
Bram Moolenaar33570922005-01-25 22:26:29 +00006432 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006433}
6434
Bram Moolenaar8c711452005-01-14 21:53:12 +00006435/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006436 * Add a number or string entry to dictionary "d".
6437 * When "str" is NULL use number "nr", otherwise use "str".
6438 * Returns FAIL when out of memory and when key already exists.
6439 */
6440 int
6441dict_add_nr_str(d, key, nr, str)
6442 dict_T *d;
6443 char *key;
6444 long nr;
6445 char_u *str;
6446{
6447 dictitem_T *item;
6448
6449 item = dictitem_alloc((char_u *)key);
6450 if (item == NULL)
6451 return FAIL;
6452 item->di_tv.v_lock = 0;
6453 if (str == NULL)
6454 {
6455 item->di_tv.v_type = VAR_NUMBER;
6456 item->di_tv.vval.v_number = nr;
6457 }
6458 else
6459 {
6460 item->di_tv.v_type = VAR_STRING;
6461 item->di_tv.vval.v_string = vim_strsave(str);
6462 }
6463 if (dict_add(d, item) == FAIL)
6464 {
6465 dictitem_free(item);
6466 return FAIL;
6467 }
6468 return OK;
6469}
6470
6471/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006472 * Get the number of items in a Dictionary.
6473 */
6474 static long
6475dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006477{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006478 if (d == NULL)
6479 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006480 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481}
6482
6483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 * Find item "key[len]" in Dictionary "d".
6485 * If "len" is negative use strlen(key).
6486 * Returns NULL when not found.
6487 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006489dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006490 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491 char_u *key;
6492 int len;
6493{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006494#define AKEYLEN 200
6495 char_u buf[AKEYLEN];
6496 char_u *akey;
6497 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 if (len < 0)
6501 akey = key;
6502 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006503 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006504 tofree = akey = vim_strnsave(key, len);
6505 if (akey == NULL)
6506 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006507 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006508 else
6509 {
6510 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006511 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006512 akey = buf;
6513 }
6514
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006516 vim_free(tofree);
6517 if (HASHITEM_EMPTY(hi))
6518 return NULL;
6519 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006520}
6521
6522/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006523 * Get a string item from a dictionary.
6524 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006525 * Returns NULL if the entry doesn't exist or out of memory.
6526 */
6527 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006528get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006529 dict_T *d;
6530 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006531 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006532{
6533 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006534 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006535
6536 di = dict_find(d, key, -1);
6537 if (di == NULL)
6538 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006539 s = get_tv_string(&di->di_tv);
6540 if (save && s != NULL)
6541 s = vim_strsave(s);
6542 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006543}
6544
6545/*
6546 * Get a number item from a dictionary.
6547 * Returns 0 if the entry doesn't exist or out of memory.
6548 */
6549 long
6550get_dict_number(d, key)
6551 dict_T *d;
6552 char_u *key;
6553{
6554 dictitem_T *di;
6555
6556 di = dict_find(d, key, -1);
6557 if (di == NULL)
6558 return 0;
6559 return get_tv_number(&di->di_tv);
6560}
6561
6562/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563 * Return an allocated string with the string representation of a Dictionary.
6564 * May return NULL.
6565 */
6566 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006567dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006569 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570{
6571 garray_T ga;
6572 int first = TRUE;
6573 char_u *tofree;
6574 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006576 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006578 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006579
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006580 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006581 return NULL;
6582 ga_init2(&ga, (int)sizeof(char), 80);
6583 ga_append(&ga, '{');
6584
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006585 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006588 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006590 --todo;
6591
6592 if (first)
6593 first = FALSE;
6594 else
6595 ga_concat(&ga, (char_u *)", ");
6596
6597 tofree = string_quote(hi->hi_key, FALSE);
6598 if (tofree != NULL)
6599 {
6600 ga_concat(&ga, tofree);
6601 vim_free(tofree);
6602 }
6603 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006604 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006605 if (s != NULL)
6606 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006607 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 if (s == NULL)
6609 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006611 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006612 if (todo > 0)
6613 {
6614 vim_free(ga.ga_data);
6615 return NULL;
6616 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617
6618 ga_append(&ga, '}');
6619 ga_append(&ga, NUL);
6620 return (char_u *)ga.ga_data;
6621}
6622
6623/*
6624 * Allocate a variable for a Dictionary and fill it from "*arg".
6625 * Return OK or FAIL. Returns NOTDONE for {expr}.
6626 */
6627 static int
6628get_dict_tv(arg, rettv, evaluate)
6629 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006630 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006631 int evaluate;
6632{
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 dict_T *d = NULL;
6634 typval_T tvkey;
6635 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006637 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006638 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006639 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006640
6641 /*
6642 * First check if it's not a curly-braces thing: {expr}.
6643 * Must do this without evaluating, otherwise a function may be called
6644 * twice. Unfortunately this means we need to call eval1() twice for the
6645 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006646 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006648 if (*start != '}')
6649 {
6650 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6651 return FAIL;
6652 if (*start == '}')
6653 return NOTDONE;
6654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655
6656 if (evaluate)
6657 {
6658 d = dict_alloc();
6659 if (d == NULL)
6660 return FAIL;
6661 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006662 tvkey.v_type = VAR_UNKNOWN;
6663 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664
6665 *arg = skipwhite(*arg + 1);
6666 while (**arg != '}' && **arg != NUL)
6667 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006668 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 goto failret;
6670 if (**arg != ':')
6671 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006672 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006673 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006674 goto failret;
6675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006676 key = get_tv_string_buf_chk(&tvkey, buf);
6677 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006679 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6680 if (key != NULL)
6681 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006682 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 goto failret;
6684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685
6686 *arg = skipwhite(*arg + 1);
6687 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006689 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006690 goto failret;
6691 }
6692 if (evaluate)
6693 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006694 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006695 if (item != NULL)
6696 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006697 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006698 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006699 clear_tv(&tv);
6700 goto failret;
6701 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006702 item = dictitem_alloc(key);
6703 clear_tv(&tvkey);
6704 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006706 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006707 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006708 if (dict_add(d, item) == FAIL)
6709 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006710 }
6711 }
6712
6713 if (**arg == '}')
6714 break;
6715 if (**arg != ',')
6716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006717 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006718 goto failret;
6719 }
6720 *arg = skipwhite(*arg + 1);
6721 }
6722
6723 if (**arg != '}')
6724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006725 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006726failret:
6727 if (evaluate)
6728 dict_free(d);
6729 return FAIL;
6730 }
6731
6732 *arg = skipwhite(*arg + 1);
6733 if (evaluate)
6734 {
6735 rettv->v_type = VAR_DICT;
6736 rettv->vval.v_dict = d;
6737 ++d->dv_refcount;
6738 }
6739
6740 return OK;
6741}
6742
Bram Moolenaar8c711452005-01-14 21:53:12 +00006743/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 * Return a string with the string representation of a variable.
6745 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006746 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006747 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006748 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006749 * May return NULL;
6750 */
6751 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006752echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006753 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006754 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006755 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006756 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006758 static int recurse = 0;
6759 char_u *r = NULL;
6760
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006763 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006764 *tofree = NULL;
6765 return NULL;
6766 }
6767 ++recurse;
6768
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006769 switch (tv->v_type)
6770 {
6771 case VAR_FUNC:
6772 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006773 r = tv->vval.v_string;
6774 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006775
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006776 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006777 if (tv->vval.v_list == NULL)
6778 {
6779 *tofree = NULL;
6780 r = NULL;
6781 }
6782 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6783 {
6784 *tofree = NULL;
6785 r = (char_u *)"[...]";
6786 }
6787 else
6788 {
6789 tv->vval.v_list->lv_copyID = copyID;
6790 *tofree = list2string(tv, copyID);
6791 r = *tofree;
6792 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006793 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006794
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006796 if (tv->vval.v_dict == NULL)
6797 {
6798 *tofree = NULL;
6799 r = NULL;
6800 }
6801 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6802 {
6803 *tofree = NULL;
6804 r = (char_u *)"{...}";
6805 }
6806 else
6807 {
6808 tv->vval.v_dict->dv_copyID = copyID;
6809 *tofree = dict2string(tv, copyID);
6810 r = *tofree;
6811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 case VAR_STRING:
6815 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816 *tofree = NULL;
6817 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006818 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006819
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006820 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006821 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006822 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006824
6825 --recurse;
6826 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006827}
6828
6829/*
6830 * Return a string with the string representation of a variable.
6831 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6832 * "numbuf" is used for a number.
6833 * Puts quotes around strings, so that they can be parsed back by eval().
6834 * May return NULL;
6835 */
6836 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006837tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006839 char_u **tofree;
6840 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006841 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006842{
6843 switch (tv->v_type)
6844 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006845 case VAR_FUNC:
6846 *tofree = string_quote(tv->vval.v_string, TRUE);
6847 return *tofree;
6848 case VAR_STRING:
6849 *tofree = string_quote(tv->vval.v_string, FALSE);
6850 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006852 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006853 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006854 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006855 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006856 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006858 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006859}
6860
6861/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006862 * Return string "str" in ' quotes, doubling ' characters.
6863 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006864 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006865 */
6866 static char_u *
6867string_quote(str, function)
6868 char_u *str;
6869 int function;
6870{
Bram Moolenaar33570922005-01-25 22:26:29 +00006871 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006872 char_u *p, *r, *s;
6873
Bram Moolenaar33570922005-01-25 22:26:29 +00006874 len = (function ? 13 : 3);
6875 if (str != NULL)
6876 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006877 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006878 for (p = str; *p != NUL; mb_ptr_adv(p))
6879 if (*p == '\'')
6880 ++len;
6881 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882 s = r = alloc(len);
6883 if (r != NULL)
6884 {
6885 if (function)
6886 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006887 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006888 r += 10;
6889 }
6890 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006892 if (str != NULL)
6893 for (p = str; *p != NUL; )
6894 {
6895 if (*p == '\'')
6896 *r++ = '\'';
6897 MB_COPY_CHAR(p, r);
6898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006899 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 if (function)
6901 *r++ = ')';
6902 *r++ = NUL;
6903 }
6904 return s;
6905}
6906
6907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 * Get the value of an environment variable.
6909 * "arg" is pointing to the '$'. It is advanced to after the name.
6910 * If the environment variable was not set, silently assume it is empty.
6911 * Always return OK.
6912 */
6913 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006914get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 int evaluate;
6918{
6919 char_u *string = NULL;
6920 int len;
6921 int cc;
6922 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006923 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
6925 ++*arg;
6926 name = *arg;
6927 len = get_env_len(arg);
6928 if (evaluate)
6929 {
6930 if (len != 0)
6931 {
6932 cc = name[len];
6933 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006934 /* first try vim_getenv(), fast for normal environment vars */
6935 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006937 {
6938 if (!mustfree)
6939 string = vim_strsave(string);
6940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 else
6942 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006943 if (mustfree)
6944 vim_free(string);
6945
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 /* next try expanding things like $VIM and ${HOME} */
6947 string = expand_env_save(name - 1);
6948 if (string != NULL && *string == '$')
6949 {
6950 vim_free(string);
6951 string = NULL;
6952 }
6953 }
6954 name[len] = cc;
6955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006956 rettv->v_type = VAR_STRING;
6957 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
6959
6960 return OK;
6961}
6962
6963/*
6964 * Array with names and number of arguments of all internal functions
6965 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6966 */
6967static struct fst
6968{
6969 char *f_name; /* function name */
6970 char f_min_argc; /* minimal number of arguments */
6971 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006973 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974} functions[] =
6975{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006976 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"append", 2, 2, f_append},
6978 {"argc", 0, 0, f_argc},
6979 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006980 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006982 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {"bufexists", 1, 1, f_bufexists},
6984 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6985 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6986 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6987 {"buflisted", 1, 1, f_buflisted},
6988 {"bufloaded", 1, 1, f_bufloaded},
6989 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006990 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {"bufwinnr", 1, 1, f_bufwinnr},
6992 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006993 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006995 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {"char2nr", 1, 1, f_char2nr},
6997 {"cindent", 1, 1, f_cindent},
6998 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006999#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007000 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007001 {"complete_add", 1, 1, f_complete_add},
7002 {"complete_check", 0, 0, f_complete_check},
7003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007005 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007006 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007008 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007009 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {"delete", 1, 1, f_delete},
7011 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007012 {"diff_filler", 1, 1, f_diff_filler},
7013 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007014 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007016 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 {"eventhandler", 0, 0, f_eventhandler},
7018 {"executable", 1, 1, f_executable},
7019 {"exists", 1, 1, f_exists},
7020 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007021 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007022 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7024 {"filereadable", 1, 1, f_filereadable},
7025 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007026 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007027 {"finddir", 1, 3, f_finddir},
7028 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"fnamemodify", 2, 2, f_fnamemodify},
7030 {"foldclosed", 1, 1, f_foldclosed},
7031 {"foldclosedend", 1, 1, f_foldclosedend},
7032 {"foldlevel", 1, 1, f_foldlevel},
7033 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007034 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007036 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007038 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007039 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {"getbufvar", 2, 2, f_getbufvar},
7041 {"getchar", 0, 1, f_getchar},
7042 {"getcharmod", 0, 0, f_getcharmod},
7043 {"getcmdline", 0, 0, f_getcmdline},
7044 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007045 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007047 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007048 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {"getfsize", 1, 1, f_getfsize},
7050 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007051 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007052 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007053 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007054 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007055 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007056 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007058 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {"getwinposx", 0, 0, f_getwinposx},
7060 {"getwinposy", 0, 0, f_getwinposy},
7061 {"getwinvar", 2, 2, f_getwinvar},
7062 {"glob", 1, 1, f_glob},
7063 {"globpath", 2, 2, f_globpath},
7064 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007065 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007066 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7068 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7069 {"histadd", 2, 2, f_histadd},
7070 {"histdel", 1, 2, f_histdel},
7071 {"histget", 1, 2, f_histget},
7072 {"histnr", 1, 1, f_histnr},
7073 {"hlID", 1, 1, f_hlID},
7074 {"hlexists", 1, 1, f_hlexists},
7075 {"hostname", 0, 0, f_hostname},
7076 {"iconv", 3, 3, f_iconv},
7077 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007078 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007079 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007081 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {"inputrestore", 0, 0, f_inputrestore},
7083 {"inputsave", 0, 0, f_inputsave},
7084 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007087 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007089 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007092 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {"libcall", 3, 3, f_libcall},
7094 {"libcallnr", 3, 3, f_libcallnr},
7095 {"line", 1, 1, f_line},
7096 {"line2byte", 1, 1, f_line2byte},
7097 {"lispindent", 1, 1, f_lispindent},
7098 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007099 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007100 {"maparg", 1, 3, f_maparg},
7101 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007102 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007103 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007104 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007105 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007106 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007107 {"max", 1, 1, f_max},
7108 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007109#ifdef vim_mkdir
7110 {"mkdir", 1, 3, f_mkdir},
7111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {"mode", 0, 0, f_mode},
7113 {"nextnonblank", 1, 1, f_nextnonblank},
7114 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007115 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007117 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007118 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007120 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007121 {"reltime", 0, 2, f_reltime},
7122 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {"remote_expr", 2, 3, f_remote_expr},
7124 {"remote_foreground", 1, 1, f_remote_foreground},
7125 {"remote_peek", 1, 2, f_remote_peek},
7126 {"remote_read", 1, 1, f_remote_read},
7127 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007128 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007130 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007132 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007133 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007134 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007135 {"searchpair", 3, 6, f_searchpair},
7136 {"searchpairpos", 3, 6, f_searchpairpos},
7137 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {"server2client", 2, 2, f_server2client},
7139 {"serverlist", 0, 0, f_serverlist},
7140 {"setbufvar", 3, 3, f_setbufvar},
7141 {"setcmdpos", 1, 1, f_setcmdpos},
7142 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007143 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007144 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007145 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007147 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {"setwinvar", 3, 3, f_setwinvar},
7149 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007150 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007151 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007152 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007153 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007154 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007155 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156#ifdef HAVE_STRFTIME
7157 {"strftime", 1, 2, f_strftime},
7158#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007160 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"strlen", 1, 1, f_strlen},
7162 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007163 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {"strtrans", 1, 1, f_strtrans},
7165 {"submatch", 1, 1, f_submatch},
7166 {"substitute", 4, 4, f_substitute},
7167 {"synID", 3, 3, f_synID},
7168 {"synIDattr", 2, 3, f_synIDattr},
7169 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007170 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007171 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007172 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007174 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007175 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007177 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {"tolower", 1, 1, f_tolower},
7179 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007180 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {"virtcol", 1, 1, f_virtcol},
7184 {"visualmode", 0, 1, f_visualmode},
7185 {"winbufnr", 1, 1, f_winbufnr},
7186 {"wincol", 0, 0, f_wincol},
7187 {"winheight", 1, 1, f_winheight},
7188 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007189 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007191 {"winrestview", 1, 1, f_winrestview},
7192 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007194 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195};
7196
7197#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7198
7199/*
7200 * Function given to ExpandGeneric() to obtain the list of internal
7201 * or user defined function names.
7202 */
7203 char_u *
7204get_function_name(xp, idx)
7205 expand_T *xp;
7206 int idx;
7207{
7208 static int intidx = -1;
7209 char_u *name;
7210
7211 if (idx == 0)
7212 intidx = -1;
7213 if (intidx < 0)
7214 {
7215 name = get_user_func_name(xp, idx);
7216 if (name != NULL)
7217 return name;
7218 }
7219 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7220 {
7221 STRCPY(IObuff, functions[intidx].f_name);
7222 STRCAT(IObuff, "(");
7223 if (functions[intidx].f_max_argc == 0)
7224 STRCAT(IObuff, ")");
7225 return IObuff;
7226 }
7227
7228 return NULL;
7229}
7230
7231/*
7232 * Function given to ExpandGeneric() to obtain the list of internal or
7233 * user defined variable or function names.
7234 */
7235/*ARGSUSED*/
7236 char_u *
7237get_expr_name(xp, idx)
7238 expand_T *xp;
7239 int idx;
7240{
7241 static int intidx = -1;
7242 char_u *name;
7243
7244 if (idx == 0)
7245 intidx = -1;
7246 if (intidx < 0)
7247 {
7248 name = get_function_name(xp, idx);
7249 if (name != NULL)
7250 return name;
7251 }
7252 return get_user_var_name(xp, ++intidx);
7253}
7254
7255#endif /* FEAT_CMDL_COMPL */
7256
7257/*
7258 * Find internal function in table above.
7259 * Return index, or -1 if not found
7260 */
7261 static int
7262find_internal_func(name)
7263 char_u *name; /* name of the function */
7264{
7265 int first = 0;
7266 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7267 int cmp;
7268 int x;
7269
7270 /*
7271 * Find the function name in the table. Binary search.
7272 */
7273 while (first <= last)
7274 {
7275 x = first + ((unsigned)(last - first) >> 1);
7276 cmp = STRCMP(name, functions[x].f_name);
7277 if (cmp < 0)
7278 last = x - 1;
7279 else if (cmp > 0)
7280 first = x + 1;
7281 else
7282 return x;
7283 }
7284 return -1;
7285}
7286
7287/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007288 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7289 * name it contains, otherwise return "name".
7290 */
7291 static char_u *
7292deref_func_name(name, lenp)
7293 char_u *name;
7294 int *lenp;
7295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 int cc;
7298
7299 cc = name[*lenp];
7300 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007301 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007302 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007304 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007305 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007306 {
7307 *lenp = 0;
7308 return (char_u *)""; /* just in case */
7309 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007310 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007312 }
7313
7314 return name;
7315}
7316
7317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 * Allocate a variable for the result of a function.
7319 * Return OK or FAIL.
7320 */
7321 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7323 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 char_u *name; /* name of the function */
7325 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 char_u **arg; /* argument, pointing to the '(' */
7328 linenr_T firstline; /* first line of range */
7329 linenr_T lastline; /* last line of range */
7330 int *doesrange; /* return: function handled range */
7331 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
7334 char_u *argp;
7335 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007336 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 int argcount = 0; /* number of arguments found */
7338
7339 /*
7340 * Get the arguments.
7341 */
7342 argp = *arg;
7343 while (argcount < MAX_FUNC_ARGS)
7344 {
7345 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7346 if (*argp == ')' || *argp == ',' || *argp == NUL)
7347 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7349 {
7350 ret = FAIL;
7351 break;
7352 }
7353 ++argcount;
7354 if (*argp != ',')
7355 break;
7356 }
7357 if (*argp == ')')
7358 ++argp;
7359 else
7360 ret = FAIL;
7361
7362 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007363 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007364 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 {
7367 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007368 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007370 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372
7373 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007374 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 *arg = skipwhite(argp);
7377 return ret;
7378}
7379
7380
7381/*
7382 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007383 * Return OK when the function can't be called, FAIL otherwise.
7384 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 */
7386 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007387call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 char_u *name; /* name of the function */
7390 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007393 typval_T *argvars; /* vars for arguments, must have "argcount"
7394 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 linenr_T firstline; /* first line of range */
7396 linenr_T lastline; /* last line of range */
7397 int *doesrange; /* return: function handled range */
7398 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
7401 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402#define ERROR_UNKNOWN 0
7403#define ERROR_TOOMANY 1
7404#define ERROR_TOOFEW 2
7405#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406#define ERROR_DICT 4
7407#define ERROR_NONE 5
7408#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 int error = ERROR_NONE;
7410 int i;
7411 int llen;
7412 ufunc_T *fp;
7413 int cc;
7414#define FLEN_FIXED 40
7415 char_u fname_buf[FLEN_FIXED + 1];
7416 char_u *fname;
7417
7418 /*
7419 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7420 * Change <SNR>123_name() to K_SNR 123_name().
7421 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7422 */
7423 cc = name[len];
7424 name[len] = NUL;
7425 llen = eval_fname_script(name);
7426 if (llen > 0)
7427 {
7428 fname_buf[0] = K_SPECIAL;
7429 fname_buf[1] = KS_EXTRA;
7430 fname_buf[2] = (int)KE_SNR;
7431 i = 3;
7432 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7433 {
7434 if (current_SID <= 0)
7435 error = ERROR_SCRIPT;
7436 else
7437 {
7438 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7439 i = (int)STRLEN(fname_buf);
7440 }
7441 }
7442 if (i + STRLEN(name + llen) < FLEN_FIXED)
7443 {
7444 STRCPY(fname_buf + i, name + llen);
7445 fname = fname_buf;
7446 }
7447 else
7448 {
7449 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7450 if (fname == NULL)
7451 error = ERROR_OTHER;
7452 else
7453 {
7454 mch_memmove(fname, fname_buf, (size_t)i);
7455 STRCPY(fname + i, name + llen);
7456 }
7457 }
7458 }
7459 else
7460 fname = name;
7461
7462 *doesrange = FALSE;
7463
7464
7465 /* execute the function if no errors detected and executing */
7466 if (evaluate && error == ERROR_NONE)
7467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007468 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 error = ERROR_UNKNOWN;
7470
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007471 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 {
7473 /*
7474 * User defined function.
7475 */
7476 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007477
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007479 /* Trigger FuncUndefined event, may load the function. */
7480 if (fp == NULL
7481 && apply_autocmds(EVENT_FUNCUNDEFINED,
7482 fname, fname, TRUE, NULL)
7483 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007485 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 fp = find_func(fname);
7487 }
7488#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007489 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007490 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007491 {
7492 /* loaded a package, search for the function again */
7493 fp = find_func(fname);
7494 }
7495
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 if (fp != NULL)
7497 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007498 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007500 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007502 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007504 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007505 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 else
7507 {
7508 /*
7509 * Call the user function.
7510 * Save and restore search patterns, script variables and
7511 * redo buffer.
7512 */
7513 save_search_patterns();
7514 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007515 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007517 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007518 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7519 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7520 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007521 /* Function was unreferenced while being used, free it
7522 * now. */
7523 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 restoreRedobuff();
7525 restore_search_patterns();
7526 error = ERROR_NONE;
7527 }
7528 }
7529 }
7530 else
7531 {
7532 /*
7533 * Find the function name in the table, call its implementation.
7534 */
7535 i = find_internal_func(fname);
7536 if (i >= 0)
7537 {
7538 if (argcount < functions[i].f_min_argc)
7539 error = ERROR_TOOFEW;
7540 else if (argcount > functions[i].f_max_argc)
7541 error = ERROR_TOOMANY;
7542 else
7543 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007544 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007545 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 error = ERROR_NONE;
7547 }
7548 }
7549 }
7550 /*
7551 * The function call (or "FuncUndefined" autocommand sequence) might
7552 * have been aborted by an error, an interrupt, or an explicitly thrown
7553 * exception that has not been caught so far. This situation can be
7554 * tested for by calling aborting(). For an error in an internal
7555 * function or for the "E132" error in call_user_func(), however, the
7556 * throw point at which the "force_abort" flag (temporarily reset by
7557 * emsg()) is normally updated has not been reached yet. We need to
7558 * update that flag first to make aborting() reliable.
7559 */
7560 update_force_abort();
7561 }
7562 if (error == ERROR_NONE)
7563 ret = OK;
7564
7565 /*
7566 * Report an error unless the argument evaluation or function call has been
7567 * cancelled due to an aborting error, an interrupt, or an exception.
7568 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 if (!aborting())
7570 {
7571 switch (error)
7572 {
7573 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007574 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 break;
7576 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007577 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 break;
7579 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007580 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 name);
7582 break;
7583 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007584 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 name);
7586 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007587 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007588 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 name);
7590 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591 }
7592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593
7594 name[len] = cc;
7595 if (fname != name && fname != fname_buf)
7596 vim_free(fname);
7597
7598 return ret;
7599}
7600
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007601/*
7602 * Give an error message with a function name. Handle <SNR> things.
7603 */
7604 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007605emsg_funcname(ermsg, name)
7606 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007607 char_u *name;
7608{
7609 char_u *p;
7610
7611 if (*name == K_SPECIAL)
7612 p = concat_str((char_u *)"<SNR>", name + 3);
7613 else
7614 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007615 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007616 if (p != name)
7617 vim_free(p);
7618}
7619
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620/*********************************************
7621 * Implementation of the built-in functions
7622 */
7623
7624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007625 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 */
7627 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007628f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 typval_T *argvars;
7630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007637 if ((l = argvars[0].vval.v_list) != NULL
7638 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7639 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007640 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007641 }
7642 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007643 EMSG(_(e_listreq));
7644}
7645
7646/*
7647 * "append(lnum, string/list)" function
7648 */
7649 static void
7650f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 typval_T *argvars;
7652 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007653{
7654 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007655 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 list_T *l = NULL;
7657 listitem_T *li = NULL;
7658 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007659 long added = 0;
7660
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 lnum = get_tv_lnum(argvars);
7662 if (lnum >= 0
7663 && lnum <= curbuf->b_ml.ml_line_count
7664 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007666 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007667 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007668 l = argvars[1].vval.v_list;
7669 if (l == NULL)
7670 return;
7671 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007672 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007673 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007674 for (;;)
7675 {
7676 if (l == NULL)
7677 tv = &argvars[1]; /* append a string */
7678 else if (li == NULL)
7679 break; /* end of list */
7680 else
7681 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007682 line = get_tv_string_chk(tv);
7683 if (line == NULL) /* type error */
7684 {
7685 rettv->vval.v_number = 1; /* Failed */
7686 break;
7687 }
7688 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007689 ++added;
7690 if (l == NULL)
7691 break;
7692 li = li->li_next;
7693 }
7694
7695 appended_lines_mark(lnum, added);
7696 if (curwin->w_cursor.lnum > lnum)
7697 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007699 else
7700 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701}
7702
7703/*
7704 * "argc()" function
7705 */
7706/* ARGSUSED */
7707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007708f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 typval_T *argvars;
7710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713}
7714
7715/*
7716 * "argidx()" function
7717 */
7718/* ARGSUSED */
7719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 typval_T *argvars;
7722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725}
7726
7727/*
7728 * "argv(nr)" function
7729 */
7730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 typval_T *argvars;
7733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735 int idx;
7736
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007737 if (argvars[0].v_type != VAR_UNKNOWN)
7738 {
7739 idx = get_tv_number_chk(&argvars[0], NULL);
7740 if (idx >= 0 && idx < ARGCOUNT)
7741 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7742 else
7743 rettv->vval.v_string = NULL;
7744 rettv->v_type = VAR_STRING;
7745 }
7746 else if (rettv_list_alloc(rettv) == OK)
7747 for (idx = 0; idx < ARGCOUNT; ++idx)
7748 list_append_string(rettv->vval.v_list,
7749 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750}
7751
7752/*
7753 * "browse(save, title, initdir, default)" function
7754 */
7755/* ARGSUSED */
7756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007758 typval_T *argvars;
7759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761#ifdef FEAT_BROWSE
7762 int save;
7763 char_u *title;
7764 char_u *initdir;
7765 char_u *defname;
7766 char_u buf[NUMBUFLEN];
7767 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007768 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007770 save = get_tv_number_chk(&argvars[0], &error);
7771 title = get_tv_string_chk(&argvars[1]);
7772 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7773 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007775 if (error || title == NULL || initdir == NULL || defname == NULL)
7776 rettv->vval.v_string = NULL;
7777 else
7778 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007779 do_browse(save ? BROWSE_SAVE : 0,
7780 title, defname, NULL, initdir, NULL, curbuf);
7781#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007782 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007783#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007784 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007785}
7786
7787/*
7788 * "browsedir(title, initdir)" function
7789 */
7790/* ARGSUSED */
7791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 typval_T *argvars;
7794 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007795{
7796#ifdef FEAT_BROWSE
7797 char_u *title;
7798 char_u *initdir;
7799 char_u buf[NUMBUFLEN];
7800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007801 title = get_tv_string_chk(&argvars[0]);
7802 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007804 if (title == NULL || initdir == NULL)
7805 rettv->vval.v_string = NULL;
7806 else
7807 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007808 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813}
7814
Bram Moolenaar33570922005-01-25 22:26:29 +00007815static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007816
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817/*
7818 * Find a buffer by number or exact name.
7819 */
7820 static buf_T *
7821find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007822 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826 if (avar->v_type == VAR_NUMBER)
7827 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007828 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007830 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007831 if (buf == NULL)
7832 {
7833 /* No full path name match, try a match with a URL or a "nofile"
7834 * buffer, these don't use the full path. */
7835 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7836 if (buf->b_fname != NULL
7837 && (path_with_url(buf->b_fname)
7838#ifdef FEAT_QUICKFIX
7839 || bt_nofile(buf)
7840#endif
7841 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007842 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007843 break;
7844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
7846 return buf;
7847}
7848
7849/*
7850 * "bufexists(expr)" function
7851 */
7852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007854 typval_T *argvars;
7855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858}
7859
7860/*
7861 * "buflisted(expr)" function
7862 */
7863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 typval_T *argvars;
7866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
7868 buf_T *buf;
7869
7870 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
7874/*
7875 * "bufloaded(expr)" function
7876 */
7877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007879 typval_T *argvars;
7880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 buf_T *buf;
7883
7884 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886}
7887
Bram Moolenaar33570922005-01-25 22:26:29 +00007888static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007889
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890/*
7891 * Get buffer by number or pattern.
7892 */
7893 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 int save_magic;
7899 char_u *save_cpo;
7900 buf_T *buf;
7901
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007902 if (tv->v_type == VAR_NUMBER)
7903 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007904 if (tv->v_type != VAR_STRING)
7905 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 if (name == NULL || *name == NUL)
7907 return curbuf;
7908 if (name[0] == '$' && name[1] == NUL)
7909 return lastbuf;
7910
7911 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7912 save_magic = p_magic;
7913 p_magic = TRUE;
7914 save_cpo = p_cpo;
7915 p_cpo = (char_u *)"";
7916
7917 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7918 TRUE, FALSE));
7919
7920 p_magic = save_magic;
7921 p_cpo = save_cpo;
7922
7923 /* If not found, try expanding the name, like done for bufexists(). */
7924 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007925 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926
7927 return buf;
7928}
7929
7930/*
7931 * "bufname(expr)" function
7932 */
7933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007935 typval_T *argvars;
7936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 buf_T *buf;
7939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007940 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 buf = get_buf_tv(&argvars[0]);
7943 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007945 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 --emsg_off;
7949}
7950
7951/*
7952 * "bufnr(expr)" function
7953 */
7954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 typval_T *argvars;
7957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007960 int error = FALSE;
7961 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007963 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007966 --emsg_off;
7967
7968 /* If the buffer isn't found and the second argument is not zero create a
7969 * new buffer. */
7970 if (buf == NULL
7971 && argvars[1].v_type != VAR_UNKNOWN
7972 && get_tv_number_chk(&argvars[1], &error) != 0
7973 && !error
7974 && (name = get_tv_string_chk(&argvars[0])) != NULL
7975 && !error)
7976 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7977
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982}
7983
7984/*
7985 * "bufwinnr(nr)" function
7986 */
7987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 typval_T *argvars;
7990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992#ifdef FEAT_WINDOWS
7993 win_T *wp;
7994 int winnr = 0;
7995#endif
7996 buf_T *buf;
7997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007998 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008000 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001#ifdef FEAT_WINDOWS
8002 for (wp = firstwin; wp; wp = wp->w_next)
8003 {
8004 ++winnr;
8005 if (wp->w_buffer == buf)
8006 break;
8007 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011#endif
8012 --emsg_off;
8013}
8014
8015/*
8016 * "byte2line(byte)" function
8017 */
8018/*ARGSUSED*/
8019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008021 typval_T *argvars;
8022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026#else
8027 long boff = 0;
8028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008029 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008033 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 (linenr_T)0, &boff);
8035#endif
8036}
8037
8038/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008039 * "byteidx()" function
8040 */
8041/*ARGSUSED*/
8042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 typval_T *argvars;
8045 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008046{
8047#ifdef FEAT_MBYTE
8048 char_u *t;
8049#endif
8050 char_u *str;
8051 long idx;
8052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008053 str = get_tv_string_chk(&argvars[0]);
8054 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008056 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008057 return;
8058
8059#ifdef FEAT_MBYTE
8060 t = str;
8061 for ( ; idx > 0; idx--)
8062 {
8063 if (*t == NUL) /* EOL reached */
8064 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008065 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008066 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008067 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008068#else
8069 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008071#endif
8072}
8073
8074/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008075 * "call(func, arglist)" function
8076 */
8077 static void
8078f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008079 typval_T *argvars;
8080 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008081{
8082 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008083 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008084 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008086 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008087 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008088
8089 rettv->vval.v_number = 0;
8090 if (argvars[1].v_type != VAR_LIST)
8091 {
8092 EMSG(_(e_listreq));
8093 return;
8094 }
8095 if (argvars[1].vval.v_list == NULL)
8096 return;
8097
8098 if (argvars[0].v_type == VAR_FUNC)
8099 func = argvars[0].vval.v_string;
8100 else
8101 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008102 if (*func == NUL)
8103 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008104
Bram Moolenaare9a41262005-01-15 22:18:47 +00008105 if (argvars[2].v_type != VAR_UNKNOWN)
8106 {
8107 if (argvars[2].v_type != VAR_DICT)
8108 {
8109 EMSG(_(e_dictreq));
8110 return;
8111 }
8112 selfdict = argvars[2].vval.v_dict;
8113 }
8114
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008115 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8116 item = item->li_next)
8117 {
8118 if (argc == MAX_FUNC_ARGS)
8119 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008120 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008121 break;
8122 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008123 /* Make a copy of each argument. This is needed to be able to set
8124 * v_lock to VAR_FIXED in the copy without changing the original list.
8125 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 copy_tv(&item->li_tv, &argv[argc++]);
8127 }
8128
8129 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008130 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008131 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8132 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008133
8134 /* Free the arguments. */
8135 while (argc > 0)
8136 clear_tv(&argv[--argc]);
8137}
8138
8139/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008140 * "changenr()" function
8141 */
8142/*ARGSUSED*/
8143 static void
8144f_changenr(argvars, rettv)
8145 typval_T *argvars;
8146 typval_T *rettv;
8147{
8148 rettv->vval.v_number = curbuf->b_u_seq_cur;
8149}
8150
8151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 * "char2nr(string)" function
8153 */
8154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008156 typval_T *argvars;
8157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159#ifdef FEAT_MBYTE
8160 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008161 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 else
8163#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165}
8166
8167/*
8168 * "cindent(lnum)" function
8169 */
8170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008171f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 typval_T *argvars;
8173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
8175#ifdef FEAT_CINDENT
8176 pos_T pos;
8177 linenr_T lnum;
8178
8179 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008180 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8182 {
8183 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008184 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 curwin->w_cursor = pos;
8186 }
8187 else
8188#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190}
8191
8192/*
8193 * "col(string)" function
8194 */
8195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008196f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008197 typval_T *argvars;
8198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
8200 colnr_T col = 0;
8201 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008202 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008204 fp = var2fpos(&argvars[0], FALSE, &fnum);
8205 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {
8207 if (fp->col == MAXCOL)
8208 {
8209 /* '> can be MAXCOL, get the length of the line then */
8210 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008211 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 else
8213 col = MAXCOL;
8214 }
8215 else
8216 {
8217 col = fp->col + 1;
8218#ifdef FEAT_VIRTUALEDIT
8219 /* col(".") when the cursor is on the NUL at the end of the line
8220 * because of "coladd" can be seen as an extra column. */
8221 if (virtual_active() && fp == &curwin->w_cursor)
8222 {
8223 char_u *p = ml_get_cursor();
8224
8225 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8226 curwin->w_virtcol - curwin->w_cursor.coladd))
8227 {
8228# ifdef FEAT_MBYTE
8229 int l;
8230
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008231 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 col += l;
8233# else
8234 if (*p != NUL && p[1] == NUL)
8235 ++col;
8236# endif
8237 }
8238 }
8239#endif
8240 }
8241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243}
8244
Bram Moolenaar572cb562005-08-05 21:35:02 +00008245#if defined(FEAT_INS_EXPAND)
8246/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008247 * "complete()" function
8248 */
8249/*ARGSUSED*/
8250 static void
8251f_complete(argvars, rettv)
8252 typval_T *argvars;
8253 typval_T *rettv;
8254{
8255 int startcol;
8256
8257 if ((State & INSERT) == 0)
8258 {
8259 EMSG(_("E785: complete() can only be used in Insert mode"));
8260 return;
8261 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008262
8263 /* Check for undo allowed here, because if something was already inserted
8264 * the line was already saved for undo and this check isn't done. */
8265 if (!undo_allowed())
8266 return;
8267
Bram Moolenaarade00832006-03-10 21:46:58 +00008268 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8269 {
8270 EMSG(_(e_invarg));
8271 return;
8272 }
8273
8274 startcol = get_tv_number_chk(&argvars[0], NULL);
8275 if (startcol <= 0)
8276 return;
8277
8278 set_completion(startcol - 1, argvars[1].vval.v_list);
8279}
8280
8281/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008282 * "complete_add()" function
8283 */
8284/*ARGSUSED*/
8285 static void
8286f_complete_add(argvars, rettv)
8287 typval_T *argvars;
8288 typval_T *rettv;
8289{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008290 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008291}
8292
8293/*
8294 * "complete_check()" function
8295 */
8296/*ARGSUSED*/
8297 static void
8298f_complete_check(argvars, rettv)
8299 typval_T *argvars;
8300 typval_T *rettv;
8301{
8302 int saved = RedrawingDisabled;
8303
8304 RedrawingDisabled = 0;
8305 ins_compl_check_keys(0);
8306 rettv->vval.v_number = compl_interrupted;
8307 RedrawingDisabled = saved;
8308}
8309#endif
8310
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311/*
8312 * "confirm(message, buttons[, default [, type]])" function
8313 */
8314/*ARGSUSED*/
8315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 typval_T *argvars;
8318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8321 char_u *message;
8322 char_u *buttons = NULL;
8323 char_u buf[NUMBUFLEN];
8324 char_u buf2[NUMBUFLEN];
8325 int def = 1;
8326 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008327 char_u *typestr;
8328 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008330 message = get_tv_string_chk(&argvars[0]);
8331 if (message == NULL)
8332 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008333 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008335 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8336 if (buttons == NULL)
8337 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008338 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008340 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008341 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008343 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8344 if (typestr == NULL)
8345 error = TRUE;
8346 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008348 switch (TOUPPER_ASC(*typestr))
8349 {
8350 case 'E': type = VIM_ERROR; break;
8351 case 'Q': type = VIM_QUESTION; break;
8352 case 'I': type = VIM_INFO; break;
8353 case 'W': type = VIM_WARNING; break;
8354 case 'G': type = VIM_GENERIC; break;
8355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 }
8357 }
8358 }
8359 }
8360
8361 if (buttons == NULL || *buttons == NUL)
8362 buttons = (char_u *)_("&Ok");
8363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008364 if (error)
8365 rettv->vval.v_number = 0;
8366 else
8367 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 def, NULL);
8369#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008370 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371#endif
8372}
8373
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008374/*
8375 * "copy()" function
8376 */
8377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008378f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 typval_T *argvars;
8380 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008381{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008382 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008383}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384
8385/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008386 * "count()" function
8387 */
8388 static void
8389f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008390 typval_T *argvars;
8391 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008392{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008393 long n = 0;
8394 int ic = FALSE;
8395
Bram Moolenaare9a41262005-01-15 22:18:47 +00008396 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008397 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008398 listitem_T *li;
8399 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008400 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008401
Bram Moolenaare9a41262005-01-15 22:18:47 +00008402 if ((l = argvars[0].vval.v_list) != NULL)
8403 {
8404 li = l->lv_first;
8405 if (argvars[2].v_type != VAR_UNKNOWN)
8406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 int error = FALSE;
8408
8409 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008410 if (argvars[3].v_type != VAR_UNKNOWN)
8411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008412 idx = get_tv_number_chk(&argvars[3], &error);
8413 if (!error)
8414 {
8415 li = list_find(l, idx);
8416 if (li == NULL)
8417 EMSGN(_(e_listidx), idx);
8418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008420 if (error)
8421 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422 }
8423
8424 for ( ; li != NULL; li = li->li_next)
8425 if (tv_equal(&li->li_tv, &argvars[1], ic))
8426 ++n;
8427 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008429 else if (argvars[0].v_type == VAR_DICT)
8430 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008431 int todo;
8432 dict_T *d;
8433 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008434
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008435 if ((d = argvars[0].vval.v_dict) != NULL)
8436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008437 int error = FALSE;
8438
Bram Moolenaare9a41262005-01-15 22:18:47 +00008439 if (argvars[2].v_type != VAR_UNKNOWN)
8440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008442 if (argvars[3].v_type != VAR_UNKNOWN)
8443 EMSG(_(e_invarg));
8444 }
8445
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008446 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008447 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008448 {
8449 if (!HASHITEM_EMPTY(hi))
8450 {
8451 --todo;
8452 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8453 ++n;
8454 }
8455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008456 }
8457 }
8458 else
8459 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008460 rettv->vval.v_number = n;
8461}
8462
8463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8465 *
8466 * Checks the existence of a cscope connection.
8467 */
8468/*ARGSUSED*/
8469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008470f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 typval_T *argvars;
8472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
8474#ifdef FEAT_CSCOPE
8475 int num = 0;
8476 char_u *dbpath = NULL;
8477 char_u *prepend = NULL;
8478 char_u buf[NUMBUFLEN];
8479
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008480 if (argvars[0].v_type != VAR_UNKNOWN
8481 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483 num = (int)get_tv_number(&argvars[0]);
8484 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008485 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008486 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492#endif
8493}
8494
8495/*
8496 * "cursor(lnum, col)" function
8497 *
8498 * Moves the cursor to the specified line and column
8499 */
8500/*ARGSUSED*/
8501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 typval_T *argvars;
8504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008507#ifdef FEAT_VIRTUALEDIT
8508 long coladd = 0;
8509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510
Bram Moolenaara5525202006-03-02 22:52:09 +00008511 if (argvars[1].v_type == VAR_UNKNOWN)
8512 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008514
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008515 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008516 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008517 line = pos.lnum;
8518 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008519#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008520 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008521#endif
8522 }
8523 else
8524 {
8525 line = get_tv_lnum(argvars);
8526 col = get_tv_number_chk(&argvars[1], NULL);
8527#ifdef FEAT_VIRTUALEDIT
8528 if (argvars[2].v_type != VAR_UNKNOWN)
8529 coladd = get_tv_number_chk(&argvars[2], NULL);
8530#endif
8531 }
8532 if (line < 0 || col < 0
8533#ifdef FEAT_VIRTUALEDIT
8534 || coladd < 0
8535#endif
8536 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008537 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (line > 0)
8539 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (col > 0)
8541 curwin->w_cursor.col = col - 1;
8542#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008543 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544#endif
8545
8546 /* Make sure the cursor is in a valid position. */
8547 check_cursor();
8548#ifdef FEAT_MBYTE
8549 /* Correct cursor for multi-byte character. */
8550 if (has_mbyte)
8551 mb_adjust_cursor();
8552#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008553
8554 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555}
8556
8557/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008558 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 */
8560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 typval_T *argvars;
8563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008565 int noref = 0;
8566
8567 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008568 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008569 if (noref < 0 || noref > 1)
8570 EMSG(_(e_invarg));
8571 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008572 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573}
8574
8575/*
8576 * "delete()" function
8577 */
8578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 typval_T *argvars;
8581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
8583 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
8589/*
8590 * "did_filetype()" function
8591 */
8592/*ARGSUSED*/
8593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 typval_T *argvars;
8596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602#endif
8603}
8604
8605/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008606 * "diff_filler()" function
8607 */
8608/*ARGSUSED*/
8609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 typval_T *argvars;
8612 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008613{
8614#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008616#endif
8617}
8618
8619/*
8620 * "diff_hlID()" function
8621 */
8622/*ARGSUSED*/
8623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 typval_T *argvars;
8626 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008627{
8628#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008629 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008630 static linenr_T prev_lnum = 0;
8631 static int changedtick = 0;
8632 static int fnum = 0;
8633 static int change_start = 0;
8634 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008635 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008636 int filler_lines;
8637 int col;
8638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008639 if (lnum < 0) /* ignore type error in {lnum} arg */
8640 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008641 if (lnum != prev_lnum
8642 || changedtick != curbuf->b_changedtick
8643 || fnum != curbuf->b_fnum)
8644 {
8645 /* New line, buffer, change: need to get the values. */
8646 filler_lines = diff_check(curwin, lnum);
8647 if (filler_lines < 0)
8648 {
8649 if (filler_lines == -1)
8650 {
8651 change_start = MAXCOL;
8652 change_end = -1;
8653 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8654 hlID = HLF_ADD; /* added line */
8655 else
8656 hlID = HLF_CHD; /* changed line */
8657 }
8658 else
8659 hlID = HLF_ADD; /* added line */
8660 }
8661 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008662 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008663 prev_lnum = lnum;
8664 changedtick = curbuf->b_changedtick;
8665 fnum = curbuf->b_fnum;
8666 }
8667
8668 if (hlID == HLF_CHD || hlID == HLF_TXD)
8669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008670 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008671 if (col >= change_start && col <= change_end)
8672 hlID = HLF_TXD; /* changed text */
8673 else
8674 hlID = HLF_CHD; /* changed line */
8675 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008676 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008677#endif
8678}
8679
8680/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008681 * "empty({expr})" function
8682 */
8683 static void
8684f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 typval_T *argvars;
8686 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008687{
8688 int n;
8689
8690 switch (argvars[0].v_type)
8691 {
8692 case VAR_STRING:
8693 case VAR_FUNC:
8694 n = argvars[0].vval.v_string == NULL
8695 || *argvars[0].vval.v_string == NUL;
8696 break;
8697 case VAR_NUMBER:
8698 n = argvars[0].vval.v_number == 0;
8699 break;
8700 case VAR_LIST:
8701 n = argvars[0].vval.v_list == NULL
8702 || argvars[0].vval.v_list->lv_first == NULL;
8703 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008704 case VAR_DICT:
8705 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008707 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008708 default:
8709 EMSG2(_(e_intern2), "f_empty()");
8710 n = 0;
8711 }
8712
8713 rettv->vval.v_number = n;
8714}
8715
8716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 * "escape({string}, {chars})" function
8718 */
8719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 typval_T *argvars;
8722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 char_u buf[NUMBUFLEN];
8725
Bram Moolenaar758711c2005-02-02 23:11:38 +00008726 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8727 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729}
8730
8731/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008732 * "eval()" function
8733 */
8734/*ARGSUSED*/
8735 static void
8736f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 typval_T *argvars;
8738 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008739{
8740 char_u *s;
8741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008742 s = get_tv_string_chk(&argvars[0]);
8743 if (s != NULL)
8744 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008746 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8747 {
8748 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008749 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008750 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008751 else if (*s != NUL)
8752 EMSG(_(e_trailing));
8753}
8754
8755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 * "eventhandler()" function
8757 */
8758/*ARGSUSED*/
8759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008761 typval_T *argvars;
8762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008764 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765}
8766
8767/*
8768 * "executable()" function
8769 */
8770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008772 typval_T *argvars;
8773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008775 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776}
8777
8778/*
8779 * "exists()" function
8780 */
8781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 typval_T *argvars;
8784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
8786 char_u *p;
8787 char_u *name;
8788 int n = FALSE;
8789 int len = 0;
8790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 if (*p == '$') /* environment variable */
8793 {
8794 /* first try "normal" environment variables (fast) */
8795 if (mch_getenv(p + 1) != NULL)
8796 n = TRUE;
8797 else
8798 {
8799 /* try expanding things like $VIM and ${HOME} */
8800 p = expand_env_save(p);
8801 if (p != NULL && *p != '$')
8802 n = TRUE;
8803 vim_free(p);
8804 }
8805 }
8806 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008809 if (*skipwhite(p) != NUL)
8810 n = FALSE; /* trailing garbage */
8811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 else if (*p == '*') /* internal or user defined function */
8813 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008814 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 }
8816 else if (*p == ':')
8817 {
8818 n = cmd_exists(p + 1);
8819 }
8820 else if (*p == '#')
8821 {
8822#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008823 if (p[1] == '#')
8824 n = autocmd_supported(p + 2);
8825 else
8826 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827#endif
8828 }
8829 else /* internal variable */
8830 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008831 char_u *tofree;
8832 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008834 /* get_name_len() takes care of expanding curly braces */
8835 name = p;
8836 len = get_name_len(&p, &tofree, TRUE, FALSE);
8837 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008839 if (tofree != NULL)
8840 name = tofree;
8841 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8842 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008844 /* handle d.key, l[idx], f(expr) */
8845 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8846 if (n)
8847 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 }
8849 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008850 if (*p != NUL)
8851 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008853 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
8855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857}
8858
8859/*
8860 * "expand()" function
8861 */
8862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *argvars;
8865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 char_u *s;
8868 int len;
8869 char_u *errormsg;
8870 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8871 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 rettv->v_type = VAR_STRING;
8875 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 if (*s == '%' || *s == '#' || *s == '<')
8877 {
8878 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 --emsg_off;
8881 }
8882 else
8883 {
8884 /* When the optional second argument is non-zero, don't remove matches
8885 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008886 if (argvars[1].v_type != VAR_UNKNOWN
8887 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889 if (!error)
8890 {
8891 ExpandInit(&xpc);
8892 xpc.xp_context = EXPAND_FILES;
8893 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008894 }
8895 else
8896 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 }
8898}
8899
8900/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008901 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008902 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008903 */
8904 static void
8905f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008906 typval_T *argvars;
8907 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008908{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008909 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008910 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008911 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 list_T *l1, *l2;
8913 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008914 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008915 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008916
Bram Moolenaare9a41262005-01-15 22:18:47 +00008917 l1 = argvars[0].vval.v_list;
8918 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008919 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8920 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921 {
8922 if (argvars[2].v_type != VAR_UNKNOWN)
8923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008924 before = get_tv_number_chk(&argvars[2], &error);
8925 if (error)
8926 return; /* type error; errmsg already given */
8927
Bram Moolenaar758711c2005-02-02 23:11:38 +00008928 if (before == l1->lv_len)
8929 item = NULL;
8930 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008931 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008932 item = list_find(l1, before);
8933 if (item == NULL)
8934 {
8935 EMSGN(_(e_listidx), before);
8936 return;
8937 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008938 }
8939 }
8940 else
8941 item = NULL;
8942 list_extend(l1, l2, item);
8943
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 copy_tv(&argvars[0], rettv);
8945 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008946 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8948 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 dict_T *d1, *d2;
8950 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008951 char_u *action;
8952 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008954 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008955
8956 d1 = argvars[0].vval.v_dict;
8957 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008958 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8959 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 {
8961 /* Check the third argument. */
8962 if (argvars[2].v_type != VAR_UNKNOWN)
8963 {
8964 static char *(av[]) = {"keep", "force", "error"};
8965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966 action = get_tv_string_chk(&argvars[2]);
8967 if (action == NULL)
8968 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008969 for (i = 0; i < 3; ++i)
8970 if (STRCMP(action, av[i]) == 0)
8971 break;
8972 if (i == 3)
8973 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008974 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008975 return;
8976 }
8977 }
8978 else
8979 action = (char_u *)"force";
8980
8981 /* Go over all entries in the second dict and add them to the
8982 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008983 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008985 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008986 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008987 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008988 --todo;
8989 di1 = dict_find(d1, hi2->hi_key, -1);
8990 if (di1 == NULL)
8991 {
8992 di1 = dictitem_copy(HI2DI(hi2));
8993 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8994 dictitem_free(di1);
8995 }
8996 else if (*action == 'e')
8997 {
8998 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8999 break;
9000 }
9001 else if (*action == 'f')
9002 {
9003 clear_tv(&di1->di_tv);
9004 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009006 }
9007 }
9008
Bram Moolenaare9a41262005-01-15 22:18:47 +00009009 copy_tv(&argvars[0], rettv);
9010 }
9011 }
9012 else
9013 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014}
9015
9016/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009017 * "feedkeys()" function
9018 */
9019/*ARGSUSED*/
9020 static void
9021f_feedkeys(argvars, rettv)
9022 typval_T *argvars;
9023 typval_T *rettv;
9024{
9025 int remap = TRUE;
9026 char_u *keys, *flags;
9027 char_u nbuf[NUMBUFLEN];
9028 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009029 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009030
9031 rettv->vval.v_number = 0;
9032 keys = get_tv_string(&argvars[0]);
9033 if (*keys != NUL)
9034 {
9035 if (argvars[1].v_type != VAR_UNKNOWN)
9036 {
9037 flags = get_tv_string_buf(&argvars[1], nbuf);
9038 for ( ; *flags != NUL; ++flags)
9039 {
9040 switch (*flags)
9041 {
9042 case 'n': remap = FALSE; break;
9043 case 'm': remap = TRUE; break;
9044 case 't': typed = TRUE; break;
9045 }
9046 }
9047 }
9048
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009049 /* Need to escape K_SPECIAL and CSI before putting the string in the
9050 * typeahead buffer. */
9051 keys_esc = vim_strsave_escape_csi(keys);
9052 if (keys_esc != NULL)
9053 {
9054 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009055 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009056 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009057 if (vgetc_busy)
9058 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009059 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009060 }
9061}
9062
9063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 * "filereadable()" function
9065 */
9066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 typval_T *argvars;
9069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
9071 FILE *fd;
9072 char_u *p;
9073 int n;
9074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9077 {
9078 n = TRUE;
9079 fclose(fd);
9080 }
9081 else
9082 n = FALSE;
9083
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
9087/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009088 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 * rights to write into.
9090 */
9091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009093 typval_T *argvars;
9094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009096 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097}
9098
Bram Moolenaar33570922005-01-25 22:26:29 +00009099static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009100
9101 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009102findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009103 typval_T *argvars;
9104 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009105 int dir;
9106{
9107#ifdef FEAT_SEARCHPATH
9108 char_u *fname;
9109 char_u *fresult = NULL;
9110 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9111 char_u *p;
9112 char_u pathbuf[NUMBUFLEN];
9113 int count = 1;
9114 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009115 int error = FALSE;
9116#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009117
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009118 rettv->vval.v_string = NULL;
9119 rettv->v_type = VAR_STRING;
9120
9121#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009123
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009124 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009126 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9127 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009128 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009129 else
9130 {
9131 if (*p != NUL)
9132 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009135 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009137 }
9138
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009139 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9140 error = TRUE;
9141
9142 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 do
9145 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009146 if (rettv->v_type == VAR_STRING)
9147 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009148 fresult = find_file_in_path_option(first ? fname : NULL,
9149 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009150 0, first, path, dir, NULL,
9151 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009152 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009153
9154 if (fresult != NULL && rettv->v_type == VAR_LIST)
9155 list_append_string(rettv->vval.v_list, fresult, -1);
9156
9157 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009158 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009159
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009160 if (rettv->v_type == VAR_STRING)
9161 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009162#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009163}
9164
Bram Moolenaar33570922005-01-25 22:26:29 +00009165static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9166static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009167
9168/*
9169 * Implementation of map() and filter().
9170 */
9171 static void
9172filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009173 typval_T *argvars;
9174 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009175 int map;
9176{
9177 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009178 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009179 listitem_T *li, *nli;
9180 list_T *l = NULL;
9181 dictitem_T *di;
9182 hashtab_T *ht;
9183 hashitem_T *hi;
9184 dict_T *d = NULL;
9185 typval_T save_val;
9186 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009187 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009188 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009189 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009190 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009191
9192 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009193 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009194 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009195 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009196 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009197 return;
9198 }
9199 else if (argvars[0].v_type == VAR_DICT)
9200 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009201 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009202 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009203 return;
9204 }
9205 else
9206 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009207 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009208 return;
9209 }
9210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009211 expr = get_tv_string_buf_chk(&argvars[1], buf);
9212 /* On type errors, the preceding call has already displayed an error
9213 * message. Avoid a misleading error message for an empty string that
9214 * was not passed as argument. */
9215 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009217 prepare_vimvar(VV_VAL, &save_val);
9218 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009219
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009220 /* We reset "did_emsg" to be able to detect whether an error
9221 * occurred during evaluation of the expression. */
9222 save_did_emsg = did_emsg;
9223 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009225 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227 prepare_vimvar(VV_KEY, &save_key);
9228 vimvars[VV_KEY].vv_type = VAR_STRING;
9229
9230 ht = &d->dv_hashtab;
9231 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009232 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009233 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009235 if (!HASHITEM_EMPTY(hi))
9236 {
9237 --todo;
9238 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009239 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 break;
9241 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009242 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009243 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009244 break;
9245 if (!map && rem)
9246 dictitem_remove(d, di);
9247 clear_tv(&vimvars[VV_KEY].vv_tv);
9248 }
9249 }
9250 hash_unlock(ht);
9251
9252 restore_vimvar(VV_KEY, &save_key);
9253 }
9254 else
9255 {
9256 for (li = l->lv_first; li != NULL; li = nli)
9257 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009258 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009259 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009261 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009262 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009263 break;
9264 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009266 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009267 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009269 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009270
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009271 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009273
9274 copy_tv(&argvars[0], rettv);
9275}
9276
9277 static int
9278filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009279 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 char_u *expr;
9281 int map;
9282 int *remp;
9283{
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009285 char_u *s;
9286
Bram Moolenaar33570922005-01-25 22:26:29 +00009287 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 s = expr;
9289 if (eval1(&s, &rettv, TRUE) == FAIL)
9290 return FAIL;
9291 if (*s != NUL) /* check for trailing chars after expr */
9292 {
9293 EMSG2(_(e_invexpr2), s);
9294 return FAIL;
9295 }
9296 if (map)
9297 {
9298 /* map(): replace the list item value */
9299 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009300 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009301 *tv = rettv;
9302 }
9303 else
9304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009305 int error = FALSE;
9306
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 /* On type error, nothing has been removed; return FAIL to stop the
9311 * loop. The error message was given by get_tv_number_chk(). */
9312 if (error)
9313 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009314 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009316 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009317}
9318
9319/*
9320 * "filter()" function
9321 */
9322 static void
9323f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009324 typval_T *argvars;
9325 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009326{
9327 filter_map(argvars, rettv, FALSE);
9328}
9329
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009330/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009331 * "finddir({fname}[, {path}[, {count}]])" function
9332 */
9333 static void
9334f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 typval_T *argvars;
9336 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009337{
9338 findfilendir(argvars, rettv, TRUE);
9339}
9340
9341/*
9342 * "findfile({fname}[, {path}[, {count}]])" function
9343 */
9344 static void
9345f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 typval_T *argvars;
9347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009348{
9349 findfilendir(argvars, rettv, FALSE);
9350}
9351
9352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 * "fnamemodify({fname}, {mods})" function
9354 */
9355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009356f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009357 typval_T *argvars;
9358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359{
9360 char_u *fname;
9361 char_u *mods;
9362 int usedlen = 0;
9363 int len;
9364 char_u *fbuf = NULL;
9365 char_u buf[NUMBUFLEN];
9366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 fname = get_tv_string_chk(&argvars[0]);
9368 mods = get_tv_string_buf_chk(&argvars[1], buf);
9369 if (fname == NULL || mods == NULL)
9370 fname = NULL;
9371 else
9372 {
9373 len = (int)STRLEN(fname);
9374 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 vim_free(fbuf);
9383}
9384
Bram Moolenaar33570922005-01-25 22:26:29 +00009385static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386
9387/*
9388 * "foldclosed()" function
9389 */
9390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009392 typval_T *argvars;
9393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 int end;
9395{
9396#ifdef FEAT_FOLDING
9397 linenr_T lnum;
9398 linenr_T first, last;
9399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9402 {
9403 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9404 {
9405 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 return;
9410 }
9411 }
9412#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414}
9415
9416/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009417 * "foldclosed()" function
9418 */
9419 static void
9420f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009421 typval_T *argvars;
9422 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009423{
9424 foldclosed_both(argvars, rettv, FALSE);
9425}
9426
9427/*
9428 * "foldclosedend()" function
9429 */
9430 static void
9431f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 typval_T *argvars;
9433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009434{
9435 foldclosed_both(argvars, rettv, TRUE);
9436}
9437
9438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 * "foldlevel()" function
9440 */
9441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 typval_T *argvars;
9444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446#ifdef FEAT_FOLDING
9447 linenr_T lnum;
9448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
9453#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455}
9456
9457/*
9458 * "foldtext()" function
9459 */
9460/*ARGSUSED*/
9461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009463 typval_T *argvars;
9464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466#ifdef FEAT_FOLDING
9467 linenr_T lnum;
9468 char_u *s;
9469 char_u *r;
9470 int len;
9471 char *txt;
9472#endif
9473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->v_type = VAR_STRING;
9475 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009477 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9478 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9479 <= curbuf->b_ml.ml_line_count
9480 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 {
9482 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009483 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9484 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 {
9486 if (!linewhite(lnum))
9487 break;
9488 ++lnum;
9489 }
9490
9491 /* Find interesting text in this line. */
9492 s = skipwhite(ml_get(lnum));
9493 /* skip C comment-start */
9494 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009497 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009498 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009499 {
9500 s = skipwhite(ml_get(lnum + 1));
9501 if (*s == '*')
9502 s = skipwhite(s + 1);
9503 }
9504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 txt = _("+-%s%3ld lines: ");
9506 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009507 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 + 20 /* for %3ld */
9509 + STRLEN(s))); /* concatenated */
9510 if (r != NULL)
9511 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009512 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9513 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9514 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 len = (int)STRLEN(r);
9516 STRCAT(r, s);
9517 /* remove 'foldmarker' and 'commentstring' */
9518 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 }
9521 }
9522#endif
9523}
9524
9525/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009526 * "foldtextresult(lnum)" function
9527 */
9528/*ARGSUSED*/
9529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009531 typval_T *argvars;
9532 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009533{
9534#ifdef FEAT_FOLDING
9535 linenr_T lnum;
9536 char_u *text;
9537 char_u buf[51];
9538 foldinfo_T foldinfo;
9539 int fold_count;
9540#endif
9541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542 rettv->v_type = VAR_STRING;
9543 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009544#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 /* treat illegal types and illegal string values for {lnum} the same */
9547 if (lnum < 0)
9548 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009549 fold_count = foldedCount(curwin, lnum, &foldinfo);
9550 if (fold_count > 0)
9551 {
9552 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9553 &foldinfo, buf);
9554 if (text == buf)
9555 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009557 }
9558#endif
9559}
9560
9561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 * "foreground()" function
9563 */
9564/*ARGSUSED*/
9565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009567 typval_T *argvars;
9568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571#ifdef FEAT_GUI
9572 if (gui.in_use)
9573 gui_mch_set_foreground();
9574#else
9575# ifdef WIN32
9576 win32_set_foreground();
9577# endif
9578#endif
9579}
9580
9581/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582 * "function()" function
9583 */
9584/*ARGSUSED*/
9585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009587 typval_T *argvars;
9588 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009589{
9590 char_u *s;
9591
Bram Moolenaara7043832005-01-21 11:56:39 +00009592 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009594 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009595 EMSG2(_(e_invarg2), s);
9596 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009597 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009598 else
9599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_string = vim_strsave(s);
9601 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009602 }
9603}
9604
9605/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009606 * "garbagecollect()" function
9607 */
9608/*ARGSUSED*/
9609 static void
9610f_garbagecollect(argvars, rettv)
9611 typval_T *argvars;
9612 typval_T *rettv;
9613{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009614 /* This is postponed until we are back at the toplevel, because we may be
9615 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9616 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009617}
9618
9619/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009620 * "get()" function
9621 */
9622 static void
9623f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009624 typval_T *argvars;
9625 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009626{
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 listitem_T *li;
9628 list_T *l;
9629 dictitem_T *di;
9630 dict_T *d;
9631 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009632
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009634 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009637 int error = FALSE;
9638
9639 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9640 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009642 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009644 else if (argvars[0].v_type == VAR_DICT)
9645 {
9646 if ((d = argvars[0].vval.v_dict) != NULL)
9647 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009648 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 if (di != NULL)
9650 tv = &di->di_tv;
9651 }
9652 }
9653 else
9654 EMSG2(_(e_listdictarg), "get()");
9655
9656 if (tv == NULL)
9657 {
9658 if (argvars[2].v_type == VAR_UNKNOWN)
9659 rettv->vval.v_number = 0;
9660 else
9661 copy_tv(&argvars[2], rettv);
9662 }
9663 else
9664 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009665}
9666
Bram Moolenaar342337a2005-07-21 21:11:17 +00009667static 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 +00009668
9669/*
9670 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009671 * Return a range (from start to end) of lines in rettv from the specified
9672 * buffer.
9673 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009674 */
9675 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009676get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009677 buf_T *buf;
9678 linenr_T start;
9679 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009680 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009681 typval_T *rettv;
9682{
9683 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009684
Bram Moolenaar342337a2005-07-21 21:11:17 +00009685 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009686 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009687 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009688 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009689 }
9690 else
9691 rettv->vval.v_number = 0;
9692
9693 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9694 return;
9695
9696 if (!retlist)
9697 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009698 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9699 p = ml_get_buf(buf, start, FALSE);
9700 else
9701 p = (char_u *)"";
9702
9703 rettv->v_type = VAR_STRING;
9704 rettv->vval.v_string = vim_strsave(p);
9705 }
9706 else
9707 {
9708 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009709 return;
9710
9711 if (start < 1)
9712 start = 1;
9713 if (end > buf->b_ml.ml_line_count)
9714 end = buf->b_ml.ml_line_count;
9715 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009716 if (list_append_string(rettv->vval.v_list,
9717 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009718 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009719 }
9720}
9721
9722/*
9723 * "getbufline()" function
9724 */
9725 static void
9726f_getbufline(argvars, rettv)
9727 typval_T *argvars;
9728 typval_T *rettv;
9729{
9730 linenr_T lnum;
9731 linenr_T end;
9732 buf_T *buf;
9733
9734 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9735 ++emsg_off;
9736 buf = get_buf_tv(&argvars[0]);
9737 --emsg_off;
9738
Bram Moolenaar661b1822005-07-28 22:36:45 +00009739 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009740 if (argvars[2].v_type == VAR_UNKNOWN)
9741 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009742 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009743 end = get_tv_lnum_buf(&argvars[2], buf);
9744
Bram Moolenaar342337a2005-07-21 21:11:17 +00009745 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009746}
9747
Bram Moolenaar0d660222005-01-07 21:51:51 +00009748/*
9749 * "getbufvar()" function
9750 */
9751 static void
9752f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009753 typval_T *argvars;
9754 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009755{
9756 buf_T *buf;
9757 buf_T *save_curbuf;
9758 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009759 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009761 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9762 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009763 ++emsg_off;
9764 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009765
9766 rettv->v_type = VAR_STRING;
9767 rettv->vval.v_string = NULL;
9768
9769 if (buf != NULL && varname != NULL)
9770 {
9771 if (*varname == '&') /* buffer-local-option */
9772 {
9773 /* set curbuf to be our buf, temporarily */
9774 save_curbuf = curbuf;
9775 curbuf = buf;
9776
9777 get_option_tv(&varname, rettv, TRUE);
9778
9779 /* restore previous notion of curbuf */
9780 curbuf = save_curbuf;
9781 }
9782 else
9783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 if (*varname == NUL)
9785 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9786 * scope prefix before the NUL byte is required by
9787 * find_var_in_ht(). */
9788 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009789 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009790 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009791 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009792 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009793 }
9794 }
9795
9796 --emsg_off;
9797}
9798
9799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 * "getchar()" function
9801 */
9802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009804 typval_T *argvars;
9805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009808 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009810 /* Position the cursor. Needed after a message that ends in a space. */
9811 windgoto(msg_row, msg_col);
9812
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 ++no_mapping;
9814 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009815 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 /* getchar(): blocking wait. */
9817 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 /* getchar(1): only check if char avail */
9820 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009821 else if (error || vpeekc() == NUL)
9822 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 n = 0;
9824 else
9825 /* getchar(0) and char avail: return char */
9826 n = safe_vgetc();
9827 --no_mapping;
9828 --allow_keys;
9829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 if (IS_SPECIAL(n) || mod_mask != 0)
9832 {
9833 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9834 int i = 0;
9835
9836 /* Turn a special key into three bytes, plus modifier. */
9837 if (mod_mask != 0)
9838 {
9839 temp[i++] = K_SPECIAL;
9840 temp[i++] = KS_MODIFIER;
9841 temp[i++] = mod_mask;
9842 }
9843 if (IS_SPECIAL(n))
9844 {
9845 temp[i++] = K_SPECIAL;
9846 temp[i++] = K_SECOND(n);
9847 temp[i++] = K_THIRD(n);
9848 }
9849#ifdef FEAT_MBYTE
9850 else if (has_mbyte)
9851 i += (*mb_char2bytes)(n, temp + i);
9852#endif
9853 else
9854 temp[i++] = n;
9855 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->v_type = VAR_STRING;
9857 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 }
9859}
9860
9861/*
9862 * "getcharmod()" function
9863 */
9864/*ARGSUSED*/
9865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009867 typval_T *argvars;
9868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871}
9872
9873/*
9874 * "getcmdline()" function
9875 */
9876/*ARGSUSED*/
9877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 typval_T *argvars;
9880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882 rettv->v_type = VAR_STRING;
9883 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884}
9885
9886/*
9887 * "getcmdpos()" function
9888 */
9889/*ARGSUSED*/
9890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 typval_T *argvars;
9893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009895 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896}
9897
9898/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009899 * "getcmdtype()" function
9900 */
9901/*ARGSUSED*/
9902 static void
9903f_getcmdtype(argvars, rettv)
9904 typval_T *argvars;
9905 typval_T *rettv;
9906{
9907 rettv->v_type = VAR_STRING;
9908 rettv->vval.v_string = alloc(2);
9909 if (rettv->vval.v_string != NULL)
9910 {
9911 rettv->vval.v_string[0] = get_cmdline_type();
9912 rettv->vval.v_string[1] = NUL;
9913 }
9914}
9915
9916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 * "getcwd()" function
9918 */
9919/*ARGSUSED*/
9920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *argvars;
9923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 char_u cwd[MAXPATHL];
9926
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 else
9931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009934 if (rettv->vval.v_string != NULL)
9935 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936#endif
9937 }
9938}
9939
9940/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009941 * "getfontname()" function
9942 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009943/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009946 typval_T *argvars;
9947 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009948{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->v_type = VAR_STRING;
9950 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009951#ifdef FEAT_GUI
9952 if (gui.in_use)
9953 {
9954 GuiFont font;
9955 char_u *name = NULL;
9956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009957 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009958 {
9959 /* Get the "Normal" font. Either the name saved by
9960 * hl_set_font_name() or from the font ID. */
9961 font = gui.norm_font;
9962 name = hl_get_font_name();
9963 }
9964 else
9965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009967 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9968 return;
9969 font = gui_mch_get_font(name, FALSE);
9970 if (font == NOFONT)
9971 return; /* Invalid font name, return empty string. */
9972 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009974 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009975 gui_mch_free_font(font);
9976 }
9977#endif
9978}
9979
9980/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009981 * "getfperm({fname})" function
9982 */
9983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009985 typval_T *argvars;
9986 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009987{
9988 char_u *fname;
9989 struct stat st;
9990 char_u *perm = NULL;
9991 char_u flags[] = "rwx";
9992 int i;
9993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009995
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009997 if (mch_stat((char *)fname, &st) >= 0)
9998 {
9999 perm = vim_strsave((char_u *)"---------");
10000 if (perm != NULL)
10001 {
10002 for (i = 0; i < 9; i++)
10003 {
10004 if (st.st_mode & (1 << (8 - i)))
10005 perm[i] = flags[i % 3];
10006 }
10007 }
10008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010010}
10011
10012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 * "getfsize({fname})" function
10014 */
10015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010017 typval_T *argvars;
10018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
10020 char_u *fname;
10021 struct stat st;
10022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026
10027 if (mch_stat((char *)fname, &st) >= 0)
10028 {
10029 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 }
10034 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036}
10037
10038/*
10039 * "getftime({fname})" function
10040 */
10041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010043 typval_T *argvars;
10044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
10046 char_u *fname;
10047 struct stat st;
10048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
10051 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055}
10056
10057/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010058 * "getftype({fname})" function
10059 */
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010064{
10065 char_u *fname;
10066 struct stat st;
10067 char_u *type = NULL;
10068 char *t;
10069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010073 if (mch_lstat((char *)fname, &st) >= 0)
10074 {
10075#ifdef S_ISREG
10076 if (S_ISREG(st.st_mode))
10077 t = "file";
10078 else if (S_ISDIR(st.st_mode))
10079 t = "dir";
10080# ifdef S_ISLNK
10081 else if (S_ISLNK(st.st_mode))
10082 t = "link";
10083# endif
10084# ifdef S_ISBLK
10085 else if (S_ISBLK(st.st_mode))
10086 t = "bdev";
10087# endif
10088# ifdef S_ISCHR
10089 else if (S_ISCHR(st.st_mode))
10090 t = "cdev";
10091# endif
10092# ifdef S_ISFIFO
10093 else if (S_ISFIFO(st.st_mode))
10094 t = "fifo";
10095# endif
10096# ifdef S_ISSOCK
10097 else if (S_ISSOCK(st.st_mode))
10098 t = "fifo";
10099# endif
10100 else
10101 t = "other";
10102#else
10103# ifdef S_IFMT
10104 switch (st.st_mode & S_IFMT)
10105 {
10106 case S_IFREG: t = "file"; break;
10107 case S_IFDIR: t = "dir"; break;
10108# ifdef S_IFLNK
10109 case S_IFLNK: t = "link"; break;
10110# endif
10111# ifdef S_IFBLK
10112 case S_IFBLK: t = "bdev"; break;
10113# endif
10114# ifdef S_IFCHR
10115 case S_IFCHR: t = "cdev"; break;
10116# endif
10117# ifdef S_IFIFO
10118 case S_IFIFO: t = "fifo"; break;
10119# endif
10120# ifdef S_IFSOCK
10121 case S_IFSOCK: t = "socket"; break;
10122# endif
10123 default: t = "other";
10124 }
10125# else
10126 if (mch_isdir(fname))
10127 t = "dir";
10128 else
10129 t = "file";
10130# endif
10131#endif
10132 type = vim_strsave((char_u *)t);
10133 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010135}
10136
10137/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010138 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010139 */
10140 static void
10141f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010142 typval_T *argvars;
10143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010144{
10145 linenr_T lnum;
10146 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010147 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010148
10149 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010150 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010151 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010152 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010153 retlist = FALSE;
10154 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010155 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010156 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010157 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010158 retlist = TRUE;
10159 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010160
Bram Moolenaar342337a2005-07-21 21:11:17 +000010161 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010162}
10163
10164/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010165 * "getpos(string)" function
10166 */
10167 static void
10168f_getpos(argvars, rettv)
10169 typval_T *argvars;
10170 typval_T *rettv;
10171{
10172 pos_T *fp;
10173 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010174 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010175
10176 if (rettv_list_alloc(rettv) == OK)
10177 {
10178 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010179 fp = var2fpos(&argvars[0], TRUE, &fnum);
10180 if (fnum != -1)
10181 list_append_number(l, (varnumber_T)fnum);
10182 else
10183 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010184 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10185 : (varnumber_T)0);
10186 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10187 : (varnumber_T)0);
10188 list_append_number(l,
10189#ifdef FEAT_VIRTUALEDIT
10190 (fp != NULL) ? (varnumber_T)fp->coladd :
10191#endif
10192 (varnumber_T)0);
10193 }
10194 else
10195 rettv->vval.v_number = FALSE;
10196}
10197
10198/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010199 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010200 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010201/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010202 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010203f_getqflist(argvars, rettv)
10204 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010205 typval_T *rettv;
10206{
10207#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010208 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010209#endif
10210
10211 rettv->vval.v_number = FALSE;
10212#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010213 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010214 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010215 wp = NULL;
10216 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10217 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010218 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010219 if (wp == NULL)
10220 return;
10221 }
10222
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010223 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010224 }
10225#endif
10226}
10227
10228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 * "getreg()" function
10230 */
10231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010232f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010233 typval_T *argvars;
10234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235{
10236 char_u *strregname;
10237 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010238 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010241 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010243 strregname = get_tv_string_chk(&argvars[0]);
10244 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010245 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010246 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010249 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 regname = (strregname == NULL ? '"' : *strregname);
10251 if (regname == 0)
10252 regname = '"';
10253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010254 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 rettv->vval.v_string = error ? NULL :
10256 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257}
10258
10259/*
10260 * "getregtype()" function
10261 */
10262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010264 typval_T *argvars;
10265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266{
10267 char_u *strregname;
10268 int regname;
10269 char_u buf[NUMBUFLEN + 2];
10270 long reglen = 0;
10271
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010272 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010273 {
10274 strregname = get_tv_string_chk(&argvars[0]);
10275 if (strregname == NULL) /* type error; errmsg already given */
10276 {
10277 rettv->v_type = VAR_STRING;
10278 rettv->vval.v_string = NULL;
10279 return;
10280 }
10281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 else
10283 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010284 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285
10286 regname = (strregname == NULL ? '"' : *strregname);
10287 if (regname == 0)
10288 regname = '"';
10289
10290 buf[0] = NUL;
10291 buf[1] = NUL;
10292 switch (get_reg_type(regname, &reglen))
10293 {
10294 case MLINE: buf[0] = 'V'; break;
10295 case MCHAR: buf[0] = 'v'; break;
10296#ifdef FEAT_VISUAL
10297 case MBLOCK:
10298 buf[0] = Ctrl_V;
10299 sprintf((char *)buf + 1, "%ld", reglen + 1);
10300 break;
10301#endif
10302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303 rettv->v_type = VAR_STRING;
10304 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305}
10306
10307/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010308 * "gettabwinvar()" function
10309 */
10310 static void
10311f_gettabwinvar(argvars, rettv)
10312 typval_T *argvars;
10313 typval_T *rettv;
10314{
10315 getwinvar(argvars, rettv, 1);
10316}
10317
10318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 * "getwinposx()" function
10320 */
10321/*ARGSUSED*/
10322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010323f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010324 typval_T *argvars;
10325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010327 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328#ifdef FEAT_GUI
10329 if (gui.in_use)
10330 {
10331 int x, y;
10332
10333 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010334 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 }
10336#endif
10337}
10338
10339/*
10340 * "getwinposy()" function
10341 */
10342/*ARGSUSED*/
10343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010345 typval_T *argvars;
10346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349#ifdef FEAT_GUI
10350 if (gui.in_use)
10351 {
10352 int x, y;
10353
10354 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 }
10357#endif
10358}
10359
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010360/*
10361 * Find window specifed by "vp" in tabpage "tp".
10362 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010363 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010364find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010365 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010366 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010367{
10368#ifdef FEAT_WINDOWS
10369 win_T *wp;
10370#endif
10371 int nr;
10372
10373 nr = get_tv_number_chk(vp, NULL);
10374
10375#ifdef FEAT_WINDOWS
10376 if (nr < 0)
10377 return NULL;
10378 if (nr == 0)
10379 return curwin;
10380
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010381 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10382 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010383 if (--nr <= 0)
10384 break;
10385 return wp;
10386#else
10387 if (nr == 0 || nr == 1)
10388 return curwin;
10389 return NULL;
10390#endif
10391}
10392
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393/*
10394 * "getwinvar()" function
10395 */
10396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010398 typval_T *argvars;
10399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010401 getwinvar(argvars, rettv, 0);
10402}
10403
10404/*
10405 * getwinvar() and gettabwinvar()
10406 */
10407 static void
10408getwinvar(argvars, rettv, off)
10409 typval_T *argvars;
10410 typval_T *rettv;
10411 int off; /* 1 for gettabwinvar() */
10412{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 win_T *win, *oldcurwin;
10414 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010415 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010416 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010418#ifdef FEAT_WINDOWS
10419 if (off == 1)
10420 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10421 else
10422 tp = curtab;
10423#endif
10424 win = find_win_by_nr(&argvars[off], tp);
10425 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 rettv->v_type = VAR_STRING;
10429 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430
10431 if (win != NULL && varname != NULL)
10432 {
10433 if (*varname == '&') /* window-local-option */
10434 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010435 /* Set curwin to be our win, temporarily. Also set curbuf, so
10436 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 oldcurwin = curwin;
10438 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010439 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
10443 /* restore previous notion of curwin */
10444 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010445 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 }
10447 else
10448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 if (*varname == NUL)
10450 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10451 * scope prefix before the NUL byte is required by
10452 * find_var_in_ht(). */
10453 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010455 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010457 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 }
10459 }
10460
10461 --emsg_off;
10462}
10463
10464/*
10465 * "glob()" function
10466 */
10467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010469 typval_T *argvars;
10470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471{
10472 expand_T xpc;
10473
10474 ExpandInit(&xpc);
10475 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476 rettv->v_type = VAR_STRING;
10477 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479}
10480
10481/*
10482 * "globpath()" function
10483 */
10484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010486 typval_T *argvars;
10487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488{
10489 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010492 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010493 if (file == NULL)
10494 rettv->vval.v_string = NULL;
10495 else
10496 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497}
10498
10499/*
10500 * "has()" function
10501 */
10502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010504 typval_T *argvars;
10505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507 int i;
10508 char_u *name;
10509 int n = FALSE;
10510 static char *(has_list[]) =
10511 {
10512#ifdef AMIGA
10513 "amiga",
10514# ifdef FEAT_ARP
10515 "arp",
10516# endif
10517#endif
10518#ifdef __BEOS__
10519 "beos",
10520#endif
10521#ifdef MSDOS
10522# ifdef DJGPP
10523 "dos32",
10524# else
10525 "dos16",
10526# endif
10527#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010528#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 "mac",
10530#endif
10531#if defined(MACOS_X_UNIX)
10532 "macunix",
10533#endif
10534#ifdef OS2
10535 "os2",
10536#endif
10537#ifdef __QNX__
10538 "qnx",
10539#endif
10540#ifdef RISCOS
10541 "riscos",
10542#endif
10543#ifdef UNIX
10544 "unix",
10545#endif
10546#ifdef VMS
10547 "vms",
10548#endif
10549#ifdef WIN16
10550 "win16",
10551#endif
10552#ifdef WIN32
10553 "win32",
10554#endif
10555#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10556 "win32unix",
10557#endif
10558#ifdef WIN64
10559 "win64",
10560#endif
10561#ifdef EBCDIC
10562 "ebcdic",
10563#endif
10564#ifndef CASE_INSENSITIVE_FILENAME
10565 "fname_case",
10566#endif
10567#ifdef FEAT_ARABIC
10568 "arabic",
10569#endif
10570#ifdef FEAT_AUTOCMD
10571 "autocmd",
10572#endif
10573#ifdef FEAT_BEVAL
10574 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010575# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10576 "balloon_multiline",
10577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578#endif
10579#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10580 "builtin_terms",
10581# ifdef ALL_BUILTIN_TCAPS
10582 "all_builtin_terms",
10583# endif
10584#endif
10585#ifdef FEAT_BYTEOFF
10586 "byte_offset",
10587#endif
10588#ifdef FEAT_CINDENT
10589 "cindent",
10590#endif
10591#ifdef FEAT_CLIENTSERVER
10592 "clientserver",
10593#endif
10594#ifdef FEAT_CLIPBOARD
10595 "clipboard",
10596#endif
10597#ifdef FEAT_CMDL_COMPL
10598 "cmdline_compl",
10599#endif
10600#ifdef FEAT_CMDHIST
10601 "cmdline_hist",
10602#endif
10603#ifdef FEAT_COMMENTS
10604 "comments",
10605#endif
10606#ifdef FEAT_CRYPT
10607 "cryptv",
10608#endif
10609#ifdef FEAT_CSCOPE
10610 "cscope",
10611#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010612#ifdef CURSOR_SHAPE
10613 "cursorshape",
10614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615#ifdef DEBUG
10616 "debug",
10617#endif
10618#ifdef FEAT_CON_DIALOG
10619 "dialog_con",
10620#endif
10621#ifdef FEAT_GUI_DIALOG
10622 "dialog_gui",
10623#endif
10624#ifdef FEAT_DIFF
10625 "diff",
10626#endif
10627#ifdef FEAT_DIGRAPHS
10628 "digraphs",
10629#endif
10630#ifdef FEAT_DND
10631 "dnd",
10632#endif
10633#ifdef FEAT_EMACS_TAGS
10634 "emacs_tags",
10635#endif
10636 "eval", /* always present, of course! */
10637#ifdef FEAT_EX_EXTRA
10638 "ex_extra",
10639#endif
10640#ifdef FEAT_SEARCH_EXTRA
10641 "extra_search",
10642#endif
10643#ifdef FEAT_FKMAP
10644 "farsi",
10645#endif
10646#ifdef FEAT_SEARCHPATH
10647 "file_in_path",
10648#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010649#if defined(UNIX) && !defined(USE_SYSTEM)
10650 "filterpipe",
10651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652#ifdef FEAT_FIND_ID
10653 "find_in_path",
10654#endif
10655#ifdef FEAT_FOLDING
10656 "folding",
10657#endif
10658#ifdef FEAT_FOOTER
10659 "footer",
10660#endif
10661#if !defined(USE_SYSTEM) && defined(UNIX)
10662 "fork",
10663#endif
10664#ifdef FEAT_GETTEXT
10665 "gettext",
10666#endif
10667#ifdef FEAT_GUI
10668 "gui",
10669#endif
10670#ifdef FEAT_GUI_ATHENA
10671# ifdef FEAT_GUI_NEXTAW
10672 "gui_neXtaw",
10673# else
10674 "gui_athena",
10675# endif
10676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677#ifdef FEAT_GUI_GTK
10678 "gui_gtk",
10679# ifdef HAVE_GTK2
10680 "gui_gtk2",
10681# endif
10682#endif
10683#ifdef FEAT_GUI_MAC
10684 "gui_mac",
10685#endif
10686#ifdef FEAT_GUI_MOTIF
10687 "gui_motif",
10688#endif
10689#ifdef FEAT_GUI_PHOTON
10690 "gui_photon",
10691#endif
10692#ifdef FEAT_GUI_W16
10693 "gui_win16",
10694#endif
10695#ifdef FEAT_GUI_W32
10696 "gui_win32",
10697#endif
10698#ifdef FEAT_HANGULIN
10699 "hangul_input",
10700#endif
10701#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10702 "iconv",
10703#endif
10704#ifdef FEAT_INS_EXPAND
10705 "insert_expand",
10706#endif
10707#ifdef FEAT_JUMPLIST
10708 "jumplist",
10709#endif
10710#ifdef FEAT_KEYMAP
10711 "keymap",
10712#endif
10713#ifdef FEAT_LANGMAP
10714 "langmap",
10715#endif
10716#ifdef FEAT_LIBCALL
10717 "libcall",
10718#endif
10719#ifdef FEAT_LINEBREAK
10720 "linebreak",
10721#endif
10722#ifdef FEAT_LISP
10723 "lispindent",
10724#endif
10725#ifdef FEAT_LISTCMDS
10726 "listcmds",
10727#endif
10728#ifdef FEAT_LOCALMAP
10729 "localmap",
10730#endif
10731#ifdef FEAT_MENU
10732 "menu",
10733#endif
10734#ifdef FEAT_SESSION
10735 "mksession",
10736#endif
10737#ifdef FEAT_MODIFY_FNAME
10738 "modify_fname",
10739#endif
10740#ifdef FEAT_MOUSE
10741 "mouse",
10742#endif
10743#ifdef FEAT_MOUSESHAPE
10744 "mouseshape",
10745#endif
10746#if defined(UNIX) || defined(VMS)
10747# ifdef FEAT_MOUSE_DEC
10748 "mouse_dec",
10749# endif
10750# ifdef FEAT_MOUSE_GPM
10751 "mouse_gpm",
10752# endif
10753# ifdef FEAT_MOUSE_JSB
10754 "mouse_jsbterm",
10755# endif
10756# ifdef FEAT_MOUSE_NET
10757 "mouse_netterm",
10758# endif
10759# ifdef FEAT_MOUSE_PTERM
10760 "mouse_pterm",
10761# endif
10762# ifdef FEAT_MOUSE_XTERM
10763 "mouse_xterm",
10764# endif
10765#endif
10766#ifdef FEAT_MBYTE
10767 "multi_byte",
10768#endif
10769#ifdef FEAT_MBYTE_IME
10770 "multi_byte_ime",
10771#endif
10772#ifdef FEAT_MULTI_LANG
10773 "multi_lang",
10774#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010775#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010776#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010777 "mzscheme",
10778#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780#ifdef FEAT_OLE
10781 "ole",
10782#endif
10783#ifdef FEAT_OSFILETYPE
10784 "osfiletype",
10785#endif
10786#ifdef FEAT_PATH_EXTRA
10787 "path_extra",
10788#endif
10789#ifdef FEAT_PERL
10790#ifndef DYNAMIC_PERL
10791 "perl",
10792#endif
10793#endif
10794#ifdef FEAT_PYTHON
10795#ifndef DYNAMIC_PYTHON
10796 "python",
10797#endif
10798#endif
10799#ifdef FEAT_POSTSCRIPT
10800 "postscript",
10801#endif
10802#ifdef FEAT_PRINTER
10803 "printer",
10804#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010805#ifdef FEAT_PROFILE
10806 "profile",
10807#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010808#ifdef FEAT_RELTIME
10809 "reltime",
10810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811#ifdef FEAT_QUICKFIX
10812 "quickfix",
10813#endif
10814#ifdef FEAT_RIGHTLEFT
10815 "rightleft",
10816#endif
10817#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10818 "ruby",
10819#endif
10820#ifdef FEAT_SCROLLBIND
10821 "scrollbind",
10822#endif
10823#ifdef FEAT_CMDL_INFO
10824 "showcmd",
10825 "cmdline_info",
10826#endif
10827#ifdef FEAT_SIGNS
10828 "signs",
10829#endif
10830#ifdef FEAT_SMARTINDENT
10831 "smartindent",
10832#endif
10833#ifdef FEAT_SNIFF
10834 "sniff",
10835#endif
10836#ifdef FEAT_STL_OPT
10837 "statusline",
10838#endif
10839#ifdef FEAT_SUN_WORKSHOP
10840 "sun_workshop",
10841#endif
10842#ifdef FEAT_NETBEANS_INTG
10843 "netbeans_intg",
10844#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010845#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010846 "spell",
10847#endif
10848#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 "syntax",
10850#endif
10851#if defined(USE_SYSTEM) || !defined(UNIX)
10852 "system",
10853#endif
10854#ifdef FEAT_TAG_BINS
10855 "tag_binary",
10856#endif
10857#ifdef FEAT_TAG_OLDSTATIC
10858 "tag_old_static",
10859#endif
10860#ifdef FEAT_TAG_ANYWHITE
10861 "tag_any_white",
10862#endif
10863#ifdef FEAT_TCL
10864# ifndef DYNAMIC_TCL
10865 "tcl",
10866# endif
10867#endif
10868#ifdef TERMINFO
10869 "terminfo",
10870#endif
10871#ifdef FEAT_TERMRESPONSE
10872 "termresponse",
10873#endif
10874#ifdef FEAT_TEXTOBJ
10875 "textobjects",
10876#endif
10877#ifdef HAVE_TGETENT
10878 "tgetent",
10879#endif
10880#ifdef FEAT_TITLE
10881 "title",
10882#endif
10883#ifdef FEAT_TOOLBAR
10884 "toolbar",
10885#endif
10886#ifdef FEAT_USR_CMDS
10887 "user-commands", /* was accidentally included in 5.4 */
10888 "user_commands",
10889#endif
10890#ifdef FEAT_VIMINFO
10891 "viminfo",
10892#endif
10893#ifdef FEAT_VERTSPLIT
10894 "vertsplit",
10895#endif
10896#ifdef FEAT_VIRTUALEDIT
10897 "virtualedit",
10898#endif
10899#ifdef FEAT_VISUAL
10900 "visual",
10901#endif
10902#ifdef FEAT_VISUALEXTRA
10903 "visualextra",
10904#endif
10905#ifdef FEAT_VREPLACE
10906 "vreplace",
10907#endif
10908#ifdef FEAT_WILDIGN
10909 "wildignore",
10910#endif
10911#ifdef FEAT_WILDMENU
10912 "wildmenu",
10913#endif
10914#ifdef FEAT_WINDOWS
10915 "windows",
10916#endif
10917#ifdef FEAT_WAK
10918 "winaltkeys",
10919#endif
10920#ifdef FEAT_WRITEBACKUP
10921 "writebackup",
10922#endif
10923#ifdef FEAT_XIM
10924 "xim",
10925#endif
10926#ifdef FEAT_XFONTSET
10927 "xfontset",
10928#endif
10929#ifdef USE_XSMP
10930 "xsmp",
10931#endif
10932#ifdef USE_XSMP_INTERACT
10933 "xsmp_interact",
10934#endif
10935#ifdef FEAT_XCLIPBOARD
10936 "xterm_clipboard",
10937#endif
10938#ifdef FEAT_XTERM_SAVE
10939 "xterm_save",
10940#endif
10941#if defined(UNIX) && defined(FEAT_X11)
10942 "X11",
10943#endif
10944 NULL
10945 };
10946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 for (i = 0; has_list[i] != NULL; ++i)
10949 if (STRICMP(name, has_list[i]) == 0)
10950 {
10951 n = TRUE;
10952 break;
10953 }
10954
10955 if (n == FALSE)
10956 {
10957 if (STRNICMP(name, "patch", 5) == 0)
10958 n = has_patch(atoi((char *)name + 5));
10959 else if (STRICMP(name, "vim_starting") == 0)
10960 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010961#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10962 else if (STRICMP(name, "balloon_multiline") == 0)
10963 n = multiline_balloon_available();
10964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965#ifdef DYNAMIC_TCL
10966 else if (STRICMP(name, "tcl") == 0)
10967 n = tcl_enabled(FALSE);
10968#endif
10969#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10970 else if (STRICMP(name, "iconv") == 0)
10971 n = iconv_enabled(FALSE);
10972#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010973#ifdef DYNAMIC_MZSCHEME
10974 else if (STRICMP(name, "mzscheme") == 0)
10975 n = mzscheme_enabled(FALSE);
10976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977#ifdef DYNAMIC_RUBY
10978 else if (STRICMP(name, "ruby") == 0)
10979 n = ruby_enabled(FALSE);
10980#endif
10981#ifdef DYNAMIC_PYTHON
10982 else if (STRICMP(name, "python") == 0)
10983 n = python_enabled(FALSE);
10984#endif
10985#ifdef DYNAMIC_PERL
10986 else if (STRICMP(name, "perl") == 0)
10987 n = perl_enabled(FALSE);
10988#endif
10989#ifdef FEAT_GUI
10990 else if (STRICMP(name, "gui_running") == 0)
10991 n = (gui.in_use || gui.starting);
10992# ifdef FEAT_GUI_W32
10993 else if (STRICMP(name, "gui_win32s") == 0)
10994 n = gui_is_win32s();
10995# endif
10996# ifdef FEAT_BROWSE
10997 else if (STRICMP(name, "browse") == 0)
10998 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10999# endif
11000#endif
11001#ifdef FEAT_SYN_HL
11002 else if (STRICMP(name, "syntax_items") == 0)
11003 n = syntax_present(curbuf);
11004#endif
11005#if defined(WIN3264)
11006 else if (STRICMP(name, "win95") == 0)
11007 n = mch_windows95();
11008#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011009#ifdef FEAT_NETBEANS_INTG
11010 else if (STRICMP(name, "netbeans_enabled") == 0)
11011 n = usingNetbeans;
11012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 }
11014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016}
11017
11018/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019 * "has_key()" function
11020 */
11021 static void
11022f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011023 typval_T *argvars;
11024 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011025{
11026 rettv->vval.v_number = 0;
11027 if (argvars[0].v_type != VAR_DICT)
11028 {
11029 EMSG(_(e_dictreq));
11030 return;
11031 }
11032 if (argvars[0].vval.v_dict == NULL)
11033 return;
11034
11035 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011036 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011037}
11038
11039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 * "hasmapto()" function
11041 */
11042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011044 typval_T *argvars;
11045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046{
11047 char_u *name;
11048 char_u *mode;
11049 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011050 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 mode = (char_u *)"nvo";
11055 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011058 if (argvars[2].v_type != VAR_UNKNOWN)
11059 abbr = get_tv_number(&argvars[2]);
11060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061
Bram Moolenaar2c932302006-03-18 21:42:09 +000011062 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066}
11067
11068/*
11069 * "histadd()" function
11070 */
11071/*ARGSUSED*/
11072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011074 typval_T *argvars;
11075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076{
11077#ifdef FEAT_CMDHIST
11078 int histype;
11079 char_u *str;
11080 char_u buf[NUMBUFLEN];
11081#endif
11082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 if (check_restricted() || check_secure())
11085 return;
11086#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11088 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 if (histype >= 0)
11090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 if (*str != NUL)
11093 {
11094 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 return;
11097 }
11098 }
11099#endif
11100}
11101
11102/*
11103 * "histdel()" function
11104 */
11105/*ARGSUSED*/
11106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011108 typval_T *argvars;
11109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110{
11111#ifdef FEAT_CMDHIST
11112 int n;
11113 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011114 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11117 if (str == NULL)
11118 n = 0;
11119 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011121 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011122 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 else
11127 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 get_tv_string_buf(&argvars[1], buf));
11130 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133#endif
11134}
11135
11136/*
11137 * "histget()" function
11138 */
11139/*ARGSUSED*/
11140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 typval_T *argvars;
11143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144{
11145#ifdef FEAT_CMDHIST
11146 int type;
11147 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11151 if (str == NULL)
11152 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 {
11155 type = get_histtype(str);
11156 if (argvars[1].v_type == VAR_UNKNOWN)
11157 idx = get_history_idx(type);
11158 else
11159 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11160 /* -1 on type error */
11161 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167}
11168
11169/*
11170 * "histnr()" function
11171 */
11172/*ARGSUSED*/
11173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011175 typval_T *argvars;
11176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177{
11178 int i;
11179
11180#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181 char_u *history = get_tv_string_chk(&argvars[0]);
11182
11183 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 if (i >= HIST_CMD && i < HIST_COUNT)
11185 i = get_history_idx(i);
11186 else
11187#endif
11188 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190}
11191
11192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 * "highlightID(name)" function
11194 */
11195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011197 typval_T *argvars;
11198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201}
11202
11203/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011204 * "highlight_exists()" function
11205 */
11206 static void
11207f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011208 typval_T *argvars;
11209 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011210{
11211 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11212}
11213
11214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 * "hostname()" function
11216 */
11217/*ARGSUSED*/
11218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 typval_T *argvars;
11221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222{
11223 char_u hostname[256];
11224
11225 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->v_type = VAR_STRING;
11227 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228}
11229
11230/*
11231 * iconv() function
11232 */
11233/*ARGSUSED*/
11234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011235f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011236 typval_T *argvars;
11237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238{
11239#ifdef FEAT_MBYTE
11240 char_u buf1[NUMBUFLEN];
11241 char_u buf2[NUMBUFLEN];
11242 char_u *from, *to, *str;
11243 vimconv_T vimconv;
11244#endif
11245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246 rettv->v_type = VAR_STRING;
11247 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248
11249#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 str = get_tv_string(&argvars[0]);
11251 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11252 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 vimconv.vc_type = CONV_NONE;
11254 convert_setup(&vimconv, from, to);
11255
11256 /* If the encodings are equal, no conversion needed. */
11257 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261
11262 convert_setup(&vimconv, NULL, NULL);
11263 vim_free(from);
11264 vim_free(to);
11265#endif
11266}
11267
11268/*
11269 * "indent()" function
11270 */
11271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 typval_T *argvars;
11274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275{
11276 linenr_T lnum;
11277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283}
11284
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011285/*
11286 * "index()" function
11287 */
11288 static void
11289f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011290 typval_T *argvars;
11291 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011292{
Bram Moolenaar33570922005-01-25 22:26:29 +000011293 list_T *l;
11294 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011295 long idx = 0;
11296 int ic = FALSE;
11297
11298 rettv->vval.v_number = -1;
11299 if (argvars[0].v_type != VAR_LIST)
11300 {
11301 EMSG(_(e_listreq));
11302 return;
11303 }
11304 l = argvars[0].vval.v_list;
11305 if (l != NULL)
11306 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011307 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011308 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011310 int error = FALSE;
11311
Bram Moolenaar758711c2005-02-02 23:11:38 +000011312 /* Start at specified item. Use the cached index that list_find()
11313 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011314 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011315 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011316 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011317 ic = get_tv_number_chk(&argvars[3], &error);
11318 if (error)
11319 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011320 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011321
Bram Moolenaar758711c2005-02-02 23:11:38 +000011322 for ( ; item != NULL; item = item->li_next, ++idx)
11323 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011324 {
11325 rettv->vval.v_number = idx;
11326 break;
11327 }
11328 }
11329}
11330
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331static int inputsecret_flag = 0;
11332
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011333static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11334
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011336 * This function is used by f_input() and f_inputdialog() functions. The third
11337 * argument to f_input() specifies the type of completion to use at the
11338 * prompt. The third argument to f_inputdialog() specifies the value to return
11339 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 */
11341 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011342get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 typval_T *argvars;
11344 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011345 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 char_u *p = NULL;
11349 int c;
11350 char_u buf[NUMBUFLEN];
11351 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011352 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011353 int xp_type = EXPAND_NOTHING;
11354 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357
11358#ifdef NO_CONSOLE_INPUT
11359 /* While starting up, there is no place to enter text. */
11360 if (no_console_input())
11361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 return;
11364 }
11365#endif
11366
11367 cmd_silent = FALSE; /* Want to see the prompt. */
11368 if (prompt != NULL)
11369 {
11370 /* Only the part of the message after the last NL is considered as
11371 * prompt for the command line */
11372 p = vim_strrchr(prompt, '\n');
11373 if (p == NULL)
11374 p = prompt;
11375 else
11376 {
11377 ++p;
11378 c = *p;
11379 *p = NUL;
11380 msg_start();
11381 msg_clr_eos();
11382 msg_puts_attr(prompt, echo_attr);
11383 msg_didout = FALSE;
11384 msg_starthere();
11385 *p = c;
11386 }
11387 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 if (argvars[1].v_type != VAR_UNKNOWN)
11390 {
11391 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11392 if (defstr != NULL)
11393 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011395 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011396 {
11397 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011398 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011399 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011400
Bram Moolenaar4463f292005-09-25 22:20:24 +000011401 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011402
Bram Moolenaar4463f292005-09-25 22:20:24 +000011403 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11404 if (xp_name == NULL)
11405 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011406
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011407 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011408
Bram Moolenaar4463f292005-09-25 22:20:24 +000011409 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11410 &xp_arg) == FAIL)
11411 return;
11412 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011413 }
11414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415 if (defstr != NULL)
11416 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011417 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11418 xp_type, xp_arg);
11419
11420 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422 /* since the user typed this, no need to wait for return */
11423 need_wait_return = FALSE;
11424 msg_didout = FALSE;
11425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 cmd_silent = cmd_silent_save;
11427}
11428
11429/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011430 * "input()" function
11431 * Also handles inputsecret() when inputsecret is set.
11432 */
11433 static void
11434f_input(argvars, rettv)
11435 typval_T *argvars;
11436 typval_T *rettv;
11437{
11438 get_user_input(argvars, rettv, FALSE);
11439}
11440
11441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 * "inputdialog()" function
11443 */
11444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011446 typval_T *argvars;
11447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448{
11449#if defined(FEAT_GUI_TEXTDIALOG)
11450 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11451 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11452 {
11453 char_u *message;
11454 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 message = get_tv_string_chk(&argvars[0]);
11458 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011459 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011460 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 else
11462 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 if (message != NULL && defstr != NULL
11464 && do_dialog(VIM_QUESTION, NULL, message,
11465 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 else
11468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469 if (message != NULL && defstr != NULL
11470 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011471 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_string = vim_strsave(
11473 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 }
11479 else
11480#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011481 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482}
11483
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011484/*
11485 * "inputlist()" function
11486 */
11487 static void
11488f_inputlist(argvars, rettv)
11489 typval_T *argvars;
11490 typval_T *rettv;
11491{
11492 listitem_T *li;
11493 int selected;
11494 int mouse_used;
11495
11496 rettv->vval.v_number = 0;
11497#ifdef NO_CONSOLE_INPUT
11498 /* While starting up, there is no place to enter text. */
11499 if (no_console_input())
11500 return;
11501#endif
11502 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11503 {
11504 EMSG2(_(e_listarg), "inputlist()");
11505 return;
11506 }
11507
11508 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011509 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011510 lines_left = Rows; /* avoid more prompt */
11511 msg_scroll = TRUE;
11512 msg_clr_eos();
11513
11514 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11515 {
11516 msg_puts(get_tv_string(&li->li_tv));
11517 msg_putchar('\n');
11518 }
11519
11520 /* Ask for choice. */
11521 selected = prompt_for_number(&mouse_used);
11522 if (mouse_used)
11523 selected -= lines_left;
11524
11525 rettv->vval.v_number = selected;
11526}
11527
11528
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11530
11531/*
11532 * "inputrestore()" function
11533 */
11534/*ARGSUSED*/
11535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011537 typval_T *argvars;
11538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539{
11540 if (ga_userinput.ga_len > 0)
11541 {
11542 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11544 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 }
11547 else if (p_verbose > 1)
11548 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011549 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 }
11552}
11553
11554/*
11555 * "inputsave()" function
11556 */
11557/*ARGSUSED*/
11558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011560 typval_T *argvars;
11561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562{
11563 /* Add an entry to the stack of typehead storage. */
11564 if (ga_grow(&ga_userinput, 1) == OK)
11565 {
11566 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11567 + ga_userinput.ga_len);
11568 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 }
11571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573}
11574
11575/*
11576 * "inputsecret()" function
11577 */
11578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011580 typval_T *argvars;
11581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582{
11583 ++cmdline_star;
11584 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 --cmdline_star;
11587 --inputsecret_flag;
11588}
11589
11590/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011591 * "insert()" function
11592 */
11593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011595 typval_T *argvars;
11596 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011597{
11598 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011599 listitem_T *item;
11600 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011601 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011602
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011603 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011605 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011606 else if ((l = argvars[0].vval.v_list) != NULL
11607 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011608 {
11609 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 before = get_tv_number_chk(&argvars[2], &error);
11611 if (error)
11612 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011613
Bram Moolenaar758711c2005-02-02 23:11:38 +000011614 if (before == l->lv_len)
11615 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011616 else
11617 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011618 item = list_find(l, before);
11619 if (item == NULL)
11620 {
11621 EMSGN(_(e_listidx), before);
11622 l = NULL;
11623 }
11624 }
11625 if (l != NULL)
11626 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011627 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011628 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011629 }
11630 }
11631}
11632
11633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 * "isdirectory()" function
11635 */
11636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 typval_T *argvars;
11639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642}
11643
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011644/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011645 * "islocked()" function
11646 */
11647 static void
11648f_islocked(argvars, rettv)
11649 typval_T *argvars;
11650 typval_T *rettv;
11651{
11652 lval_T lv;
11653 char_u *end;
11654 dictitem_T *di;
11655
11656 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011657 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11658 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011659 if (end != NULL && lv.ll_name != NULL)
11660 {
11661 if (*end != NUL)
11662 EMSG(_(e_trailing));
11663 else
11664 {
11665 if (lv.ll_tv == NULL)
11666 {
11667 if (check_changedtick(lv.ll_name))
11668 rettv->vval.v_number = 1; /* always locked */
11669 else
11670 {
11671 di = find_var(lv.ll_name, NULL);
11672 if (di != NULL)
11673 {
11674 /* Consider a variable locked when:
11675 * 1. the variable itself is locked
11676 * 2. the value of the variable is locked.
11677 * 3. the List or Dict value is locked.
11678 */
11679 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11680 || tv_islocked(&di->di_tv));
11681 }
11682 }
11683 }
11684 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011685 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011686 else if (lv.ll_newkey != NULL)
11687 EMSG2(_(e_dictkey), lv.ll_newkey);
11688 else if (lv.ll_list != NULL)
11689 /* List item. */
11690 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11691 else
11692 /* Dictionary item. */
11693 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11694 }
11695 }
11696
11697 clear_lval(&lv);
11698}
11699
Bram Moolenaar33570922005-01-25 22:26:29 +000011700static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011701
11702/*
11703 * Turn a dict into a list:
11704 * "what" == 0: list of keys
11705 * "what" == 1: list of values
11706 * "what" == 2: list of items
11707 */
11708 static void
11709dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011710 typval_T *argvars;
11711 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011712 int what;
11713{
Bram Moolenaar33570922005-01-25 22:26:29 +000011714 list_T *l2;
11715 dictitem_T *di;
11716 hashitem_T *hi;
11717 listitem_T *li;
11718 listitem_T *li2;
11719 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011720 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011721
11722 rettv->vval.v_number = 0;
11723 if (argvars[0].v_type != VAR_DICT)
11724 {
11725 EMSG(_(e_dictreq));
11726 return;
11727 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011728 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011729 return;
11730
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011731 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011732 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011733
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011734 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011735 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011736 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011737 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011739 --todo;
11740 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011741
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011742 li = listitem_alloc();
11743 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011744 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011745 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011746
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011747 if (what == 0)
11748 {
11749 /* keys() */
11750 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011751 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011752 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11753 }
11754 else if (what == 1)
11755 {
11756 /* values() */
11757 copy_tv(&di->di_tv, &li->li_tv);
11758 }
11759 else
11760 {
11761 /* items() */
11762 l2 = list_alloc();
11763 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011764 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011765 li->li_tv.vval.v_list = l2;
11766 if (l2 == NULL)
11767 break;
11768 ++l2->lv_refcount;
11769
11770 li2 = listitem_alloc();
11771 if (li2 == NULL)
11772 break;
11773 list_append(l2, li2);
11774 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011775 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011776 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11777
11778 li2 = listitem_alloc();
11779 if (li2 == NULL)
11780 break;
11781 list_append(l2, li2);
11782 copy_tv(&di->di_tv, &li2->li_tv);
11783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011784 }
11785 }
11786}
11787
11788/*
11789 * "items(dict)" function
11790 */
11791 static void
11792f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 typval_T *argvars;
11794 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011795{
11796 dict_list(argvars, rettv, 2);
11797}
11798
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011800 * "join()" function
11801 */
11802 static void
11803f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011806{
11807 garray_T ga;
11808 char_u *sep;
11809
11810 rettv->vval.v_number = 0;
11811 if (argvars[0].v_type != VAR_LIST)
11812 {
11813 EMSG(_(e_listreq));
11814 return;
11815 }
11816 if (argvars[0].vval.v_list == NULL)
11817 return;
11818 if (argvars[1].v_type == VAR_UNKNOWN)
11819 sep = (char_u *)" ";
11820 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011821 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011822
11823 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824
11825 if (sep != NULL)
11826 {
11827 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011828 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829 ga_append(&ga, NUL);
11830 rettv->vval.v_string = (char_u *)ga.ga_data;
11831 }
11832 else
11833 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011834}
11835
11836/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011837 * "keys()" function
11838 */
11839 static void
11840f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011841 typval_T *argvars;
11842 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011843{
11844 dict_list(argvars, rettv, 0);
11845}
11846
11847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 * "last_buffer_nr()" function.
11849 */
11850/*ARGSUSED*/
11851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011853 typval_T *argvars;
11854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855{
11856 int n = 0;
11857 buf_T *buf;
11858
11859 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11860 if (n < buf->b_fnum)
11861 n = buf->b_fnum;
11862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011863 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011864}
11865
11866/*
11867 * "len()" function
11868 */
11869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011871 typval_T *argvars;
11872 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011873{
11874 switch (argvars[0].v_type)
11875 {
11876 case VAR_STRING:
11877 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->vval.v_number = (varnumber_T)STRLEN(
11879 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011880 break;
11881 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011883 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011884 case VAR_DICT:
11885 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11886 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011887 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011888 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011889 break;
11890 }
11891}
11892
Bram Moolenaar33570922005-01-25 22:26:29 +000011893static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011894
11895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011897 typval_T *argvars;
11898 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011899 int type;
11900{
11901#ifdef FEAT_LIBCALL
11902 char_u *string_in;
11903 char_u **string_result;
11904 int nr_result;
11905#endif
11906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011908 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011909 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011910 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011912
11913 if (check_restricted() || check_secure())
11914 return;
11915
11916#ifdef FEAT_LIBCALL
11917 /* The first two args must be strings, otherwise its meaningless */
11918 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11919 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011920 string_in = NULL;
11921 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011922 string_in = argvars[2].vval.v_string;
11923 if (type == VAR_NUMBER)
11924 string_result = NULL;
11925 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011927 if (mch_libcall(argvars[0].vval.v_string,
11928 argvars[1].vval.v_string,
11929 string_in,
11930 argvars[2].vval.v_number,
11931 string_result,
11932 &nr_result) == OK
11933 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011935 }
11936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937}
11938
11939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011940 * "libcall()" function
11941 */
11942 static void
11943f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 typval_T *argvars;
11945 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011946{
11947 libcall_common(argvars, rettv, VAR_STRING);
11948}
11949
11950/*
11951 * "libcallnr()" function
11952 */
11953 static void
11954f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011955 typval_T *argvars;
11956 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011957{
11958 libcall_common(argvars, rettv, VAR_NUMBER);
11959}
11960
11961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 * "line(string)" function
11963 */
11964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011966 typval_T *argvars;
11967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968{
11969 linenr_T lnum = 0;
11970 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011971 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011973 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 if (fp != NULL)
11975 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977}
11978
11979/*
11980 * "line2byte(lnum)" function
11981 */
11982/*ARGSUSED*/
11983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011985 typval_T *argvars;
11986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987{
11988#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990#else
11991 linenr_T lnum;
11992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11998 if (rettv->vval.v_number >= 0)
11999 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000#endif
12001}
12002
12003/*
12004 * "lispindent(lnum)" function
12005 */
12006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 typval_T *argvars;
12009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010{
12011#ifdef FEAT_LISP
12012 pos_T pos;
12013 linenr_T lnum;
12014
12015 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12018 {
12019 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 curwin->w_cursor = pos;
12022 }
12023 else
12024#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026}
12027
12028/*
12029 * "localtime()" function
12030 */
12031/*ARGSUSED*/
12032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012034 typval_T *argvars;
12035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038}
12039
Bram Moolenaar33570922005-01-25 22:26:29 +000012040static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041
12042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012044 typval_T *argvars;
12045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 int exact;
12047{
12048 char_u *keys;
12049 char_u *which;
12050 char_u buf[NUMBUFLEN];
12051 char_u *keys_buf = NULL;
12052 char_u *rhs;
12053 int mode;
12054 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012055 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056
12057 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->v_type = VAR_STRING;
12059 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 if (*keys == NUL)
12063 return;
12064
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012065 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012068 if (argvars[2].v_type != VAR_UNKNOWN)
12069 abbr = get_tv_number(&argvars[2]);
12070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 else
12072 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 if (which == NULL)
12074 return;
12075
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 mode = get_map_mode(&which, 0);
12077
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012078 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012079 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 vim_free(keys_buf);
12081 if (rhs != NULL)
12082 {
12083 ga_init(&ga);
12084 ga.ga_itemsize = 1;
12085 ga.ga_growsize = 40;
12086
12087 while (*rhs != NUL)
12088 ga_concat(&ga, str2special(&rhs, FALSE));
12089
12090 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 }
12093}
12094
12095/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012096 * "map()" function
12097 */
12098 static void
12099f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012100 typval_T *argvars;
12101 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012102{
12103 filter_map(argvars, rettv, TRUE);
12104}
12105
12106/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012107 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 */
12109 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012110f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012111 typval_T *argvars;
12112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012114 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115}
12116
12117/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012118 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 */
12120 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012121f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012122 typval_T *argvars;
12123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012125 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126}
12127
Bram Moolenaar33570922005-01-25 22:26:29 +000012128static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129
12130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 typval_T *argvars;
12133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 int type;
12135{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012136 char_u *str = NULL;
12137 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138 char_u *pat;
12139 regmatch_T regmatch;
12140 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012141 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 char_u *save_cpo;
12143 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012144 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012145 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012146 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012147 list_T *l = NULL;
12148 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012149 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012150 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151
12152 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12153 save_cpo = p_cpo;
12154 p_cpo = (char_u *)"";
12155
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012156 rettv->vval.v_number = -1;
12157 if (type == 3)
12158 {
12159 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012160 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012161 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012162 }
12163 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165 rettv->v_type = VAR_STRING;
12166 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012169 if (argvars[0].v_type == VAR_LIST)
12170 {
12171 if ((l = argvars[0].vval.v_list) == NULL)
12172 goto theend;
12173 li = l->lv_first;
12174 }
12175 else
12176 expr = str = get_tv_string(&argvars[0]);
12177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012178 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12179 if (pat == NULL)
12180 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012181
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012182 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 int error = FALSE;
12185
12186 start = get_tv_number_chk(&argvars[2], &error);
12187 if (error)
12188 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012189 if (l != NULL)
12190 {
12191 li = list_find(l, start);
12192 if (li == NULL)
12193 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012194 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012195 }
12196 else
12197 {
12198 if (start < 0)
12199 start = 0;
12200 if (start > (long)STRLEN(str))
12201 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012202 /* When "count" argument is there ignore matches before "start",
12203 * otherwise skip part of the string. Differs when pattern is "^"
12204 * or "\<". */
12205 if (argvars[3].v_type != VAR_UNKNOWN)
12206 startcol = start;
12207 else
12208 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012209 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012210
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012211 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012212 nth = get_tv_number_chk(&argvars[3], &error);
12213 if (error)
12214 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 }
12216
12217 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12218 if (regmatch.regprog != NULL)
12219 {
12220 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012221
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012222 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012223 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012224 if (l != NULL)
12225 {
12226 if (li == NULL)
12227 {
12228 match = FALSE;
12229 break;
12230 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012231 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012232 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012233 if (str == NULL)
12234 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012235 }
12236
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012237 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012238
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012239 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012240 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012241 if (l == NULL && !match)
12242 break;
12243
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012244 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012245 if (l != NULL)
12246 {
12247 li = li->li_next;
12248 ++idx;
12249 }
12250 else
12251 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012252#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012253 startcol = (colnr_T)(regmatch.startp[0]
12254 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012255#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012256 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012257#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012258 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012259 }
12260
12261 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012263 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012265 int i;
12266
12267 /* return list with matched string and submatches */
12268 for (i = 0; i < NSUBEXP; ++i)
12269 {
12270 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012271 {
12272 if (list_append_string(rettv->vval.v_list,
12273 (char_u *)"", 0) == FAIL)
12274 break;
12275 }
12276 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012277 regmatch.startp[i],
12278 (int)(regmatch.endp[i] - regmatch.startp[i]))
12279 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012280 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012281 }
12282 }
12283 else if (type == 2)
12284 {
12285 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012286 if (l != NULL)
12287 copy_tv(&li->li_tv, rettv);
12288 else
12289 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012291 }
12292 else if (l != NULL)
12293 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 else
12295 {
12296 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 (varnumber_T)(regmatch.startp[0] - str);
12299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012302 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 }
12304 }
12305 vim_free(regmatch.regprog);
12306 }
12307
12308theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012309 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 p_cpo = save_cpo;
12311}
12312
12313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012314 * "match()" function
12315 */
12316 static void
12317f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 typval_T *argvars;
12319 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012320{
12321 find_some_match(argvars, rettv, 1);
12322}
12323
12324/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012325 * "matcharg()" function
12326 */
12327 static void
12328f_matcharg(argvars, rettv)
12329 typval_T *argvars;
12330 typval_T *rettv;
12331{
12332 if (rettv_list_alloc(rettv) == OK)
12333 {
12334#ifdef FEAT_SEARCH_EXTRA
12335 int mi = get_tv_number(&argvars[0]);
12336
12337 if (mi >= 1 && mi <= 3)
12338 {
12339 list_append_string(rettv->vval.v_list,
12340 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12341 list_append_string(rettv->vval.v_list,
12342 curwin->w_match_pat[mi - 1], -1);
12343 }
12344#endif
12345 }
12346}
12347
12348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012349 * "matchend()" function
12350 */
12351 static void
12352f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012353 typval_T *argvars;
12354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012355{
12356 find_some_match(argvars, rettv, 0);
12357}
12358
12359/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012360 * "matchlist()" function
12361 */
12362 static void
12363f_matchlist(argvars, rettv)
12364 typval_T *argvars;
12365 typval_T *rettv;
12366{
12367 find_some_match(argvars, rettv, 3);
12368}
12369
12370/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371 * "matchstr()" function
12372 */
12373 static void
12374f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012375 typval_T *argvars;
12376 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012377{
12378 find_some_match(argvars, rettv, 2);
12379}
12380
Bram Moolenaar33570922005-01-25 22:26:29 +000012381static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012382
12383 static void
12384max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012385 typval_T *argvars;
12386 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012387 int domax;
12388{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012389 long n = 0;
12390 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012391 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012392
12393 if (argvars[0].v_type == VAR_LIST)
12394 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012395 list_T *l;
12396 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012397
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012398 l = argvars[0].vval.v_list;
12399 if (l != NULL)
12400 {
12401 li = l->lv_first;
12402 if (li != NULL)
12403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012404 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012405 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012406 {
12407 li = li->li_next;
12408 if (li == NULL)
12409 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012411 if (domax ? i > n : i < n)
12412 n = i;
12413 }
12414 }
12415 }
12416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012417 else if (argvars[0].v_type == VAR_DICT)
12418 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012419 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012420 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012422 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012423
12424 d = argvars[0].vval.v_dict;
12425 if (d != NULL)
12426 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012427 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012428 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012429 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012430 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012432 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012433 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012434 if (first)
12435 {
12436 n = i;
12437 first = FALSE;
12438 }
12439 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012440 n = i;
12441 }
12442 }
12443 }
12444 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012445 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012446 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012447 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012448}
12449
12450/*
12451 * "max()" function
12452 */
12453 static void
12454f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012455 typval_T *argvars;
12456 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012457{
12458 max_min(argvars, rettv, TRUE);
12459}
12460
12461/*
12462 * "min()" function
12463 */
12464 static void
12465f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012466 typval_T *argvars;
12467 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012468{
12469 max_min(argvars, rettv, FALSE);
12470}
12471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012472static int mkdir_recurse __ARGS((char_u *dir, int prot));
12473
12474/*
12475 * Create the directory in which "dir" is located, and higher levels when
12476 * needed.
12477 */
12478 static int
12479mkdir_recurse(dir, prot)
12480 char_u *dir;
12481 int prot;
12482{
12483 char_u *p;
12484 char_u *updir;
12485 int r = FAIL;
12486
12487 /* Get end of directory name in "dir".
12488 * We're done when it's "/" or "c:/". */
12489 p = gettail_sep(dir);
12490 if (p <= get_past_head(dir))
12491 return OK;
12492
12493 /* If the directory exists we're done. Otherwise: create it.*/
12494 updir = vim_strnsave(dir, (int)(p - dir));
12495 if (updir == NULL)
12496 return FAIL;
12497 if (mch_isdir(updir))
12498 r = OK;
12499 else if (mkdir_recurse(updir, prot) == OK)
12500 r = vim_mkdir_emsg(updir, prot);
12501 vim_free(updir);
12502 return r;
12503}
12504
12505#ifdef vim_mkdir
12506/*
12507 * "mkdir()" function
12508 */
12509 static void
12510f_mkdir(argvars, rettv)
12511 typval_T *argvars;
12512 typval_T *rettv;
12513{
12514 char_u *dir;
12515 char_u buf[NUMBUFLEN];
12516 int prot = 0755;
12517
12518 rettv->vval.v_number = FAIL;
12519 if (check_restricted() || check_secure())
12520 return;
12521
12522 dir = get_tv_string_buf(&argvars[0], buf);
12523 if (argvars[1].v_type != VAR_UNKNOWN)
12524 {
12525 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012526 prot = get_tv_number_chk(&argvars[2], NULL);
12527 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012528 mkdir_recurse(dir, prot);
12529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012530 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012531}
12532#endif
12533
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 * "mode()" function
12536 */
12537/*ARGSUSED*/
12538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012540 typval_T *argvars;
12541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542{
12543 char_u buf[2];
12544
12545#ifdef FEAT_VISUAL
12546 if (VIsual_active)
12547 {
12548 if (VIsual_select)
12549 buf[0] = VIsual_mode + 's' - 'v';
12550 else
12551 buf[0] = VIsual_mode;
12552 }
12553 else
12554#endif
12555 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12556 buf[0] = 'r';
12557 else if (State & INSERT)
12558 {
12559 if (State & REPLACE_FLAG)
12560 buf[0] = 'R';
12561 else
12562 buf[0] = 'i';
12563 }
12564 else if (State & CMDLINE)
12565 buf[0] = 'c';
12566 else
12567 buf[0] = 'n';
12568
12569 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 rettv->vval.v_string = vim_strsave(buf);
12571 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572}
12573
12574/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012575 * "nextnonblank()" function
12576 */
12577 static void
12578f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012579 typval_T *argvars;
12580 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581{
12582 linenr_T lnum;
12583
12584 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012586 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587 {
12588 lnum = 0;
12589 break;
12590 }
12591 if (*skipwhite(ml_get(lnum)) != NUL)
12592 break;
12593 }
12594 rettv->vval.v_number = lnum;
12595}
12596
12597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598 * "nr2char()" function
12599 */
12600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012601f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012602 typval_T *argvars;
12603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604{
12605 char_u buf[NUMBUFLEN];
12606
12607#ifdef FEAT_MBYTE
12608 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610 else
12611#endif
12612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 buf[1] = NUL;
12615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->v_type = VAR_STRING;
12617 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012618}
12619
12620/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012621 * "pathshorten()" function
12622 */
12623 static void
12624f_pathshorten(argvars, rettv)
12625 typval_T *argvars;
12626 typval_T *rettv;
12627{
12628 char_u *p;
12629
12630 rettv->v_type = VAR_STRING;
12631 p = get_tv_string_chk(&argvars[0]);
12632 if (p == NULL)
12633 rettv->vval.v_string = NULL;
12634 else
12635 {
12636 p = vim_strsave(p);
12637 rettv->vval.v_string = p;
12638 if (p != NULL)
12639 shorten_dir(p);
12640 }
12641}
12642
12643/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012644 * "prevnonblank()" function
12645 */
12646 static void
12647f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012648 typval_T *argvars;
12649 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012650{
12651 linenr_T lnum;
12652
12653 lnum = get_tv_lnum(argvars);
12654 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12655 lnum = 0;
12656 else
12657 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12658 --lnum;
12659 rettv->vval.v_number = lnum;
12660}
12661
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012662#ifdef HAVE_STDARG_H
12663/* This dummy va_list is here because:
12664 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12665 * - locally in the function results in a "used before set" warning
12666 * - using va_start() to initialize it gives "function with fixed args" error */
12667static va_list ap;
12668#endif
12669
Bram Moolenaar8c711452005-01-14 21:53:12 +000012670/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012671 * "printf()" function
12672 */
12673 static void
12674f_printf(argvars, rettv)
12675 typval_T *argvars;
12676 typval_T *rettv;
12677{
12678 rettv->v_type = VAR_STRING;
12679 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012680#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012681 {
12682 char_u buf[NUMBUFLEN];
12683 int len;
12684 char_u *s;
12685 int saved_did_emsg = did_emsg;
12686 char *fmt;
12687
12688 /* Get the required length, allocate the buffer and do it for real. */
12689 did_emsg = FALSE;
12690 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012691 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012692 if (!did_emsg)
12693 {
12694 s = alloc(len + 1);
12695 if (s != NULL)
12696 {
12697 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012698 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012699 }
12700 }
12701 did_emsg |= saved_did_emsg;
12702 }
12703#endif
12704}
12705
12706/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012707 * "pumvisible()" function
12708 */
12709/*ARGSUSED*/
12710 static void
12711f_pumvisible(argvars, rettv)
12712 typval_T *argvars;
12713 typval_T *rettv;
12714{
12715 rettv->vval.v_number = 0;
12716#ifdef FEAT_INS_EXPAND
12717 if (pum_visible())
12718 rettv->vval.v_number = 1;
12719#endif
12720}
12721
12722/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012723 * "range()" function
12724 */
12725 static void
12726f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012727 typval_T *argvars;
12728 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012729{
12730 long start;
12731 long end;
12732 long stride = 1;
12733 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012734 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012736 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012737 if (argvars[1].v_type == VAR_UNKNOWN)
12738 {
12739 end = start - 1;
12740 start = 0;
12741 }
12742 else
12743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012745 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012747 }
12748
12749 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012750 if (error)
12751 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012752 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012753 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012754 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012755 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012756 else
12757 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012758 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012759 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012760 if (list_append_number(rettv->vval.v_list,
12761 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012762 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012763 }
12764}
12765
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012766/*
12767 * "readfile()" function
12768 */
12769 static void
12770f_readfile(argvars, rettv)
12771 typval_T *argvars;
12772 typval_T *rettv;
12773{
12774 int binary = FALSE;
12775 char_u *fname;
12776 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012777 listitem_T *li;
12778#define FREAD_SIZE 200 /* optimized for text lines */
12779 char_u buf[FREAD_SIZE];
12780 int readlen; /* size of last fread() */
12781 int buflen; /* nr of valid chars in buf[] */
12782 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12783 int tolist; /* first byte in buf[] still to be put in list */
12784 int chop; /* how many CR to chop off */
12785 char_u *prev = NULL; /* previously read bytes, if any */
12786 int prevlen = 0; /* length of "prev" if not NULL */
12787 char_u *s;
12788 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012789 long maxline = MAXLNUM;
12790 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012791
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012792 if (argvars[1].v_type != VAR_UNKNOWN)
12793 {
12794 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12795 binary = TRUE;
12796 if (argvars[2].v_type != VAR_UNKNOWN)
12797 maxline = get_tv_number(&argvars[2]);
12798 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012799
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012800 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012801 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012802
12803 /* Always open the file in binary mode, library functions have a mind of
12804 * their own about CR-LF conversion. */
12805 fname = get_tv_string(&argvars[0]);
12806 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12807 {
12808 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12809 return;
12810 }
12811
12812 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012813 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012814 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012815 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012816 buflen = filtd + readlen;
12817 tolist = 0;
12818 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12819 {
12820 if (buf[filtd] == '\n' || readlen <= 0)
12821 {
12822 /* Only when in binary mode add an empty list item when the
12823 * last line ends in a '\n'. */
12824 if (!binary && readlen == 0 && filtd == 0)
12825 break;
12826
12827 /* Found end-of-line or end-of-file: add a text line to the
12828 * list. */
12829 chop = 0;
12830 if (!binary)
12831 while (filtd - chop - 1 >= tolist
12832 && buf[filtd - chop - 1] == '\r')
12833 ++chop;
12834 len = filtd - tolist - chop;
12835 if (prev == NULL)
12836 s = vim_strnsave(buf + tolist, len);
12837 else
12838 {
12839 s = alloc((unsigned)(prevlen + len + 1));
12840 if (s != NULL)
12841 {
12842 mch_memmove(s, prev, prevlen);
12843 vim_free(prev);
12844 prev = NULL;
12845 mch_memmove(s + prevlen, buf + tolist, len);
12846 s[prevlen + len] = NUL;
12847 }
12848 }
12849 tolist = filtd + 1;
12850
12851 li = listitem_alloc();
12852 if (li == NULL)
12853 {
12854 vim_free(s);
12855 break;
12856 }
12857 li->li_tv.v_type = VAR_STRING;
12858 li->li_tv.v_lock = 0;
12859 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012860 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012861
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012862 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012863 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012864 if (readlen <= 0)
12865 break;
12866 }
12867 else if (buf[filtd] == NUL)
12868 buf[filtd] = '\n';
12869 }
12870 if (readlen <= 0)
12871 break;
12872
12873 if (tolist == 0)
12874 {
12875 /* "buf" is full, need to move text to an allocated buffer */
12876 if (prev == NULL)
12877 {
12878 prev = vim_strnsave(buf, buflen);
12879 prevlen = buflen;
12880 }
12881 else
12882 {
12883 s = alloc((unsigned)(prevlen + buflen));
12884 if (s != NULL)
12885 {
12886 mch_memmove(s, prev, prevlen);
12887 mch_memmove(s + prevlen, buf, buflen);
12888 vim_free(prev);
12889 prev = s;
12890 prevlen += buflen;
12891 }
12892 }
12893 filtd = 0;
12894 }
12895 else
12896 {
12897 mch_memmove(buf, buf + tolist, buflen - tolist);
12898 filtd -= tolist;
12899 }
12900 }
12901
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012902 /*
12903 * For a negative line count use only the lines at the end of the file,
12904 * free the rest.
12905 */
12906 if (maxline < 0)
12907 while (cnt > -maxline)
12908 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012909 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012910 --cnt;
12911 }
12912
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012913 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012914 fclose(fd);
12915}
12916
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012917#if defined(FEAT_RELTIME)
12918static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12919
12920/*
12921 * Convert a List to proftime_T.
12922 * Return FAIL when there is something wrong.
12923 */
12924 static int
12925list2proftime(arg, tm)
12926 typval_T *arg;
12927 proftime_T *tm;
12928{
12929 long n1, n2;
12930 int error = FALSE;
12931
12932 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12933 || arg->vval.v_list->lv_len != 2)
12934 return FAIL;
12935 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12936 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12937# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012938 tm->HighPart = n1;
12939 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012940# else
12941 tm->tv_sec = n1;
12942 tm->tv_usec = n2;
12943# endif
12944 return error ? FAIL : OK;
12945}
12946#endif /* FEAT_RELTIME */
12947
12948/*
12949 * "reltime()" function
12950 */
12951 static void
12952f_reltime(argvars, rettv)
12953 typval_T *argvars;
12954 typval_T *rettv;
12955{
12956#ifdef FEAT_RELTIME
12957 proftime_T res;
12958 proftime_T start;
12959
12960 if (argvars[0].v_type == VAR_UNKNOWN)
12961 {
12962 /* No arguments: get current time. */
12963 profile_start(&res);
12964 }
12965 else if (argvars[1].v_type == VAR_UNKNOWN)
12966 {
12967 if (list2proftime(&argvars[0], &res) == FAIL)
12968 return;
12969 profile_end(&res);
12970 }
12971 else
12972 {
12973 /* Two arguments: compute the difference. */
12974 if (list2proftime(&argvars[0], &start) == FAIL
12975 || list2proftime(&argvars[1], &res) == FAIL)
12976 return;
12977 profile_sub(&res, &start);
12978 }
12979
12980 if (rettv_list_alloc(rettv) == OK)
12981 {
12982 long n1, n2;
12983
12984# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012985 n1 = res.HighPart;
12986 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012987# else
12988 n1 = res.tv_sec;
12989 n2 = res.tv_usec;
12990# endif
12991 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12992 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12993 }
12994#endif
12995}
12996
12997/*
12998 * "reltimestr()" function
12999 */
13000 static void
13001f_reltimestr(argvars, rettv)
13002 typval_T *argvars;
13003 typval_T *rettv;
13004{
13005#ifdef FEAT_RELTIME
13006 proftime_T tm;
13007#endif
13008
13009 rettv->v_type = VAR_STRING;
13010 rettv->vval.v_string = NULL;
13011#ifdef FEAT_RELTIME
13012 if (list2proftime(&argvars[0], &tm) == OK)
13013 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13014#endif
13015}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013016
Bram Moolenaar0d660222005-01-07 21:51:51 +000013017#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13018static void make_connection __ARGS((void));
13019static int check_connection __ARGS((void));
13020
13021 static void
13022make_connection()
13023{
13024 if (X_DISPLAY == NULL
13025# ifdef FEAT_GUI
13026 && !gui.in_use
13027# endif
13028 )
13029 {
13030 x_force_connect = TRUE;
13031 setup_term_clip();
13032 x_force_connect = FALSE;
13033 }
13034}
13035
13036 static int
13037check_connection()
13038{
13039 make_connection();
13040 if (X_DISPLAY == NULL)
13041 {
13042 EMSG(_("E240: No connection to Vim server"));
13043 return FAIL;
13044 }
13045 return OK;
13046}
13047#endif
13048
13049#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013050static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013051
13052 static void
13053remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013054 typval_T *argvars;
13055 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013056 int expr;
13057{
13058 char_u *server_name;
13059 char_u *keys;
13060 char_u *r = NULL;
13061 char_u buf[NUMBUFLEN];
13062# ifdef WIN32
13063 HWND w;
13064# else
13065 Window w;
13066# endif
13067
13068 if (check_restricted() || check_secure())
13069 return;
13070
13071# ifdef FEAT_X11
13072 if (check_connection() == FAIL)
13073 return;
13074# endif
13075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013076 server_name = get_tv_string_chk(&argvars[0]);
13077 if (server_name == NULL)
13078 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013079 keys = get_tv_string_buf(&argvars[1], buf);
13080# ifdef WIN32
13081 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13082# else
13083 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13084 < 0)
13085# endif
13086 {
13087 if (r != NULL)
13088 EMSG(r); /* sending worked but evaluation failed */
13089 else
13090 EMSG2(_("E241: Unable to send to %s"), server_name);
13091 return;
13092 }
13093
13094 rettv->vval.v_string = r;
13095
13096 if (argvars[2].v_type != VAR_UNKNOWN)
13097 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013098 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013099 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013101
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013102 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013103 v.di_tv.v_type = VAR_STRING;
13104 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013105 idvar = get_tv_string_chk(&argvars[2]);
13106 if (idvar != NULL)
13107 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013108 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013109 }
13110}
13111#endif
13112
13113/*
13114 * "remote_expr()" function
13115 */
13116/*ARGSUSED*/
13117 static void
13118f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 typval_T *argvars;
13120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013121{
13122 rettv->v_type = VAR_STRING;
13123 rettv->vval.v_string = NULL;
13124#ifdef FEAT_CLIENTSERVER
13125 remote_common(argvars, rettv, TRUE);
13126#endif
13127}
13128
13129/*
13130 * "remote_foreground()" function
13131 */
13132/*ARGSUSED*/
13133 static void
13134f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013135 typval_T *argvars;
13136 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013137{
13138 rettv->vval.v_number = 0;
13139#ifdef FEAT_CLIENTSERVER
13140# ifdef WIN32
13141 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013142 {
13143 char_u *server_name = get_tv_string_chk(&argvars[0]);
13144
13145 if (server_name != NULL)
13146 serverForeground(server_name);
13147 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013148# else
13149 /* Send a foreground() expression to the server. */
13150 argvars[1].v_type = VAR_STRING;
13151 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13152 argvars[2].v_type = VAR_UNKNOWN;
13153 remote_common(argvars, rettv, TRUE);
13154 vim_free(argvars[1].vval.v_string);
13155# endif
13156#endif
13157}
13158
13159/*ARGSUSED*/
13160 static void
13161f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013162 typval_T *argvars;
13163 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013164{
13165#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013167 char_u *s = NULL;
13168# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013169 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013170# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013171 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013172
13173 if (check_restricted() || check_secure())
13174 {
13175 rettv->vval.v_number = -1;
13176 return;
13177 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013178 serverid = get_tv_string_chk(&argvars[0]);
13179 if (serverid == NULL)
13180 {
13181 rettv->vval.v_number = -1;
13182 return; /* type error; errmsg already given */
13183 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013184# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013185 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013186 if (n == 0)
13187 rettv->vval.v_number = -1;
13188 else
13189 {
13190 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13191 rettv->vval.v_number = (s != NULL);
13192 }
13193# else
13194 rettv->vval.v_number = 0;
13195 if (check_connection() == FAIL)
13196 return;
13197
13198 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013200# endif
13201
13202 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013204 char_u *retvar;
13205
Bram Moolenaar33570922005-01-25 22:26:29 +000013206 v.di_tv.v_type = VAR_STRING;
13207 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013208 retvar = get_tv_string_chk(&argvars[1]);
13209 if (retvar != NULL)
13210 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013211 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013212 }
13213#else
13214 rettv->vval.v_number = -1;
13215#endif
13216}
13217
13218/*ARGSUSED*/
13219 static void
13220f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013221 typval_T *argvars;
13222 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013223{
13224 char_u *r = NULL;
13225
13226#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013227 char_u *serverid = get_tv_string_chk(&argvars[0]);
13228
13229 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230 {
13231# ifdef WIN32
13232 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013233 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013234
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013235 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013236 if (n != 0)
13237 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13238 if (r == NULL)
13239# else
13240 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013241 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013242# endif
13243 EMSG(_("E277: Unable to read a server reply"));
13244 }
13245#endif
13246 rettv->v_type = VAR_STRING;
13247 rettv->vval.v_string = r;
13248}
13249
13250/*
13251 * "remote_send()" function
13252 */
13253/*ARGSUSED*/
13254 static void
13255f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013256 typval_T *argvars;
13257 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013258{
13259 rettv->v_type = VAR_STRING;
13260 rettv->vval.v_string = NULL;
13261#ifdef FEAT_CLIENTSERVER
13262 remote_common(argvars, rettv, FALSE);
13263#endif
13264}
13265
13266/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013267 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013268 */
13269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013271 typval_T *argvars;
13272 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013273{
Bram Moolenaar33570922005-01-25 22:26:29 +000013274 list_T *l;
13275 listitem_T *item, *item2;
13276 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013277 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013278 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013279 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013280 dict_T *d;
13281 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013282
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013283 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013284 if (argvars[0].v_type == VAR_DICT)
13285 {
13286 if (argvars[2].v_type != VAR_UNKNOWN)
13287 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013288 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013289 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013291 key = get_tv_string_chk(&argvars[1]);
13292 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 di = dict_find(d, key, -1);
13295 if (di == NULL)
13296 EMSG2(_(e_dictkey), key);
13297 else
13298 {
13299 *rettv = di->di_tv;
13300 init_tv(&di->di_tv);
13301 dictitem_remove(d, di);
13302 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013303 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013304 }
13305 }
13306 else if (argvars[0].v_type != VAR_LIST)
13307 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013308 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013309 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 int error = FALSE;
13312
13313 idx = get_tv_number_chk(&argvars[1], &error);
13314 if (error)
13315 ; /* type error: do nothing, errmsg already given */
13316 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013317 EMSGN(_(e_listidx), idx);
13318 else
13319 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013320 if (argvars[2].v_type == VAR_UNKNOWN)
13321 {
13322 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013323 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013324 *rettv = item->li_tv;
13325 vim_free(item);
13326 }
13327 else
13328 {
13329 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013330 end = get_tv_number_chk(&argvars[2], &error);
13331 if (error)
13332 ; /* type error: do nothing */
13333 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013334 EMSGN(_(e_listidx), end);
13335 else
13336 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013337 int cnt = 0;
13338
13339 for (li = item; li != NULL; li = li->li_next)
13340 {
13341 ++cnt;
13342 if (li == item2)
13343 break;
13344 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013345 if (li == NULL) /* didn't find "item2" after "item" */
13346 EMSG(_(e_invrange));
13347 else
13348 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013349 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013350 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013351 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013352 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013353 l->lv_first = item;
13354 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013355 item->li_prev = NULL;
13356 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013357 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013358 }
13359 }
13360 }
13361 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013362 }
13363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364}
13365
13366/*
13367 * "rename({from}, {to})" function
13368 */
13369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013371 typval_T *argvars;
13372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373{
13374 char_u buf[NUMBUFLEN];
13375
13376 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013379 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13380 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381}
13382
13383/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013384 * "repeat()" function
13385 */
13386/*ARGSUSED*/
13387 static void
13388f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013389 typval_T *argvars;
13390 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013391{
13392 char_u *p;
13393 int n;
13394 int slen;
13395 int len;
13396 char_u *r;
13397 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013398
13399 n = get_tv_number(&argvars[1]);
13400 if (argvars[0].v_type == VAR_LIST)
13401 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013402 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013403 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013404 if (list_extend(rettv->vval.v_list,
13405 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013406 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013407 }
13408 else
13409 {
13410 p = get_tv_string(&argvars[0]);
13411 rettv->v_type = VAR_STRING;
13412 rettv->vval.v_string = NULL;
13413
13414 slen = (int)STRLEN(p);
13415 len = slen * n;
13416 if (len <= 0)
13417 return;
13418
13419 r = alloc(len + 1);
13420 if (r != NULL)
13421 {
13422 for (i = 0; i < n; i++)
13423 mch_memmove(r + i * slen, p, (size_t)slen);
13424 r[len] = NUL;
13425 }
13426
13427 rettv->vval.v_string = r;
13428 }
13429}
13430
13431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 * "resolve()" function
13433 */
13434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013435f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 typval_T *argvars;
13437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438{
13439 char_u *p;
13440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442#ifdef FEAT_SHORTCUT
13443 {
13444 char_u *v = NULL;
13445
13446 v = mch_resolve_shortcut(p);
13447 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013448 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451 }
13452#else
13453# ifdef HAVE_READLINK
13454 {
13455 char_u buf[MAXPATHL + 1];
13456 char_u *cpy;
13457 int len;
13458 char_u *remain = NULL;
13459 char_u *q;
13460 int is_relative_to_current = FALSE;
13461 int has_trailing_pathsep = FALSE;
13462 int limit = 100;
13463
13464 p = vim_strsave(p);
13465
13466 if (p[0] == '.' && (vim_ispathsep(p[1])
13467 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13468 is_relative_to_current = TRUE;
13469
13470 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013471 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 has_trailing_pathsep = TRUE;
13473
13474 q = getnextcomp(p);
13475 if (*q != NUL)
13476 {
13477 /* Separate the first path component in "p", and keep the
13478 * remainder (beginning with the path separator). */
13479 remain = vim_strsave(q - 1);
13480 q[-1] = NUL;
13481 }
13482
13483 for (;;)
13484 {
13485 for (;;)
13486 {
13487 len = readlink((char *)p, (char *)buf, MAXPATHL);
13488 if (len <= 0)
13489 break;
13490 buf[len] = NUL;
13491
13492 if (limit-- == 0)
13493 {
13494 vim_free(p);
13495 vim_free(remain);
13496 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 goto fail;
13499 }
13500
13501 /* Ensure that the result will have a trailing path separator
13502 * if the argument has one. */
13503 if (remain == NULL && has_trailing_pathsep)
13504 add_pathsep(buf);
13505
13506 /* Separate the first path component in the link value and
13507 * concatenate the remainders. */
13508 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13509 if (*q != NUL)
13510 {
13511 if (remain == NULL)
13512 remain = vim_strsave(q - 1);
13513 else
13514 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013515 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 if (cpy != NULL)
13517 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 vim_free(remain);
13519 remain = cpy;
13520 }
13521 }
13522 q[-1] = NUL;
13523 }
13524
13525 q = gettail(p);
13526 if (q > p && *q == NUL)
13527 {
13528 /* Ignore trailing path separator. */
13529 q[-1] = NUL;
13530 q = gettail(p);
13531 }
13532 if (q > p && !mch_isFullName(buf))
13533 {
13534 /* symlink is relative to directory of argument */
13535 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13536 if (cpy != NULL)
13537 {
13538 STRCPY(cpy, p);
13539 STRCPY(gettail(cpy), buf);
13540 vim_free(p);
13541 p = cpy;
13542 }
13543 }
13544 else
13545 {
13546 vim_free(p);
13547 p = vim_strsave(buf);
13548 }
13549 }
13550
13551 if (remain == NULL)
13552 break;
13553
13554 /* Append the first path component of "remain" to "p". */
13555 q = getnextcomp(remain + 1);
13556 len = q - remain - (*q != NUL);
13557 cpy = vim_strnsave(p, STRLEN(p) + len);
13558 if (cpy != NULL)
13559 {
13560 STRNCAT(cpy, remain, len);
13561 vim_free(p);
13562 p = cpy;
13563 }
13564 /* Shorten "remain". */
13565 if (*q != NUL)
13566 STRCPY(remain, q - 1);
13567 else
13568 {
13569 vim_free(remain);
13570 remain = NULL;
13571 }
13572 }
13573
13574 /* If the result is a relative path name, make it explicitly relative to
13575 * the current directory if and only if the argument had this form. */
13576 if (!vim_ispathsep(*p))
13577 {
13578 if (is_relative_to_current
13579 && *p != NUL
13580 && !(p[0] == '.'
13581 && (p[1] == NUL
13582 || vim_ispathsep(p[1])
13583 || (p[1] == '.'
13584 && (p[2] == NUL
13585 || vim_ispathsep(p[2]))))))
13586 {
13587 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013588 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 if (cpy != NULL)
13590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 vim_free(p);
13592 p = cpy;
13593 }
13594 }
13595 else if (!is_relative_to_current)
13596 {
13597 /* Strip leading "./". */
13598 q = p;
13599 while (q[0] == '.' && vim_ispathsep(q[1]))
13600 q += 2;
13601 if (q > p)
13602 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13603 }
13604 }
13605
13606 /* Ensure that the result will have no trailing path separator
13607 * if the argument had none. But keep "/" or "//". */
13608 if (!has_trailing_pathsep)
13609 {
13610 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013611 if (after_pathsep(p, q))
13612 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 }
13614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 }
13617# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619# endif
13620#endif
13621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623
13624#ifdef HAVE_READLINK
13625fail:
13626#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628}
13629
13630/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013631 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 */
13633 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013634f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637{
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 list_T *l;
13639 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641 rettv->vval.v_number = 0;
13642 if (argvars[0].v_type != VAR_LIST)
13643 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013644 else if ((l = argvars[0].vval.v_list) != NULL
13645 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013646 {
13647 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013648 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013649 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013650 while (li != NULL)
13651 {
13652 ni = li->li_prev;
13653 list_append(l, li);
13654 li = ni;
13655 }
13656 rettv->vval.v_list = l;
13657 rettv->v_type = VAR_LIST;
13658 ++l->lv_refcount;
13659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660}
13661
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013662#define SP_NOMOVE 0x01 /* don't move cursor */
13663#define SP_REPEAT 0x02 /* repeat to find outer pair */
13664#define SP_RETCOUNT 0x04 /* return matchcount */
13665#define SP_SETPCMARK 0x08 /* set previous context mark */
13666#define SP_START 0x10 /* accept match at start position */
13667#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13668#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013669
Bram Moolenaar33570922005-01-25 22:26:29 +000013670static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013671
13672/*
13673 * Get flags for a search function.
13674 * Possibly sets "p_ws".
13675 * Returns BACKWARD, FORWARD or zero (for an error).
13676 */
13677 static int
13678get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013679 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013680 int *flagsp;
13681{
13682 int dir = FORWARD;
13683 char_u *flags;
13684 char_u nbuf[NUMBUFLEN];
13685 int mask;
13686
13687 if (varp->v_type != VAR_UNKNOWN)
13688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013689 flags = get_tv_string_buf_chk(varp, nbuf);
13690 if (flags == NULL)
13691 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692 while (*flags != NUL)
13693 {
13694 switch (*flags)
13695 {
13696 case 'b': dir = BACKWARD; break;
13697 case 'w': p_ws = TRUE; break;
13698 case 'W': p_ws = FALSE; break;
13699 default: mask = 0;
13700 if (flagsp != NULL)
13701 switch (*flags)
13702 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013703 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013704 case 'e': mask = SP_END; break;
13705 case 'm': mask = SP_RETCOUNT; break;
13706 case 'n': mask = SP_NOMOVE; break;
13707 case 'p': mask = SP_SUBPAT; break;
13708 case 'r': mask = SP_REPEAT; break;
13709 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013710 }
13711 if (mask == 0)
13712 {
13713 EMSG2(_(e_invarg2), flags);
13714 dir = 0;
13715 }
13716 else
13717 *flagsp |= mask;
13718 }
13719 if (dir == 0)
13720 break;
13721 ++flags;
13722 }
13723 }
13724 return dir;
13725}
13726
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013728 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013730 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013731search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013733 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013734 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013736 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 char_u *pat;
13738 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013739 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 int save_p_ws = p_ws;
13741 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013742 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013743 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013744 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013745 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013748 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013749 if (dir == 0)
13750 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013751 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013752 if (flags & SP_START)
13753 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013754 if (flags & SP_END)
13755 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013756
13757 /* Optional extra argument: line number to stop searching. */
13758 if (argvars[1].v_type != VAR_UNKNOWN
13759 && argvars[2].v_type != VAR_UNKNOWN)
13760 {
13761 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13762 if (lnum_stop < 0)
13763 goto theend;
13764 }
13765
Bram Moolenaar231334e2005-07-25 20:46:57 +000013766 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013767 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013768 * Check to make sure only those flags are set.
13769 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13770 * flags cannot be set. Check for that condition also.
13771 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013772 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013773 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013776 goto theend;
13777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013779 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013780 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13781 options, RE_SEARCH, (linenr_T)lnum_stop);
13782 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013784 if (flags & SP_SUBPAT)
13785 retval = subpatnum;
13786 else
13787 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013788 if (flags & SP_SETPCMARK)
13789 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013791 if (match_pos != NULL)
13792 {
13793 /* Store the match cursor position */
13794 match_pos->lnum = pos.lnum;
13795 match_pos->col = pos.col + 1;
13796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 /* "/$" will put the cursor after the end of the line, may need to
13798 * correct that here */
13799 check_cursor();
13800 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013801
13802 /* If 'n' flag is used: restore cursor position. */
13803 if (flags & SP_NOMOVE)
13804 curwin->w_cursor = save_cursor;
13805theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013807
13808 return retval;
13809}
13810
13811/*
13812 * "search()" function
13813 */
13814 static void
13815f_search(argvars, rettv)
13816 typval_T *argvars;
13817 typval_T *rettv;
13818{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013819 int flags = 0;
13820
13821 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822}
13823
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013825 * "searchdecl()" function
13826 */
13827 static void
13828f_searchdecl(argvars, rettv)
13829 typval_T *argvars;
13830 typval_T *rettv;
13831{
13832 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013833 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013834 int error = FALSE;
13835 char_u *name;
13836
13837 rettv->vval.v_number = 1; /* default: FAIL */
13838
13839 name = get_tv_string_chk(&argvars[0]);
13840 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013841 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013842 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013843 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13844 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13845 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013846 if (!error && name != NULL)
13847 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013848 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013849}
13850
13851/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013852 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013854 static int
13855searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013856 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013857 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858{
13859 char_u *spat, *mpat, *epat;
13860 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 int dir;
13863 int flags = 0;
13864 char_u nbuf1[NUMBUFLEN];
13865 char_u nbuf2[NUMBUFLEN];
13866 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013867 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013868 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013871 spat = get_tv_string_chk(&argvars[0]);
13872 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13873 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13874 if (spat == NULL || mpat == NULL || epat == NULL)
13875 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 /* Handle the optional fourth argument: flags */
13878 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013879 if (dir == 0)
13880 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013881
13882 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013883 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13884 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013885 if ((flags & (SP_END | SP_SUBPAT)) != 0
13886 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013887 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013888 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013889 goto theend;
13890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013892 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013893 if (argvars[3].v_type == VAR_UNKNOWN
13894 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 skip = (char_u *)"";
13896 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013899 if (argvars[5].v_type != VAR_UNKNOWN)
13900 {
13901 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13902 if (lnum_stop < 0)
13903 goto theend;
13904 }
13905 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013906 if (skip == NULL)
13907 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013909 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13910 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013911
13912theend:
13913 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013914
13915 return retval;
13916}
13917
13918/*
13919 * "searchpair()" function
13920 */
13921 static void
13922f_searchpair(argvars, rettv)
13923 typval_T *argvars;
13924 typval_T *rettv;
13925{
13926 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13927}
13928
13929/*
13930 * "searchpairpos()" function
13931 */
13932 static void
13933f_searchpairpos(argvars, rettv)
13934 typval_T *argvars;
13935 typval_T *rettv;
13936{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013937 pos_T match_pos;
13938 int lnum = 0;
13939 int col = 0;
13940
13941 rettv->vval.v_number = 0;
13942
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013943 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013944 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013945
13946 if (searchpair_cmn(argvars, &match_pos) > 0)
13947 {
13948 lnum = match_pos.lnum;
13949 col = match_pos.col;
13950 }
13951
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013952 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13953 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013954}
13955
13956/*
13957 * Search for a start/middle/end thing.
13958 * Used by searchpair(), see its documentation for the details.
13959 * Returns 0 or -1 for no match,
13960 */
13961 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013962do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013963 char_u *spat; /* start pattern */
13964 char_u *mpat; /* middle pattern */
13965 char_u *epat; /* end pattern */
13966 int dir; /* BACKWARD or FORWARD */
13967 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013968 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013969 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013970 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013971{
13972 char_u *save_cpo;
13973 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13974 long retval = 0;
13975 pos_T pos;
13976 pos_T firstpos;
13977 pos_T foundpos;
13978 pos_T save_cursor;
13979 pos_T save_pos;
13980 int n;
13981 int r;
13982 int nest = 1;
13983 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013984 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013985
13986 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13987 save_cpo = p_cpo;
13988 p_cpo = (char_u *)"";
13989
13990 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13991 * start/middle/end (pat3, for the top pair). */
13992 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13993 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13994 if (pat2 == NULL || pat3 == NULL)
13995 goto theend;
13996 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13997 if (*mpat == NUL)
13998 STRCPY(pat3, pat2);
13999 else
14000 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14001 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014002 if (flags & SP_START)
14003 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014004
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 save_cursor = curwin->w_cursor;
14006 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014007 clearpos(&firstpos);
14008 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 pat = pat3;
14010 for (;;)
14011 {
14012 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014013 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14015 /* didn't find it or found the first match again: FAIL */
14016 break;
14017
14018 if (firstpos.lnum == 0)
14019 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014020 if (equalpos(pos, foundpos))
14021 {
14022 /* Found the same position again. Can happen with a pattern that
14023 * has "\zs" at the end and searching backwards. Advance one
14024 * character and try again. */
14025 if (dir == BACKWARD)
14026 decl(&pos);
14027 else
14028 incl(&pos);
14029 }
14030 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031
14032 /* If the skip pattern matches, ignore this match. */
14033 if (*skip != NUL)
14034 {
14035 save_pos = curwin->w_cursor;
14036 curwin->w_cursor = pos;
14037 r = eval_to_bool(skip, &err, NULL, FALSE);
14038 curwin->w_cursor = save_pos;
14039 if (err)
14040 {
14041 /* Evaluating {skip} caused an error, break here. */
14042 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014043 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 break;
14045 }
14046 if (r)
14047 continue;
14048 }
14049
14050 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14051 {
14052 /* Found end when searching backwards or start when searching
14053 * forward: nested pair. */
14054 ++nest;
14055 pat = pat2; /* nested, don't search for middle */
14056 }
14057 else
14058 {
14059 /* Found end when searching forward or start when searching
14060 * backward: end of (nested) pair; or found middle in outer pair. */
14061 if (--nest == 1)
14062 pat = pat3; /* outer level, search for middle */
14063 }
14064
14065 if (nest == 0)
14066 {
14067 /* Found the match: return matchcount or line number. */
14068 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014069 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014071 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014072 if (flags & SP_SETPCMARK)
14073 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 curwin->w_cursor = pos;
14075 if (!(flags & SP_REPEAT))
14076 break;
14077 nest = 1; /* search for next unmatched */
14078 }
14079 }
14080
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014081 if (match_pos != NULL)
14082 {
14083 /* Store the match cursor position */
14084 match_pos->lnum = curwin->w_cursor.lnum;
14085 match_pos->col = curwin->w_cursor.col + 1;
14086 }
14087
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014089 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 curwin->w_cursor = save_cursor;
14091
14092theend:
14093 vim_free(pat2);
14094 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014096
14097 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098}
14099
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014100/*
14101 * "searchpos()" function
14102 */
14103 static void
14104f_searchpos(argvars, rettv)
14105 typval_T *argvars;
14106 typval_T *rettv;
14107{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014108 pos_T match_pos;
14109 int lnum = 0;
14110 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014111 int n;
14112 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014113
14114 rettv->vval.v_number = 0;
14115
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014116 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014117 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014118
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014119 n = search_cmn(argvars, &match_pos, &flags);
14120 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014121 {
14122 lnum = match_pos.lnum;
14123 col = match_pos.col;
14124 }
14125
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014126 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14127 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014128 if (flags & SP_SUBPAT)
14129 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014130}
14131
14132
Bram Moolenaar0d660222005-01-07 21:51:51 +000014133/*ARGSUSED*/
14134 static void
14135f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014136 typval_T *argvars;
14137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014139#ifdef FEAT_CLIENTSERVER
14140 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 char_u *server = get_tv_string_chk(&argvars[0]);
14142 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143
Bram Moolenaar0d660222005-01-07 21:51:51 +000014144 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 if (server == NULL || reply == NULL)
14146 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147 if (check_restricted() || check_secure())
14148 return;
14149# ifdef FEAT_X11
14150 if (check_connection() == FAIL)
14151 return;
14152# endif
14153
14154 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156 EMSG(_("E258: Unable to send to client"));
14157 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159 rettv->vval.v_number = 0;
14160#else
14161 rettv->vval.v_number = -1;
14162#endif
14163}
14164
14165/*ARGSUSED*/
14166 static void
14167f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014168 typval_T *argvars;
14169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014170{
14171 char_u *r = NULL;
14172
14173#ifdef FEAT_CLIENTSERVER
14174# ifdef WIN32
14175 r = serverGetVimNames();
14176# else
14177 make_connection();
14178 if (X_DISPLAY != NULL)
14179 r = serverGetVimNames(X_DISPLAY);
14180# endif
14181#endif
14182 rettv->v_type = VAR_STRING;
14183 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184}
14185
14186/*
14187 * "setbufvar()" function
14188 */
14189/*ARGSUSED*/
14190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 typval_T *argvars;
14193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194{
14195 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014198 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 char_u nbuf[NUMBUFLEN];
14200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014201 rettv->vval.v_number = 0;
14202
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 if (check_restricted() || check_secure())
14204 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14206 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014207 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 varp = &argvars[2];
14209
14210 if (buf != NULL && varname != NULL && varp != NULL)
14211 {
14212 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214
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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242}
14243
14244/*
14245 * "setcmdpos()" function
14246 */
14247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014248f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014249 typval_T *argvars;
14250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 int pos = (int)get_tv_number(&argvars[0]) - 1;
14253
14254 if (pos >= 0)
14255 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256}
14257
14258/*
14259 * "setline()" function
14260 */
14261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014262f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014263 typval_T *argvars;
14264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265{
14266 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014267 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014268 list_T *l = NULL;
14269 listitem_T *li = NULL;
14270 long added = 0;
14271 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014273 lnum = get_tv_lnum(&argvars[0]);
14274 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014276 l = argvars[1].vval.v_list;
14277 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014279 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014280 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014281
14282 rettv->vval.v_number = 0; /* OK */
14283 for (;;)
14284 {
14285 if (l != NULL)
14286 {
14287 /* list argument, get next string */
14288 if (li == NULL)
14289 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014290 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014291 li = li->li_next;
14292 }
14293
14294 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014296 break;
14297 if (lnum <= curbuf->b_ml.ml_line_count)
14298 {
14299 /* existing line, replace it */
14300 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14301 {
14302 changed_bytes(lnum, 0);
14303 check_cursor_col();
14304 rettv->vval.v_number = 0; /* OK */
14305 }
14306 }
14307 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14308 {
14309 /* lnum is one past the last line, append the line */
14310 ++added;
14311 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14312 rettv->vval.v_number = 0; /* OK */
14313 }
14314
14315 if (l == NULL) /* only one string argument */
14316 break;
14317 ++lnum;
14318 }
14319
14320 if (added > 0)
14321 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322}
14323
14324/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014325 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014326 */
14327/*ARGSUSED*/
14328 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014329set_qf_ll_list(wp, list_arg, action_arg, rettv)
14330 win_T *wp;
14331 typval_T *list_arg;
14332 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014333 typval_T *rettv;
14334{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014335#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014336 char_u *act;
14337 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014338#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014339
Bram Moolenaar2641f772005-03-25 21:58:17 +000014340 rettv->vval.v_number = -1;
14341
14342#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014343 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014344 EMSG(_(e_listreq));
14345 else
14346 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014347 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014348
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014349 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014350 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014351 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 if (act == NULL)
14353 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014354 if (*act == 'a' || *act == 'r')
14355 action = *act;
14356 }
14357
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014358 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014359 rettv->vval.v_number = 0;
14360 }
14361#endif
14362}
14363
14364/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014365 * "setloclist()" function
14366 */
14367/*ARGSUSED*/
14368 static void
14369f_setloclist(argvars, rettv)
14370 typval_T *argvars;
14371 typval_T *rettv;
14372{
14373 win_T *win;
14374
14375 rettv->vval.v_number = -1;
14376
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014377 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014378 if (win != NULL)
14379 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14380}
14381
14382/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014383 * "setpos()" function
14384 */
14385/*ARGSUSED*/
14386 static void
14387f_setpos(argvars, rettv)
14388 typval_T *argvars;
14389 typval_T *rettv;
14390{
14391 pos_T pos;
14392 int fnum;
14393 char_u *name;
14394
14395 name = get_tv_string_chk(argvars);
14396 if (name != NULL)
14397 {
14398 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14399 {
14400 --pos.col;
14401 if (name[0] == '.') /* cursor */
14402 {
14403 if (fnum == curbuf->b_fnum)
14404 {
14405 curwin->w_cursor = pos;
14406 check_cursor();
14407 }
14408 else
14409 EMSG(_(e_invarg));
14410 }
14411 else if (name[0] == '\'') /* mark */
14412 (void)setmark_pos(name[1], &pos, fnum);
14413 else
14414 EMSG(_(e_invarg));
14415 }
14416 }
14417}
14418
14419/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014420 * "setqflist()" function
14421 */
14422/*ARGSUSED*/
14423 static void
14424f_setqflist(argvars, rettv)
14425 typval_T *argvars;
14426 typval_T *rettv;
14427{
14428 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14429}
14430
14431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 * "setreg()" function
14433 */
14434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014436 typval_T *argvars;
14437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438{
14439 int regname;
14440 char_u *strregname;
14441 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014442 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 int append;
14444 char_u yank_type;
14445 long block_len;
14446
14447 block_len = -1;
14448 yank_type = MAUTO;
14449 append = FALSE;
14450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454 if (strregname == NULL)
14455 return; /* type error; errmsg already given */
14456 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 if (regname == 0 || regname == '@')
14458 regname = '"';
14459 else if (regname == '=')
14460 return;
14461
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014462 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 stropt = get_tv_string_chk(&argvars[2]);
14465 if (stropt == NULL)
14466 return; /* type error */
14467 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 switch (*stropt)
14469 {
14470 case 'a': case 'A': /* append */
14471 append = TRUE;
14472 break;
14473 case 'v': case 'c': /* character-wise selection */
14474 yank_type = MCHAR;
14475 break;
14476 case 'V': case 'l': /* line-wise selection */
14477 yank_type = MLINE;
14478 break;
14479#ifdef FEAT_VISUAL
14480 case 'b': case Ctrl_V: /* block-wise selection */
14481 yank_type = MBLOCK;
14482 if (VIM_ISDIGIT(stropt[1]))
14483 {
14484 ++stropt;
14485 block_len = getdigits(&stropt) - 1;
14486 --stropt;
14487 }
14488 break;
14489#endif
14490 }
14491 }
14492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014493 strval = get_tv_string_chk(&argvars[1]);
14494 if (strval != NULL)
14495 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014497 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498}
14499
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014500/*
14501 * "settabwinvar()" function
14502 */
14503 static void
14504f_settabwinvar(argvars, rettv)
14505 typval_T *argvars;
14506 typval_T *rettv;
14507{
14508 setwinvar(argvars, rettv, 1);
14509}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510
14511/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014512 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 typval_T *argvars;
14517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014519 setwinvar(argvars, rettv, 0);
14520}
14521
14522/*
14523 * "setwinvar()" and "settabwinvar()" functions
14524 */
14525 static void
14526setwinvar(argvars, rettv, off)
14527 typval_T *argvars;
14528 typval_T *rettv;
14529 int off;
14530{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 win_T *win;
14532#ifdef FEAT_WINDOWS
14533 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014534 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535#endif
14536 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014537 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014539 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014541 rettv->vval.v_number = 0;
14542
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 if (check_restricted() || check_secure())
14544 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014545
14546#ifdef FEAT_WINDOWS
14547 if (off == 1)
14548 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14549 else
14550 tp = curtab;
14551#endif
14552 win = find_win_by_nr(&argvars[off], tp);
14553 varname = get_tv_string_chk(&argvars[off + 1]);
14554 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555
14556 if (win != NULL && varname != NULL && varp != NULL)
14557 {
14558#ifdef FEAT_WINDOWS
14559 /* set curwin to be our win, temporarily */
14560 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014561 save_curtab = curtab;
14562 goto_tabpage_tp(tp);
14563 if (!win_valid(win))
14564 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 curwin = win;
14566 curbuf = curwin->w_buffer;
14567#endif
14568
14569 if (*varname == '&')
14570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014571 long numval;
14572 char_u *strval;
14573 int error = FALSE;
14574
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014576 numval = get_tv_number_chk(varp, &error);
14577 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 if (!error && strval != NULL)
14579 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 }
14581 else
14582 {
14583 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14584 if (winvarname != NULL)
14585 {
14586 STRCPY(winvarname, "w:");
14587 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014588 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 vim_free(winvarname);
14590 }
14591 }
14592
14593#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014594 /* Restore current tabpage and window, if still valid (autocomands can
14595 * make them invalid). */
14596 if (valid_tabpage(save_curtab))
14597 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 if (win_valid(save_curwin))
14599 {
14600 curwin = save_curwin;
14601 curbuf = curwin->w_buffer;
14602 }
14603#endif
14604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605}
14606
14607/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609 */
14610 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014612 typval_T *argvars;
14613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014615 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616
Bram Moolenaar0d660222005-01-07 21:51:51 +000014617 p = get_tv_string(&argvars[0]);
14618 rettv->vval.v_string = vim_strsave(p);
14619 simplify_filename(rettv->vval.v_string); /* simplify in place */
14620 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621}
14622
Bram Moolenaar0d660222005-01-07 21:51:51 +000014623static int
14624#ifdef __BORLANDC__
14625 _RTLENTRYF
14626#endif
14627 item_compare __ARGS((const void *s1, const void *s2));
14628static int
14629#ifdef __BORLANDC__
14630 _RTLENTRYF
14631#endif
14632 item_compare2 __ARGS((const void *s1, const void *s2));
14633
14634static int item_compare_ic;
14635static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637#define ITEM_COMPARE_FAIL 999
14638
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642 static int
14643#ifdef __BORLANDC__
14644_RTLENTRYF
14645#endif
14646item_compare(s1, s2)
14647 const void *s1;
14648 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014650 char_u *p1, *p2;
14651 char_u *tofree1, *tofree2;
14652 int res;
14653 char_u numbuf1[NUMBUFLEN];
14654 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014656 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14657 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014658 if (item_compare_ic)
14659 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014661 res = STRCMP(p1, p2);
14662 vim_free(tofree1);
14663 vim_free(tofree2);
14664 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665}
14666
14667 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668#ifdef __BORLANDC__
14669_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671item_compare2(s1, s2)
14672 const void *s1;
14673 const void *s2;
14674{
14675 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014676 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014677 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014680 /* shortcut after failure in previous call; compare all items equal */
14681 if (item_compare_func_err)
14682 return 0;
14683
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014684 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14685 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014686 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14687 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014688
14689 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014690 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014691 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692 clear_tv(&argv[0]);
14693 clear_tv(&argv[1]);
14694
14695 if (res == FAIL)
14696 res = ITEM_COMPARE_FAIL;
14697 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 /* return value has wrong type */
14699 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14700 if (item_compare_func_err)
14701 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702 clear_tv(&rettv);
14703 return res;
14704}
14705
14706/*
14707 * "sort({list})" function
14708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014710f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014711 typval_T *argvars;
14712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713{
Bram Moolenaar33570922005-01-25 22:26:29 +000014714 list_T *l;
14715 listitem_T *li;
14716 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014717 long len;
14718 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720 rettv->vval.v_number = 0;
14721 if (argvars[0].v_type != VAR_LIST)
14722 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 else
14724 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014725 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014726 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014727 return;
14728 rettv->vval.v_list = l;
14729 rettv->v_type = VAR_LIST;
14730 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731
Bram Moolenaar0d660222005-01-07 21:51:51 +000014732 len = list_len(l);
14733 if (len <= 1)
14734 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736 item_compare_ic = FALSE;
14737 item_compare_func = NULL;
14738 if (argvars[1].v_type != VAR_UNKNOWN)
14739 {
14740 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014741 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014742 else
14743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 int error = FALSE;
14745
14746 i = get_tv_number_chk(&argvars[1], &error);
14747 if (error)
14748 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749 if (i == 1)
14750 item_compare_ic = TRUE;
14751 else
14752 item_compare_func = get_tv_string(&argvars[1]);
14753 }
14754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755
Bram Moolenaar0d660222005-01-07 21:51:51 +000014756 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014757 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014758 if (ptrs == NULL)
14759 return;
14760 i = 0;
14761 for (li = l->lv_first; li != NULL; li = li->li_next)
14762 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014764 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014765 /* test the compare function */
14766 if (item_compare_func != NULL
14767 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14768 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014769 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014771 {
14772 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014773 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014774 item_compare_func == NULL ? item_compare : item_compare2);
14775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 if (!item_compare_func_err)
14777 {
14778 /* Clear the List and append the items in the sorted order. */
14779 l->lv_first = l->lv_last = NULL;
14780 l->lv_len = 0;
14781 for (i = 0; i < len; ++i)
14782 list_append(l, ptrs[i]);
14783 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014784 }
14785
14786 vim_free(ptrs);
14787 }
14788}
14789
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014790/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014791 * "soundfold({word})" function
14792 */
14793 static void
14794f_soundfold(argvars, rettv)
14795 typval_T *argvars;
14796 typval_T *rettv;
14797{
14798 char_u *s;
14799
14800 rettv->v_type = VAR_STRING;
14801 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014802#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014803 rettv->vval.v_string = eval_soundfold(s);
14804#else
14805 rettv->vval.v_string = vim_strsave(s);
14806#endif
14807}
14808
14809/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014810 * "spellbadword()" function
14811 */
14812/* ARGSUSED */
14813 static void
14814f_spellbadword(argvars, rettv)
14815 typval_T *argvars;
14816 typval_T *rettv;
14817{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014818 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014819 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014820 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014821
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014822 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014823 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014824
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014825#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014826 if (argvars[0].v_type == VAR_UNKNOWN)
14827 {
14828 /* Find the start and length of the badly spelled word. */
14829 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14830 if (len != 0)
14831 word = ml_get_cursor();
14832 }
14833 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14834 {
14835 char_u *str = get_tv_string_chk(&argvars[0]);
14836 int capcol = -1;
14837
14838 if (str != NULL)
14839 {
14840 /* Check the argument for spelling. */
14841 while (*str != NUL)
14842 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014843 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014844 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014845 {
14846 word = str;
14847 break;
14848 }
14849 str += len;
14850 }
14851 }
14852 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014853#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014854
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014855 list_append_string(rettv->vval.v_list, word, len);
14856 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014857 attr == HLF_SPB ? "bad" :
14858 attr == HLF_SPR ? "rare" :
14859 attr == HLF_SPL ? "local" :
14860 attr == HLF_SPC ? "caps" :
14861 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014862}
14863
14864/*
14865 * "spellsuggest()" function
14866 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014867/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014868 static void
14869f_spellsuggest(argvars, rettv)
14870 typval_T *argvars;
14871 typval_T *rettv;
14872{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014873#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014874 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014875 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014876 int maxcount;
14877 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014878 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014879 listitem_T *li;
14880 int need_capital = FALSE;
14881#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014882
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014883 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014884 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014885
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014886#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014887 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14888 {
14889 str = get_tv_string(&argvars[0]);
14890 if (argvars[1].v_type != VAR_UNKNOWN)
14891 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014892 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014893 if (maxcount <= 0)
14894 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014895 if (argvars[2].v_type != VAR_UNKNOWN)
14896 {
14897 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14898 if (typeerr)
14899 return;
14900 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014901 }
14902 else
14903 maxcount = 25;
14904
Bram Moolenaar4770d092006-01-12 23:22:24 +000014905 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014906
14907 for (i = 0; i < ga.ga_len; ++i)
14908 {
14909 str = ((char_u **)ga.ga_data)[i];
14910
14911 li = listitem_alloc();
14912 if (li == NULL)
14913 vim_free(str);
14914 else
14915 {
14916 li->li_tv.v_type = VAR_STRING;
14917 li->li_tv.v_lock = 0;
14918 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014919 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014920 }
14921 }
14922 ga_clear(&ga);
14923 }
14924#endif
14925}
14926
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014928f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014929 typval_T *argvars;
14930 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931{
14932 char_u *str;
14933 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014934 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935 regmatch_T regmatch;
14936 char_u patbuf[NUMBUFLEN];
14937 char_u *save_cpo;
14938 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014939 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014940 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014941 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014942
14943 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14944 save_cpo = p_cpo;
14945 p_cpo = (char_u *)"";
14946
14947 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014948 if (argvars[1].v_type != VAR_UNKNOWN)
14949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014950 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14951 if (pat == NULL)
14952 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014953 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014954 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014955 }
14956 if (pat == NULL || *pat == NUL)
14957 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014959 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014961 if (typeerr)
14962 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963
Bram Moolenaar0d660222005-01-07 21:51:51 +000014964 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14965 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014967 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014968 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014970 if (*str == NUL)
14971 match = FALSE; /* empty item at the end */
14972 else
14973 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014974 if (match)
14975 end = regmatch.startp[0];
14976 else
14977 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014978 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14979 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014981 if (list_append_string(rettv->vval.v_list, str,
14982 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014983 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 }
14985 if (!match)
14986 break;
14987 /* Advance to just after the match. */
14988 if (regmatch.endp[0] > str)
14989 col = 0;
14990 else
14991 {
14992 /* Don't get stuck at the same match. */
14993#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014994 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014995#else
14996 col = 1;
14997#endif
14998 }
14999 str = regmatch.endp[0];
15000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001
Bram Moolenaar0d660222005-01-07 21:51:51 +000015002 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004
Bram Moolenaar0d660222005-01-07 21:51:51 +000015005 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006}
15007
Bram Moolenaar2c932302006-03-18 21:42:09 +000015008/*
15009 * "str2nr()" function
15010 */
15011 static void
15012f_str2nr(argvars, rettv)
15013 typval_T *argvars;
15014 typval_T *rettv;
15015{
15016 int base = 10;
15017 char_u *p;
15018 long n;
15019
15020 if (argvars[1].v_type != VAR_UNKNOWN)
15021 {
15022 base = get_tv_number(&argvars[1]);
15023 if (base != 8 && base != 10 && base != 16)
15024 {
15025 EMSG(_(e_invarg));
15026 return;
15027 }
15028 }
15029
15030 p = skipwhite(get_tv_string(&argvars[0]));
15031 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15032 rettv->vval.v_number = n;
15033}
15034
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035#ifdef HAVE_STRFTIME
15036/*
15037 * "strftime({format}[, {time}])" function
15038 */
15039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015041 typval_T *argvars;
15042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043{
15044 char_u result_buf[256];
15045 struct tm *curtime;
15046 time_t seconds;
15047 char_u *p;
15048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015049 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015052 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 seconds = time(NULL);
15054 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 curtime = localtime(&seconds);
15057 /* MSVC returns NULL for an invalid value of seconds. */
15058 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015059 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 else
15061 {
15062# ifdef FEAT_MBYTE
15063 vimconv_T conv;
15064 char_u *enc;
15065
15066 conv.vc_type = CONV_NONE;
15067 enc = enc_locale();
15068 convert_setup(&conv, p_enc, enc);
15069 if (conv.vc_type != CONV_NONE)
15070 p = string_convert(&conv, p, NULL);
15071# endif
15072 if (p != NULL)
15073 (void)strftime((char *)result_buf, sizeof(result_buf),
15074 (char *)p, curtime);
15075 else
15076 result_buf[0] = NUL;
15077
15078# ifdef FEAT_MBYTE
15079 if (conv.vc_type != CONV_NONE)
15080 vim_free(p);
15081 convert_setup(&conv, enc, p_enc);
15082 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 else
15085# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015086 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087
15088# ifdef FEAT_MBYTE
15089 /* Release conversion descriptors */
15090 convert_setup(&conv, NULL, NULL);
15091 vim_free(enc);
15092# endif
15093 }
15094}
15095#endif
15096
15097/*
15098 * "stridx()" function
15099 */
15100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015101f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015102 typval_T *argvars;
15103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104{
15105 char_u buf[NUMBUFLEN];
15106 char_u *needle;
15107 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015110 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015112 needle = get_tv_string_chk(&argvars[1]);
15113 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015114 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015115 if (needle == NULL || haystack == NULL)
15116 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 if (argvars[2].v_type != VAR_UNKNOWN)
15119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015120 int error = FALSE;
15121
15122 start_idx = get_tv_number_chk(&argvars[2], &error);
15123 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015124 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015125 if (start_idx >= 0)
15126 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015127 }
15128
15129 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15130 if (pos != NULL)
15131 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132}
15133
15134/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015135 * "string()" function
15136 */
15137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015138f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 typval_T *argvars;
15140 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015141{
15142 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015143 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015145 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015146 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015147 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015148 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149}
15150
15151/*
15152 * "strlen()" function
15153 */
15154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015155f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015156 typval_T *argvars;
15157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 rettv->vval.v_number = (varnumber_T)(STRLEN(
15160 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161}
15162
15163/*
15164 * "strpart()" function
15165 */
15166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015167f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015168 typval_T *argvars;
15169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170{
15171 char_u *p;
15172 int n;
15173 int len;
15174 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015175 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015177 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178 slen = (int)STRLEN(p);
15179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015180 n = get_tv_number_chk(&argvars[1], &error);
15181 if (error)
15182 len = 0;
15183 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015184 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 else
15186 len = slen - n; /* default len: all bytes that are available. */
15187
15188 /*
15189 * Only return the overlap between the specified part and the actual
15190 * string.
15191 */
15192 if (n < 0)
15193 {
15194 len += n;
15195 n = 0;
15196 }
15197 else if (n > slen)
15198 n = slen;
15199 if (len < 0)
15200 len = 0;
15201 else if (n + len > slen)
15202 len = slen - n;
15203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204 rettv->v_type = VAR_STRING;
15205 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206}
15207
15208/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015209 * "strridx()" function
15210 */
15211 static void
15212f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 typval_T *argvars;
15214 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015215{
15216 char_u buf[NUMBUFLEN];
15217 char_u *needle;
15218 char_u *haystack;
15219 char_u *rest;
15220 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015221 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015223 needle = get_tv_string_chk(&argvars[1]);
15224 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225
15226 rettv->vval.v_number = -1;
15227 if (needle == NULL || haystack == NULL)
15228 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015229
15230 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015231 if (argvars[2].v_type != VAR_UNKNOWN)
15232 {
15233 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015234 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015235 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015236 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015237 }
15238 else
15239 end_idx = haystack_len;
15240
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015242 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015243 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015244 lastmatch = haystack + end_idx;
15245 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015247 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015248 for (rest = haystack; *rest != '\0'; ++rest)
15249 {
15250 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015251 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252 break;
15253 lastmatch = rest;
15254 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015255 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015256
15257 if (lastmatch == NULL)
15258 rettv->vval.v_number = -1;
15259 else
15260 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15261}
15262
15263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 * "strtrans()" function
15265 */
15266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015268 typval_T *argvars;
15269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271 rettv->v_type = VAR_STRING;
15272 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273}
15274
15275/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015276 * "submatch()" function
15277 */
15278 static void
15279f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015280 typval_T *argvars;
15281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015282{
15283 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015284 rettv->vval.v_string =
15285 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286}
15287
15288/*
15289 * "substitute()" function
15290 */
15291 static void
15292f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015293 typval_T *argvars;
15294 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015295{
15296 char_u patbuf[NUMBUFLEN];
15297 char_u subbuf[NUMBUFLEN];
15298 char_u flagsbuf[NUMBUFLEN];
15299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015300 char_u *str = get_tv_string_chk(&argvars[0]);
15301 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15302 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15303 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15304
Bram Moolenaar0d660222005-01-07 21:51:51 +000015305 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015306 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15307 rettv->vval.v_string = NULL;
15308 else
15309 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015310}
15311
15312/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015313 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 */
15315/*ARGSUSED*/
15316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015317f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015318 typval_T *argvars;
15319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320{
15321 int id = 0;
15322#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015323 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 long col;
15325 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015326 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015328 lnum = get_tv_lnum(argvars); /* -1 on type error */
15329 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15330 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015332 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015333 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015334 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335#endif
15336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338}
15339
15340/*
15341 * "synIDattr(id, what [, mode])" function
15342 */
15343/*ARGSUSED*/
15344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015345f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015346 typval_T *argvars;
15347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348{
15349 char_u *p = NULL;
15350#ifdef FEAT_SYN_HL
15351 int id;
15352 char_u *what;
15353 char_u *mode;
15354 char_u modebuf[NUMBUFLEN];
15355 int modec;
15356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 id = get_tv_number(&argvars[0]);
15358 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015361 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 modec = TOLOWER_ASC(mode[0]);
15363 if (modec != 't' && modec != 'c'
15364#ifdef FEAT_GUI
15365 && modec != 'g'
15366#endif
15367 )
15368 modec = 0; /* replace invalid with current */
15369 }
15370 else
15371 {
15372#ifdef FEAT_GUI
15373 if (gui.in_use)
15374 modec = 'g';
15375 else
15376#endif
15377 if (t_colors > 1)
15378 modec = 'c';
15379 else
15380 modec = 't';
15381 }
15382
15383
15384 switch (TOLOWER_ASC(what[0]))
15385 {
15386 case 'b':
15387 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15388 p = highlight_color(id, what, modec);
15389 else /* bold */
15390 p = highlight_has_attr(id, HL_BOLD, modec);
15391 break;
15392
15393 case 'f': /* fg[#] */
15394 p = highlight_color(id, what, modec);
15395 break;
15396
15397 case 'i':
15398 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15399 p = highlight_has_attr(id, HL_INVERSE, modec);
15400 else /* italic */
15401 p = highlight_has_attr(id, HL_ITALIC, modec);
15402 break;
15403
15404 case 'n': /* name */
15405 p = get_highlight_name(NULL, id - 1);
15406 break;
15407
15408 case 'r': /* reverse */
15409 p = highlight_has_attr(id, HL_INVERSE, modec);
15410 break;
15411
15412 case 's': /* standout */
15413 p = highlight_has_attr(id, HL_STANDOUT, modec);
15414 break;
15415
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015416 case 'u':
15417 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15418 /* underline */
15419 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15420 else
15421 /* undercurl */
15422 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 break;
15424 }
15425
15426 if (p != NULL)
15427 p = vim_strsave(p);
15428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->v_type = VAR_STRING;
15430 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431}
15432
15433/*
15434 * "synIDtrans(id)" function
15435 */
15436/*ARGSUSED*/
15437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015439 typval_T *argvars;
15440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441{
15442 int id;
15443
15444#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446
15447 if (id > 0)
15448 id = syn_get_final_id(id);
15449 else
15450#endif
15451 id = 0;
15452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454}
15455
15456/*
15457 * "system()" function
15458 */
15459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015460f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015461 typval_T *argvars;
15462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015464 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015466 char_u *infile = NULL;
15467 char_u buf[NUMBUFLEN];
15468 int err = FALSE;
15469 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015471 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015472 {
15473 /*
15474 * Write the string to a temp file, to be used for input of the shell
15475 * command.
15476 */
15477 if ((infile = vim_tempname('i')) == NULL)
15478 {
15479 EMSG(_(e_notmp));
15480 return;
15481 }
15482
15483 fd = mch_fopen((char *)infile, WRITEBIN);
15484 if (fd == NULL)
15485 {
15486 EMSG2(_(e_notopen), infile);
15487 goto done;
15488 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015489 p = get_tv_string_buf_chk(&argvars[1], buf);
15490 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015491 {
15492 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015493 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015494 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015495 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15496 err = TRUE;
15497 if (fclose(fd) != 0)
15498 err = TRUE;
15499 if (err)
15500 {
15501 EMSG(_("E677: Error writing temp file"));
15502 goto done;
15503 }
15504 }
15505
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015506 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15507 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015508
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509#ifdef USE_CR
15510 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015511 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 {
15513 char_u *s;
15514
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015515 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 {
15517 if (*s == CAR)
15518 *s = NL;
15519 }
15520 }
15521#else
15522# ifdef USE_CRNL
15523 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015524 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 {
15526 char_u *s, *d;
15527
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015528 d = res;
15529 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 {
15531 if (s[0] == CAR && s[1] == NL)
15532 ++s;
15533 *d++ = *s;
15534 }
15535 *d = NUL;
15536 }
15537# endif
15538#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015539
15540done:
15541 if (infile != NULL)
15542 {
15543 mch_remove(infile);
15544 vim_free(infile);
15545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015546 rettv->v_type = VAR_STRING;
15547 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548}
15549
15550/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015551 * "tabpagebuflist()" function
15552 */
15553/* ARGSUSED */
15554 static void
15555f_tabpagebuflist(argvars, rettv)
15556 typval_T *argvars;
15557 typval_T *rettv;
15558{
15559#ifndef FEAT_WINDOWS
15560 rettv->vval.v_number = 0;
15561#else
15562 tabpage_T *tp;
15563 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015564
15565 if (argvars[0].v_type == VAR_UNKNOWN)
15566 wp = firstwin;
15567 else
15568 {
15569 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15570 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015571 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015572 }
15573 if (wp == NULL)
15574 rettv->vval.v_number = 0;
15575 else
15576 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015577 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015578 rettv->vval.v_number = 0;
15579 else
15580 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015581 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015582 if (list_append_number(rettv->vval.v_list,
15583 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015584 break;
15585 }
15586 }
15587#endif
15588}
15589
15590
15591/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015592 * "tabpagenr()" function
15593 */
15594/* ARGSUSED */
15595 static void
15596f_tabpagenr(argvars, rettv)
15597 typval_T *argvars;
15598 typval_T *rettv;
15599{
15600 int nr = 1;
15601#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015602 char_u *arg;
15603
15604 if (argvars[0].v_type != VAR_UNKNOWN)
15605 {
15606 arg = get_tv_string_chk(&argvars[0]);
15607 nr = 0;
15608 if (arg != NULL)
15609 {
15610 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015611 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015612 else
15613 EMSG2(_(e_invexpr2), arg);
15614 }
15615 }
15616 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015617 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015618#endif
15619 rettv->vval.v_number = nr;
15620}
15621
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015622
15623#ifdef FEAT_WINDOWS
15624static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15625
15626/*
15627 * Common code for tabpagewinnr() and winnr().
15628 */
15629 static int
15630get_winnr(tp, argvar)
15631 tabpage_T *tp;
15632 typval_T *argvar;
15633{
15634 win_T *twin;
15635 int nr = 1;
15636 win_T *wp;
15637 char_u *arg;
15638
15639 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15640 if (argvar->v_type != VAR_UNKNOWN)
15641 {
15642 arg = get_tv_string_chk(argvar);
15643 if (arg == NULL)
15644 nr = 0; /* type error; errmsg already given */
15645 else if (STRCMP(arg, "$") == 0)
15646 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15647 else if (STRCMP(arg, "#") == 0)
15648 {
15649 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15650 if (twin == NULL)
15651 nr = 0;
15652 }
15653 else
15654 {
15655 EMSG2(_(e_invexpr2), arg);
15656 nr = 0;
15657 }
15658 }
15659
15660 if (nr > 0)
15661 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15662 wp != twin; wp = wp->w_next)
15663 ++nr;
15664 return nr;
15665}
15666#endif
15667
15668/*
15669 * "tabpagewinnr()" function
15670 */
15671/* ARGSUSED */
15672 static void
15673f_tabpagewinnr(argvars, rettv)
15674 typval_T *argvars;
15675 typval_T *rettv;
15676{
15677 int nr = 1;
15678#ifdef FEAT_WINDOWS
15679 tabpage_T *tp;
15680
15681 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15682 if (tp == NULL)
15683 nr = 0;
15684 else
15685 nr = get_winnr(tp, &argvars[1]);
15686#endif
15687 rettv->vval.v_number = nr;
15688}
15689
15690
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015691/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015692 * "tagfiles()" function
15693 */
15694/*ARGSUSED*/
15695 static void
15696f_tagfiles(argvars, rettv)
15697 typval_T *argvars;
15698 typval_T *rettv;
15699{
15700 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015701 tagname_T tn;
15702 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015703
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015704 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015705 {
15706 rettv->vval.v_number = 0;
15707 return;
15708 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015710 for (first = TRUE; ; first = FALSE)
15711 if (get_tagfname(&tn, first, fname) == FAIL
15712 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015713 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015714 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015715}
15716
15717/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015718 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015719 */
15720 static void
15721f_taglist(argvars, rettv)
15722 typval_T *argvars;
15723 typval_T *rettv;
15724{
15725 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015726
15727 tag_pattern = get_tv_string(&argvars[0]);
15728
15729 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015730 if (*tag_pattern == NUL)
15731 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015732
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015733 if (rettv_list_alloc(rettv) == OK)
15734 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015735}
15736
15737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 * "tempname()" function
15739 */
15740/*ARGSUSED*/
15741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015742f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015743 typval_T *argvars;
15744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745{
15746 static int x = 'A';
15747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015748 rettv->v_type = VAR_STRING;
15749 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750
15751 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15752 * names. Skip 'I' and 'O', they are used for shell redirection. */
15753 do
15754 {
15755 if (x == 'Z')
15756 x = '0';
15757 else if (x == '9')
15758 x = 'A';
15759 else
15760 {
15761#ifdef EBCDIC
15762 if (x == 'I')
15763 x = 'J';
15764 else if (x == 'R')
15765 x = 'S';
15766 else
15767#endif
15768 ++x;
15769 }
15770 } while (x == 'I' || x == 'O');
15771}
15772
15773/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015774 * "test(list)" function: Just checking the walls...
15775 */
15776/*ARGSUSED*/
15777 static void
15778f_test(argvars, rettv)
15779 typval_T *argvars;
15780 typval_T *rettv;
15781{
15782 /* Used for unit testing. Change the code below to your liking. */
15783#if 0
15784 listitem_T *li;
15785 list_T *l;
15786 char_u *bad, *good;
15787
15788 if (argvars[0].v_type != VAR_LIST)
15789 return;
15790 l = argvars[0].vval.v_list;
15791 if (l == NULL)
15792 return;
15793 li = l->lv_first;
15794 if (li == NULL)
15795 return;
15796 bad = get_tv_string(&li->li_tv);
15797 li = li->li_next;
15798 if (li == NULL)
15799 return;
15800 good = get_tv_string(&li->li_tv);
15801 rettv->vval.v_number = test_edit_score(bad, good);
15802#endif
15803}
15804
15805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 * "tolower(string)" function
15807 */
15808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015810 typval_T *argvars;
15811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812{
15813 char_u *p;
15814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015815 p = vim_strsave(get_tv_string(&argvars[0]));
15816 rettv->v_type = VAR_STRING;
15817 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818
15819 if (p != NULL)
15820 while (*p != NUL)
15821 {
15822#ifdef FEAT_MBYTE
15823 int l;
15824
15825 if (enc_utf8)
15826 {
15827 int c, lc;
15828
15829 c = utf_ptr2char(p);
15830 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015831 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 /* TODO: reallocate string when byte count changes. */
15833 if (utf_char2len(lc) == l)
15834 utf_char2bytes(lc, p);
15835 p += l;
15836 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015837 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 p += l; /* skip multi-byte character */
15839 else
15840#endif
15841 {
15842 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15843 ++p;
15844 }
15845 }
15846}
15847
15848/*
15849 * "toupper(string)" function
15850 */
15851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015852f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 typval_T *argvars;
15854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015856 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015857 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858}
15859
15860/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015861 * "tr(string, fromstr, tostr)" function
15862 */
15863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015864f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015865 typval_T *argvars;
15866 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015867{
15868 char_u *instr;
15869 char_u *fromstr;
15870 char_u *tostr;
15871 char_u *p;
15872#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015873 int inlen;
15874 int fromlen;
15875 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015876 int idx;
15877 char_u *cpstr;
15878 int cplen;
15879 int first = TRUE;
15880#endif
15881 char_u buf[NUMBUFLEN];
15882 char_u buf2[NUMBUFLEN];
15883 garray_T ga;
15884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015885 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015886 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15887 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015888
15889 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015890 rettv->v_type = VAR_STRING;
15891 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015892 if (fromstr == NULL || tostr == NULL)
15893 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015894 ga_init2(&ga, (int)sizeof(char), 80);
15895
15896#ifdef FEAT_MBYTE
15897 if (!has_mbyte)
15898#endif
15899 /* not multi-byte: fromstr and tostr must be the same length */
15900 if (STRLEN(fromstr) != STRLEN(tostr))
15901 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015902#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015903error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015904#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015905 EMSG2(_(e_invarg2), fromstr);
15906 ga_clear(&ga);
15907 return;
15908 }
15909
15910 /* fromstr and tostr have to contain the same number of chars */
15911 while (*instr != NUL)
15912 {
15913#ifdef FEAT_MBYTE
15914 if (has_mbyte)
15915 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015916 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015917 cpstr = instr;
15918 cplen = inlen;
15919 idx = 0;
15920 for (p = fromstr; *p != NUL; p += fromlen)
15921 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015922 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015923 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15924 {
15925 for (p = tostr; *p != NUL; p += tolen)
15926 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015927 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015928 if (idx-- == 0)
15929 {
15930 cplen = tolen;
15931 cpstr = p;
15932 break;
15933 }
15934 }
15935 if (*p == NUL) /* tostr is shorter than fromstr */
15936 goto error;
15937 break;
15938 }
15939 ++idx;
15940 }
15941
15942 if (first && cpstr == instr)
15943 {
15944 /* Check that fromstr and tostr have the same number of
15945 * (multi-byte) characters. Done only once when a character
15946 * of instr doesn't appear in fromstr. */
15947 first = FALSE;
15948 for (p = tostr; *p != NUL; p += tolen)
15949 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015950 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015951 --idx;
15952 }
15953 if (idx != 0)
15954 goto error;
15955 }
15956
15957 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015958 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015959 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015960
15961 instr += inlen;
15962 }
15963 else
15964#endif
15965 {
15966 /* When not using multi-byte chars we can do it faster. */
15967 p = vim_strchr(fromstr, *instr);
15968 if (p != NULL)
15969 ga_append(&ga, tostr[p - fromstr]);
15970 else
15971 ga_append(&ga, *instr);
15972 ++instr;
15973 }
15974 }
15975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015976 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015977}
15978
15979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 * "type(expr)" function
15981 */
15982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015983f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015984 typval_T *argvars;
15985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015987 int n;
15988
15989 switch (argvars[0].v_type)
15990 {
15991 case VAR_NUMBER: n = 0; break;
15992 case VAR_STRING: n = 1; break;
15993 case VAR_FUNC: n = 2; break;
15994 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015995 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015996 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15997 }
15998 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999}
16000
16001/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016002 * "values(dict)" function
16003 */
16004 static void
16005f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016006 typval_T *argvars;
16007 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016008{
16009 dict_list(argvars, rettv, 1);
16010}
16011
16012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 * "virtcol(string)" function
16014 */
16015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016016f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016017 typval_T *argvars;
16018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019{
16020 colnr_T vcol = 0;
16021 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016022 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016024 fp = var2fpos(&argvars[0], FALSE, &fnum);
16025 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16026 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027 {
16028 getvvcol(curwin, fp, NULL, NULL, &vcol);
16029 ++vcol;
16030 }
16031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016032 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033}
16034
16035/*
16036 * "visualmode()" function
16037 */
16038/*ARGSUSED*/
16039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016040f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016041 typval_T *argvars;
16042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043{
16044#ifdef FEAT_VISUAL
16045 char_u str[2];
16046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016047 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 str[0] = curbuf->b_visual_mode_eval;
16049 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016050 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051
16052 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016053 if ((argvars[0].v_type == VAR_NUMBER
16054 && argvars[0].vval.v_number != 0)
16055 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016056 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 curbuf->b_visual_mode_eval = NUL;
16058#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016059 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060#endif
16061}
16062
16063/*
16064 * "winbufnr(nr)" function
16065 */
16066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016067f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016068 typval_T *argvars;
16069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070{
16071 win_T *wp;
16072
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016073 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016077 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078}
16079
16080/*
16081 * "wincol()" function
16082 */
16083/*ARGSUSED*/
16084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016085f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 typval_T *argvars;
16087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088{
16089 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016090 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091}
16092
16093/*
16094 * "winheight(nr)" function
16095 */
16096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016098 typval_T *argvars;
16099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100{
16101 win_T *wp;
16102
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016103 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016107 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108}
16109
16110/*
16111 * "winline()" function
16112 */
16113/*ARGSUSED*/
16114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016115f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016116 typval_T *argvars;
16117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118{
16119 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016120 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121}
16122
16123/*
16124 * "winnr()" function
16125 */
16126/* ARGSUSED */
16127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016128f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016129 typval_T *argvars;
16130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131{
16132 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016133
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016135 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016137 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138}
16139
16140/*
16141 * "winrestcmd()" function
16142 */
16143/* ARGSUSED */
16144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 typval_T *argvars;
16147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148{
16149#ifdef FEAT_WINDOWS
16150 win_T *wp;
16151 int winnr = 1;
16152 garray_T ga;
16153 char_u buf[50];
16154
16155 ga_init2(&ga, (int)sizeof(char), 70);
16156 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16157 {
16158 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16159 ga_concat(&ga, buf);
16160# ifdef FEAT_VERTSPLIT
16161 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16162 ga_concat(&ga, buf);
16163# endif
16164 ++winnr;
16165 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016166 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016168 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016172 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173}
16174
16175/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016176 * "winrestview()" function
16177 */
16178/* ARGSUSED */
16179 static void
16180f_winrestview(argvars, rettv)
16181 typval_T *argvars;
16182 typval_T *rettv;
16183{
16184 dict_T *dict;
16185
16186 if (argvars[0].v_type != VAR_DICT
16187 || (dict = argvars[0].vval.v_dict) == NULL)
16188 EMSG(_(e_invarg));
16189 else
16190 {
16191 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16192 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16193#ifdef FEAT_VIRTUALEDIT
16194 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16195#endif
16196 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016197 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016198
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016199 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016200#ifdef FEAT_DIFF
16201 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16202#endif
16203 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16204 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16205
16206 check_cursor();
16207 changed_cline_bef_curs();
16208 invalidate_botline();
16209 redraw_later(VALID);
16210
16211 if (curwin->w_topline == 0)
16212 curwin->w_topline = 1;
16213 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16214 curwin->w_topline = curbuf->b_ml.ml_line_count;
16215#ifdef FEAT_DIFF
16216 check_topfill(curwin, TRUE);
16217#endif
16218 }
16219}
16220
16221/*
16222 * "winsaveview()" function
16223 */
16224/* ARGSUSED */
16225 static void
16226f_winsaveview(argvars, rettv)
16227 typval_T *argvars;
16228 typval_T *rettv;
16229{
16230 dict_T *dict;
16231
16232 dict = dict_alloc();
16233 if (dict == NULL)
16234 return;
16235 rettv->v_type = VAR_DICT;
16236 rettv->vval.v_dict = dict;
16237 ++dict->dv_refcount;
16238
16239 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16240 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16241#ifdef FEAT_VIRTUALEDIT
16242 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16243#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016244 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016245 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16246
16247 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16248#ifdef FEAT_DIFF
16249 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16250#endif
16251 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16252 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16253}
16254
16255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 * "winwidth(nr)" function
16257 */
16258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016259f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016260 typval_T *argvars;
16261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262{
16263 win_T *wp;
16264
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016265 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016267 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 else
16269#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016272 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273#endif
16274}
16275
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016277 * "writefile()" function
16278 */
16279 static void
16280f_writefile(argvars, rettv)
16281 typval_T *argvars;
16282 typval_T *rettv;
16283{
16284 int binary = FALSE;
16285 char_u *fname;
16286 FILE *fd;
16287 listitem_T *li;
16288 char_u *s;
16289 int ret = 0;
16290 int c;
16291
16292 if (argvars[0].v_type != VAR_LIST)
16293 {
16294 EMSG2(_(e_listarg), "writefile()");
16295 return;
16296 }
16297 if (argvars[0].vval.v_list == NULL)
16298 return;
16299
16300 if (argvars[2].v_type != VAR_UNKNOWN
16301 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16302 binary = TRUE;
16303
16304 /* Always open the file in binary mode, library functions have a mind of
16305 * their own about CR-LF conversion. */
16306 fname = get_tv_string(&argvars[1]);
16307 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16308 {
16309 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16310 ret = -1;
16311 }
16312 else
16313 {
16314 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16315 li = li->li_next)
16316 {
16317 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16318 {
16319 if (*s == '\n')
16320 c = putc(NUL, fd);
16321 else
16322 c = putc(*s, fd);
16323 if (c == EOF)
16324 {
16325 ret = -1;
16326 break;
16327 }
16328 }
16329 if (!binary || li->li_next != NULL)
16330 if (putc('\n', fd) == EOF)
16331 {
16332 ret = -1;
16333 break;
16334 }
16335 if (ret < 0)
16336 {
16337 EMSG(_(e_write));
16338 break;
16339 }
16340 }
16341 fclose(fd);
16342 }
16343
16344 rettv->vval.v_number = ret;
16345}
16346
16347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016349 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 */
16351 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016352var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016353 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016355 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016357 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016359 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360
Bram Moolenaara5525202006-03-02 22:52:09 +000016361 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016362 if (varp->v_type == VAR_LIST)
16363 {
16364 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016365 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016366 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016367
16368 l = varp->vval.v_list;
16369 if (l == NULL)
16370 return NULL;
16371
16372 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016373 pos.lnum = list_find_nr(l, 0L, &error);
16374 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016375 return NULL; /* invalid line number */
16376
16377 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016378 pos.col = list_find_nr(l, 1L, &error);
16379 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016380 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016381 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016382 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016383 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016384 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016385 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016386
Bram Moolenaara5525202006-03-02 22:52:09 +000016387#ifdef FEAT_VIRTUALEDIT
16388 /* Get the virtual offset. Defaults to zero. */
16389 pos.coladd = list_find_nr(l, 2L, &error);
16390 if (error)
16391 pos.coladd = 0;
16392#endif
16393
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016394 return &pos;
16395 }
16396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016397 name = get_tv_string_chk(varp);
16398 if (name == NULL)
16399 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 if (name[0] == '.') /* cursor */
16401 return &curwin->w_cursor;
16402 if (name[0] == '\'') /* mark */
16403 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016404 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16406 return NULL;
16407 return pp;
16408 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016409
16410#ifdef FEAT_VIRTUALEDIT
16411 pos.coladd = 0;
16412#endif
16413
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016414 if (name[0] == 'w' && lnum)
16415 {
16416 pos.col = 0;
16417 if (name[1] == '0') /* "w0": first visible line */
16418 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016419 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016420 pos.lnum = curwin->w_topline;
16421 return &pos;
16422 }
16423 else if (name[1] == '$') /* "w$": last visible line */
16424 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016425 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016426 pos.lnum = curwin->w_botline - 1;
16427 return &pos;
16428 }
16429 }
16430 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 {
16432 if (lnum)
16433 {
16434 pos.lnum = curbuf->b_ml.ml_line_count;
16435 pos.col = 0;
16436 }
16437 else
16438 {
16439 pos.lnum = curwin->w_cursor.lnum;
16440 pos.col = (colnr_T)STRLEN(ml_get_curline());
16441 }
16442 return &pos;
16443 }
16444 return NULL;
16445}
16446
16447/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016448 * Convert list in "arg" into a position and optional file number.
16449 * When "fnump" is NULL there is no file number, only 3 items.
16450 * Note that the column is passed on as-is, the caller may want to decrement
16451 * it to use 1 for the first column.
16452 * Return FAIL when conversion is not possible, doesn't check the position for
16453 * validity.
16454 */
16455 static int
16456list2fpos(arg, posp, fnump)
16457 typval_T *arg;
16458 pos_T *posp;
16459 int *fnump;
16460{
16461 list_T *l = arg->vval.v_list;
16462 long i = 0;
16463 long n;
16464
Bram Moolenaarbde35262006-07-23 20:12:24 +000016465 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16466 * when "fnump" isn't NULL and "coladd" is optional. */
16467 if (arg->v_type != VAR_LIST
16468 || l == NULL
16469 || l->lv_len < (fnump == NULL ? 2 : 3)
16470 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016471 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)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016496 posp->coladd = 0;
16497 else
16498 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016499#endif
16500
16501 return OK;
16502}
16503
16504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 * Get the length of an environment variable name.
16506 * Advance "arg" to the first character after the name.
16507 * Return 0 for error.
16508 */
16509 static int
16510get_env_len(arg)
16511 char_u **arg;
16512{
16513 char_u *p;
16514 int len;
16515
16516 for (p = *arg; vim_isIDc(*p); ++p)
16517 ;
16518 if (p == *arg) /* no name found */
16519 return 0;
16520
16521 len = (int)(p - *arg);
16522 *arg = p;
16523 return len;
16524}
16525
16526/*
16527 * Get the length of the name of a function or internal variable.
16528 * "arg" is advanced to the first non-white character after the name.
16529 * Return 0 if something is wrong.
16530 */
16531 static int
16532get_id_len(arg)
16533 char_u **arg;
16534{
16535 char_u *p;
16536 int len;
16537
16538 /* Find the end of the name. */
16539 for (p = *arg; eval_isnamec(*p); ++p)
16540 ;
16541 if (p == *arg) /* no name found */
16542 return 0;
16543
16544 len = (int)(p - *arg);
16545 *arg = skipwhite(p);
16546
16547 return len;
16548}
16549
16550/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016551 * Get the length of the name of a variable or function.
16552 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016554 * Return -1 if curly braces expansion failed.
16555 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 * If the name contains 'magic' {}'s, expand them and return the
16557 * expanded name in an allocated string via 'alias' - caller must free.
16558 */
16559 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016560get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 char_u **arg;
16562 char_u **alias;
16563 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016564 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565{
16566 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 char_u *p;
16568 char_u *expr_start;
16569 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570
16571 *alias = NULL; /* default to no alias */
16572
16573 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16574 && (*arg)[2] == (int)KE_SNR)
16575 {
16576 /* hard coded <SNR>, already translated */
16577 *arg += 3;
16578 return get_id_len(arg) + 3;
16579 }
16580 len = eval_fname_script(*arg);
16581 if (len > 0)
16582 {
16583 /* literal "<SID>", "s:" or "<SNR>" */
16584 *arg += len;
16585 }
16586
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016590 p = find_name_end(*arg, &expr_start, &expr_end,
16591 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 if (expr_start != NULL)
16593 {
16594 char_u *temp_string;
16595
16596 if (!evaluate)
16597 {
16598 len += (int)(p - *arg);
16599 *arg = skipwhite(p);
16600 return len;
16601 }
16602
16603 /*
16604 * Include any <SID> etc in the expanded string:
16605 * Thus the -len here.
16606 */
16607 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16608 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016609 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 *alias = temp_string;
16611 *arg = skipwhite(p);
16612 return (int)STRLEN(temp_string);
16613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614
16615 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016616 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617 EMSG2(_(e_invexpr2), *arg);
16618
16619 return len;
16620}
16621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016622/*
16623 * Find the end of a variable or function name, taking care of magic braces.
16624 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16625 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016626 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016627 * Return a pointer to just after the name. Equal to "arg" if there is no
16628 * valid name.
16629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016631find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 char_u *arg;
16633 char_u **expr_start;
16634 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016635 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637 int mb_nest = 0;
16638 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 char_u *p;
16640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016641 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643 *expr_start = NULL;
16644 *expr_end = NULL;
16645 }
16646
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016647 /* Quick check for valid starting character. */
16648 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16649 return arg;
16650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651 for (p = arg; *p != NUL
16652 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016653 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016654 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016655 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016656 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016657 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016658 if (*p == '\'')
16659 {
16660 /* skip over 'string' to avoid counting [ and ] inside it. */
16661 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16662 ;
16663 if (*p == NUL)
16664 break;
16665 }
16666 else if (*p == '"')
16667 {
16668 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16669 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16670 if (*p == '\\' && p[1] != NUL)
16671 ++p;
16672 if (*p == NUL)
16673 break;
16674 }
16675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016676 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678 if (*p == '[')
16679 ++br_nest;
16680 else if (*p == ']')
16681 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016684 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016686 if (*p == '{')
16687 {
16688 mb_nest++;
16689 if (expr_start != NULL && *expr_start == NULL)
16690 *expr_start = p;
16691 }
16692 else if (*p == '}')
16693 {
16694 mb_nest--;
16695 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16696 *expr_end = p;
16697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 }
16700
16701 return p;
16702}
16703
16704/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016705 * Expands out the 'magic' {}'s in a variable/function name.
16706 * Note that this can call itself recursively, to deal with
16707 * constructs like foo{bar}{baz}{bam}
16708 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16709 * "in_start" ^
16710 * "expr_start" ^
16711 * "expr_end" ^
16712 * "in_end" ^
16713 *
16714 * Returns a new allocated string, which the caller must free.
16715 * Returns NULL for failure.
16716 */
16717 static char_u *
16718make_expanded_name(in_start, expr_start, expr_end, in_end)
16719 char_u *in_start;
16720 char_u *expr_start;
16721 char_u *expr_end;
16722 char_u *in_end;
16723{
16724 char_u c1;
16725 char_u *retval = NULL;
16726 char_u *temp_result;
16727 char_u *nextcmd = NULL;
16728
16729 if (expr_end == NULL || in_end == NULL)
16730 return NULL;
16731 *expr_start = NUL;
16732 *expr_end = NUL;
16733 c1 = *in_end;
16734 *in_end = NUL;
16735
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016736 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016737 if (temp_result != NULL && nextcmd == NULL)
16738 {
16739 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16740 + (in_end - expr_end) + 1));
16741 if (retval != NULL)
16742 {
16743 STRCPY(retval, in_start);
16744 STRCAT(retval, temp_result);
16745 STRCAT(retval, expr_end + 1);
16746 }
16747 }
16748 vim_free(temp_result);
16749
16750 *in_end = c1; /* put char back for error messages */
16751 *expr_start = '{';
16752 *expr_end = '}';
16753
16754 if (retval != NULL)
16755 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016756 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016757 if (expr_start != NULL)
16758 {
16759 /* Further expansion! */
16760 temp_result = make_expanded_name(retval, expr_start,
16761 expr_end, temp_result);
16762 vim_free(retval);
16763 retval = temp_result;
16764 }
16765 }
16766
16767 return retval;
16768}
16769
16770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016772 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 */
16774 static int
16775eval_isnamec(c)
16776 int c;
16777{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016778 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16779}
16780
16781/*
16782 * Return TRUE if character "c" can be used as the first character in a
16783 * variable or function name (excluding '{' and '}').
16784 */
16785 static int
16786eval_isnamec1(c)
16787 int c;
16788{
16789 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790}
16791
16792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793 * Set number v: variable to "val".
16794 */
16795 void
16796set_vim_var_nr(idx, val)
16797 int idx;
16798 long val;
16799{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016800 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801}
16802
16803/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016804 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 */
16806 long
16807get_vim_var_nr(idx)
16808 int idx;
16809{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016810 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811}
16812
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016813#if defined(FEAT_AUTOCMD) || defined(PROTO)
16814/*
16815 * Get string v: variable value. Uses a static buffer, can only be used once.
16816 */
16817 char_u *
16818get_vim_var_str(idx)
16819 int idx;
16820{
16821 return get_tv_string(&vimvars[idx].vv_tv);
16822}
16823#endif
16824
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825/*
16826 * Set v:count, v:count1 and v:prevcount.
16827 */
16828 void
16829set_vcount(count, count1)
16830 long count;
16831 long count1;
16832{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016833 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16834 vimvars[VV_COUNT].vv_nr = count;
16835 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836}
16837
16838/*
16839 * Set string v: variable to a copy of "val".
16840 */
16841 void
16842set_vim_var_string(idx, val, len)
16843 int idx;
16844 char_u *val;
16845 int len; /* length of "val" to use or -1 (whole string) */
16846{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016847 /* Need to do this (at least) once, since we can't initialize a union.
16848 * Will always be invoked when "v:progname" is set. */
16849 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16850
Bram Moolenaare9a41262005-01-15 22:18:47 +000016851 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016853 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016855 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016857 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858}
16859
16860/*
16861 * Set v:register if needed.
16862 */
16863 void
16864set_reg_var(c)
16865 int c;
16866{
16867 char_u regname;
16868
16869 if (c == 0 || c == ' ')
16870 regname = '"';
16871 else
16872 regname = c;
16873 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016874 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 set_vim_var_string(VV_REG, &regname, 1);
16876}
16877
16878/*
16879 * Get or set v:exception. If "oldval" == NULL, return the current value.
16880 * Otherwise, restore the value to "oldval" and return NULL.
16881 * Must always be called in pairs to save and restore v:exception! Does not
16882 * take care of memory allocations.
16883 */
16884 char_u *
16885v_exception(oldval)
16886 char_u *oldval;
16887{
16888 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016889 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
Bram Moolenaare9a41262005-01-15 22:18:47 +000016891 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892 return NULL;
16893}
16894
16895/*
16896 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16897 * Otherwise, restore the value to "oldval" and return NULL.
16898 * Must always be called in pairs to save and restore v:throwpoint! Does not
16899 * take care of memory allocations.
16900 */
16901 char_u *
16902v_throwpoint(oldval)
16903 char_u *oldval;
16904{
16905 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016906 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907
Bram Moolenaare9a41262005-01-15 22:18:47 +000016908 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 return NULL;
16910}
16911
16912#if defined(FEAT_AUTOCMD) || defined(PROTO)
16913/*
16914 * Set v:cmdarg.
16915 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16916 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16917 * Must always be called in pairs!
16918 */
16919 char_u *
16920set_cmdarg(eap, oldarg)
16921 exarg_T *eap;
16922 char_u *oldarg;
16923{
16924 char_u *oldval;
16925 char_u *newval;
16926 unsigned len;
16927
Bram Moolenaare9a41262005-01-15 22:18:47 +000016928 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016929 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016931 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016932 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016933 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 }
16935
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016936 if (eap->force_bin == FORCE_BIN)
16937 len = 6;
16938 else if (eap->force_bin == FORCE_NOBIN)
16939 len = 8;
16940 else
16941 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016942
16943 if (eap->read_edit)
16944 len += 7;
16945
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016946 if (eap->force_ff != 0)
16947 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16948# ifdef FEAT_MBYTE
16949 if (eap->force_enc != 0)
16950 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016951 if (eap->bad_char != 0)
16952 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016953# endif
16954
16955 newval = alloc(len + 1);
16956 if (newval == NULL)
16957 return NULL;
16958
16959 if (eap->force_bin == FORCE_BIN)
16960 sprintf((char *)newval, " ++bin");
16961 else if (eap->force_bin == FORCE_NOBIN)
16962 sprintf((char *)newval, " ++nobin");
16963 else
16964 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016965
16966 if (eap->read_edit)
16967 STRCAT(newval, " ++edit");
16968
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016969 if (eap->force_ff != 0)
16970 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16971 eap->cmd + eap->force_ff);
16972# ifdef FEAT_MBYTE
16973 if (eap->force_enc != 0)
16974 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16975 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016976 if (eap->bad_char != 0)
16977 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16978 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016979# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016980 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016981 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982}
16983#endif
16984
16985/*
16986 * Get the value of internal variable "name".
16987 * Return OK or FAIL.
16988 */
16989 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016990get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 char_u *name;
16992 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016993 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016994 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995{
16996 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016997 typval_T *tv = NULL;
16998 typval_T atv;
16999 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001
17002 /* truncate the name, so that we can use strcmp() */
17003 cc = name[len];
17004 name[len] = NUL;
17005
17006 /*
17007 * Check for "b:changedtick".
17008 */
17009 if (STRCMP(name, "b:changedtick") == 0)
17010 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017011 atv.v_type = VAR_NUMBER;
17012 atv.vval.v_number = curbuf->b_changedtick;
17013 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 }
17015
17016 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 * Check for user-defined variables.
17018 */
17019 else
17020 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017021 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017023 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 }
17025
Bram Moolenaare9a41262005-01-15 22:18:47 +000017026 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017028 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 ret = FAIL;
17031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017032 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017033 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034
17035 name[len] = cc;
17036
17037 return ret;
17038}
17039
17040/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017041 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17042 * Also handle function call with Funcref variable: func(expr)
17043 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17044 */
17045 static int
17046handle_subscript(arg, rettv, evaluate, verbose)
17047 char_u **arg;
17048 typval_T *rettv;
17049 int evaluate; /* do more than finding the end */
17050 int verbose; /* give error messages */
17051{
17052 int ret = OK;
17053 dict_T *selfdict = NULL;
17054 char_u *s;
17055 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017056 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017057
17058 while (ret == OK
17059 && (**arg == '['
17060 || (**arg == '.' && rettv->v_type == VAR_DICT)
17061 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17062 && !vim_iswhite(*(*arg - 1)))
17063 {
17064 if (**arg == '(')
17065 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017066 /* need to copy the funcref so that we can clear rettv */
17067 functv = *rettv;
17068 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017069
17070 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017071 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017072 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017073 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17074 &len, evaluate, selfdict);
17075
17076 /* Clear the funcref afterwards, so that deleting it while
17077 * evaluating the arguments is possible (see test55). */
17078 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017079
17080 /* Stop the expression evaluation when immediately aborting on
17081 * error, or when an interrupt occurred or an exception was thrown
17082 * but not caught. */
17083 if (aborting())
17084 {
17085 if (ret == OK)
17086 clear_tv(rettv);
17087 ret = FAIL;
17088 }
17089 dict_unref(selfdict);
17090 selfdict = NULL;
17091 }
17092 else /* **arg == '[' || **arg == '.' */
17093 {
17094 dict_unref(selfdict);
17095 if (rettv->v_type == VAR_DICT)
17096 {
17097 selfdict = rettv->vval.v_dict;
17098 if (selfdict != NULL)
17099 ++selfdict->dv_refcount;
17100 }
17101 else
17102 selfdict = NULL;
17103 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17104 {
17105 clear_tv(rettv);
17106 ret = FAIL;
17107 }
17108 }
17109 }
17110 dict_unref(selfdict);
17111 return ret;
17112}
17113
17114/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017115 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17116 * value).
17117 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017118 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017120{
Bram Moolenaar33570922005-01-25 22:26:29 +000017121 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017122}
17123
17124/*
17125 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 * The string "s" must have been allocated, it is consumed.
17127 * Return NULL for out of memory, the variable otherwise.
17128 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017130alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 char_u *s;
17132{
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 rettv = alloc_tv();
17136 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017138 rettv->v_type = VAR_STRING;
17139 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 }
17141 else
17142 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017143 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144}
17145
17146/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017147 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017149 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017150free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152{
17153 if (varp != NULL)
17154 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017155 switch (varp->v_type)
17156 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017157 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017158 func_unref(varp->vval.v_string);
17159 /*FALLTHROUGH*/
17160 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017161 vim_free(varp->vval.v_string);
17162 break;
17163 case VAR_LIST:
17164 list_unref(varp->vval.v_list);
17165 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017166 case VAR_DICT:
17167 dict_unref(varp->vval.v_dict);
17168 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017169 case VAR_NUMBER:
17170 case VAR_UNKNOWN:
17171 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017172 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017173 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017174 break;
17175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 vim_free(varp);
17177 }
17178}
17179
17180/*
17181 * Free the memory for a variable value and set the value to NULL or 0.
17182 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017183 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017184clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017185 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186{
17187 if (varp != NULL)
17188 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017189 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017191 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017192 func_unref(varp->vval.v_string);
17193 /*FALLTHROUGH*/
17194 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017195 vim_free(varp->vval.v_string);
17196 varp->vval.v_string = NULL;
17197 break;
17198 case VAR_LIST:
17199 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017200 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017201 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017202 case VAR_DICT:
17203 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017204 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017205 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017206 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017207 varp->vval.v_number = 0;
17208 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017209 case VAR_UNKNOWN:
17210 break;
17211 default:
17212 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017214 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 }
17216}
17217
17218/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017219 * Set the value of a variable to NULL without freeing items.
17220 */
17221 static void
17222init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017223 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017224{
17225 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017226 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017227}
17228
17229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 * Get the number value of a variable.
17231 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017232 * For incompatible types, return 0.
17233 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17234 * caller of incompatible types: it sets *denote to TRUE if "denote"
17235 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 */
17237 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017239 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017241 int error = FALSE;
17242
17243 return get_tv_number_chk(varp, &error); /* return 0L on error */
17244}
17245
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017246 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017247get_tv_number_chk(varp, denote)
17248 typval_T *varp;
17249 int *denote;
17250{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017251 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017253 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017255 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017257 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017258 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017259 break;
17260 case VAR_STRING:
17261 if (varp->vval.v_string != NULL)
17262 vim_str2nr(varp->vval.v_string, NULL, NULL,
17263 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017264 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017265 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017266 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017267 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017268 case VAR_DICT:
17269 EMSG(_("E728: Using a Dictionary as a number"));
17270 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017271 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017272 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017275 if (denote == NULL) /* useful for values that must be unsigned */
17276 n = -1;
17277 else
17278 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017279 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280}
17281
17282/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017283 * Get the lnum from the first argument.
17284 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017285 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 */
17287 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017288get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017289 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290{
Bram Moolenaar33570922005-01-25 22:26:29 +000017291 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 linenr_T lnum;
17293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017294 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 if (lnum == 0) /* no valid number, try using line() */
17296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 rettv.v_type = VAR_NUMBER;
17298 f_line(argvars, &rettv);
17299 lnum = rettv.vval.v_number;
17300 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 }
17302 return lnum;
17303}
17304
17305/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017306 * Get the lnum from the first argument.
17307 * Also accepts "$", then "buf" is used.
17308 * Returns 0 on error.
17309 */
17310 static linenr_T
17311get_tv_lnum_buf(argvars, buf)
17312 typval_T *argvars;
17313 buf_T *buf;
17314{
17315 if (argvars[0].v_type == VAR_STRING
17316 && argvars[0].vval.v_string != NULL
17317 && argvars[0].vval.v_string[0] == '$'
17318 && buf != NULL)
17319 return buf->b_ml.ml_line_count;
17320 return get_tv_number_chk(&argvars[0], NULL);
17321}
17322
17323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 * Get the string value of a variable.
17325 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017326 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17327 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 * If the String variable has never been set, return an empty string.
17329 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017330 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17331 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 */
17333 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017334get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017335 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336{
17337 static char_u mybuf[NUMBUFLEN];
17338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017339 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340}
17341
17342 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017343get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017344 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017345 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017347 char_u *res = get_tv_string_buf_chk(varp, buf);
17348
17349 return res != NULL ? res : (char_u *)"";
17350}
17351
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017352 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353get_tv_string_chk(varp)
17354 typval_T *varp;
17355{
17356 static char_u mybuf[NUMBUFLEN];
17357
17358 return get_tv_string_buf_chk(varp, mybuf);
17359}
17360
17361 static char_u *
17362get_tv_string_buf_chk(varp, buf)
17363 typval_T *varp;
17364 char_u *buf;
17365{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017366 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017368 case VAR_NUMBER:
17369 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17370 return buf;
17371 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017372 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017373 break;
17374 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017375 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017376 break;
17377 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017378 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017379 break;
17380 case VAR_STRING:
17381 if (varp->vval.v_string != NULL)
17382 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017383 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017384 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017385 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017386 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017388 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389}
17390
17391/*
17392 * Find variable "name" in the list of variables.
17393 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017394 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017395 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017396 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017399find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017401 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017404 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405
Bram Moolenaara7043832005-01-21 11:56:39 +000017406 ht = find_var_ht(name, &varname);
17407 if (htp != NULL)
17408 *htp = ht;
17409 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017411 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412}
17413
17414/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017415 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017416 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017418 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017419find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017420 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017421 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017422 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017423{
Bram Moolenaar33570922005-01-25 22:26:29 +000017424 hashitem_T *hi;
17425
17426 if (*varname == NUL)
17427 {
17428 /* Must be something like "s:", otherwise "ht" would be NULL. */
17429 switch (varname[-2])
17430 {
17431 case 's': return &SCRIPT_SV(current_SID).sv_var;
17432 case 'g': return &globvars_var;
17433 case 'v': return &vimvars_var;
17434 case 'b': return &curbuf->b_bufvar;
17435 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017436#ifdef FEAT_WINDOWS
17437 case 't': return &curtab->tp_winvar;
17438#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017439 case 'l': return current_funccal == NULL
17440 ? NULL : &current_funccal->l_vars_var;
17441 case 'a': return current_funccal == NULL
17442 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017443 }
17444 return NULL;
17445 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017446
17447 hi = hash_find(ht, varname);
17448 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017449 {
17450 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017451 * worked find the variable again. Don't auto-load a script if it was
17452 * loaded already, otherwise it would be loaded every time when
17453 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017454 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017455 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017456 hi = hash_find(ht, varname);
17457 if (HASHITEM_EMPTY(hi))
17458 return NULL;
17459 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017460 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017461}
17462
17463/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017464 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017465 * Set "varname" to the start of name without ':'.
17466 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017467 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017468find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469 char_u *name;
17470 char_u **varname;
17471{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017472 hashitem_T *hi;
17473
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 if (name[1] != ':')
17475 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017476 /* The name must not start with a colon or #. */
17477 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 return NULL;
17479 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017480
17481 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017482 hi = hash_find(&compat_hashtab, name);
17483 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017484 return &compat_hashtab;
17485
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017487 return &globvarht; /* global variable */
17488 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 }
17490 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017491 if (*name == 'g') /* global variable */
17492 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017493 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17494 */
17495 if (vim_strchr(name + 2, ':') != NULL
17496 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017497 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017499 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017502#ifdef FEAT_WINDOWS
17503 if (*name == 't') /* tab page variable */
17504 return &curtab->tp_vars.dv_hashtab;
17505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 if (*name == 'v') /* v: variable */
17507 return &vimvarht;
17508 if (*name == 'a' && current_funccal != NULL) /* function argument */
17509 return &current_funccal->l_avars.dv_hashtab;
17510 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17511 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 if (*name == 's' /* script variable */
17513 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17514 return &SCRIPT_VARS(current_SID);
17515 return NULL;
17516}
17517
17518/*
17519 * Get the string value of a (global/local) variable.
17520 * Returns NULL when it doesn't exist.
17521 */
17522 char_u *
17523get_var_value(name)
17524 char_u *name;
17525{
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527
Bram Moolenaara7043832005-01-21 11:56:39 +000017528 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529 if (v == NULL)
17530 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532}
17533
17534/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017535 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 * sourcing this script and when executing functions defined in the script.
17537 */
17538 void
17539new_script_vars(id)
17540 scid_T id;
17541{
Bram Moolenaara7043832005-01-21 11:56:39 +000017542 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 hashtab_T *ht;
17544 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017545
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17547 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017548 /* Re-allocating ga_data means that an ht_array pointing to
17549 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017550 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017551 for (i = 1; i <= ga_scripts.ga_len; ++i)
17552 {
17553 ht = &SCRIPT_VARS(i);
17554 if (ht->ht_mask == HT_INIT_SIZE - 1)
17555 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 sv = &SCRIPT_SV(i);
17557 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017558 }
17559
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 while (ga_scripts.ga_len < id)
17561 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017562 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17563 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565 }
17566 }
17567}
17568
17569/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017570 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17571 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 */
17573 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017574init_var_dict(dict, dict_var)
17575 dict_T *dict;
17576 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577{
Bram Moolenaar33570922005-01-25 22:26:29 +000017578 hash_init(&dict->dv_hashtab);
17579 dict->dv_refcount = 99999;
17580 dict_var->di_tv.vval.v_dict = dict;
17581 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017582 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17584 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585}
17586
17587/*
17588 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017589 * Frees all allocated variables and the value they contain.
17590 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 */
17592 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017593vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017594 hashtab_T *ht;
17595{
17596 vars_clear_ext(ht, TRUE);
17597}
17598
17599/*
17600 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17601 */
17602 static void
17603vars_clear_ext(ht, free_val)
17604 hashtab_T *ht;
17605 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606{
Bram Moolenaara7043832005-01-21 11:56:39 +000017607 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 hashitem_T *hi;
17609 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017612 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017613 for (hi = ht->ht_array; todo > 0; ++hi)
17614 {
17615 if (!HASHITEM_EMPTY(hi))
17616 {
17617 --todo;
17618
Bram Moolenaar33570922005-01-25 22:26:29 +000017619 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017620 * ht_array might change then. hash_clear() takes care of it
17621 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 v = HI2DI(hi);
17623 if (free_val)
17624 clear_tv(&v->di_tv);
17625 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17626 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017627 }
17628 }
17629 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017630 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631}
17632
Bram Moolenaara7043832005-01-21 11:56:39 +000017633/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017634 * Delete a variable from hashtab "ht" at item "hi".
17635 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017638delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 hashtab_T *ht;
17640 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641{
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017643
17644 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017645 clear_tv(&di->di_tv);
17646 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647}
17648
17649/*
17650 * List the value of one internal variable.
17651 */
17652 static void
17653list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017654 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 char_u *prefix;
17656{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017657 char_u *tofree;
17658 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017659 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017660
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017661 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017662 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017663 s == NULL ? (char_u *)"" : s);
17664 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665}
17666
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 static void
17668list_one_var_a(prefix, name, type, string)
17669 char_u *prefix;
17670 char_u *name;
17671 int type;
17672 char_u *string;
17673{
17674 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17675 if (name != NULL) /* "a:" vars don't have a name stored */
17676 msg_puts(name);
17677 msg_putchar(' ');
17678 msg_advance(22);
17679 if (type == VAR_NUMBER)
17680 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017681 else if (type == VAR_FUNC)
17682 msg_putchar('*');
17683 else if (type == VAR_LIST)
17684 {
17685 msg_putchar('[');
17686 if (*string == '[')
17687 ++string;
17688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017689 else if (type == VAR_DICT)
17690 {
17691 msg_putchar('{');
17692 if (*string == '{')
17693 ++string;
17694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 else
17696 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017697
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017699
17700 if (type == VAR_FUNC)
17701 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702}
17703
17704/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017705 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 * If the variable already exists, the value is updated.
17707 * Otherwise the variable is created.
17708 */
17709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017712 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714{
Bram Moolenaar33570922005-01-25 22:26:29 +000017715 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017717 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017718 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017720 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017721 {
17722 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17723 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17724 ? name[2] : name[0]))
17725 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017726 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017727 return;
17728 }
17729 if (function_exists(name))
17730 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017731 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017732 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017733 return;
17734 }
17735 }
17736
Bram Moolenaara7043832005-01-21 11:56:39 +000017737 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017738 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017739 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017740 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017741 return;
17742 }
17743
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017744 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017745 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017748 if (var_check_ro(v->di_flags, name)
17749 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 return;
17751 if (v->di_tv.v_type != tv->v_type
17752 && !((v->di_tv.v_type == VAR_STRING
17753 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017754 && (tv->v_type == VAR_STRING
17755 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017756 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017757 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017758 return;
17759 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017760
17761 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017762 * Handle setting internal v: variables separately: we don't change
17763 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 */
17765 if (ht == &vimvarht)
17766 {
17767 if (v->di_tv.v_type == VAR_STRING)
17768 {
17769 vim_free(v->di_tv.vval.v_string);
17770 if (copy || tv->v_type != VAR_STRING)
17771 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17772 else
17773 {
17774 /* Take over the string to avoid an extra alloc/free. */
17775 v->di_tv.vval.v_string = tv->vval.v_string;
17776 tv->vval.v_string = NULL;
17777 }
17778 }
17779 else if (v->di_tv.v_type != VAR_NUMBER)
17780 EMSG2(_(e_intern2), "set_var()");
17781 else
17782 v->di_tv.vval.v_number = get_tv_number(tv);
17783 return;
17784 }
17785
17786 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 }
17788 else /* add a new variable */
17789 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017790 /* Can't add "v:" variable. */
17791 if (ht == &vimvarht)
17792 {
17793 EMSG2(_(e_illvar), name);
17794 return;
17795 }
17796
Bram Moolenaar92124a32005-06-17 22:03:40 +000017797 /* Make sure the variable name is valid. */
17798 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017799 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17800 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017801 {
17802 EMSG2(_(e_illvar), varname);
17803 return;
17804 }
17805
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017806 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17807 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017808 if (v == NULL)
17809 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017811 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017813 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 return;
17815 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017816 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017819 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017820 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017821 else
17822 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017823 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017824 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017825 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827}
17828
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017829/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017830 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017831 * Also give an error message.
17832 */
17833 static int
17834var_check_ro(flags, name)
17835 int flags;
17836 char_u *name;
17837{
17838 if (flags & DI_FLAGS_RO)
17839 {
17840 EMSG2(_(e_readonlyvar), name);
17841 return TRUE;
17842 }
17843 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17844 {
17845 EMSG2(_(e_readonlysbx), name);
17846 return TRUE;
17847 }
17848 return FALSE;
17849}
17850
17851/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017852 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17853 * Also give an error message.
17854 */
17855 static int
17856var_check_fixed(flags, name)
17857 int flags;
17858 char_u *name;
17859{
17860 if (flags & DI_FLAGS_FIX)
17861 {
17862 EMSG2(_("E795: Cannot delete variable %s"), name);
17863 return TRUE;
17864 }
17865 return FALSE;
17866}
17867
17868/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017869 * Return TRUE if typeval "tv" is set to be locked (immutable).
17870 * Also give an error message, using "name".
17871 */
17872 static int
17873tv_check_lock(lock, name)
17874 int lock;
17875 char_u *name;
17876{
17877 if (lock & VAR_LOCKED)
17878 {
17879 EMSG2(_("E741: Value is locked: %s"),
17880 name == NULL ? (char_u *)_("Unknown") : name);
17881 return TRUE;
17882 }
17883 if (lock & VAR_FIXED)
17884 {
17885 EMSG2(_("E742: Cannot change value of %s"),
17886 name == NULL ? (char_u *)_("Unknown") : name);
17887 return TRUE;
17888 }
17889 return FALSE;
17890}
17891
17892/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017894 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017895 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017898copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017899 typval_T *from;
17900 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017902 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017903 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017904 switch (from->v_type)
17905 {
17906 case VAR_NUMBER:
17907 to->vval.v_number = from->vval.v_number;
17908 break;
17909 case VAR_STRING:
17910 case VAR_FUNC:
17911 if (from->vval.v_string == NULL)
17912 to->vval.v_string = NULL;
17913 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017915 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017916 if (from->v_type == VAR_FUNC)
17917 func_ref(to->vval.v_string);
17918 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017919 break;
17920 case VAR_LIST:
17921 if (from->vval.v_list == NULL)
17922 to->vval.v_list = NULL;
17923 else
17924 {
17925 to->vval.v_list = from->vval.v_list;
17926 ++to->vval.v_list->lv_refcount;
17927 }
17928 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017929 case VAR_DICT:
17930 if (from->vval.v_dict == NULL)
17931 to->vval.v_dict = NULL;
17932 else
17933 {
17934 to->vval.v_dict = from->vval.v_dict;
17935 ++to->vval.v_dict->dv_refcount;
17936 }
17937 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017938 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017939 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017940 break;
17941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942}
17943
17944/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017945 * Make a copy of an item.
17946 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017947 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17948 * reference to an already copied list/dict can be used.
17949 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017950 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017951 static int
17952item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017953 typval_T *from;
17954 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017955 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017956 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017957{
17958 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017959 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017960
Bram Moolenaar33570922005-01-25 22:26:29 +000017961 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017962 {
17963 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017964 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017965 }
17966 ++recurse;
17967
17968 switch (from->v_type)
17969 {
17970 case VAR_NUMBER:
17971 case VAR_STRING:
17972 case VAR_FUNC:
17973 copy_tv(from, to);
17974 break;
17975 case VAR_LIST:
17976 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017977 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017978 if (from->vval.v_list == NULL)
17979 to->vval.v_list = NULL;
17980 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17981 {
17982 /* use the copy made earlier */
17983 to->vval.v_list = from->vval.v_list->lv_copylist;
17984 ++to->vval.v_list->lv_refcount;
17985 }
17986 else
17987 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17988 if (to->vval.v_list == NULL)
17989 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017990 break;
17991 case VAR_DICT:
17992 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017993 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017994 if (from->vval.v_dict == NULL)
17995 to->vval.v_dict = NULL;
17996 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17997 {
17998 /* use the copy made earlier */
17999 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18000 ++to->vval.v_dict->dv_refcount;
18001 }
18002 else
18003 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18004 if (to->vval.v_dict == NULL)
18005 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018006 break;
18007 default:
18008 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018009 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018010 }
18011 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018012 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018013}
18014
18015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 * ":echo expr1 ..." print each argument separated with a space, add a
18017 * newline at the end.
18018 * ":echon expr1 ..." print each argument plain.
18019 */
18020 void
18021ex_echo(eap)
18022 exarg_T *eap;
18023{
18024 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018026 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 char_u *p;
18028 int needclr = TRUE;
18029 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018030 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031
18032 if (eap->skip)
18033 ++emsg_skip;
18034 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18035 {
18036 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018037 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 {
18039 /*
18040 * Report the invalid expression unless the expression evaluation
18041 * has been cancelled due to an aborting error, an interrupt, or an
18042 * exception.
18043 */
18044 if (!aborting())
18045 EMSG2(_(e_invexpr2), p);
18046 break;
18047 }
18048 if (!eap->skip)
18049 {
18050 if (atstart)
18051 {
18052 atstart = FALSE;
18053 /* Call msg_start() after eval1(), evaluating the expression
18054 * may cause a message to appear. */
18055 if (eap->cmdidx == CMD_echo)
18056 msg_start();
18057 }
18058 else if (eap->cmdidx == CMD_echo)
18059 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018060 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018061 if (p != NULL)
18062 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018064 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018066 if (*p != TAB && needclr)
18067 {
18068 /* remove any text still there from the command */
18069 msg_clr_eos();
18070 needclr = FALSE;
18071 }
18072 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 }
18074 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018075 {
18076#ifdef FEAT_MBYTE
18077 if (has_mbyte)
18078 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018079 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018080
18081 (void)msg_outtrans_len_attr(p, i, echo_attr);
18082 p += i - 1;
18083 }
18084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018086 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018089 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018091 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 arg = skipwhite(arg);
18093 }
18094 eap->nextcmd = check_nextcmd(arg);
18095
18096 if (eap->skip)
18097 --emsg_skip;
18098 else
18099 {
18100 /* remove text that may still be there from the command */
18101 if (needclr)
18102 msg_clr_eos();
18103 if (eap->cmdidx == CMD_echo)
18104 msg_end();
18105 }
18106}
18107
18108/*
18109 * ":echohl {name}".
18110 */
18111 void
18112ex_echohl(eap)
18113 exarg_T *eap;
18114{
18115 int id;
18116
18117 id = syn_name2id(eap->arg);
18118 if (id == 0)
18119 echo_attr = 0;
18120 else
18121 echo_attr = syn_id2attr(id);
18122}
18123
18124/*
18125 * ":execute expr1 ..." execute the result of an expression.
18126 * ":echomsg expr1 ..." Print a message
18127 * ":echoerr expr1 ..." Print an error
18128 * Each gets spaces around each argument and a newline at the end for
18129 * echo commands
18130 */
18131 void
18132ex_execute(eap)
18133 exarg_T *eap;
18134{
18135 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018136 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 int ret = OK;
18138 char_u *p;
18139 garray_T ga;
18140 int len;
18141 int save_did_emsg;
18142
18143 ga_init2(&ga, 1, 80);
18144
18145 if (eap->skip)
18146 ++emsg_skip;
18147 while (*arg != NUL && *arg != '|' && *arg != '\n')
18148 {
18149 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018150 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 {
18152 /*
18153 * Report the invalid expression unless the expression evaluation
18154 * has been cancelled due to an aborting error, an interrupt, or an
18155 * exception.
18156 */
18157 if (!aborting())
18158 EMSG2(_(e_invexpr2), p);
18159 ret = FAIL;
18160 break;
18161 }
18162
18163 if (!eap->skip)
18164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018165 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 len = (int)STRLEN(p);
18167 if (ga_grow(&ga, len + 2) == FAIL)
18168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018169 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 ret = FAIL;
18171 break;
18172 }
18173 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 ga.ga_len += len;
18177 }
18178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018179 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 arg = skipwhite(arg);
18181 }
18182
18183 if (ret != FAIL && ga.ga_data != NULL)
18184 {
18185 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018188 out_flush();
18189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 else if (eap->cmdidx == CMD_echoerr)
18191 {
18192 /* We don't want to abort following commands, restore did_emsg. */
18193 save_did_emsg = did_emsg;
18194 EMSG((char_u *)ga.ga_data);
18195 if (!force_abort)
18196 did_emsg = save_did_emsg;
18197 }
18198 else if (eap->cmdidx == CMD_execute)
18199 do_cmdline((char_u *)ga.ga_data,
18200 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18201 }
18202
18203 ga_clear(&ga);
18204
18205 if (eap->skip)
18206 --emsg_skip;
18207
18208 eap->nextcmd = check_nextcmd(arg);
18209}
18210
18211/*
18212 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18213 * "arg" points to the "&" or '+' when called, to "option" when returning.
18214 * Returns NULL when no option name found. Otherwise pointer to the char
18215 * after the option name.
18216 */
18217 static char_u *
18218find_option_end(arg, opt_flags)
18219 char_u **arg;
18220 int *opt_flags;
18221{
18222 char_u *p = *arg;
18223
18224 ++p;
18225 if (*p == 'g' && p[1] == ':')
18226 {
18227 *opt_flags = OPT_GLOBAL;
18228 p += 2;
18229 }
18230 else if (*p == 'l' && p[1] == ':')
18231 {
18232 *opt_flags = OPT_LOCAL;
18233 p += 2;
18234 }
18235 else
18236 *opt_flags = 0;
18237
18238 if (!ASCII_ISALPHA(*p))
18239 return NULL;
18240 *arg = p;
18241
18242 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18243 p += 4; /* termcap option */
18244 else
18245 while (ASCII_ISALPHA(*p))
18246 ++p;
18247 return p;
18248}
18249
18250/*
18251 * ":function"
18252 */
18253 void
18254ex_function(eap)
18255 exarg_T *eap;
18256{
18257 char_u *theline;
18258 int j;
18259 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 char_u *name = NULL;
18262 char_u *p;
18263 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018264 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 garray_T newargs;
18266 garray_T newlines;
18267 int varargs = FALSE;
18268 int mustend = FALSE;
18269 int flags = 0;
18270 ufunc_T *fp;
18271 int indent;
18272 int nesting;
18273 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018274 dictitem_T *v;
18275 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018276 static int func_nr = 0; /* number for nameless function */
18277 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018278 hashtab_T *ht;
18279 int todo;
18280 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018281 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282
18283 /*
18284 * ":function" without argument: list functions.
18285 */
18286 if (ends_excmd(*eap->arg))
18287 {
18288 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018289 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018290 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018291 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018292 {
18293 if (!HASHITEM_EMPTY(hi))
18294 {
18295 --todo;
18296 fp = HI2UF(hi);
18297 if (!isdigit(*fp->uf_name))
18298 list_func_head(fp, FALSE);
18299 }
18300 }
18301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 eap->nextcmd = check_nextcmd(eap->arg);
18303 return;
18304 }
18305
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018306 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018307 * ":function /pat": list functions matching pattern.
18308 */
18309 if (*eap->arg == '/')
18310 {
18311 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18312 if (!eap->skip)
18313 {
18314 regmatch_T regmatch;
18315
18316 c = *p;
18317 *p = NUL;
18318 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18319 *p = c;
18320 if (regmatch.regprog != NULL)
18321 {
18322 regmatch.rm_ic = p_ic;
18323
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018324 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018325 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18326 {
18327 if (!HASHITEM_EMPTY(hi))
18328 {
18329 --todo;
18330 fp = HI2UF(hi);
18331 if (!isdigit(*fp->uf_name)
18332 && vim_regexec(&regmatch, fp->uf_name, 0))
18333 list_func_head(fp, FALSE);
18334 }
18335 }
18336 }
18337 }
18338 if (*p == '/')
18339 ++p;
18340 eap->nextcmd = check_nextcmd(p);
18341 return;
18342 }
18343
18344 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018345 * Get the function name. There are these situations:
18346 * func normal function name
18347 * "name" == func, "fudi.fd_dict" == NULL
18348 * dict.func new dictionary entry
18349 * "name" == NULL, "fudi.fd_dict" set,
18350 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18351 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018352 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018353 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18354 * dict.func existing dict entry that's not a Funcref
18355 * "name" == NULL, "fudi.fd_dict" set,
18356 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018359 name = trans_function_name(&p, eap->skip, 0, &fudi);
18360 paren = (vim_strchr(p, '(') != NULL);
18361 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 {
18363 /*
18364 * Return on an invalid expression in braces, unless the expression
18365 * evaluation has been cancelled due to an aborting error, an
18366 * interrupt, or an exception.
18367 */
18368 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018370 if (!eap->skip && fudi.fd_newkey != NULL)
18371 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018372 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 else
18376 eap->skip = TRUE;
18377 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018378
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 /* An error in a function call during evaluation of an expression in magic
18380 * braces should not cause the function not to be defined. */
18381 saved_did_emsg = did_emsg;
18382 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383
18384 /*
18385 * ":function func" with only function name: list function.
18386 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018387 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 {
18389 if (!ends_excmd(*skipwhite(p)))
18390 {
18391 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018392 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 }
18394 eap->nextcmd = check_nextcmd(p);
18395 if (eap->nextcmd != NULL)
18396 *p = NUL;
18397 if (!eap->skip && !got_int)
18398 {
18399 fp = find_func(name);
18400 if (fp != NULL)
18401 {
18402 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018403 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018405 if (FUNCLINE(fp, j) == NULL)
18406 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 msg_putchar('\n');
18408 msg_outnum((long)(j + 1));
18409 if (j < 9)
18410 msg_putchar(' ');
18411 if (j < 99)
18412 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018413 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 out_flush(); /* show a line at a time */
18415 ui_breakcheck();
18416 }
18417 if (!got_int)
18418 {
18419 msg_putchar('\n');
18420 msg_puts((char_u *)" endfunction");
18421 }
18422 }
18423 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018424 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018426 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 }
18428
18429 /*
18430 * ":function name(arg1, arg2)" Define function.
18431 */
18432 p = skipwhite(p);
18433 if (*p != '(')
18434 {
18435 if (!eap->skip)
18436 {
18437 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018438 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 }
18440 /* attempt to continue by skipping some text */
18441 if (vim_strchr(p, '(') != NULL)
18442 p = vim_strchr(p, '(');
18443 }
18444 p = skipwhite(p + 1);
18445
18446 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18447 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18448
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018449 if (!eap->skip)
18450 {
18451 /* Check the name of the function. */
18452 if (name != NULL)
18453 arg = name;
18454 else
18455 arg = fudi.fd_newkey;
18456 if (arg != NULL)
18457 {
18458 if (*arg == K_SPECIAL)
18459 j = 3;
18460 else
18461 j = 0;
18462 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18463 : eval_isnamec(arg[j])))
18464 ++j;
18465 if (arg[j] != NUL)
18466 emsg_funcname(_(e_invarg2), arg);
18467 }
18468 }
18469
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 /*
18471 * Isolate the arguments: "arg1, arg2, ...)"
18472 */
18473 while (*p != ')')
18474 {
18475 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18476 {
18477 varargs = TRUE;
18478 p += 3;
18479 mustend = TRUE;
18480 }
18481 else
18482 {
18483 arg = p;
18484 while (ASCII_ISALNUM(*p) || *p == '_')
18485 ++p;
18486 if (arg == p || isdigit(*arg)
18487 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18488 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18489 {
18490 if (!eap->skip)
18491 EMSG2(_("E125: Illegal argument: %s"), arg);
18492 break;
18493 }
18494 if (ga_grow(&newargs, 1) == FAIL)
18495 goto erret;
18496 c = *p;
18497 *p = NUL;
18498 arg = vim_strsave(arg);
18499 if (arg == NULL)
18500 goto erret;
18501 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18502 *p = c;
18503 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 if (*p == ',')
18505 ++p;
18506 else
18507 mustend = TRUE;
18508 }
18509 p = skipwhite(p);
18510 if (mustend && *p != ')')
18511 {
18512 if (!eap->skip)
18513 EMSG2(_(e_invarg2), eap->arg);
18514 break;
18515 }
18516 }
18517 ++p; /* skip the ')' */
18518
Bram Moolenaare9a41262005-01-15 22:18:47 +000018519 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 for (;;)
18521 {
18522 p = skipwhite(p);
18523 if (STRNCMP(p, "range", 5) == 0)
18524 {
18525 flags |= FC_RANGE;
18526 p += 5;
18527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018528 else if (STRNCMP(p, "dict", 4) == 0)
18529 {
18530 flags |= FC_DICT;
18531 p += 4;
18532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 else if (STRNCMP(p, "abort", 5) == 0)
18534 {
18535 flags |= FC_ABORT;
18536 p += 5;
18537 }
18538 else
18539 break;
18540 }
18541
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018542 /* When there is a line break use what follows for the function body.
18543 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18544 if (*p == '\n')
18545 line_arg = p + 1;
18546 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 EMSG(_(e_trailing));
18548
18549 /*
18550 * Read the body of the function, until ":endfunction" is found.
18551 */
18552 if (KeyTyped)
18553 {
18554 /* Check if the function already exists, don't let the user type the
18555 * whole function before telling him it doesn't work! For a script we
18556 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018557 if (!eap->skip && !eap->forceit)
18558 {
18559 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18560 EMSG(_(e_funcdict));
18561 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018562 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018565 if (!eap->skip && did_emsg)
18566 goto erret;
18567
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 msg_putchar('\n'); /* don't overwrite the function name */
18569 cmdline_row = msg_row;
18570 }
18571
18572 indent = 2;
18573 nesting = 0;
18574 for (;;)
18575 {
18576 msg_scroll = TRUE;
18577 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018578 sourcing_lnum_off = sourcing_lnum;
18579
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018580 if (line_arg != NULL)
18581 {
18582 /* Use eap->arg, split up in parts by line breaks. */
18583 theline = line_arg;
18584 p = vim_strchr(theline, '\n');
18585 if (p == NULL)
18586 line_arg += STRLEN(line_arg);
18587 else
18588 {
18589 *p = NUL;
18590 line_arg = p + 1;
18591 }
18592 }
18593 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 theline = getcmdline(':', 0L, indent);
18595 else
18596 theline = eap->getline(':', eap->cookie, indent);
18597 if (KeyTyped)
18598 lines_left = Rows - 1;
18599 if (theline == NULL)
18600 {
18601 EMSG(_("E126: Missing :endfunction"));
18602 goto erret;
18603 }
18604
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018605 /* Detect line continuation: sourcing_lnum increased more than one. */
18606 if (sourcing_lnum > sourcing_lnum_off + 1)
18607 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18608 else
18609 sourcing_lnum_off = 0;
18610
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 if (skip_until != NULL)
18612 {
18613 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18614 * don't check for ":endfunc". */
18615 if (STRCMP(theline, skip_until) == 0)
18616 {
18617 vim_free(skip_until);
18618 skip_until = NULL;
18619 }
18620 }
18621 else
18622 {
18623 /* skip ':' and blanks*/
18624 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18625 ;
18626
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018627 /* Check for "endfunction". */
18628 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018630 if (line_arg == NULL)
18631 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 break;
18633 }
18634
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018635 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 * at "end". */
18637 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18638 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018639 else if (STRNCMP(p, "if", 2) == 0
18640 || STRNCMP(p, "wh", 2) == 0
18641 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 || STRNCMP(p, "try", 3) == 0)
18643 indent += 2;
18644
18645 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018646 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018648 if (*p == '!')
18649 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 p += eval_fname_script(p);
18651 if (ASCII_ISALPHA(*p))
18652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018653 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 if (*skipwhite(p) == '(')
18655 {
18656 ++nesting;
18657 indent += 2;
18658 }
18659 }
18660 }
18661
18662 /* Check for ":append" or ":insert". */
18663 p = skip_range(p, NULL);
18664 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18665 || (p[0] == 'i'
18666 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18667 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18668 skip_until = vim_strsave((char_u *)".");
18669
18670 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18671 arg = skipwhite(skiptowhite(p));
18672 if (arg[0] == '<' && arg[1] =='<'
18673 && ((p[0] == 'p' && p[1] == 'y'
18674 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18675 || (p[0] == 'p' && p[1] == 'e'
18676 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18677 || (p[0] == 't' && p[1] == 'c'
18678 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18679 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18680 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018681 || (p[0] == 'm' && p[1] == 'z'
18682 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 ))
18684 {
18685 /* ":python <<" continues until a dot, like ":append" */
18686 p = skipwhite(arg + 2);
18687 if (*p == NUL)
18688 skip_until = vim_strsave((char_u *)".");
18689 else
18690 skip_until = vim_strsave(p);
18691 }
18692 }
18693
18694 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018695 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018696 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018697 if (line_arg == NULL)
18698 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018700 }
18701
18702 /* Copy the line to newly allocated memory. get_one_sourceline()
18703 * allocates 250 bytes per line, this saves 80% on average. The cost
18704 * is an extra alloc/free. */
18705 p = vim_strsave(theline);
18706 if (p != NULL)
18707 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018708 if (line_arg == NULL)
18709 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018710 theline = p;
18711 }
18712
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018713 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18714
18715 /* Add NULL lines for continuation lines, so that the line count is
18716 * equal to the index in the growarray. */
18717 while (sourcing_lnum_off-- > 0)
18718 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018719
18720 /* Check for end of eap->arg. */
18721 if (line_arg != NULL && *line_arg == NUL)
18722 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 }
18724
18725 /* Don't define the function when skipping commands or when an error was
18726 * detected. */
18727 if (eap->skip || did_emsg)
18728 goto erret;
18729
18730 /*
18731 * If there are no errors, add the function
18732 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018733 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018734 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018735 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018736 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018737 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018738 emsg_funcname("E707: Function name conflicts with variable: %s",
18739 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018740 goto erret;
18741 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018742
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018743 fp = find_func(name);
18744 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018746 if (!eap->forceit)
18747 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018748 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018749 goto erret;
18750 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018751 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018752 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018753 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018754 name);
18755 goto erret;
18756 }
18757 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018758 ga_clear_strings(&(fp->uf_args));
18759 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018760 vim_free(name);
18761 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 }
18764 else
18765 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018766 char numbuf[20];
18767
18768 fp = NULL;
18769 if (fudi.fd_newkey == NULL && !eap->forceit)
18770 {
18771 EMSG(_(e_funcdict));
18772 goto erret;
18773 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018774 if (fudi.fd_di == NULL)
18775 {
18776 /* Can't add a function to a locked dictionary */
18777 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18778 goto erret;
18779 }
18780 /* Can't change an existing function if it is locked */
18781 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18782 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018783
18784 /* Give the function a sequential number. Can only be used with a
18785 * Funcref! */
18786 vim_free(name);
18787 sprintf(numbuf, "%d", ++func_nr);
18788 name = vim_strsave((char_u *)numbuf);
18789 if (name == NULL)
18790 goto erret;
18791 }
18792
18793 if (fp == NULL)
18794 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018795 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018796 {
18797 int slen, plen;
18798 char_u *scriptname;
18799
18800 /* Check that the autoload name matches the script name. */
18801 j = FAIL;
18802 if (sourcing_name != NULL)
18803 {
18804 scriptname = autoload_name(name);
18805 if (scriptname != NULL)
18806 {
18807 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018808 plen = (int)STRLEN(p);
18809 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018810 if (slen > plen && fnamecmp(p,
18811 sourcing_name + slen - plen) == 0)
18812 j = OK;
18813 vim_free(scriptname);
18814 }
18815 }
18816 if (j == FAIL)
18817 {
18818 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18819 goto erret;
18820 }
18821 }
18822
18823 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 if (fp == NULL)
18825 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018826
18827 if (fudi.fd_dict != NULL)
18828 {
18829 if (fudi.fd_di == NULL)
18830 {
18831 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018832 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018833 if (fudi.fd_di == NULL)
18834 {
18835 vim_free(fp);
18836 goto erret;
18837 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018838 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18839 {
18840 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018841 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018842 goto erret;
18843 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018844 }
18845 else
18846 /* overwrite existing dict entry */
18847 clear_tv(&fudi.fd_di->di_tv);
18848 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018849 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018850 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018851 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018852
18853 /* behave like "dict" was used */
18854 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018855 }
18856
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018858 STRCPY(fp->uf_name, name);
18859 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018861 fp->uf_args = newargs;
18862 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018863#ifdef FEAT_PROFILE
18864 fp->uf_tml_count = NULL;
18865 fp->uf_tml_total = NULL;
18866 fp->uf_tml_self = NULL;
18867 fp->uf_profiling = FALSE;
18868 if (prof_def_func())
18869 func_do_profile(fp);
18870#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018871 fp->uf_varargs = varargs;
18872 fp->uf_flags = flags;
18873 fp->uf_calls = 0;
18874 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018875 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876
18877erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 ga_clear_strings(&newargs);
18879 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018880ret_free:
18881 vim_free(skip_until);
18882 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885}
18886
18887/*
18888 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018889 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018891 * flags:
18892 * TFN_INT: internal function name OK
18893 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894 * Advances "pp" to just after the function name (if no error).
18895 */
18896 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018897trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 char_u **pp;
18899 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018900 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018901 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018903 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 char_u *start;
18905 char_u *end;
18906 int lead;
18907 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018909 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018910
18911 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018912 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018914
18915 /* Check for hard coded <SNR>: already translated function ID (from a user
18916 * command). */
18917 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18918 && (*pp)[2] == (int)KE_SNR)
18919 {
18920 *pp += 3;
18921 len = get_id_len(pp) + 3;
18922 return vim_strnsave(start, len);
18923 }
18924
18925 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18926 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018928 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018930
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018931 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18932 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018933 if (end == start)
18934 {
18935 if (!skip)
18936 EMSG(_("E129: Function name required"));
18937 goto theend;
18938 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018939 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018940 {
18941 /*
18942 * Report an invalid expression in braces, unless the expression
18943 * evaluation has been cancelled due to an aborting error, an
18944 * interrupt, or an exception.
18945 */
18946 if (!aborting())
18947 {
18948 if (end != NULL)
18949 EMSG2(_(e_invarg2), start);
18950 }
18951 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018952 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018953 goto theend;
18954 }
18955
18956 if (lv.ll_tv != NULL)
18957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018958 if (fdp != NULL)
18959 {
18960 fdp->fd_dict = lv.ll_dict;
18961 fdp->fd_newkey = lv.ll_newkey;
18962 lv.ll_newkey = NULL;
18963 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018965 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18966 {
18967 name = vim_strsave(lv.ll_tv->vval.v_string);
18968 *pp = end;
18969 }
18970 else
18971 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018972 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18973 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018974 EMSG(_(e_funcref));
18975 else
18976 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018977 name = NULL;
18978 }
18979 goto theend;
18980 }
18981
18982 if (lv.ll_name == NULL)
18983 {
18984 /* Error found, but continue after the function name. */
18985 *pp = end;
18986 goto theend;
18987 }
18988
18989 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018990 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018991 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018992 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18993 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18994 {
18995 /* When there was "s:" already or the name expanded to get a
18996 * leading "s:" then remove it. */
18997 lv.ll_name += 2;
18998 len -= 2;
18999 lead = 2;
19000 }
19001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019002 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019003 {
19004 if (lead == 2) /* skip over "s:" */
19005 lv.ll_name += 2;
19006 len = (int)(end - lv.ll_name);
19007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019008
19009 /*
19010 * Copy the function name to allocated memory.
19011 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19012 * Accept <SNR>123_name() outside a script.
19013 */
19014 if (skip)
19015 lead = 0; /* do nothing */
19016 else if (lead > 0)
19017 {
19018 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019019 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19020 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019021 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019022 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019023 if (current_SID <= 0)
19024 {
19025 EMSG(_(e_usingsid));
19026 goto theend;
19027 }
19028 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19029 lead += (int)STRLEN(sid_buf);
19030 }
19031 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019032 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019033 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019034 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019035 goto theend;
19036 }
19037 name = alloc((unsigned)(len + lead + 1));
19038 if (name != NULL)
19039 {
19040 if (lead > 0)
19041 {
19042 name[0] = K_SPECIAL;
19043 name[1] = KS_EXTRA;
19044 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019045 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019046 STRCPY(name + 3, sid_buf);
19047 }
19048 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19049 name[len + lead] = NUL;
19050 }
19051 *pp = end;
19052
19053theend:
19054 clear_lval(&lv);
19055 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056}
19057
19058/*
19059 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19060 * Return 2 if "p" starts with "s:".
19061 * Return 0 otherwise.
19062 */
19063 static int
19064eval_fname_script(p)
19065 char_u *p;
19066{
19067 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19068 || STRNICMP(p + 1, "SNR>", 4) == 0))
19069 return 5;
19070 if (p[0] == 's' && p[1] == ':')
19071 return 2;
19072 return 0;
19073}
19074
19075/*
19076 * Return TRUE if "p" starts with "<SID>" or "s:".
19077 * Only works if eval_fname_script() returned non-zero for "p"!
19078 */
19079 static int
19080eval_fname_sid(p)
19081 char_u *p;
19082{
19083 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19084}
19085
19086/*
19087 * List the head of the function: "name(arg1, arg2)".
19088 */
19089 static void
19090list_func_head(fp, indent)
19091 ufunc_T *fp;
19092 int indent;
19093{
19094 int j;
19095
19096 msg_start();
19097 if (indent)
19098 MSG_PUTS(" ");
19099 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019100 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 {
19102 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019103 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 }
19105 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019106 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019108 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 {
19110 if (j)
19111 MSG_PUTS(", ");
19112 msg_puts(FUNCARG(fp, j));
19113 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019114 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 {
19116 if (j)
19117 MSG_PUTS(", ");
19118 MSG_PUTS("...");
19119 }
19120 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019121 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019122 if (p_verbose > 0)
19123 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124}
19125
19126/*
19127 * Find a function by name, return pointer to it in ufuncs.
19128 * Return NULL for unknown function.
19129 */
19130 static ufunc_T *
19131find_func(name)
19132 char_u *name;
19133{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019134 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019136 hi = hash_find(&func_hashtab, name);
19137 if (!HASHITEM_EMPTY(hi))
19138 return HI2UF(hi);
19139 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140}
19141
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019142#if defined(EXITFREE) || defined(PROTO)
19143 void
19144free_all_functions()
19145{
19146 hashitem_T *hi;
19147
19148 /* Need to start all over every time, because func_free() may change the
19149 * hash table. */
19150 while (func_hashtab.ht_used > 0)
19151 for (hi = func_hashtab.ht_array; ; ++hi)
19152 if (!HASHITEM_EMPTY(hi))
19153 {
19154 func_free(HI2UF(hi));
19155 break;
19156 }
19157}
19158#endif
19159
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019160/*
19161 * Return TRUE if a function "name" exists.
19162 */
19163 static int
19164function_exists(name)
19165 char_u *name;
19166{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019167 char_u *nm = name;
19168 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019169 int n = FALSE;
19170
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019171 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019172 nm = skipwhite(nm);
19173
19174 /* Only accept "funcname", "funcname ", "funcname (..." and
19175 * "funcname(...", not "funcname!...". */
19176 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019177 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019178 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019179 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019180 else
19181 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019182 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019183 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019184 return n;
19185}
19186
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019187/*
19188 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019189 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019190 */
19191 static int
19192builtin_function(name)
19193 char_u *name;
19194{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019195 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19196 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019197}
19198
Bram Moolenaar05159a02005-02-26 23:04:13 +000019199#if defined(FEAT_PROFILE) || defined(PROTO)
19200/*
19201 * Start profiling function "fp".
19202 */
19203 static void
19204func_do_profile(fp)
19205 ufunc_T *fp;
19206{
19207 fp->uf_tm_count = 0;
19208 profile_zero(&fp->uf_tm_self);
19209 profile_zero(&fp->uf_tm_total);
19210 if (fp->uf_tml_count == NULL)
19211 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19212 (sizeof(int) * fp->uf_lines.ga_len));
19213 if (fp->uf_tml_total == NULL)
19214 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19215 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19216 if (fp->uf_tml_self == NULL)
19217 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19218 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19219 fp->uf_tml_idx = -1;
19220 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19221 || fp->uf_tml_self == NULL)
19222 return; /* out of memory */
19223
19224 fp->uf_profiling = TRUE;
19225}
19226
19227/*
19228 * Dump the profiling results for all functions in file "fd".
19229 */
19230 void
19231func_dump_profile(fd)
19232 FILE *fd;
19233{
19234 hashitem_T *hi;
19235 int todo;
19236 ufunc_T *fp;
19237 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019238 ufunc_T **sorttab;
19239 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019240
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019241 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019242 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19243
Bram Moolenaar05159a02005-02-26 23:04:13 +000019244 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19245 {
19246 if (!HASHITEM_EMPTY(hi))
19247 {
19248 --todo;
19249 fp = HI2UF(hi);
19250 if (fp->uf_profiling)
19251 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019252 if (sorttab != NULL)
19253 sorttab[st_len++] = fp;
19254
Bram Moolenaar05159a02005-02-26 23:04:13 +000019255 if (fp->uf_name[0] == K_SPECIAL)
19256 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19257 else
19258 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19259 if (fp->uf_tm_count == 1)
19260 fprintf(fd, "Called 1 time\n");
19261 else
19262 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19263 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19264 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19265 fprintf(fd, "\n");
19266 fprintf(fd, "count total (s) self (s)\n");
19267
19268 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19269 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019270 if (FUNCLINE(fp, i) == NULL)
19271 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019272 prof_func_line(fd, fp->uf_tml_count[i],
19273 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019274 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19275 }
19276 fprintf(fd, "\n");
19277 }
19278 }
19279 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019280
19281 if (sorttab != NULL && st_len > 0)
19282 {
19283 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19284 prof_total_cmp);
19285 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19286 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19287 prof_self_cmp);
19288 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19289 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019290}
Bram Moolenaar73830342005-02-28 22:48:19 +000019291
19292 static void
19293prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19294 FILE *fd;
19295 ufunc_T **sorttab;
19296 int st_len;
19297 char *title;
19298 int prefer_self; /* when equal print only self time */
19299{
19300 int i;
19301 ufunc_T *fp;
19302
19303 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19304 fprintf(fd, "count total (s) self (s) function\n");
19305 for (i = 0; i < 20 && i < st_len; ++i)
19306 {
19307 fp = sorttab[i];
19308 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19309 prefer_self);
19310 if (fp->uf_name[0] == K_SPECIAL)
19311 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19312 else
19313 fprintf(fd, " %s()\n", fp->uf_name);
19314 }
19315 fprintf(fd, "\n");
19316}
19317
19318/*
19319 * Print the count and times for one function or function line.
19320 */
19321 static void
19322prof_func_line(fd, count, total, self, prefer_self)
19323 FILE *fd;
19324 int count;
19325 proftime_T *total;
19326 proftime_T *self;
19327 int prefer_self; /* when equal print only self time */
19328{
19329 if (count > 0)
19330 {
19331 fprintf(fd, "%5d ", count);
19332 if (prefer_self && profile_equal(total, self))
19333 fprintf(fd, " ");
19334 else
19335 fprintf(fd, "%s ", profile_msg(total));
19336 if (!prefer_self && profile_equal(total, self))
19337 fprintf(fd, " ");
19338 else
19339 fprintf(fd, "%s ", profile_msg(self));
19340 }
19341 else
19342 fprintf(fd, " ");
19343}
19344
19345/*
19346 * Compare function for total time sorting.
19347 */
19348 static int
19349#ifdef __BORLANDC__
19350_RTLENTRYF
19351#endif
19352prof_total_cmp(s1, s2)
19353 const void *s1;
19354 const void *s2;
19355{
19356 ufunc_T *p1, *p2;
19357
19358 p1 = *(ufunc_T **)s1;
19359 p2 = *(ufunc_T **)s2;
19360 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19361}
19362
19363/*
19364 * Compare function for self time sorting.
19365 */
19366 static int
19367#ifdef __BORLANDC__
19368_RTLENTRYF
19369#endif
19370prof_self_cmp(s1, s2)
19371 const void *s1;
19372 const void *s2;
19373{
19374 ufunc_T *p1, *p2;
19375
19376 p1 = *(ufunc_T **)s1;
19377 p2 = *(ufunc_T **)s2;
19378 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19379}
19380
Bram Moolenaar05159a02005-02-26 23:04:13 +000019381#endif
19382
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019383/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019384 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019385 * Return TRUE if a package was loaded.
19386 */
19387 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019388script_autoload(name, reload)
19389 char_u *name;
19390 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019391{
19392 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019393 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019394 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019395 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019396
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019397 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019398 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019399 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019400 return FALSE;
19401
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019402 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019403
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019404 /* Find the name in the list of previously loaded package names. Skip
19405 * "autoload/", it's always the same. */
19406 for (i = 0; i < ga_loaded.ga_len; ++i)
19407 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19408 break;
19409 if (!reload && i < ga_loaded.ga_len)
19410 ret = FALSE; /* was loaded already */
19411 else
19412 {
19413 /* Remember the name if it wasn't loaded already. */
19414 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19415 {
19416 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19417 tofree = NULL;
19418 }
19419
19420 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019421 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019422 ret = TRUE;
19423 }
19424
19425 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019426 return ret;
19427}
19428
19429/*
19430 * Return the autoload script name for a function or variable name.
19431 * Returns NULL when out of memory.
19432 */
19433 static char_u *
19434autoload_name(name)
19435 char_u *name;
19436{
19437 char_u *p;
19438 char_u *scriptname;
19439
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019440 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019441 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19442 if (scriptname == NULL)
19443 return FALSE;
19444 STRCPY(scriptname, "autoload/");
19445 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019446 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019447 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019448 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019449 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019450 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019451}
19452
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19454
19455/*
19456 * Function given to ExpandGeneric() to obtain the list of user defined
19457 * function names.
19458 */
19459 char_u *
19460get_user_func_name(xp, idx)
19461 expand_T *xp;
19462 int idx;
19463{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019464 static long_u done;
19465 static hashitem_T *hi;
19466 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467
19468 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019470 done = 0;
19471 hi = func_hashtab.ht_array;
19472 }
19473 if (done < func_hashtab.ht_used)
19474 {
19475 if (done++ > 0)
19476 ++hi;
19477 while (HASHITEM_EMPTY(hi))
19478 ++hi;
19479 fp = HI2UF(hi);
19480
19481 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19482 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483
19484 cat_func_name(IObuff, fp);
19485 if (xp->xp_context != EXPAND_USER_FUNC)
19486 {
19487 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019488 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 STRCAT(IObuff, ")");
19490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 return IObuff;
19492 }
19493 return NULL;
19494}
19495
19496#endif /* FEAT_CMDL_COMPL */
19497
19498/*
19499 * Copy the function name of "fp" to buffer "buf".
19500 * "buf" must be able to hold the function name plus three bytes.
19501 * Takes care of script-local function names.
19502 */
19503 static void
19504cat_func_name(buf, fp)
19505 char_u *buf;
19506 ufunc_T *fp;
19507{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019508 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 {
19510 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019511 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 }
19513 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019514 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515}
19516
19517/*
19518 * ":delfunction {name}"
19519 */
19520 void
19521ex_delfunction(eap)
19522 exarg_T *eap;
19523{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019524 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 char_u *p;
19526 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019527 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528
19529 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019530 name = trans_function_name(&p, eap->skip, 0, &fudi);
19531 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019533 {
19534 if (fudi.fd_dict != NULL && !eap->skip)
19535 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 if (!ends_excmd(*skipwhite(p)))
19539 {
19540 vim_free(name);
19541 EMSG(_(e_trailing));
19542 return;
19543 }
19544 eap->nextcmd = check_nextcmd(p);
19545 if (eap->nextcmd != NULL)
19546 *p = NUL;
19547
19548 if (!eap->skip)
19549 fp = find_func(name);
19550 vim_free(name);
19551
19552 if (!eap->skip)
19553 {
19554 if (fp == NULL)
19555 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019556 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 return;
19558 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019559 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 {
19561 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19562 return;
19563 }
19564
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019565 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019567 /* Delete the dict item that refers to the function, it will
19568 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019569 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019571 else
19572 func_free(fp);
19573 }
19574}
19575
19576/*
19577 * Free a function and remove it from the list of functions.
19578 */
19579 static void
19580func_free(fp)
19581 ufunc_T *fp;
19582{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019583 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019584
19585 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019586 ga_clear_strings(&(fp->uf_args));
19587 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019588#ifdef FEAT_PROFILE
19589 vim_free(fp->uf_tml_count);
19590 vim_free(fp->uf_tml_total);
19591 vim_free(fp->uf_tml_self);
19592#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019593
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019594 /* remove the function from the function hashtable */
19595 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19596 if (HASHITEM_EMPTY(hi))
19597 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019598 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019599 hash_remove(&func_hashtab, hi);
19600
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019601 vim_free(fp);
19602}
19603
19604/*
19605 * Unreference a Function: decrement the reference count and free it when it
19606 * becomes zero. Only for numbered functions.
19607 */
19608 static void
19609func_unref(name)
19610 char_u *name;
19611{
19612 ufunc_T *fp;
19613
19614 if (name != NULL && isdigit(*name))
19615 {
19616 fp = find_func(name);
19617 if (fp == NULL)
19618 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019619 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019620 {
19621 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019622 * when "uf_calls" becomes zero. */
19623 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019624 func_free(fp);
19625 }
19626 }
19627}
19628
19629/*
19630 * Count a reference to a Function.
19631 */
19632 static void
19633func_ref(name)
19634 char_u *name;
19635{
19636 ufunc_T *fp;
19637
19638 if (name != NULL && isdigit(*name))
19639 {
19640 fp = find_func(name);
19641 if (fp == NULL)
19642 EMSG2(_(e_intern2), "func_ref()");
19643 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019644 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 }
19646}
19647
19648/*
19649 * Call a user function.
19650 */
19651 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019652call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 ufunc_T *fp; /* pointer to function */
19654 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 typval_T *argvars; /* arguments */
19656 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 linenr_T firstline; /* first line of range */
19658 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019659 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660{
Bram Moolenaar33570922005-01-25 22:26:29 +000019661 char_u *save_sourcing_name;
19662 linenr_T save_sourcing_lnum;
19663 scid_T save_current_SID;
19664 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019665 int save_did_emsg;
19666 static int depth = 0;
19667 dictitem_T *v;
19668 int fixvar_idx = 0; /* index in fixvar[] */
19669 int i;
19670 int ai;
19671 char_u numbuf[NUMBUFLEN];
19672 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019673#ifdef FEAT_PROFILE
19674 proftime_T wait_start;
19675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676
19677 /* If depth of calling is getting too high, don't execute the function */
19678 if (depth >= p_mfd)
19679 {
19680 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019681 rettv->v_type = VAR_NUMBER;
19682 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 return;
19684 }
19685 ++depth;
19686
19687 line_breakcheck(); /* check for CTRL-C hit */
19688
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019689 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019690 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692 fc.rettv = rettv;
19693 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694 fc.linenr = 0;
19695 fc.returned = FALSE;
19696 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019698 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 fc.dbg_tick = debug_tick;
19700
Bram Moolenaar33570922005-01-25 22:26:29 +000019701 /*
19702 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19703 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19704 * each argument variable and saves a lot of time.
19705 */
19706 /*
19707 * Init l: variables.
19708 */
19709 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019710 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019711 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019712 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19713 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019714 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019715 name = v->di_key;
19716 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019717 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19718 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19719 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019720 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 v->di_tv.vval.v_dict = selfdict;
19722 ++selfdict->dv_refcount;
19723 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019724
Bram Moolenaar33570922005-01-25 22:26:29 +000019725 /*
19726 * Init a: variables.
19727 * Set a:0 to "argcount".
19728 * Set a:000 to a list with room for the "..." arguments.
19729 */
19730 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19731 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019732 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019733 v = &fc.fixvar[fixvar_idx++].var;
19734 STRCPY(v->di_key, "000");
19735 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19736 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19737 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019738 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 v->di_tv.vval.v_list = &fc.l_varlist;
19740 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19741 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019742 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019743
19744 /*
19745 * Set a:firstline to "firstline" and a:lastline to "lastline".
19746 * Set a:name to named arguments.
19747 * Set a:N to the "..." arguments.
19748 */
19749 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19750 (varnumber_T)firstline);
19751 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19752 (varnumber_T)lastline);
19753 for (i = 0; i < argcount; ++i)
19754 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019755 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019756 if (ai < 0)
19757 /* named argument a:name */
19758 name = FUNCARG(fp, i);
19759 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019760 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019761 /* "..." argument a:1, a:2, etc. */
19762 sprintf((char *)numbuf, "%d", ai + 1);
19763 name = numbuf;
19764 }
19765 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19766 {
19767 v = &fc.fixvar[fixvar_idx++].var;
19768 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19769 }
19770 else
19771 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019772 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19773 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019774 if (v == NULL)
19775 break;
19776 v->di_flags = DI_FLAGS_RO;
19777 }
19778 STRCPY(v->di_key, name);
19779 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19780
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019781 /* Note: the values are copied directly to avoid alloc/free.
19782 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019784 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019785
19786 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19787 {
19788 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19789 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019790 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019791 }
19792 }
19793
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 /* Don't redraw while executing the function. */
19795 ++RedrawingDisabled;
19796 save_sourcing_name = sourcing_name;
19797 save_sourcing_lnum = sourcing_lnum;
19798 sourcing_lnum = 1;
19799 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019800 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 if (sourcing_name != NULL)
19802 {
19803 if (save_sourcing_name != NULL
19804 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19805 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19806 else
19807 STRCPY(sourcing_name, "function ");
19808 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19809
19810 if (p_verbose >= 12)
19811 {
19812 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019813 verbose_enter_scroll();
19814
Bram Moolenaar555b2802005-05-19 21:08:39 +000019815 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816 if (p_verbose >= 14)
19817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019819 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019820 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821
19822 msg_puts((char_u *)"(");
19823 for (i = 0; i < argcount; ++i)
19824 {
19825 if (i > 0)
19826 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019827 if (argvars[i].v_type == VAR_NUMBER)
19828 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 else
19830 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019831 trunc_string(tv2string(&argvars[i], &tofree,
19832 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019834 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 }
19836 }
19837 msg_puts((char_u *)")");
19838 }
19839 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019840
19841 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 --no_wait_return;
19843 }
19844 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019845#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019846 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019847 {
19848 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19849 func_do_profile(fp);
19850 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019851 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019852 {
19853 ++fp->uf_tm_count;
19854 profile_start(&fp->uf_tm_start);
19855 profile_zero(&fp->uf_tm_children);
19856 }
19857 script_prof_save(&wait_start);
19858 }
19859#endif
19860
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019862 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 save_did_emsg = did_emsg;
19864 did_emsg = FALSE;
19865
19866 /* call do_cmdline() to execute the lines */
19867 do_cmdline(NULL, get_func_line, (void *)&fc,
19868 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19869
19870 --RedrawingDisabled;
19871
19872 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019873 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875 clear_tv(rettv);
19876 rettv->v_type = VAR_NUMBER;
19877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 }
19879
Bram Moolenaar05159a02005-02-26 23:04:13 +000019880#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019881 if (do_profiling == PROF_YES && (fp->uf_profiling
19882 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019883 {
19884 profile_end(&fp->uf_tm_start);
19885 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19886 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019887 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019888 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019889 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019890 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19891 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019892 }
19893 }
19894#endif
19895
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 /* when being verbose, mention the return value */
19897 if (p_verbose >= 12)
19898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019900 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019903 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019904 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019905 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19906 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019907 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019909 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019910 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019911 char_u *tofree;
19912
Bram Moolenaar555b2802005-05-19 21:08:39 +000019913 /* The value may be very long. Skip the middle part, so that we
19914 * have some idea how it starts and ends. smsg() would always
19915 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000019916 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019917 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019918 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019919 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 }
19921 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019922
19923 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 --no_wait_return;
19925 }
19926
19927 vim_free(sourcing_name);
19928 sourcing_name = save_sourcing_name;
19929 sourcing_lnum = save_sourcing_lnum;
19930 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019931#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019932 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019933 script_prof_restore(&wait_start);
19934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935
19936 if (p_verbose >= 12 && sourcing_name != NULL)
19937 {
19938 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019939 verbose_enter_scroll();
19940
Bram Moolenaar555b2802005-05-19 21:08:39 +000019941 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019943
19944 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 --no_wait_return;
19946 }
19947
19948 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019949 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950
Bram Moolenaar33570922005-01-25 22:26:29 +000019951 /* The a: variables typevals were not alloced, only free the allocated
19952 * variables. */
19953 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19954
19955 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 --depth;
19957}
19958
19959/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019960 * Add a number variable "name" to dict "dp" with value "nr".
19961 */
19962 static void
19963add_nr_var(dp, v, name, nr)
19964 dict_T *dp;
19965 dictitem_T *v;
19966 char *name;
19967 varnumber_T nr;
19968{
19969 STRCPY(v->di_key, name);
19970 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19971 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19972 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019973 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019974 v->di_tv.vval.v_number = nr;
19975}
19976
19977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 * ":return [expr]"
19979 */
19980 void
19981ex_return(eap)
19982 exarg_T *eap;
19983{
19984 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019985 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 int returning = FALSE;
19987
19988 if (current_funccal == NULL)
19989 {
19990 EMSG(_("E133: :return not inside a function"));
19991 return;
19992 }
19993
19994 if (eap->skip)
19995 ++emsg_skip;
19996
19997 eap->nextcmd = NULL;
19998 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 {
20001 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020002 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 }
20006 /* It's safer to return also on error. */
20007 else if (!eap->skip)
20008 {
20009 /*
20010 * Return unless the expression evaluation has been cancelled due to an
20011 * aborting error, an interrupt, or an exception.
20012 */
20013 if (!aborting())
20014 returning = do_return(eap, FALSE, TRUE, NULL);
20015 }
20016
20017 /* When skipping or the return gets pending, advance to the next command
20018 * in this line (!returning). Otherwise, ignore the rest of the line.
20019 * Following lines will be ignored by get_func_line(). */
20020 if (returning)
20021 eap->nextcmd = NULL;
20022 else if (eap->nextcmd == NULL) /* no argument */
20023 eap->nextcmd = check_nextcmd(arg);
20024
20025 if (eap->skip)
20026 --emsg_skip;
20027}
20028
20029/*
20030 * Return from a function. Possibly makes the return pending. Also called
20031 * for a pending return at the ":endtry" or after returning from an extra
20032 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020033 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 * FALSE when the return gets pending.
20036 */
20037 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020038do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 exarg_T *eap;
20040 int reanimate;
20041 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043{
20044 int idx;
20045 struct condstack *cstack = eap->cstack;
20046
20047 if (reanimate)
20048 /* Undo the return. */
20049 current_funccal->returned = FALSE;
20050
20051 /*
20052 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20053 * not in its finally clause (which then is to be executed next) is found.
20054 * In this case, make the ":return" pending for execution at the ":endtry".
20055 * Otherwise, return normally.
20056 */
20057 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20058 if (idx >= 0)
20059 {
20060 cstack->cs_pending[idx] = CSTP_RETURN;
20061
20062 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063 /* A pending return again gets pending. "rettv" points to an
20064 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 else
20068 {
20069 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020070 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020072 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020074 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 {
20076 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020078 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 else
20080 EMSG(_(e_outofmem));
20081 }
20082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020083 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084
20085 if (reanimate)
20086 {
20087 /* The pending return value could be overwritten by a ":return"
20088 * without argument in a finally clause; reset the default
20089 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020090 current_funccal->rettv->v_type = VAR_NUMBER;
20091 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 }
20093 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020094 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 }
20096 else
20097 {
20098 current_funccal->returned = TRUE;
20099
20100 /* If the return is carried out now, store the return value. For
20101 * a return immediately after reanimation, the value is already
20102 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020103 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020105 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020106 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020108 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 }
20110 }
20111
20112 return idx < 0;
20113}
20114
20115/*
20116 * Free the variable with a pending return value.
20117 */
20118 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020119discard_pending_return(rettv)
20120 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121{
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123}
20124
20125/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020126 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 * is an allocated string. Used by report_pending() for verbose messages.
20128 */
20129 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020130get_return_cmd(rettv)
20131 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020133 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020134 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020135 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020137 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020138 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020139 if (s == NULL)
20140 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141
20142 STRCPY(IObuff, ":return ");
20143 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20144 if (STRLEN(s) + 8 >= IOSIZE)
20145 STRCPY(IObuff + IOSIZE - 4, "...");
20146 vim_free(tofree);
20147 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148}
20149
20150/*
20151 * Get next function line.
20152 * Called by do_cmdline() to get the next line.
20153 * Returns allocated string, or NULL for end of function.
20154 */
20155/* ARGSUSED */
20156 char_u *
20157get_func_line(c, cookie, indent)
20158 int c; /* not used */
20159 void *cookie;
20160 int indent; /* not used */
20161{
Bram Moolenaar33570922005-01-25 22:26:29 +000020162 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020163 ufunc_T *fp = fcp->func;
20164 char_u *retval;
20165 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166
20167 /* If breakpoints have been added/deleted need to check for it. */
20168 if (fcp->dbg_tick != debug_tick)
20169 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020170 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 sourcing_lnum);
20172 fcp->dbg_tick = debug_tick;
20173 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020174#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020175 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020176 func_line_end(cookie);
20177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178
Bram Moolenaar05159a02005-02-26 23:04:13 +000020179 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020180 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20181 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 retval = NULL;
20183 else
20184 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020185 /* Skip NULL lines (continuation lines). */
20186 while (fcp->linenr < gap->ga_len
20187 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20188 ++fcp->linenr;
20189 if (fcp->linenr >= gap->ga_len)
20190 retval = NULL;
20191 else
20192 {
20193 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20194 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020195#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020196 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020197 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020198#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 }
20201
20202 /* Did we encounter a breakpoint? */
20203 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20204 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020205 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020207 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 sourcing_lnum);
20209 fcp->dbg_tick = debug_tick;
20210 }
20211
20212 return retval;
20213}
20214
Bram Moolenaar05159a02005-02-26 23:04:13 +000020215#if defined(FEAT_PROFILE) || defined(PROTO)
20216/*
20217 * Called when starting to read a function line.
20218 * "sourcing_lnum" must be correct!
20219 * When skipping lines it may not actually be executed, but we won't find out
20220 * until later and we need to store the time now.
20221 */
20222 void
20223func_line_start(cookie)
20224 void *cookie;
20225{
20226 funccall_T *fcp = (funccall_T *)cookie;
20227 ufunc_T *fp = fcp->func;
20228
20229 if (fp->uf_profiling && sourcing_lnum >= 1
20230 && sourcing_lnum <= fp->uf_lines.ga_len)
20231 {
20232 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020233 /* Skip continuation lines. */
20234 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20235 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020236 fp->uf_tml_execed = FALSE;
20237 profile_start(&fp->uf_tml_start);
20238 profile_zero(&fp->uf_tml_children);
20239 profile_get_wait(&fp->uf_tml_wait);
20240 }
20241}
20242
20243/*
20244 * Called when actually executing a function line.
20245 */
20246 void
20247func_line_exec(cookie)
20248 void *cookie;
20249{
20250 funccall_T *fcp = (funccall_T *)cookie;
20251 ufunc_T *fp = fcp->func;
20252
20253 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20254 fp->uf_tml_execed = TRUE;
20255}
20256
20257/*
20258 * Called when done with a function line.
20259 */
20260 void
20261func_line_end(cookie)
20262 void *cookie;
20263{
20264 funccall_T *fcp = (funccall_T *)cookie;
20265 ufunc_T *fp = fcp->func;
20266
20267 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20268 {
20269 if (fp->uf_tml_execed)
20270 {
20271 ++fp->uf_tml_count[fp->uf_tml_idx];
20272 profile_end(&fp->uf_tml_start);
20273 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020274 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020275 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20276 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020277 }
20278 fp->uf_tml_idx = -1;
20279 }
20280}
20281#endif
20282
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283/*
20284 * Return TRUE if the currently active function should be ended, because a
20285 * return was encountered or an error occured. Used inside a ":while".
20286 */
20287 int
20288func_has_ended(cookie)
20289 void *cookie;
20290{
Bram Moolenaar33570922005-01-25 22:26:29 +000020291 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292
20293 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20294 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020295 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 || fcp->returned);
20297}
20298
20299/*
20300 * return TRUE if cookie indicates a function which "abort"s on errors.
20301 */
20302 int
20303func_has_abort(cookie)
20304 void *cookie;
20305{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020306 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307}
20308
20309#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20310typedef enum
20311{
20312 VAR_FLAVOUR_DEFAULT,
20313 VAR_FLAVOUR_SESSION,
20314 VAR_FLAVOUR_VIMINFO
20315} var_flavour_T;
20316
20317static var_flavour_T var_flavour __ARGS((char_u *varname));
20318
20319 static var_flavour_T
20320var_flavour(varname)
20321 char_u *varname;
20322{
20323 char_u *p = varname;
20324
20325 if (ASCII_ISUPPER(*p))
20326 {
20327 while (*(++p))
20328 if (ASCII_ISLOWER(*p))
20329 return VAR_FLAVOUR_SESSION;
20330 return VAR_FLAVOUR_VIMINFO;
20331 }
20332 else
20333 return VAR_FLAVOUR_DEFAULT;
20334}
20335#endif
20336
20337#if defined(FEAT_VIMINFO) || defined(PROTO)
20338/*
20339 * Restore global vars that start with a capital from the viminfo file
20340 */
20341 int
20342read_viminfo_varlist(virp, writing)
20343 vir_T *virp;
20344 int writing;
20345{
20346 char_u *tab;
20347 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349
20350 if (!writing && (find_viminfo_parameter('!') != NULL))
20351 {
20352 tab = vim_strchr(virp->vir_line + 1, '\t');
20353 if (tab != NULL)
20354 {
20355 *tab++ = '\0'; /* isolate the variable name */
20356 if (*tab == 'S') /* string var */
20357 is_string = TRUE;
20358
20359 tab = vim_strchr(tab, '\t');
20360 if (tab != NULL)
20361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 if (is_string)
20363 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020364 tv.v_type = VAR_STRING;
20365 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 }
20368 else
20369 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020370 tv.v_type = VAR_NUMBER;
20371 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020373 set_var(virp->vir_line + 1, &tv, FALSE);
20374 if (is_string)
20375 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 }
20377 }
20378 }
20379
20380 return viminfo_readline(virp);
20381}
20382
20383/*
20384 * Write global vars that start with a capital to the viminfo file
20385 */
20386 void
20387write_viminfo_varlist(fp)
20388 FILE *fp;
20389{
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 hashitem_T *hi;
20391 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020392 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020393 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020394 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020395 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020396 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397
20398 if (find_viminfo_parameter('!') == NULL)
20399 return;
20400
20401 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020402
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020403 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020404 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020406 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020408 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 this_var = HI2DI(hi);
20410 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020412 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020413 {
20414 case VAR_STRING: s = "STR"; break;
20415 case VAR_NUMBER: s = "NUM"; break;
20416 default: continue;
20417 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020419 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020420 if (p != NULL)
20421 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020422 vim_free(tofree);
20423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 }
20425 }
20426}
20427#endif
20428
20429#if defined(FEAT_SESSION) || defined(PROTO)
20430 int
20431store_session_globals(fd)
20432 FILE *fd;
20433{
Bram Moolenaar33570922005-01-25 22:26:29 +000020434 hashitem_T *hi;
20435 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020436 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 char_u *p, *t;
20438
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020439 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020440 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020442 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020444 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020445 this_var = HI2DI(hi);
20446 if ((this_var->di_tv.v_type == VAR_NUMBER
20447 || this_var->di_tv.v_type == VAR_STRING)
20448 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020449 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020450 /* Escape special characters with a backslash. Turn a LF and
20451 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020452 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020453 (char_u *)"\\\"\n\r");
20454 if (p == NULL) /* out of memory */
20455 break;
20456 for (t = p; *t != NUL; ++t)
20457 if (*t == '\n')
20458 *t = 'n';
20459 else if (*t == '\r')
20460 *t = 'r';
20461 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020462 this_var->di_key,
20463 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20464 : ' ',
20465 p,
20466 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20467 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020468 || put_eol(fd) == FAIL)
20469 {
20470 vim_free(p);
20471 return FAIL;
20472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 vim_free(p);
20474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 }
20476 }
20477 return OK;
20478}
20479#endif
20480
Bram Moolenaar661b1822005-07-28 22:36:45 +000020481/*
20482 * Display script name where an item was last set.
20483 * Should only be invoked when 'verbose' is non-zero.
20484 */
20485 void
20486last_set_msg(scriptID)
20487 scid_T scriptID;
20488{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020489 char_u *p;
20490
Bram Moolenaar661b1822005-07-28 22:36:45 +000020491 if (scriptID != 0)
20492 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020493 p = home_replace_save(NULL, get_scriptname(scriptID));
20494 if (p != NULL)
20495 {
20496 verbose_enter();
20497 MSG_PUTS(_("\n\tLast set from "));
20498 MSG_PUTS(p);
20499 vim_free(p);
20500 verbose_leave();
20501 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020502 }
20503}
20504
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505#endif /* FEAT_EVAL */
20506
20507#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20508
20509
20510#ifdef WIN3264
20511/*
20512 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20513 */
20514static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20515static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20516static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20517
20518/*
20519 * Get the short pathname of a file.
20520 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20521 */
20522 static int
20523get_short_pathname(fnamep, bufp, fnamelen)
20524 char_u **fnamep;
20525 char_u **bufp;
20526 int *fnamelen;
20527{
20528 int l,len;
20529 char_u *newbuf;
20530
20531 len = *fnamelen;
20532
20533 l = GetShortPathName(*fnamep, *fnamep, len);
20534 if (l > len - 1)
20535 {
20536 /* If that doesn't work (not enough space), then save the string
20537 * and try again with a new buffer big enough
20538 */
20539 newbuf = vim_strnsave(*fnamep, l);
20540 if (newbuf == NULL)
20541 return 0;
20542
20543 vim_free(*bufp);
20544 *fnamep = *bufp = newbuf;
20545
20546 l = GetShortPathName(*fnamep,*fnamep,l+1);
20547
20548 /* Really should always succeed, as the buffer is big enough */
20549 }
20550
20551 *fnamelen = l;
20552 return 1;
20553}
20554
20555/*
20556 * Create a short path name. Returns the length of the buffer it needs.
20557 * Doesn't copy over the end of the buffer passed in.
20558 */
20559 static int
20560shortpath_for_invalid_fname(fname, bufp, fnamelen)
20561 char_u **fname;
20562 char_u **bufp;
20563 int *fnamelen;
20564{
20565 char_u *s, *p, *pbuf2, *pbuf3;
20566 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020567 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568
20569 /* Make a copy */
20570 len2 = *fnamelen;
20571 pbuf2 = vim_strnsave(*fname, len2);
20572 pbuf3 = NULL;
20573
20574 s = pbuf2 + len2 - 1; /* Find the end */
20575 slen = 1;
20576 plen = len2;
20577
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020578 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 {
20580 --s;
20581 ++slen;
20582 --plen;
20583 }
20584
20585 do
20586 {
20587 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020588 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 {
20590 --s;
20591 ++slen;
20592 --plen;
20593 }
20594 if (s <= pbuf2)
20595 break;
20596
20597 /* Remeber the character that is about to be blatted */
20598 ch = *s;
20599 *s = 0; /* get_short_pathname requires a null-terminated string */
20600
20601 /* Try it in situ */
20602 p = pbuf2;
20603 if (!get_short_pathname(&p, &pbuf3, &plen))
20604 {
20605 vim_free(pbuf2);
20606 return -1;
20607 }
20608 *s = ch; /* Preserve the string */
20609 } while (plen == 0);
20610
20611 if (plen > 0)
20612 {
20613 /* Remeber the length of the new string. */
20614 *fnamelen = len = plen + slen;
20615 vim_free(*bufp);
20616 if (len > len2)
20617 {
20618 /* If there's not enough space in the currently allocated string,
20619 * then copy it to a buffer big enough.
20620 */
20621 *fname= *bufp = vim_strnsave(p, len);
20622 if (*fname == NULL)
20623 return -1;
20624 }
20625 else
20626 {
20627 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20628 *fname = *bufp = pbuf2;
20629 if (p != pbuf2)
20630 strncpy(*fname, p, plen);
20631 pbuf2 = NULL;
20632 }
20633 /* Concat the next bit */
20634 strncpy(*fname + plen, s, slen);
20635 (*fname)[len] = '\0';
20636 }
20637 vim_free(pbuf3);
20638 vim_free(pbuf2);
20639 return 0;
20640}
20641
20642/*
20643 * Get a pathname for a partial path.
20644 */
20645 static int
20646shortpath_for_partial(fnamep, bufp, fnamelen)
20647 char_u **fnamep;
20648 char_u **bufp;
20649 int *fnamelen;
20650{
20651 int sepcount, len, tflen;
20652 char_u *p;
20653 char_u *pbuf, *tfname;
20654 int hasTilde;
20655
20656 /* Count up the path seperators from the RHS.. so we know which part
20657 * of the path to return.
20658 */
20659 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020660 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 if (vim_ispathsep(*p))
20662 ++sepcount;
20663
20664 /* Need full path first (use expand_env() to remove a "~/") */
20665 hasTilde = (**fnamep == '~');
20666 if (hasTilde)
20667 pbuf = tfname = expand_env_save(*fnamep);
20668 else
20669 pbuf = tfname = FullName_save(*fnamep, FALSE);
20670
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020671 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672
20673 if (!get_short_pathname(&tfname, &pbuf, &len))
20674 return -1;
20675
20676 if (len == 0)
20677 {
20678 /* Don't have a valid filename, so shorten the rest of the
20679 * path if we can. This CAN give us invalid 8.3 filenames, but
20680 * there's not a lot of point in guessing what it might be.
20681 */
20682 len = tflen;
20683 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20684 return -1;
20685 }
20686
20687 /* Count the paths backward to find the beginning of the desired string. */
20688 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020689 {
20690#ifdef FEAT_MBYTE
20691 if (has_mbyte)
20692 p -= mb_head_off(tfname, p);
20693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 if (vim_ispathsep(*p))
20695 {
20696 if (sepcount == 0 || (hasTilde && sepcount == 1))
20697 break;
20698 else
20699 sepcount --;
20700 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 if (hasTilde)
20703 {
20704 --p;
20705 if (p >= tfname)
20706 *p = '~';
20707 else
20708 return -1;
20709 }
20710 else
20711 ++p;
20712
20713 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20714 vim_free(*bufp);
20715 *fnamelen = (int)STRLEN(p);
20716 *bufp = pbuf;
20717 *fnamep = p;
20718
20719 return 0;
20720}
20721#endif /* WIN3264 */
20722
20723/*
20724 * Adjust a filename, according to a string of modifiers.
20725 * *fnamep must be NUL terminated when called. When returning, the length is
20726 * determined by *fnamelen.
20727 * Returns valid flags.
20728 * When there is an error, *fnamep is set to NULL.
20729 */
20730 int
20731modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20732 char_u *src; /* string with modifiers */
20733 int *usedlen; /* characters after src that are used */
20734 char_u **fnamep; /* file name so far */
20735 char_u **bufp; /* buffer for allocated file name or NULL */
20736 int *fnamelen; /* length of fnamep */
20737{
20738 int valid = 0;
20739 char_u *tail;
20740 char_u *s, *p, *pbuf;
20741 char_u dirname[MAXPATHL];
20742 int c;
20743 int has_fullname = 0;
20744#ifdef WIN3264
20745 int has_shortname = 0;
20746#endif
20747
20748repeat:
20749 /* ":p" - full path/file_name */
20750 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20751 {
20752 has_fullname = 1;
20753
20754 valid |= VALID_PATH;
20755 *usedlen += 2;
20756
20757 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20758 if ((*fnamep)[0] == '~'
20759#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20760 && ((*fnamep)[1] == '/'
20761# ifdef BACKSLASH_IN_FILENAME
20762 || (*fnamep)[1] == '\\'
20763# endif
20764 || (*fnamep)[1] == NUL)
20765
20766#endif
20767 )
20768 {
20769 *fnamep = expand_env_save(*fnamep);
20770 vim_free(*bufp); /* free any allocated file name */
20771 *bufp = *fnamep;
20772 if (*fnamep == NULL)
20773 return -1;
20774 }
20775
20776 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020777 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 {
20779 if (vim_ispathsep(*p)
20780 && p[1] == '.'
20781 && (p[2] == NUL
20782 || vim_ispathsep(p[2])
20783 || (p[2] == '.'
20784 && (p[3] == NUL || vim_ispathsep(p[3])))))
20785 break;
20786 }
20787
20788 /* FullName_save() is slow, don't use it when not needed. */
20789 if (*p != NUL || !vim_isAbsName(*fnamep))
20790 {
20791 *fnamep = FullName_save(*fnamep, *p != NUL);
20792 vim_free(*bufp); /* free any allocated file name */
20793 *bufp = *fnamep;
20794 if (*fnamep == NULL)
20795 return -1;
20796 }
20797
20798 /* Append a path separator to a directory. */
20799 if (mch_isdir(*fnamep))
20800 {
20801 /* Make room for one or two extra characters. */
20802 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20803 vim_free(*bufp); /* free any allocated file name */
20804 *bufp = *fnamep;
20805 if (*fnamep == NULL)
20806 return -1;
20807 add_pathsep(*fnamep);
20808 }
20809 }
20810
20811 /* ":." - path relative to the current directory */
20812 /* ":~" - path relative to the home directory */
20813 /* ":8" - shortname path - postponed till after */
20814 while (src[*usedlen] == ':'
20815 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20816 {
20817 *usedlen += 2;
20818 if (c == '8')
20819 {
20820#ifdef WIN3264
20821 has_shortname = 1; /* Postpone this. */
20822#endif
20823 continue;
20824 }
20825 pbuf = NULL;
20826 /* Need full path first (use expand_env() to remove a "~/") */
20827 if (!has_fullname)
20828 {
20829 if (c == '.' && **fnamep == '~')
20830 p = pbuf = expand_env_save(*fnamep);
20831 else
20832 p = pbuf = FullName_save(*fnamep, FALSE);
20833 }
20834 else
20835 p = *fnamep;
20836
20837 has_fullname = 0;
20838
20839 if (p != NULL)
20840 {
20841 if (c == '.')
20842 {
20843 mch_dirname(dirname, MAXPATHL);
20844 s = shorten_fname(p, dirname);
20845 if (s != NULL)
20846 {
20847 *fnamep = s;
20848 if (pbuf != NULL)
20849 {
20850 vim_free(*bufp); /* free any allocated file name */
20851 *bufp = pbuf;
20852 pbuf = NULL;
20853 }
20854 }
20855 }
20856 else
20857 {
20858 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20859 /* Only replace it when it starts with '~' */
20860 if (*dirname == '~')
20861 {
20862 s = vim_strsave(dirname);
20863 if (s != NULL)
20864 {
20865 *fnamep = s;
20866 vim_free(*bufp);
20867 *bufp = s;
20868 }
20869 }
20870 }
20871 vim_free(pbuf);
20872 }
20873 }
20874
20875 tail = gettail(*fnamep);
20876 *fnamelen = (int)STRLEN(*fnamep);
20877
20878 /* ":h" - head, remove "/file_name", can be repeated */
20879 /* Don't remove the first "/" or "c:\" */
20880 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20881 {
20882 valid |= VALID_HEAD;
20883 *usedlen += 2;
20884 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020885 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 --tail;
20887 *fnamelen = (int)(tail - *fnamep);
20888#ifdef VMS
20889 if (*fnamelen > 0)
20890 *fnamelen += 1; /* the path separator is part of the path */
20891#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020892 while (tail > s && !after_pathsep(s, tail))
20893 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 }
20895
20896 /* ":8" - shortname */
20897 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20898 {
20899 *usedlen += 2;
20900#ifdef WIN3264
20901 has_shortname = 1;
20902#endif
20903 }
20904
20905#ifdef WIN3264
20906 /* Check shortname after we have done 'heads' and before we do 'tails'
20907 */
20908 if (has_shortname)
20909 {
20910 pbuf = NULL;
20911 /* Copy the string if it is shortened by :h */
20912 if (*fnamelen < (int)STRLEN(*fnamep))
20913 {
20914 p = vim_strnsave(*fnamep, *fnamelen);
20915 if (p == 0)
20916 return -1;
20917 vim_free(*bufp);
20918 *bufp = *fnamep = p;
20919 }
20920
20921 /* Split into two implementations - makes it easier. First is where
20922 * there isn't a full name already, second is where there is.
20923 */
20924 if (!has_fullname && !vim_isAbsName(*fnamep))
20925 {
20926 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20927 return -1;
20928 }
20929 else
20930 {
20931 int l;
20932
20933 /* Simple case, already have the full-name
20934 * Nearly always shorter, so try first time. */
20935 l = *fnamelen;
20936 if (!get_short_pathname(fnamep, bufp, &l))
20937 return -1;
20938
20939 if (l == 0)
20940 {
20941 /* Couldn't find the filename.. search the paths.
20942 */
20943 l = *fnamelen;
20944 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20945 return -1;
20946 }
20947 *fnamelen = l;
20948 }
20949 }
20950#endif /* WIN3264 */
20951
20952 /* ":t" - tail, just the basename */
20953 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20954 {
20955 *usedlen += 2;
20956 *fnamelen -= (int)(tail - *fnamep);
20957 *fnamep = tail;
20958 }
20959
20960 /* ":e" - extension, can be repeated */
20961 /* ":r" - root, without extension, can be repeated */
20962 while (src[*usedlen] == ':'
20963 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20964 {
20965 /* find a '.' in the tail:
20966 * - for second :e: before the current fname
20967 * - otherwise: The last '.'
20968 */
20969 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20970 s = *fnamep - 2;
20971 else
20972 s = *fnamep + *fnamelen - 1;
20973 for ( ; s > tail; --s)
20974 if (s[0] == '.')
20975 break;
20976 if (src[*usedlen + 1] == 'e') /* :e */
20977 {
20978 if (s > tail)
20979 {
20980 *fnamelen += (int)(*fnamep - (s + 1));
20981 *fnamep = s + 1;
20982#ifdef VMS
20983 /* cut version from the extension */
20984 s = *fnamep + *fnamelen - 1;
20985 for ( ; s > *fnamep; --s)
20986 if (s[0] == ';')
20987 break;
20988 if (s > *fnamep)
20989 *fnamelen = s - *fnamep;
20990#endif
20991 }
20992 else if (*fnamep <= tail)
20993 *fnamelen = 0;
20994 }
20995 else /* :r */
20996 {
20997 if (s > tail) /* remove one extension */
20998 *fnamelen = (int)(s - *fnamep);
20999 }
21000 *usedlen += 2;
21001 }
21002
21003 /* ":s?pat?foo?" - substitute */
21004 /* ":gs?pat?foo?" - global substitute */
21005 if (src[*usedlen] == ':'
21006 && (src[*usedlen + 1] == 's'
21007 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21008 {
21009 char_u *str;
21010 char_u *pat;
21011 char_u *sub;
21012 int sep;
21013 char_u *flags;
21014 int didit = FALSE;
21015
21016 flags = (char_u *)"";
21017 s = src + *usedlen + 2;
21018 if (src[*usedlen + 1] == 'g')
21019 {
21020 flags = (char_u *)"g";
21021 ++s;
21022 }
21023
21024 sep = *s++;
21025 if (sep)
21026 {
21027 /* find end of pattern */
21028 p = vim_strchr(s, sep);
21029 if (p != NULL)
21030 {
21031 pat = vim_strnsave(s, (int)(p - s));
21032 if (pat != NULL)
21033 {
21034 s = p + 1;
21035 /* find end of substitution */
21036 p = vim_strchr(s, sep);
21037 if (p != NULL)
21038 {
21039 sub = vim_strnsave(s, (int)(p - s));
21040 str = vim_strnsave(*fnamep, *fnamelen);
21041 if (sub != NULL && str != NULL)
21042 {
21043 *usedlen = (int)(p + 1 - src);
21044 s = do_string_sub(str, pat, sub, flags);
21045 if (s != NULL)
21046 {
21047 *fnamep = s;
21048 *fnamelen = (int)STRLEN(s);
21049 vim_free(*bufp);
21050 *bufp = s;
21051 didit = TRUE;
21052 }
21053 }
21054 vim_free(sub);
21055 vim_free(str);
21056 }
21057 vim_free(pat);
21058 }
21059 }
21060 /* after using ":s", repeat all the modifiers */
21061 if (didit)
21062 goto repeat;
21063 }
21064 }
21065
21066 return valid;
21067}
21068
21069/*
21070 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21071 * "flags" can be "g" to do a global substitute.
21072 * Returns an allocated string, NULL for error.
21073 */
21074 char_u *
21075do_string_sub(str, pat, sub, flags)
21076 char_u *str;
21077 char_u *pat;
21078 char_u *sub;
21079 char_u *flags;
21080{
21081 int sublen;
21082 regmatch_T regmatch;
21083 int i;
21084 int do_all;
21085 char_u *tail;
21086 garray_T ga;
21087 char_u *ret;
21088 char_u *save_cpo;
21089
21090 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21091 save_cpo = p_cpo;
21092 p_cpo = (char_u *)"";
21093
21094 ga_init2(&ga, 1, 200);
21095
21096 do_all = (flags[0] == 'g');
21097
21098 regmatch.rm_ic = p_ic;
21099 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21100 if (regmatch.regprog != NULL)
21101 {
21102 tail = str;
21103 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21104 {
21105 /*
21106 * Get some space for a temporary buffer to do the substitution
21107 * into. It will contain:
21108 * - The text up to where the match is.
21109 * - The substituted text.
21110 * - The text after the match.
21111 */
21112 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21113 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21114 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21115 {
21116 ga_clear(&ga);
21117 break;
21118 }
21119
21120 /* copy the text up to where the match is */
21121 i = (int)(regmatch.startp[0] - tail);
21122 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21123 /* add the substituted text */
21124 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21125 + ga.ga_len + i, TRUE, TRUE, FALSE);
21126 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 /* avoid getting stuck on a match with an empty string */
21128 if (tail == regmatch.endp[0])
21129 {
21130 if (*tail == NUL)
21131 break;
21132 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21133 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 }
21135 else
21136 {
21137 tail = regmatch.endp[0];
21138 if (*tail == NUL)
21139 break;
21140 }
21141 if (!do_all)
21142 break;
21143 }
21144
21145 if (ga.ga_data != NULL)
21146 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21147
21148 vim_free(regmatch.regprog);
21149 }
21150
21151 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21152 ga_clear(&ga);
21153 p_cpo = save_cpo;
21154
21155 return ret;
21156}
21157
21158#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */